Kurdtvs-Live-Kurdish-TV-Kur.../Mastodon/State/AppContext.swift

71 lines
2.4 KiB
Swift
Raw Normal View History

2021-01-27 07:50:13 +01:00
//
// AppContext.swift
// Mastodon
//
// Created by Cirno MainasuK on 2021-1-27.
//
import os.log
import UIKit
import Combine
import CoreData
import CoreDataStack
class AppContext: ObservableObject {
var disposeBag = Set<AnyCancellable>()
@Published var viewStateStore = ViewStateStore()
let coreDataStack: CoreDataStack
let managedObjectContext: NSManagedObjectContext
let backgroundManagedObjectContext: NSManagedObjectContext
2021-01-28 09:10:30 +01:00
let apiService: APIService
2021-02-03 09:01:08 +01:00
let authenticationService: AuthenticationService
2021-01-27 07:50:13 +01:00
let documentStore: DocumentStore
private var documentStoreSubscription: AnyCancellable!
2021-03-10 07:36:28 +01:00
let videoPlaybackService = VideoPlaybackService()
let audioPlaybackService = AudioPlaybackService()
2021-03-10 07:36:28 +01:00
2021-01-27 07:50:13 +01:00
let overrideTraitCollection = CurrentValueSubject<UITraitCollection?, Never>(nil)
init() {
let _coreDataStack = CoreDataStack()
let _managedObjectContext = _coreDataStack.persistentContainer.viewContext
let _backgroundManagedObjectContext = _coreDataStack.persistentContainer.newBackgroundContext()
coreDataStack = _coreDataStack
managedObjectContext = _managedObjectContext
backgroundManagedObjectContext = _backgroundManagedObjectContext
2021-01-28 09:10:30 +01:00
let _apiService = APIService(backgroundManagedObjectContext: _backgroundManagedObjectContext)
apiService = _apiService
2021-02-03 09:01:08 +01:00
authenticationService = AuthenticationService(
managedObjectContext: _managedObjectContext,
backgroundManagedObjectContext: _backgroundManagedObjectContext,
apiService: _apiService
)
2021-01-28 09:10:30 +01:00
2021-01-27 07:50:13 +01:00
documentStore = DocumentStore()
documentStoreSubscription = documentStore.objectWillChange
.receive(on: DispatchQueue.main)
.sink { [unowned self] in
self.objectWillChange.send()
}
backgroundManagedObjectContext.mergePolicy = NSMergePolicy.mergeByPropertyObjectTrump
NotificationCenter.default.publisher(for: .NSManagedObjectContextDidSave, object: backgroundManagedObjectContext)
.sink { [weak self] notification in
guard let self = self else { return }
self.managedObjectContext.perform {
self.managedObjectContext.mergeChanges(fromContextDidSave: notification)
}
}
.store(in: &disposeBag)
}
}