From f70c2ed6df42da25202e86f2c69f5dc37b2c5a81 Mon Sep 17 00:00:00 2001 From: Marc Palmer Date: Wed, 5 Sep 2018 15:11:08 +0100 Subject: [PATCH 1/2] Auto-scroll to the current/next session in Programme --- .../ProgrammeTableViewController.swift | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/iOSDevUK/Controllers/Sessions/ProgrammeTableViewController.swift b/iOSDevUK/Controllers/Sessions/ProgrammeTableViewController.swift index ac60549..159b056 100644 --- a/iOSDevUK/Controllers/Sessions/ProgrammeTableViewController.swift +++ b/iOSDevUK/Controllers/Sessions/ProgrammeTableViewController.swift @@ -21,6 +21,8 @@ class ProgrammeTableViewController: UIViewController, UITableViewDelegate, UITab var selectedSessionItem: SessionItem? + var shouldScrollToCurrent: Bool = true + // MARK: - Lifecycle override func viewDidLoad() { @@ -43,6 +45,16 @@ class ProgrammeTableViewController: UIViewController, UITableViewDelegate, UITab super.didReceiveMemoryWarning() } + override func viewDidAppear(_ animated: Bool) { + super.viewDidAppear(animated) + + if shouldScrollToCurrent { + // We'd rather not animate, but how can we know when the table view knows the correct + // size for all the cells in order to scroll to the right place, before the view appears? + scrollToCurrentSession(animated: true) + } + } + // MARK: - Data Initialisation func accessDayList(withContext context: NSManagedObjectContext) -> [Day] { @@ -86,6 +98,41 @@ class ProgrammeTableViewController: UIViewController, UITableViewDelegate, UITab } } + /// Find the current or next session for the current set of sessions and scroll to it. + func scrollToCurrentSession(animated: Bool) { + guard let fetchedResultsController = fetchedResultsController, + let sections = fetchedResultsController.sections else { + return + } + + /// Find the session in the already-fetched results + let now = Date() + let nextOrCurrentSession = fetchedResultsController.fetchedObjects?.first { session in + guard let startTimeNSDate = session.startTime, + let endTimeNSDate = session.endTime else { + return false + } + let startTime = startTimeNSDate as Date + let endTime = endTimeNSDate as Date + return (startTime <= now && endTime >= now) || (startTime >= now) + } + + /// If we found one, resolve it to a section index and row + if let sessionToScrollTo = nextOrCurrentSession { + for (sectionIndex, section) in sections.enumerated() { + if let rowIndex = section.objects?.firstIndex(where: { + let session = $0 as! Session + return session == sessionToScrollTo + }) { + tableView.scrollToRow(at: IndexPath(row: rowIndex, section: sectionIndex), at: .top, animated: animated) + break + } + } + } + + shouldScrollToCurrent = false + } + // MARK: - Segmented Control func setupSegmentedControlWithDays(_ days: [Day]) { From 2b05d64e893235a657978d6c7343a1f7fb17ded4 Mon Sep 17 00:00:00 2001 From: Marc Palmer Date: Thu, 6 Sep 2018 17:15:51 +0100 Subject: [PATCH 2/2] Code review updates --- .../ProgrammeTableViewController.swift | 21 +++++-------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/iOSDevUK/Controllers/Sessions/ProgrammeTableViewController.swift b/iOSDevUK/Controllers/Sessions/ProgrammeTableViewController.swift index 159b056..8e616cf 100644 --- a/iOSDevUK/Controllers/Sessions/ProgrammeTableViewController.swift +++ b/iOSDevUK/Controllers/Sessions/ProgrammeTableViewController.swift @@ -100,33 +100,22 @@ class ProgrammeTableViewController: UIViewController, UITableViewDelegate, UITab /// Find the current or next session for the current set of sessions and scroll to it. func scrollToCurrentSession(animated: Bool) { - guard let fetchedResultsController = fetchedResultsController, - let sections = fetchedResultsController.sections else { + guard let fetchedResultsController = fetchedResultsController else { return } /// Find the session in the already-fetched results - let now = Date() let nextOrCurrentSession = fetchedResultsController.fetchedObjects?.first { session in - guard let startTimeNSDate = session.startTime, - let endTimeNSDate = session.endTime else { + guard let endTime = session.endTime else { return false } - let startTime = startTimeNSDate as Date - let endTime = endTimeNSDate as Date - return (startTime <= now && endTime >= now) || (startTime >= now) + return endTime.timeIntervalSinceNow > 0 } /// If we found one, resolve it to a section index and row if let sessionToScrollTo = nextOrCurrentSession { - for (sectionIndex, section) in sections.enumerated() { - if let rowIndex = section.objects?.firstIndex(where: { - let session = $0 as! Session - return session == sessionToScrollTo - }) { - tableView.scrollToRow(at: IndexPath(row: rowIndex, section: sectionIndex), at: .top, animated: animated) - break - } + if let indexPathOfSessionToScrollTo = fetchedResultsController.indexPath(forObject: sessionToScrollTo) { + tableView.scrollToRow(at: indexPathOfSessionToScrollTo, at: .top, animated: animated) } }