Skip to content

Commit bce34ba

Browse files
committed
Scroll to the current session instead of just scrolling to the top
1 parent d7656c8 commit bce34ba

File tree

3 files changed

+47
-31
lines changed

3 files changed

+47
-31
lines changed

trySwift/RootTabBar/RootTabBarController.swift

+9-6
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,21 @@ class RootTabBarController: UITabBarController, UITabBarControllerDelegate {
2020
if navigationController.viewControllers.count > 1 {
2121
navigationController.popViewController(animated: true)
2222
} else {
23-
navigationController.viewControllers.first?.scrollToTop()
23+
if let firstController = navigationController.viewControllers.first {
24+
if let scrollableToTop = firstController as? ScrollableToTop {
25+
scrollableToTop.scrollAfterTabTap()
26+
} else {
27+
firstController.view.findScrollSubview()?.setContentOffset(.zero, animated: true)
28+
}
29+
}
2430
}
2531
}
2632
}
2733
}
2834
}
2935

30-
private extension UIViewController {
31-
func scrollToTop() {
32-
let scrollView = view.findScrollSubview()
33-
scrollView?.setContentOffset(.zero, animated: true)
34-
}
36+
protocol ScrollableToTop {
37+
func scrollAfterTabTap()
3538
}
3639

3740
private extension UIView {

trySwift/ScheduleViewController.swift

+7-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ class ScheduleViewController: ButtonBarPagerTabStripViewController {
4747

4848
override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
4949
return days.map { SessionsTableViewController(conferenceDay: $0, scheduleViewController: self) }
50-
5150
}
5251

5352
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
@@ -59,6 +58,13 @@ class ScheduleViewController: ButtonBarPagerTabStripViewController {
5958
}
6059
}
6160

61+
extension ScheduleViewController: ScrollableToTop {
62+
func scrollAfterTabTap() {
63+
let controller = viewControllers[currentIndex] as! SessionsTableViewController
64+
controller.scrollAfterTabTap()
65+
}
66+
}
67+
6268
private extension ScheduleViewController {
6369

6470
@discardableResult

trySwift/SessionsTableViewController.swift

+31-24
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import UIKit
1010
import XLPagerTabStrip
1111
import TrySwiftData
1212

13+
1314
class SessionsTableViewController: UITableViewController {
1415
private lazy var needsToScrollToCurrentSession = Calendar.current.isDateInToday(conferenceDay.date)
1516

@@ -33,15 +34,15 @@ class SessionsTableViewController: UITableViewController {
3334
super.viewDidLoad()
3435

3536
configureTableView()
36-
37+
3738
if traitCollection.forceTouchCapability == .available {
3839
registerForPreviewing(with: self, sourceView: tableView)
3940
}
4041
}
41-
42+
4243
override func viewDidAppear(_ animated: Bool) {
4344
super.viewDidAppear(animated)
44-
45+
4546
if needsToScrollToCurrentSession {
4647
needsToScrollToCurrentSession = false
4748
scrollToCurrentSession(animated: false)
@@ -55,32 +56,32 @@ class SessionsTableViewController: UITableViewController {
5556
let isCollapsed = splitViewController?.isCollapsed,
5657
!isCollapsed,
5758
!didShowDetail else { return }
58-
59+
5960
didShowDetail = true
6061
scheduleViewController?.performSegue(withIdentifier: sessionDetailsSegue, sender: firstSelectableSessionVC)
6162
}
6263
}
6364

6465
// MARK: - Table view data source
6566
extension SessionsTableViewController {
66-
67+
6768
override func numberOfSections(in tableView: UITableView) -> Int {
6869
return conferenceDay.sessionBlocks.count
6970
}
70-
71+
7172
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
7273
return conferenceDay.sessionBlocks[section].sessions.count
7374
}
74-
75+
7576
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
7677
let cell = tableView.dequeueReusableCell(forIndexPath: indexPath) as SessionTableViewCell
77-
78+
7879
let session = conferenceDay.sessionBlocks[indexPath.section].sessions[indexPath.row]
7980
cell.configure(withSession: session)
80-
81+
8182
return cell
8283
}
83-
84+
8485
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
8586
let session = conferenceDay.sessionBlocks[section]
8687
let sessionDateFormatter = DateFormatter.sessionDateFormatter
@@ -108,33 +109,33 @@ extension SessionsTableViewController: IndicatorInfoProvider {
108109
}
109110

110111
extension SessionsTableViewController: UIViewControllerPreviewingDelegate {
111-
112+
112113
func previewingContext(_ previewingContext: UIViewControllerPreviewing, viewControllerForLocation location: CGPoint) -> UIViewController? {
113114
guard let indexPath = tableView.indexPathForRow(at: location) else { return nil }
114115
// This will show the cell clearly and blur the rest of the screen for our peek.
115116
previewingContext.sourceRect = tableView.rectForRow(at: indexPath)
116117
let session = conferenceDay.sessionBlocks[indexPath.section].sessions[indexPath.row]
117118
return viewController(for: session)
118119
}
119-
120+
120121
func previewingContext(_ previewingContext: UIViewControllerPreviewing, commit viewControllerToCommit: UIViewController) {
121122
scheduleViewController?.performSegue(withIdentifier: sessionDetailsSegue, sender: viewControllerToCommit)
122123
}
123124
}
124125

125126
extension SessionsTableViewController {
126-
127+
127128
func configureTableView() {
128-
129+
129130
tableView.register(SessionTableViewCell.self)
130-
131+
131132
tableView.estimatedRowHeight = 160
132133
tableView.rowHeight = UITableViewAutomaticDimension
133134
}
134135
}
135136

136137
private extension SessionsTableViewController {
137-
138+
138139
func viewController(for session: Session) -> UIViewController? {
139140
switch session.type {
140141
case .talk, .lightningTalk:
@@ -174,39 +175,39 @@ private extension SessionsTableViewController {
174175
default:
175176
return nil
176177
}
177-
178+
178179
return nil
179180
}
180-
181+
181182
func sessionDetails(_ presentation: Presentation, session: Session) -> UIViewController {
182183
let storyboard = UIStoryboard(name: "Main", bundle: nil)
183184
let sessionDetailsVC = storyboard.instantiateViewController(withIdentifier: String(describing: SessionDetailsViewController.self)) as! SessionDetailsViewController
184185
sessionDetailsVC.session = session
185186
sessionDetailsVC.presentation = presentation
186187
return sessionDetailsVC
187188
}
188-
189+
189190
func officeHourDetails(_ speaker: Speaker, session: Session) -> UIViewController {
190191
let officeHoursVC = OfficeHoursDetailViewController()
191192
officeHoursVC.speaker = speaker
192193
officeHoursVC.session = session
193194
return officeHoursVC
194195
}
195-
196+
196197
func webDisplay(_ event: Event) -> UIViewController {
197198
let webViewController = WebDisplayViewController()
198199
webViewController.url = URL(string: event.website!)
199200
webViewController.displayTitle = event.title
200201
return webViewController
201202
}
202-
203+
203204
func webDisplay(_ sponsor: Sponsor) -> UIViewController {
204205
let webViewController = WebDisplayViewController()
205206
webViewController.url = URL(string: sponsor.url!)
206207
webViewController.displayTitle = sponsor.name
207208
return webViewController
208209
}
209-
210+
210211
func venueDetails(_ venue: Venue) -> UIViewController {
211212
let venueDetailsVC = VenueTableViewController(venue: venue)
212213
venueDetailsVC.tableView.contentInset = UIEdgeInsets(top: 80,left: 0,bottom: 0,right: 0)
@@ -224,15 +225,21 @@ private extension SessionsTableViewController {
224225
}
225226

226227
extension SessionsTableViewController {
227-
228+
228229
func scrollToCurrentSession(animated: Bool) {
229230
let secondsFromGMT = TimeZone.current.secondsFromGMT()
230231
guard
231232
let date = Date().changed(second: secondsFromGMT),
232233
let section = conferenceDay.sessionBlocks.index(where: { date < $0.endTime }),
233234
!conferenceDay.sessionBlocks[section].sessions.isEmpty
234235
else { return }
235-
236+
236237
tableView.scrollToRow(at: IndexPath(row: 0, section: section), at: .top, animated: animated)
237238
}
238239
}
240+
241+
extension SessionsTableViewController: ScrollableToTop {
242+
func scrollAfterTabTap() {
243+
scrollToCurrentSession(animated: true)
244+
}
245+
}

0 commit comments

Comments
 (0)