Observe the top visible session
MODIFY
frontend/ios/DroidKaigi 2019/Scenes/Session/SessionDataSource.swift
frontend/ios/DroidKaigi 2019/Scenes/Session/SessionDataSource.swift
@@ -16,7 +16,8 @@ final class SessionDataSource: NSObject, UITableViewDataSource {
typealias Element = [SessionByStartTime]
var items: Element = []
- var toggleFavorite = PublishSubject<Session>()
+ let topVisibleSession = PublishSubject<Session>()
+ let toggleFavorite = PublishSubject<Session>()
private var cellHeightsCache = [IndexPath: CGFloat]()
@@ -64,12 +65,29 @@ extension SessionDataSource: UITableViewDelegate {
}
}
+extension SessionDataSource: UIScrollViewDelegate {
+
+ func scrollViewDidScroll(_ scrollView: UIScrollView) {
+ if let tableView = scrollView as? UITableView,
+ let indexPath = tableView.indexPathsForVisibleRows?.first {
+ let session = items[indexPath.section].sessions[indexPath.row]
+ topVisibleSession.onNext(session)
+ }
+ }
+}
+
extension SessionDataSource: RxTableViewDataSourceType {
- public func tableView(_ tableView: UITableView, observedEvent: Event<Element>) {
+ func tableView(_ tableView: UITableView, observedEvent: Event<Element>) {
Binder(self) { dataSource, element in
dataSource.items = element
tableView.reloadData()
+
+ // get the top visible session before scrolling table view
+ if let indexPath = tableView.indexPathsForVisibleRows?.first {
+ let session = dataSource.items[indexPath.section].sessions[indexPath.row]
+ dataSource.topVisibleSession.onNext(session)
+ }
}
.on(observedEvent)
}

MODIFY
frontend/ios/DroidKaigi 2019/Scenes/Session/SessionsViewController.swift
frontend/ios/DroidKaigi 2019/Scenes/Session/SessionsViewController.swift
@@ -67,6 +67,10 @@ final class SessionsViewController: UIViewController {
output.sessions
.drive(tableView.rx.items(dataSource: dataSource))
.disposed(by: bag)
+
+ dataSource.topVisibleSession
+ .subscribe(onNext: { print($0.id_) })
+ .disposed(by: bag)
}
}

Observe the top visible session