diff --git a/iOS-NOTTODO/iOS-NOTTODO/Presentation/MyPageAccount/Model/MyPageAccountModel.swift b/iOS-NOTTODO/iOS-NOTTODO/Presentation/MyPageAccount/Model/MyPageAccountModel.swift index 73adab6..dda9b32 100644 --- a/iOS-NOTTODO/iOS-NOTTODO/Presentation/MyPageAccount/Model/MyPageAccountModel.swift +++ b/iOS-NOTTODO/iOS-NOTTODO/Presentation/MyPageAccount/Model/MyPageAccountModel.swift @@ -13,6 +13,7 @@ struct MyPageAccountModel: Equatable { } struct AccountRowData: Hashable { + var uuid = UUID() var title: String var content: String? var titleColor: UIColor = .white @@ -20,17 +21,25 @@ struct AccountRowData: Hashable { var isOn: Bool = false static func userInfo() -> [AccountRowData] { - return [AccountRowData(title: I18N.nickname, + return [AccountRowData(title: I18N.nickname, content: KeychainUtil.getBool(DefaultKeys.isAppleLogin) ? KeychainUtil.getAppleUsername() : KeychainUtil.getKakaoNickname()), - AccountRowData(title: I18N.email, + AccountRowData(title: I18N.email, content: KeychainUtil.getBool(DefaultKeys.isAppleLogin) ? KeychainUtil.getAppleEmail() : KeychainUtil.getKakaoEmail()), - AccountRowData(title: I18N.account, + AccountRowData(title: I18N.account, content: KeychainUtil.getBool(DefaultKeys.isAppleLogin) ? "apple" : "kakao"), AccountRowData(title: I18N.notification, isSwitch: true)] } static func logout() -> [AccountRowData] { - return [AccountRowData(title: I18N.logout, + return [AccountRowData(title: I18N.logout, titleColor: .ntdRed!)] } + + func hash(into hasher: inout Hasher) { + hasher.combine(uuid) + } + + static func ==(lhs: AccountRowData, rhs: AccountRowData) -> Bool { + return lhs.uuid == rhs.uuid + } } diff --git a/iOS-NOTTODO/iOS-NOTTODO/Presentation/MyPageAccount/ViewControllers/MyPageAccountCollectionViewCell.swift b/iOS-NOTTODO/iOS-NOTTODO/Presentation/MyPageAccount/ViewControllers/MyPageAccountCollectionViewCell.swift index 9f49514..4b50a88 100644 --- a/iOS-NOTTODO/iOS-NOTTODO/Presentation/MyPageAccount/ViewControllers/MyPageAccountCollectionViewCell.swift +++ b/iOS-NOTTODO/iOS-NOTTODO/Presentation/MyPageAccount/ViewControllers/MyPageAccountCollectionViewCell.swift @@ -94,13 +94,15 @@ extension MyPageAccountCollectionViewCell { titleLabel.text = data.title contentLabel.text = data.content notificationSwitch.setOn(data.isOn, animated: true) - + notificationSwitch.isHidden = !data.isSwitch contentLabel.isHidden = data.isSwitch } func setBindings() { - notificationSwitch.tapPublisher + + notificationSwitch.statePublisher + .receive(on: RunLoop.main) .sink { [weak self] isOn in guard let self else { return } self.switchTapped.send(isOn) @@ -162,12 +164,10 @@ extension UIControl { } extension UISwitch { - var tapPublisher: AnyPublisher { + var statePublisher: AnyPublisher { controlPublisher(for: .valueChanged) - .map { control in - guard let uiSwitch = control as? UISwitch else { return false } - return uiSwitch.isOn - } + .map { $0 as! UISwitch } + .map { $0.isOn } .eraseToAnyPublisher() } } diff --git a/iOS-NOTTODO/iOS-NOTTODO/Presentation/MyPageAccount/ViewControllers/MyPageAccountViewController.swift b/iOS-NOTTODO/iOS-NOTTODO/Presentation/MyPageAccount/ViewControllers/MyPageAccountViewController.swift index 3bb45fb..aeb7756 100644 --- a/iOS-NOTTODO/iOS-NOTTODO/Presentation/MyPageAccount/ViewControllers/MyPageAccountViewController.swift +++ b/iOS-NOTTODO/iOS-NOTTODO/Presentation/MyPageAccount/ViewControllers/MyPageAccountViewController.swift @@ -61,11 +61,6 @@ final class MyPageAccountViewController: UIViewController { setupDataSource() setBindings() } - - deinit { - cancelBag.forEach { $0.cancel() } - print("🤍 🤍 🤍🤍 🤍 deinit") - } } // MARK: - Methods @@ -77,7 +72,11 @@ private extension MyPageAccountViewController { navigationView.do { $0.setTitle(I18N.myInfoAccount) - $0.delegate = self + $0.buttonTapped.sink { [weak self] _ in + guard let self else { return } + self.backButtonTapped.send(()) + } + .store(in: &navigationView.cancelBag) } collectionView.do { @@ -118,17 +117,20 @@ private extension MyPageAccountViewController { } private func setupDataSource() { - let cellRegistration = CellRegistration {cell, _, item in + + let cellRegistration = CellRegistration { [weak self] cell, _, item in cell.configure(data: item) cell.switchTapped .receive(on: RunLoop.main) .sink { [weak self] isOn in - self?.switchButtonTapped.send(isOn) + guard let self else { return } + self.switchButtonTapped.send(isOn) } .store(in: &cell.cancelBag) } dataSource = DataSource(collectionView: collectionView, cellProvider: { collectionView, indexPath, item in + return collectionView.dequeueConfiguredReusableCell(using: cellRegistration, for: indexPath, item: item) @@ -136,6 +138,7 @@ private extension MyPageAccountViewController { } private func setBindings() { + let input = MyPageAccountViewModelInput(viewWillAppearSubject: viewWillAppearSubject, withdrawalTapped: withdrawalTapped, logoutTapped: logoutTapped, @@ -147,7 +150,8 @@ private extension MyPageAccountViewController { output.viewWillAppearSubject .receive(on: RunLoop.main) .sink { [weak self] in - self?.setSnapShot(userInfo: $0.profileData, logout: $0.logout) + guard let self else { return } + self.setSnapShot(userInfo: $0.profileData, logout: $0.logout) } .store(in: &cancelBag) @@ -166,7 +170,7 @@ private extension MyPageAccountViewController { snapShot.appendItems(userInfo, toSection: .account) snapShot.appendItems(logout, toSection: .logout) - dataSource?.applySnapshotUsingReloadData(snapShot) + dataSource?.apply(snapShot, animatingDifferences: true) } private func layout() -> UICollectionViewLayout { @@ -183,14 +187,10 @@ extension MyPageAccountViewController: UICollectionViewDelegate { } } -extension MyPageAccountViewController: NavigationDelegate { +extension MyPageAccountViewController { @objc private func presentToWithdraw() { withdrawalTapped.send(()) } - - func popViewController() { - backButtonTapped.send(()) - } } diff --git a/iOS-NOTTODO/iOS-NOTTODO/Presentation/MyPageAccount/ViewModel/MyPageAccountViewModelImpl.swift b/iOS-NOTTODO/iOS-NOTTODO/Presentation/MyPageAccount/ViewModel/MyPageAccountViewModelImpl.swift index 9139a45..73bea7b 100644 --- a/iOS-NOTTODO/iOS-NOTTODO/Presentation/MyPageAccount/ViewModel/MyPageAccountViewModelImpl.swift +++ b/iOS-NOTTODO/iOS-NOTTODO/Presentation/MyPageAccount/ViewModel/MyPageAccountViewModelImpl.swift @@ -30,12 +30,16 @@ final class MyPageAccountViewModelImpl: MyPageAccountViewModel { let viewWillAppearAndForeground = Publishers.Merge(input.viewWillAppearSubject, NotificationCenter.default.willEnterForeground.map { _ in }) viewWillAppearAndForeground - .flatMap { _ in - self.getAuthorizationStatus() + .flatMap { [weak self] _ in + guard let self = self else { + return Empty().eraseToAnyPublisher() + } + return self.getAuthorizationStatus() .map { isAuthorized -> MyPageAccountModel in var profileData = AccountRowData.userInfo() let logoutData = AccountRowData.logout() profileData[3].isOn = isAuthorized + KeychainUtil.setBool(isAuthorized, forKey: DefaultKeys.isNotificationAccepted) return MyPageAccountModel(profileData: profileData, logout: logoutData) } .eraseToAnyPublisher() @@ -45,7 +49,7 @@ final class MyPageAccountViewModelImpl: MyPageAccountViewModel { self.mypageAccountModel.send(model) }) .store(in: &cancelBag) - + input.switchButtonTapped .sink { [weak self] _ in guard let self = self else { return } @@ -103,6 +107,6 @@ final class MyPageAccountViewModelImpl: MyPageAccountViewModel { } deinit { - cancelBag.forEach { $0.cancel() } - } + cancelBag.forEach { $0.cancel() } + } }