Skip to content

Commit

Permalink
[Fix] #244 - mypage account 메모리 릭 해결 및 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
jeongdung-eo committed Mar 23, 2024
1 parent 4f4f7aa commit a48c85b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,33 @@ struct MyPageAccountModel: Equatable {
}

struct AccountRowData: Hashable {
var uuid = UUID()
var title: String
var content: String?
var titleColor: UIColor = .white
var isSwitch: Bool = false
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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -162,12 +164,10 @@ extension UIControl {
}

extension UISwitch {
var tapPublisher: AnyPublisher<Bool, Never> {
var statePublisher: AnyPublisher<Bool, Never> {
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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@ final class MyPageAccountViewController: UIViewController {
setupDataSource()
setBindings()
}

deinit {
cancelBag.forEach { $0.cancel() }
print("🤍 🤍 🤍🤍 🤍 deinit")
}
}

// MARK: - Methods
Expand All @@ -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 {
Expand Down Expand Up @@ -118,24 +117,28 @@ private extension MyPageAccountViewController {
}

private func setupDataSource() {
let cellRegistration = CellRegistration<MyPageAccountCollectionViewCell, AccountRowData> {cell, _, item in

let cellRegistration = CellRegistration<MyPageAccountCollectionViewCell, AccountRowData> { [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)
})
}

private func setBindings() {

let input = MyPageAccountViewModelInput(viewWillAppearSubject: viewWillAppearSubject,
withdrawalTapped: withdrawalTapped,
logoutTapped: logoutTapped,
Expand All @@ -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)

Expand All @@ -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 {
Expand All @@ -183,14 +187,10 @@ extension MyPageAccountViewController: UICollectionViewDelegate {
}
}

extension MyPageAccountViewController: NavigationDelegate {
extension MyPageAccountViewController {

@objc
private func presentToWithdraw() {
withdrawalTapped.send(())
}

func popViewController() {
backButtonTapped.send(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<MyPageAccountModel, Never>().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()
Expand All @@ -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 }
Expand Down Expand Up @@ -103,6 +107,6 @@ final class MyPageAccountViewModelImpl: MyPageAccountViewModel {
}

deinit {
cancelBag.forEach { $0.cancel() }
}
cancelBag.forEach { $0.cancel() }
}
}

0 comments on commit a48c85b

Please sign in to comment.