Combine session stream with favorite stream
MODIFY
frontend/ios/DroidKaigi 2019/Extensions/Session+Extension.swift
frontend/ios/DroidKaigi 2019/Extensions/Session+Extension.swift
import Foundation
import ioscombined
+extension Session {
+
+ var startTimeText: String {
+ struct Static {
+ static let dateFormatter: DateFormatter = {
+ let dateFormatter = DateFormatter()
+ dateFormatter.timeZone = TimeZone(identifier: "Asia/Tokyo")
+ dateFormatter.locale = NSLocale.current
+ dateFormatter.dateFormat = "HH:mm"
+ return dateFormatter
+ }()
+ }
+ return Static.dateFormatter.string(from: Date(timeIntervalSince1970: startTime / 1000))
+ }
+}
+
extension SpeechSession {
var tagContents: [TagContent] {
@@ -22,4 +38,69 @@ extension SpeechSession {
tags.append(.category(category: category))
return tags
}
+
+ // redefine for now
+ func doCopy(id: String? = nil,
+ dayNumber: Int32? = nil,
+ startTime: Double? = nil,
+ endTime: Double? = nil,
+ title: LocaledString? = nil,
+ desc: String? = nil,
+ room: Room? = nil,
+ format: String? = nil,
+ lang: Lang? = nil,
+ category: ioscombined.Category? = nil,
+ intendedAudience: String? = nil,
+ videoUrl: String? = nil,
+ slideUrl: String? = nil,
+ isInterpretationTarget: Bool? = nil,
+ isFavorited: Bool? = nil,
+ speakers: [Speaker]? = nil,
+ forBeginners: Bool? = nil,
+ message: LocaledString? = nil
+ ) -> SpeechSession {
+ return doCopy(id: id ?? self.id_,
+ dayNumber: dayNumber ?? self.dayNumber,
+ startTime: startTime ?? self.startTime,
+ endTime: endTime ?? self.endTime,
+ title: title ?? self.title,
+ desc: desc ?? self.desc,
+ room: room ?? self.room,
+ format: format ?? self.format,
+ lang: lang ?? self.lang,
+ category: category ?? self.category,
+ intendedAudience: intendedAudience ?? self.intendedAudience,
+ videoUrl: videoUrl ?? self.videoUrl,
+ slideUrl: slideUrl ?? self.slideUrl,
+ isInterpretationTarget: isInterpretationTarget ?? self.isInterpretationTarget,
+ isFavorited: isFavorited ?? self.isFavorited,
+ speakers: speakers ?? self.speakers,
+ forBeginners: forBeginners ?? self.forBeginners,
+ message: message ?? self.message)
+ }
+}
+
+extension ServiceSession {
+
+ // redefine for now
+ func doCopy(id: String? = nil,
+ dayNumber: Int32? = nil,
+ startTime: Double? = nil,
+ endTime: Double? = nil,
+ title: LocaledString? = nil,
+ desc: String? = nil,
+ room: Room? = nil,
+ sessionType: SessionType? = nil,
+ isFavorited: Bool? = nil
+ ) -> ServiceSession {
+ return doCopy(id: id ?? self.id_,
+ dayNumber: dayNumber ?? self.dayNumber,
+ startTime: startTime ?? self.startTime,
+ endTime: endTime ?? self.endTime,
+ title: title ?? self.title,
+ desc: desc ?? self.desc,
+ room: room ?? self.room,
+ sessionType: sessionType ?? self.sessionType,
+ isFavorited: isFavorited ?? self.isFavorited)
+ }
}

MODIFY
frontend/ios/DroidKaigi 2019/Scenes/Session/SessionDataSource.swift
frontend/ios/DroidKaigi 2019/Scenes/Session/SessionDataSource.swift
@@ -16,7 +16,7 @@ final class SessionDataSource: NSObject, UITableViewDataSource {
typealias Element = [SessionByStartTime]
var items: Element = []
- var toggleFavorite = PublishSubject<String>()
+ var toggleFavorite = PublishSubject<Session>()
func numberOfSections(in tableView: UITableView) -> Int {
return items.count
@@ -31,7 +31,7 @@ final class SessionDataSource: NSObject, UITableViewDataSource {
let session = items[indexPath.section].sessions[indexPath.row]
cell.session = session
cell.favoriteButtonDidTapped
- .map { _ in session.id_ }
+ .map { _ in session }
.bind(to: toggleFavorite)
.disposed(by: cell.bag)
return cell

MODIFY
frontend/ios/DroidKaigi 2019/Scenes/Session/SessionsViewController.swift
frontend/ios/DroidKaigi 2019/Scenes/Session/SessionsViewController.swift
@@ -45,7 +45,8 @@ final class SessionsViewController: UIViewController, StoryboardInstantiable {
private func bind() {
let viewWillAppear = rx.methodInvoked(#selector(self.viewWillAppear)).map { _ in }
- let input = SessionsViewModel.Input(viewWillAppear: viewWillAppear)
+ let toggleFavorite = dataSource.toggleFavorite
+ let input = SessionsViewModel.Input(viewWillAppear: viewWillAppear, toggleFavorite: toggleFavorite)
let output = viewModel.transform(input: input)
output.error
.drive(onNext: { errorMessage in
@@ -57,10 +58,6 @@ final class SessionsViewController: UIViewController, StoryboardInstantiable {
output.sessions
.drive(tableView.rx.items(dataSource: dataSource))
.disposed(by: bag)
-
- dataSource.toggleFavorite
- .subscribe { print($0) }
- .disposed(by: bag)
}
}

MODIFY
frontend/ios/DroidKaigi 2019/Scenes/Session/SessionsViewModel.swift
frontend/ios/DroidKaigi 2019/Scenes/Session/SessionsViewModel.swift
@@ -26,6 +26,7 @@ extension SessionsViewModel {
struct Input {
let viewWillAppear: Observable<Void>
+ let toggleFavorite: Observable<Session>
}
struct Output {
@@ -34,6 +35,10 @@ extension SessionsViewModel {
}
func transform(input: Input) -> Output {
+ input.toggleFavorite
+ .subscribe(onNext: { [weak self] in self?.favoriteRepository.toggle(sessionId: $0.id_) })
+ .disposed(by: bag)
+
let sessionContents = input.viewWillAppear
.flatMap { [weak self] (_) -> Observable<SessionContents> in
guard let `self` = self else { return Observable.empty() }
@@ -47,28 +52,32 @@ extension SessionsViewModel {
let favoriteSessionIds = favoriteRepository.sessionIdsDidChanged
- favoriteSessionIds.subscribe { print($0) }.disposed(by: bag)
-
- let dateFormatter = DateFormatter()
- dateFormatter.timeZone = TimeZone(identifier: "Asia/Tokyo")
- dateFormatter.locale = NSLocale.current
- dateFormatter.dateFormat = "HH:mm"
-
- let sessions = sessionContents.map { [weak self] sessionContents -> [SessionByStartTime] in
+ let sessions = Observable.combineLatest(sessionContents, favoriteSessionIds)
+ .map { [weak self] (sessionContents, favoriteSessionIds) -> [SessionByStartTime] in
guard let `self` = self else { return [] }
return sessionContents.sessions
.filter { $0.dayNumber == self.day.day }
+ .map { (session: Session) -> Session in
+ if let session = session as? ServiceSession {
+ return session.doCopy(isFavorited: favoriteSessionIds.contains(session.id_))
+ } else if let session = session as? SpeechSession {
+ return session.doCopy(isFavorited: favoriteSessionIds.contains(session.id_))
+ } else {
+ return session
+ }
+ }
.reduce(into: [SessionByStartTime]()) { result, session in
if let index = result.firstIndex(where: { $0.startTime == session.startTime }) {
result[index].sessions.append(session)
return
}
result.append(SessionByStartTime(startDayText: session.startDayText,
- startTimeText: dateFormatter.string(from: Date(timeIntervalSince1970: session.startTime / 1000)),
+ startTimeText: session.startTimeText,
startTime: session.startTime,
sessions: [session]))
}
- }.asDriver(onErrorJustReturn: [])
+ }
+ .asDriver(onErrorJustReturn: [])
let error = _error.asDriver()
return Output(sessions: sessions, error: error)
}

Combine session stream with favorite stream