mastodon-ios/Mastodon/Scene/Compose/ComposeViewModel+PublishSta...

93 lines
3.5 KiB
Swift
Raw Normal View History

2021-03-18 10:33:07 +01:00
//
// ComposeViewModel+PublishState.swift
// Mastodon
//
// Created by MainasuK Cirno on 2021-3-18.
//
import os.log
import Foundation
2021-03-18 12:42:26 +01:00
import Combine
2021-03-18 10:33:07 +01:00
import GameplayKit
import MastodonSDK
extension ComposeViewModel {
class PublishState: GKState {
weak var viewModel: ComposeViewModel?
init(viewModel: ComposeViewModel) {
self.viewModel = viewModel
}
override func didEnter(from previousState: GKState?) {
os_log("%{public}s[%{public}ld], %{public}s: enter %s, previous: %s", ((#file as NSString).lastPathComponent), #line, #function, self.debugDescription, previousState.debugDescription)
}
}
}
extension ComposeViewModel.PublishState {
class Initial: ComposeViewModel.PublishState {
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
return stateClass == Publishing.self
}
}
class Publishing: ComposeViewModel.PublishState {
2021-03-18 12:42:26 +01:00
var publishingSubscription: AnyCancellable?
2021-03-18 10:33:07 +01:00
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
return stateClass == Fail.self || stateClass == Finish.self
}
override func didEnter(from previousState: GKState?) {
super.didEnter(from: previousState)
guard let viewModel = viewModel, let stateMachine = stateMachine else { return }
guard let mastodonAuthenticationBox = viewModel.activeAuthenticationBox.value else {
stateMachine.enter(Fail.self)
return
}
2021-03-18 12:42:26 +01:00
let mediaIDs = viewModel.attachmentServices.value.compactMap { attachmentService in
attachmentService.attachment.value?.id
}
2021-03-18 10:33:07 +01:00
let query = Mastodon.API.Statuses.PublishStatusQuery(
status: viewModel.composeStatusAttribute.composeContent.value,
2021-03-18 12:42:26 +01:00
mediaIDs: mediaIDs
2021-03-18 10:33:07 +01:00
)
2021-03-18 12:42:26 +01:00
publishingSubscription = viewModel.context.apiService.publishStatus(
2021-03-18 10:33:07 +01:00
domain: mastodonAuthenticationBox.domain,
query: query,
mastodonAuthenticationBox: mastodonAuthenticationBox
)
.receive(on: DispatchQueue.main)
.sink { completion in
switch completion {
case .failure(let error):
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: publish status %s", ((#file as NSString).lastPathComponent), #line, #function, error.localizedDescription)
stateMachine.enter(Fail.self)
case .finished:
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: publish status success", ((#file as NSString).lastPathComponent), #line, #function)
stateMachine.enter(Finish.self)
}
2021-03-18 12:42:26 +01:00
} receiveValue: { response in
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: status %s published: %s", ((#file as NSString).lastPathComponent), #line, #function, response.value.id, response.value.uri)
2021-03-18 10:33:07 +01:00
}
}
}
class Fail: ComposeViewModel.PublishState {
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
// allow discard publishing
return stateClass == Publishing.self || stateClass == Finish.self
}
}
class Finish: ComposeViewModel.PublishState {
override func isValidNextState(_ stateClass: AnyClass) -> Bool {
return false
}
}
}