Skip to content

Commit

Permalink
Add logout logic
Browse files Browse the repository at this point in the history
  • Loading branch information
GianniCarlo committed Apr 12, 2022
1 parent e766957 commit aac5f00
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 4 deletions.
2 changes: 1 addition & 1 deletion BookPlayer/Coordinators/AccountCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class AccountCoordinator: Coordinator {

override func start() {
let vc = AccountViewController.instantiate(from: .Profile)
let viewModel = AccountViewModel()
let viewModel = AccountViewModel(accountService: self.accountService)
viewModel.coordinator = self
vc.viewModel = viewModel
vc.navigationItem.largeTitleDisplayMode = .never
Expand Down
17 changes: 17 additions & 0 deletions BookPlayer/Profile/Account Screen/AccountViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class AccountViewController: BaseTableViewController<AccountCoordinator, Account
@IBOutlet weak var completeAccountButton: UIButton!
@IBOutlet var secondaryLabels: [UILabel]!

enum AccountSection: Int {
case info = 0, pro, logout, delete
}

override func viewDidLoad() {
super.viewDidLoad()

Expand Down Expand Up @@ -51,6 +55,19 @@ class AccountViewController: BaseTableViewController<AccountCoordinator, Account
return CGFloat.leastNormalMagnitude
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath as IndexPath, animated: true)

switch indexPath.section {
case AccountSection.logout.rawValue:
self.viewModel.handleLogout()
case AccountSection.delete.rawValue:
self.viewModel.handleDelete()
default:
break
}
}

@IBAction func didPressClose(_ sender: UIBarButtonItem) {
self.viewModel.dismiss()
}
Expand Down
16 changes: 16 additions & 0 deletions BookPlayer/Profile/Account Screen/AccountViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,25 @@
//

import Foundation
import BookPlayerKit

class AccountViewModel: BaseViewModel<AccountCoordinator> {
let accountService: AccountServiceProtocol

init(accountService: AccountServiceProtocol) {
self.accountService = accountService
}

func showCompleteAccount() {
self.coordinator.showCompleteAccount()
}

func handleLogout() {
self.accountService.logout()
self.dismiss()
}

func handleDelete() {
// TODO: handle delete account
}
}
1 change: 0 additions & 1 deletion BookPlayer/Profile/Login Screen/LoginViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class LoginViewModel: BaseViewModel<LoginCoordinator> {
Purchases.shared.logIn(appleIDCredential.user) { _, _, _ in }

// TODO: network call to create the user in backend
NotificationCenter.default.post(name: .accountUpdate, object: self)

self.coordinator.showCompleteAccount()
default:
Expand Down
2 changes: 0 additions & 2 deletions BookPlayer/Settings/Plus Screen/PlusViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,5 @@ final class PlusViewModel {
hasSubscription: nil,
accessToken: nil
)

NotificationCenter.default.post(name: .accountUpdate, object: self)
}
}
4 changes: 4 additions & 0 deletions Shared/Services/AccountService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public final class AccountService: AccountServiceProtocol {
}

self.dataManager.saveContext()

NotificationCenter.default.post(name: .accountUpdate, object: self)
}

public func logout() {
Expand All @@ -105,6 +107,8 @@ public final class AccountService: AccountServiceProtocol {
account.accessToken = ""

self.dataManager.saveContext()

NotificationCenter.default.post(name: .accountUpdate, object: self)
}

public func deleteAccount() {
Expand Down

0 comments on commit aac5f00

Please sign in to comment.