Skip to content

Commit

Permalink
introduce coordinator for handling navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
krugerk committed Jan 26, 2025
1 parent 638b04a commit dc06f35
Show file tree
Hide file tree
Showing 10 changed files with 332 additions and 161 deletions.
4 changes: 4 additions & 0 deletions BeeSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
9B65F2322CFA6427009674A7 /* DeeplinkGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9B65F2312CFA6418009674A7 /* DeeplinkGenerator.swift */; };
9B8CA57D24B120CA009C86C2 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 9B8CA57C24B120CA009C86C2 /* LaunchScreen.storyboard */; };
9BD4C4E82D45A09F00B03E99 /* MainCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9BD4C4E72D45A09F00B03E99 /* MainCoordinator.swift */; };
9BFB27E92CFE770F0056D10D /* FreshnessIndicatorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9BFB27E82CFE770F0056D10D /* FreshnessIndicatorView.swift */; };
A10D4E931B07948500A72D29 /* DatapointsTableView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A10D4E921B07948500A72D29 /* DatapointsTableView.swift */; };
A10DC2DF207BFCBA00FB7B3A /* RemoveHKMetricViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A10DC2DE207BFCBA00FB7B3A /* RemoveHKMetricViewController.swift */; };
Expand Down Expand Up @@ -218,6 +219,7 @@
/* Begin PBXFileReference section */
9B65F2312CFA6418009674A7 /* DeeplinkGenerator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeeplinkGenerator.swift; sourceTree = "<group>"; };
9B8CA57C24B120CA009C86C2 /* LaunchScreen.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = LaunchScreen.storyboard; sourceTree = "<group>"; };
9BD4C4E72D45A09F00B03E99 /* MainCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainCoordinator.swift; sourceTree = "<group>"; };
9BFB27E82CFE770F0056D10D /* FreshnessIndicatorView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FreshnessIndicatorView.swift; sourceTree = "<group>"; };
A10D4E921B07948500A72D29 /* DatapointsTableView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatapointsTableView.swift; sourceTree = "<group>"; };
A10DC2DE207BFCBA00FB7B3A /* RemoveHKMetricViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoveHKMetricViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -473,6 +475,7 @@
A196CB161AE4142E00B90A3E /* BeeSwift */ = {
isa = PBXGroup;
children = (
9BD4C4E72D45A09F00B03E99 /* MainCoordinator.swift */,
9B65F2312CFA6418009674A7 /* DeeplinkGenerator.swift */,
A1E618E51E79E01900D8ED93 /* Cells */,
E46071002B43DA7100305DB4 /* Gallery */,
Expand Down Expand Up @@ -1023,6 +1026,7 @@
A10DC2DF207BFCBA00FB7B3A /* RemoveHKMetricViewController.swift in Sources */,
E412DAE12B86A8F70099E483 /* GoalImageView.swift in Sources */,
A1619EA41BEECC1500E14B3A /* EditDefaultNotificationsViewController.swift in Sources */,
9BD4C4E82D45A09F00B03E99 /* MainCoordinator.swift in Sources */,
A149B3701AEF528C00F19A09 /* SettingsViewController.swift in Sources */,
E46DC80F2AA58DF20059FDFE /* PullToRefreshHint.swift in Sources */,
A1E618E21E78158700D8ED93 /* HealthKitConfigViewController.swift in Sources */,
Expand Down
62 changes: 7 additions & 55 deletions BeeSwift/Gallery/GalleryViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import BeeKit


class GalleryViewController: UIViewController {
private weak var coordinator: MainCoordinator?
let logger = Logger(subsystem: "com.beeminder.beeminder", category: "GalleryViewController")

public enum NotificationName {
Expand Down Expand Up @@ -131,13 +132,15 @@ class GalleryViewController: UIViewController {
versionManager: VersionManager,
goalManager: GoalManager,
healthStoreManager: HealthStoreManager,
requestManager: RequestManager) {
requestManager: RequestManager,
coordinator: MainCoordinator) {
self.currentUserManager = currentUserManager
self.viewContext = viewContext
self.versionManager = versionManager
self.goalManager = goalManager
self.healthStoreManager = healthStoreManager
self.requestManager = requestManager
self.coordinator = coordinator

let fetchRequest = Goal.fetchRequest() as! NSFetchRequest<Goal>
fetchRequest.sortDescriptors = Self.preferredSort
Expand All @@ -158,8 +161,6 @@ class GalleryViewController: UIViewController {
super.viewDidLoad()

NotificationCenter.default.addObserver(self, selector: #selector(self.handleSignIn), name: CurrentUserManager.NotificationName.signedIn, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.handleSignOut), name: CurrentUserManager.NotificationName.signedOut, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.openGoalFromNotification(_:)), name: GalleryViewController.NotificationName.openGoal, object: nil)

self.view.addSubview(self.stackView)
stackView.snp.makeConstraints { (make) -> Void in
Expand Down Expand Up @@ -274,20 +275,8 @@ class GalleryViewController: UIViewController {
}
}

override func viewDidAppear(_ animated: Bool) {
if !currentUserManager.signedIn(context: viewContext) {
let signInVC = SignInViewController(currentUserManager: currentUserManager)
signInVC.modalPresentationStyle = .fullScreen
self.present(signInVC, animated: true, completion: nil)
}
}

@objc func settingsButtonPressed() {
self.navigationController?.pushViewController(SettingsViewController(
currentUserManager: currentUserManager,
viewContext: viewContext,
goalManager: goalManager,
requestManager: requestManager), animated: true)
coordinator?.showSettings()
}

@objc func searchButtonPressed() {
Expand All @@ -307,23 +296,13 @@ class GalleryViewController: UIViewController {
}

@objc func handleSignIn() {
self.dismiss(animated: true, completion: nil)
self.fetchGoals()

UNUserNotificationCenter.current().requestAuthorization(options: UNAuthorizationOptions([.alert, .badge, .sound])) { [weak self] (success, error) in
self?.logger.info("Requested persons authorization upon signin to allow local and remote notifications; successful? \(success)")
self?.logger.info("Requested person's authorization upon signin to allow local and remote notifications; successful? \(success)")
}
}

@objc func handleSignOut() {
if self.presentedViewController != nil {
if type(of: self.presentedViewController!) == SignInViewController.self { return }
}
let signInVC = SignInViewController(currentUserManager: currentUserManager)
signInVC.modalPresentationStyle = .fullScreen
self.present(signInVC, animated: true, completion: nil)
}

func updateDeadbeatVisibility() {
self.deadbeatView.isHidden = !isUserKnownDeadbeat
}
Expand Down Expand Up @@ -415,35 +394,8 @@ class GalleryViewController: UIViewController {
})
}

@objc func openGoalFromNotification(_ notification: Notification) {
guard let notif = notification as NSNotification? else { return }
var matchingGoal: Goal?

if let identifier = notif.userInfo?["identifier"] as? String {
if let url = URL(string: identifier), let objectID = viewContext.persistentStoreCoordinator?.managedObjectID(forURIRepresentation: url) {
matchingGoal = viewContext.object(with: objectID) as? Goal
}
}
else if let slug = notif.userInfo?["slug"] as? String {
matchingGoal = self.filteredGoals.filter({ (goal) -> Bool in
return goal.slug == slug
}).last
}
if matchingGoal != nil {
self.navigationController?.popToRootViewController(animated: false)
self.openGoal(matchingGoal!)
}
}

func openGoal(_ goal: Goal) {
let goalViewController = GoalViewController(
goal: goal,
healthStoreManager: healthStoreManager,
goalManager: goalManager,
requestManager: requestManager,
currentUserManager: currentUserManager,
viewContext: viewContext)
self.navigationController?.pushViewController(goalViewController, animated: true)
coordinator?.showGoal(goal)
}

private func configureDataSource() {
Expand Down
14 changes: 6 additions & 8 deletions BeeSwift/GoalViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class GoalViewController: UIViewController, UIScrollViewDelegate, DatapointTabl
private let requestManager: RequestManager
private let currentUserManager: CurrentUserManager
private let viewContext: NSManagedObjectContext
private weak var coordinator: MainCoordinator?

private let timeElapsedView = FreshnessIndicatorView()
fileprivate var goalImageView = GoalImageView(isThumbnail: false)
Expand All @@ -56,13 +57,15 @@ class GoalViewController: UIViewController, UIScrollViewDelegate, DatapointTabl
goalManager: GoalManager,
requestManager: RequestManager,
currentUserManager: CurrentUserManager,
viewContext: NSManagedObjectContext) {
viewContext: NSManagedObjectContext,
coordinator: MainCoordinator) {
self.goal = goal
self.healthStoreManager = healthStoreManager
self.goalManager = goalManager
self.requestManager = requestManager
self.currentUserManager = currentUserManager
self.viewContext = viewContext
self.coordinator = coordinator
super.init(nibName: nil, bundle: nil)
}

Expand Down Expand Up @@ -347,9 +350,7 @@ class GoalViewController: UIViewController, UIScrollViewDelegate, DatapointTabl
}

@objc func timerButtonPressed() {
let controller = TimerViewController(goal: self.goal, requestManager: self.requestManager)
controller.modalPresentationStyle = .fullScreen
self.present(controller, animated: true, completion: nil)
coordinator?.showTimerForGoal(goal)
}

@objc func refreshButtonPressed() {
Expand Down Expand Up @@ -387,10 +388,7 @@ class GoalViewController: UIViewController, UIScrollViewDelegate, DatapointTabl
guard !self.goal.hideDataEntry else { return }
guard let existingDatapoint = datapoint as? DataPoint else { return }

let editDatapointViewController = EditDatapointViewController(goal: goal, datapoint: existingDatapoint, requestManager: self.requestManager, goalManager: self.goalManager)
let navigationController = UINavigationController(rootViewController: editDatapointViewController)
navigationController.modalPresentationStyle = .formSheet
self.present(navigationController, animated: true, completion: nil)
coordinator?.showEditDatapointForGoal(goal: goal, datapoint: existingDatapoint)
}

@objc func dateStepperValueChanged() {
Expand Down
Loading

0 comments on commit dc06f35

Please sign in to comment.