Skip to content

Commit dae8274

Browse files
Merge pull request #57 from rizumita/fix-schedule
Showing current session block when startup and schedule tab re-tapped
2 parents 883c805 + 76fbf31 commit dae8274

File tree

2 files changed

+54
-11
lines changed

2 files changed

+54
-11
lines changed

trySwift/ScheduleViewController.swift

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class ScheduleViewController: ButtonBarPagerTabStripViewController {
3030
buttonBarView.backgroundColor = .white
3131
settings.style.selectedBarBackgroundColor = .white
3232
buttonBarView.selectedBar.backgroundColor = .trySwiftAccentColor()
33+
34+
tabBarController?.delegate = self
3335
}
3436

3537
override func viewDidAppear(_ animated: Bool) {
@@ -59,14 +61,35 @@ class ScheduleViewController: ButtonBarPagerTabStripViewController {
5961

6062
private extension ScheduleViewController {
6163

62-
func moveToCorrectDate() {
63-
if days.count > 1 {
64-
let calendar = NSCalendar.current
65-
if let todaysIndex = days.enumerated()
66-
.first(where:{ calendar.isDateInToday($0.element.date) })
67-
.map({$0.offset}) {
68-
moveToViewController(at: todaysIndex, animated: false)
69-
}
64+
@discardableResult
65+
func moveToCorrectDate(animated: Bool = false) -> Int? {
66+
if days.count > 1 {
67+
let calendar = Calendar.current
68+
if let todaysIndex = days.enumerated()
69+
.first(where:{ calendar.isDateInToday($0.element.date) })
70+
.map({$0.offset}) {
71+
moveToViewController(at: todaysIndex, animated: animated)
72+
return todaysIndex
73+
}
74+
}
75+
76+
return .none
77+
}
78+
}
79+
80+
extension ScheduleViewController: UITabBarControllerDelegate {
81+
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
82+
guard
83+
tabBarController.selectedViewController === viewController,
84+
navigationController?.viewControllers.last === self
85+
else { return true }
86+
87+
guard
88+
let index = moveToCorrectDate(animated: true),
89+
let controller = viewControllers[index] as? SessionsTableViewController
90+
else { return true }
91+
controller.scrollToCurrentSession(animated: true)
92+
93+
return true
7094
}
71-
}
7295
}

trySwift/SessionsTableViewController.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import XLPagerTabStrip
1111
import TrySwiftData
1212

1313
class SessionsTableViewController: UITableViewController {
14-
14+
private lazy var needsToScrollToCurrentSession = Calendar.current.isDateInToday(conferenceDay.date)
15+
1516
var conferenceDay: ConferenceDay
1617
weak var scheduleViewController: ScheduleViewController?
1718

@@ -40,7 +41,12 @@ class SessionsTableViewController: UITableViewController {
4041

4142
override func viewDidAppear(_ animated: Bool) {
4243
super.viewDidAppear(animated)
43-
44+
45+
if needsToScrollToCurrentSession {
46+
needsToScrollToCurrentSession = false
47+
scrollToCurrentSession(animated: false)
48+
}
49+
4450
guard
4551
let firstSelectableSession = conferenceDay.sessionBlocks
4652
.flatMap({ $0.sessions })
@@ -216,3 +222,17 @@ private extension SessionsTableViewController {
216222
return workshopDetailVC
217223
}
218224
}
225+
226+
extension SessionsTableViewController {
227+
228+
func scrollToCurrentSession(animated: Bool) {
229+
let secondsFromGMT = TimeZone.current.secondsFromGMT()
230+
guard
231+
let date = Date().changed(second: secondsFromGMT),
232+
let section = conferenceDay.sessionBlocks.index(where: { date < $0.endTime }),
233+
!conferenceDay.sessionBlocks[section].sessions.isEmpty
234+
else { return }
235+
236+
tableView.scrollToRow(at: IndexPath(row: 0, section: section), at: .top, animated: animated)
237+
}
238+
}

0 commit comments

Comments
 (0)