From dd5ead43e3d105c4340084e6080a69e52e3f114e Mon Sep 17 00:00:00 2001 From: hooni Date: Fri, 6 Sep 2024 00:24:58 +0900 Subject: [PATCH] =?UTF-8?q?fix/#363=20=EC=98=A8=EB=B3=B4=EB=94=A9=20+=20?= =?UTF-8?q?=EB=A7=88=EC=9D=B4=ED=8E=98=EC=9D=B4=EC=A7=80=20=ED=82=B9?= =?UTF-8?q?=ED=94=BC=EC=85=94=EA=B4=80=EB=A0=A8=20=EB=AA=A8=EB=93=A0?= =?UTF-8?q?=EC=98=A4=EB=A5=98=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ViewController/MyPageViewController.swift | 22 +++++--- .../ProfileSetupViewController.swift | 18 +++--- .../ViewModel/ProfileSetupViewModel.swift | 55 +++++++------------ 3 files changed, 41 insertions(+), 54 deletions(-) diff --git a/KkuMulKum/Source/MyPage/ViewController/MyPageViewController.swift b/KkuMulKum/Source/MyPage/ViewController/MyPageViewController.swift index 6839ef38..868bc614 100644 --- a/KkuMulKum/Source/MyPage/ViewController/MyPageViewController.swift +++ b/KkuMulKum/Source/MyPage/ViewController/MyPageViewController.swift @@ -140,12 +140,16 @@ class MyPageViewController: BaseViewController, CustomActionSheetDelegate { updateProfileImage(with: userInfo.profileImageURL) } - private func updateProfileImage(with urlString: String?) { + private func updateProfileImage(with urlString: String?, localImage: UIImage? = nil) { print("Attempting to update profile image with URL: \(urlString ?? "nil")") + if let localImage = localImage { + rootView.contentView.profileImageView.image = localImage + } + if let urlString = urlString, let url = URL(string: urlString) { rootView.contentView.profileImageView.kf.setImage( with: url, - placeholder: UIImage.imgProfile, + placeholder: localImage ?? UIImage.imgProfile, options: [ .transition(.fade(0.2)), .forceRefresh, @@ -154,16 +158,18 @@ class MyPageViewController: BaseViewController, CustomActionSheetDelegate { completionHandler: { result in switch result { case .success(let value): - print("Profile image loaded successfully. Size: \(value.image.size)") + print("Profile image loaded successfully from server. Size: \(value.image.size)") case .failure(let error): - print("Failed to load profile image: \(error.localizedDescription)") - self.rootView.contentView.profileImageView.image = UIImage.imgProfile + print("Failed to load profile image from server: \(error.localizedDescription)") + if self.rootView.contentView.profileImageView.image == nil { + self.rootView.contentView.profileImageView.image = UIImage.imgProfile + } } } ) } else { - print("Invalid URL or nil. Setting default profile image.") - rootView.contentView.profileImageView.image = UIImage.imgProfile + print("Invalid URL or nil. Using local image or default profile image.") + rootView.contentView.profileImageView.image = localImage ?? UIImage.imgProfile } } @@ -212,7 +218,7 @@ class MyPageViewController: BaseViewController, CustomActionSheetDelegate { if let imageDataString = imageDataString, let imageData = Data(base64Encoded: imageDataString), let image = UIImage(data: imageData) { - self?.rootView.contentView.profileImageView.image = image + self?.updateProfileImage(with: nil, localImage: image) } self?.needsUserInfoRefresh = true }) diff --git a/KkuMulKum/Source/Onboarding/Profile/VIewController/ProfileSetupViewController.swift b/KkuMulKum/Source/Onboarding/Profile/VIewController/ProfileSetupViewController.swift index aa5086a4..c2f46519 100644 --- a/KkuMulKum/Source/Onboarding/Profile/VIewController/ProfileSetupViewController.swift +++ b/KkuMulKum/Source/Onboarding/Profile/VIewController/ProfileSetupViewController.swift @@ -50,21 +50,17 @@ class ProfileSetupViewController: BaseViewController { } @objc private func confirmButtonTapped() { - Task { - let success = await viewModel.uploadProfileImage() - if success { - DispatchQueue.main.async { - let welcomeVC = WelcomeViewController( - viewModel: WelcomeViewModel(nickname: self.viewModel.nickname) - ) - welcomeVC.modalPresentationStyle = .fullScreen - self.present(welcomeVC, animated: true, completion: nil) - } - } + if viewModel.isConfirmButtonEnabled.value { + viewModel.uploadProfileImage() + navigateToWelcomeScreen() } } @objc private func skipButtonTapped() { + navigateToWelcomeScreen() + } + + private func navigateToWelcomeScreen() { let welcomeVC = WelcomeViewController( viewModel: WelcomeViewModel(nickname: viewModel.nickname) ) diff --git a/KkuMulKum/Source/Onboarding/Profile/ViewModel/ProfileSetupViewModel.swift b/KkuMulKum/Source/Onboarding/Profile/ViewModel/ProfileSetupViewModel.swift index ff0611b2..44a60828 100644 --- a/KkuMulKum/Source/Onboarding/Profile/ViewModel/ProfileSetupViewModel.swift +++ b/KkuMulKum/Source/Onboarding/Profile/ViewModel/ProfileSetupViewModel.swift @@ -48,43 +48,28 @@ class ProfileSetupViewModel { return imageData } - func uploadProfileImage() async -> Bool { - print("uploadProfileImage 함수 호출됨") - guard let imageData = imageData else { - print("이미지 데이터가 없습니다.") - serverResponse.value = "이미지 데이터가 없습니다." - return false - } - - if imageData.count > maxImageSizeBytes { - print("이미지 크기가 최대 허용 크기를 초과합니다.") - serverResponse.value = "이미지 크기가 너무 큽니다. 더 작은 이미지를 선택해주세요." - return false - } - - print("업로드할 이미지 데이터 크기: \(imageData.count) bytes") - - let fileName = "profile_image.jpg" - let mimeType = "image/jpeg" - - do { - let _: EmptyModel = try await authService.performRequest( - .updateProfileImage( - image: imageData, - fileName: fileName, - mimeType: mimeType - ) - ) - serverResponse.value = "프로필 이미지가 성공적으로 업로드되었습니다." - print("프로필 이미지 업로드 성공") + func uploadProfileImage() { + guard let imageData = imageData else { + serverResponse.value = "이미지 데이터가 없습니다." + return + } - clearImageCache() - return true - } catch { - handleError(error as? NetworkError ?? .unknownError("알 수 없는 오류가 발생했습니다.")) - return false + Task { + do { + let _: EmptyModel = try await authService.performRequest( + .updateProfileImage( + image: imageData, + fileName: "profile_image.jpg", + mimeType: "image/jpeg" + ) + ) + print("프로필 이미지가 성공적으로 업로드되었습니다.") + clearImageCache() + } catch { + print("프로필 이미지 업로드 실패: \(error.localizedDescription)") + } + } } - } private func handleError(_ error: NetworkError) { switch error {