Skip to content

Commit

Permalink
fix/#363 마이페이지 오류 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
hooni0918 committed Sep 5, 2024
1 parent ebd5103 commit f6a7a1d
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class MyPageEditViewController: BaseViewController {
private let viewModel: MyPageEditViewModel
private let disposeBag = DisposeBag()
private let newProfileImageSubject = PublishSubject<UIImage?>()
let profileImageUpdated = PublishSubject<String?>()

init(viewModel: MyPageEditViewModel) {
self.viewModel = viewModel
Expand Down Expand Up @@ -73,6 +74,13 @@ class MyPageEditViewController: BaseViewController {
newProfileImage: newProfileImageSubject.asObservable()
)

input.newProfileImage
.compactMap { $0 }
.subscribe(onNext: { [weak self] image in
self?.viewModel.updateProfileImage(image)
})
.disposed(by: disposeBag)

let output = viewModel.transform(input: input, disposeBag: disposeBag)

output.profileImage
Expand Down
134 changes: 63 additions & 71 deletions KkuMulKum/Source/MyPage/ViewController/MyPageViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,25 @@ class MyPageViewController: BaseViewController, CustomActionSheetDelegate {
private let rootView = MyPageView()
private let viewModel = MyPageViewModel()
private let disposeBag = DisposeBag()
private var needsUserInfoRefresh = true

override func loadView() {
view = rootView
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
viewModel.fetchUserInfo()
if needsUserInfoRefresh {
viewModel.fetchUserInfo()
needsUserInfoRefresh = false
}
}

override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .green1

bindViewModel()
viewModel.fetchUserInfo()
}

override func setupView() {
Expand Down Expand Up @@ -105,18 +108,18 @@ class MyPageViewController: BaseViewController, CustomActionSheetDelegate {
.disposed(by: disposeBag)

viewModel.logoutResult
.observe(on: MainScheduler.instance)
.subscribe(onNext: { [weak self] result in
switch result {
case .success:
print("Logout successful")
self?.navigateToLoginScreen()
case .failure(let error):
print("Logout failed: \(error)")
}
})
.disposed(by: disposeBag)
.observe(on: MainScheduler.instance)
.subscribe(onNext: { [weak self] result in
switch result {
case .success:
print("Logout successful")
self?.navigateToLoginScreen()
case .failure(let error):
print("Logout failed: \(error)")
}
})
.disposed(by: disposeBag)


viewModel.userInfo
.observe(on: MainScheduler.instance)
Expand All @@ -138,31 +141,31 @@ class MyPageViewController: BaseViewController, CustomActionSheetDelegate {
}

private func updateProfileImage(with urlString: String?) {
print("Attempting to update profile image with URL: \(urlString ?? "nil")")
if let urlString = urlString, let url = URL(string: urlString) {
rootView.contentView.profileImageView.kf.setImage(
with: url,
placeholder: UIImage.imgProfile,
options: [
.transition(.fade(0.2)),
.forceRefresh,
.cacheOriginalImage
],
completionHandler: { result in
switch result {
case .success(let value):
print("Profile image loaded successfully. Size: \(value.image.size)")
case .failure(let error):
print("Failed to load profile image: \(error.localizedDescription)")
self.rootView.contentView.profileImageView.image = UIImage.imgProfile
}
print("Attempting to update profile image with URL: \(urlString ?? "nil")")
if let urlString = urlString, let url = URL(string: urlString) {
rootView.contentView.profileImageView.kf.setImage(
with: url,
placeholder: UIImage.imgProfile,
options: [
.transition(.fade(0.2)),
.forceRefresh,
.cacheOriginalImage
],
completionHandler: { result in
switch result {
case .success(let value):
print("Profile image loaded successfully. Size: \(value.image.size)")
case .failure(let error):
print("Failed to load profile image: \(error.localizedDescription)")
self.rootView.contentView.profileImageView.image = UIImage.imgProfile
}
)
} else {
print("Invalid URL or nil. Setting default profile image.")
rootView.contentView.profileImageView.image = UIImage.imgProfile
}
}
)
} else {
print("Invalid URL or nil. Setting default profile image.")
rootView.contentView.profileImageView.image = UIImage.imgProfile
}
}

private func loadImage(from urlString: String, into imageView: UIImageView) {
guard let url = URL(string: urlString) else {
Expand Down Expand Up @@ -200,34 +203,23 @@ class MyPageViewController: BaseViewController, CustomActionSheetDelegate {
}

private func pushEditProfileViewController() {
let authService = AuthService()
let editProfileViewModel = MyPageEditViewModel(authService: authService)
let editProfileViewController = MyPageEditViewController(viewModel: editProfileViewModel)

editProfileViewModel.profileImageUpdated
.observe(on: MainScheduler.instance)
.subscribe(onNext: { [weak self] imageDataString in
print("Profile image update received. Data string length: \(imageDataString?.count ?? 0)")
if let imageDataString = imageDataString,
let imageData = Data(base64Encoded: imageDataString),
let image = UIImage(data: imageData) {
print("Successfully created UIImage from updated profile data")
self?.rootView.contentView.profileImageView.image = image
// Force refresh Kingfisher cache
KingfisherManager.shared.cache.removeImage(forKey: self?.viewModel.userInfo.value?.profileImageURL ?? "")
} else {
print("Failed to create UIImage from updated profile data. Setting default image.")
self?.rootView.contentView.profileImageView.image = UIImage.imgProfile
}
// Refresh user info to get the updated profile image URL
self?.viewModel.fetchUserInfo()
})
.disposed(by: disposeBag)

editProfileViewController.hidesBottomBarWhenPushed = true

navigationController?.pushViewController(editProfileViewController, animated: true)
}
let editViewModel = MyPageEditViewModel(authService: AuthService())
let editVC = MyPageEditViewController(viewModel: editViewModel)

editViewModel.profileImageUpdated
.observe(on: MainScheduler.instance)
.subscribe(onNext: { [weak self] imageDataString in
if let imageDataString = imageDataString,
let imageData = Data(base64Encoded: imageDataString),
let image = UIImage(data: imageData) {
self?.rootView.contentView.profileImageView.image = image
}
self?.needsUserInfoRefresh = true
})
.disposed(by: disposeBag)

navigationController?.pushViewController(editVC, animated: true)
}

private func pushAskViewController() {
let askViewController = MyPageAskViewController(viewModel: self.viewModel)
Expand All @@ -240,12 +232,12 @@ class MyPageViewController: BaseViewController, CustomActionSheetDelegate {
}

private func navigateToLoginScreen() {
let loginViewModel = LoginViewModel()
let loginViewController = LoginViewController(viewModel: loginViewModel)
let navigationController = UINavigationController(rootViewController: loginViewController)
navigationController.modalPresentationStyle = .fullScreen
self.view.window?.rootViewController = navigationController
self.view.window?.makeKeyAndVisible()
let loginViewModel = LoginViewModel()
let loginViewController = LoginViewController(viewModel: loginViewModel)
let navigationController = UINavigationController(rootViewController: loginViewController)
navigationController.modalPresentationStyle = .fullScreen
self.view.window?.rootViewController = navigationController
self.view.window?.makeKeyAndVisible()
}

func actionButtonDidTap(for kind: ActionSheetKind) {
Expand Down
27 changes: 27 additions & 0 deletions KkuMulKum/Source/MyPage/ViewModel/MyPageEditViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class MyPageEditViewModel: ViewModelType {
private let userInfo = BehaviorRelay<LoginUserModel?>(value: nil)
let profileImageUpdated = PublishSubject<String?>()


struct Input {
let profileImageTap: Observable<Void>
let confirmButtonTap: Observable<Void>
Expand Down Expand Up @@ -130,6 +131,32 @@ class MyPageEditViewModel: ViewModelType {
)
}

func updateProfileImage(_ image: UIImage) {
guard let imageData = image.jpegData(compressionQuality: 0.8) else {
print("Failed to convert image to data")
return
}

Task {
do {
let _: EmptyModel = try await self.authService.performRequest(
.updateProfileImage(
image: imageData,
fileName: "profile_image.jpg",
mimeType: "image/jpeg"
)
)
DispatchQueue.main.async { [weak self] in
self?.profileImageUpdated.onNext(imageData.base64EncodedString())
}
} catch {
let networkError = error as? NetworkError ?? .unknownError("오류 발생.")
print("Profile image upload failed: \(networkError)")
self.profileImageUpdated.onNext(nil)
}
}
}

func fetchUserInfo() {
Task {
do {
Expand Down

0 comments on commit f6a7a1d

Please sign in to comment.