mirror of
https://github.com/tappollo/WWDC.git
synced 2026-04-30 04:45:17 +08:00
Fixed object relationships in storage
This commit is contained in:
@@ -30,6 +30,9 @@ public class Event: Object {
|
||||
/// Sessions held at this event
|
||||
public let sessions = List<Session>()
|
||||
|
||||
/// Session instances for schedule
|
||||
public var sessionInstances = List<SessionInstance>()
|
||||
|
||||
public override class func primaryKey() -> String? {
|
||||
return "identifier"
|
||||
}
|
||||
|
||||
@@ -23,6 +23,10 @@ final class SessionAssetsJSONAdapter: Adapter {
|
||||
typealias OutputType = [SessionAsset]
|
||||
|
||||
func adapt(_ input: JSON) -> Result<[SessionAsset], AdapterError> {
|
||||
guard let title = input[AssetKeys.title].string else {
|
||||
return .error(.missingKey(AssetKeys.title))
|
||||
}
|
||||
|
||||
guard let sessionId = input[AssetKeys.id].string else {
|
||||
return .error(.missingKey(AssetKeys.id))
|
||||
}
|
||||
@@ -31,10 +35,6 @@ final class SessionAssetsJSONAdapter: Adapter {
|
||||
return .error(.missingKey(AssetKeys.year))
|
||||
}
|
||||
|
||||
guard let title = input[AssetKeys.title].string else {
|
||||
return .error(.missingKey(AssetKeys.title))
|
||||
}
|
||||
|
||||
guard let url = input[AssetKeys.url].string else {
|
||||
return .error(.missingKey(AssetKeys.url))
|
||||
}
|
||||
|
||||
@@ -31,6 +31,13 @@ public final class Storage {
|
||||
}
|
||||
}
|
||||
|
||||
private func associateSessionInstances(with event: Event, withoutNotifying tokens: [NotificationToken]) {
|
||||
let instances = realm.objects(SessionInstance.self).filter("startTime >= %@ AND endTime <= %@", event.startDate, event.endDate)
|
||||
event.sessionInstances = List<SessionInstance>(instances)
|
||||
|
||||
store(objects: [event], withoutNotifying: tokens)
|
||||
}
|
||||
|
||||
public func store(schedule: ScheduleResponse, withoutNotifying tokens: [NotificationToken] = []) {
|
||||
schedule.rooms.forEach { room in
|
||||
let instances = schedule.instances.filter({ $0.roomName == room.name })
|
||||
@@ -47,6 +54,8 @@ public final class Storage {
|
||||
store(objects: schedule.instances, withoutNotifying: tokens)
|
||||
store(objects: schedule.rooms, withoutNotifying: tokens)
|
||||
store(objects: schedule.tracks, withoutNotifying: tokens)
|
||||
|
||||
realm.objects(Event.self).forEach({ self.associateSessionInstances(with: $0, withoutNotifying: tokens) })
|
||||
}
|
||||
|
||||
public func store(sessionsResponse: SessionsResponse, withoutNotifying tokens: [NotificationToken] = []) {
|
||||
@@ -70,24 +79,18 @@ public final class Storage {
|
||||
session.assets.append(objectsIn: assets)
|
||||
}
|
||||
|
||||
sessionsResponse.events.forEach({ self.associateSessionInstances(with: $0, withoutNotifying: tokens) })
|
||||
|
||||
store(objects: sessionsResponse.assets, withoutNotifying: tokens)
|
||||
store(objects: sessionsResponse.sessions, withoutNotifying: tokens)
|
||||
store(objects: sessionsResponse.events, withoutNotifying: tokens)
|
||||
store(objects: tracks, withoutNotifying: tokens)
|
||||
}
|
||||
|
||||
public typealias ScheduleObservable = Observable<Results<SessionInstance>>
|
||||
|
||||
public lazy var schedule: ScheduleObservable = {
|
||||
let latestEvent = self.realm.objects(Event.self).sorted(byKeyPath: "startDate", ascending: false).first
|
||||
public lazy var events: Observable<Results<Event>> = {
|
||||
let eventsSortedByDateDescending = self.realm.objects(Event.self).sorted(byKeyPath: "startDate", ascending: false)
|
||||
|
||||
return Observable.from(latestEvent).flatMap { event -> ScheduleObservable in
|
||||
let results = self.realm.objects(SessionInstance.self)
|
||||
.filter("startTime >= %@ AND endTime <= %@", event.startDate, event.endDate)
|
||||
.sorted(byKeyPath: "startTime")
|
||||
|
||||
return Observable.collection(from: results)
|
||||
}
|
||||
return Observable.collection(from: eventsSortedByDateDescending)
|
||||
}()
|
||||
|
||||
}
|
||||
|
||||
@@ -40,8 +40,8 @@ class ViewController: NSViewController {
|
||||
do {
|
||||
storage = try Storage(realmConfiguration!)
|
||||
|
||||
storage.schedule.observeOn(MainScheduler.instance).subscribe(onNext: { schedule in
|
||||
NSLog("Schedule = \(schedule.toArray())")
|
||||
storage.events.observeOn(MainScheduler.instance).subscribe(onNext: { events in
|
||||
NSLog("Events = \(events.toArray())")
|
||||
}).addDisposableTo(self.disposeBag)
|
||||
|
||||
client.fetchSessions { [weak self] result in
|
||||
@@ -59,6 +59,7 @@ class ViewController: NSViewController {
|
||||
NSAlert(error: error).runModal()
|
||||
case .success(let response):
|
||||
self?.storage.store(schedule: response)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user