// // RemoteThreadViewModel.swift // Mastodon // // Created by MainasuK Cirno on 2021-4-12. // import os.log import UIKit import CoreDataStack import MastodonSDK final class RemoteThreadViewModel: ThreadViewModel { init(context: AppContext, statusID: Mastodon.Entity.Status.ID) { super.init(context: context, optionalStatus: nil) guard let activeMastodonAuthenticationBox = context.authenticationService.activeMastodonAuthenticationBox.value else { return } let domain = activeMastodonAuthenticationBox.domain context.apiService.status( domain: domain, statusID: statusID, authorizationBox: activeMastodonAuthenticationBox ) .retry(3) .sink { completion in switch completion { case .failure(let error): // TODO: handle error os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: remote status %s fetch failed: %s", ((#file as NSString).lastPathComponent), #line, #function, statusID, error.localizedDescription) case .finished: os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: remote status %s fetched", ((#file as NSString).lastPathComponent), #line, #function, statusID) } } receiveValue: { [weak self] response in guard let self = self else { return } let managedObjectContext = context.managedObjectContext let request = Status.sortedFetchRequest request.fetchLimit = 1 request.predicate = Status.predicate(domain: domain, id: response.value.id) guard let status = managedObjectContext.safeFetch(request).first else { assertionFailure() return } self.rootItem.value = .root(statusObjectID: status.objectID, attribute: Item.StatusAttribute()) } .store(in: &disposeBag) } }