Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A coordinator handles the app's navigation #626

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 0 additions & 13 deletions BeeSwift/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,4 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
private func removeAllLocalNotifications() {
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
}
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
if userActivity.activityType == CSSearchableItemActionType {
guard let goalIdentifier = userActivity.userInfo?[CSSearchableItemActivityIdentifier] as? String else { return false
}
NotificationCenter.default.post(name: GalleryViewController.NotificationName.openGoal, object: nil, userInfo: ["identifier": goalIdentifier])
} else if let intent = userActivity.interaction?.intent as? AddDataIntent {
guard let goalSlug = intent.goal else { return false }
NotificationCenter.default.post(name: GalleryViewController.NotificationName.openGoal, object: nil, userInfo: ["slug": goalSlug])
} else if let goalSlug = userActivity.userInfo?["slug"] {
NotificationCenter.default.post(name: GalleryViewController.NotificationName.openGoal, object: nil, userInfo: ["slug": goalSlug])
}
return true
}
}
62 changes: 7 additions & 55 deletions BeeSwift/Gallery/GalleryViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class GalleryViewController: UIViewController {
private let goalManager: GoalManager
private let healthStoreManager: HealthStoreManager
private let requestManager: RequestManager
private weak var coordinator: MainCoordinator?

private lazy var stackView: UIStackView = {
let stackView = UIStackView()
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 @@ -159,8 +162,6 @@ class GalleryViewController: UIViewController {

NotificationCenter.default.addObserver(self, selector: #selector(self.userDefaultsDidChange), name: UserDefaults.didChangeNotification, object: nil)
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 @@ -275,20 +276,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 @@ -314,23 +303,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 @@ -426,35 +405,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
Loading