2021-03-11 08:41:27 +01:00
|
|
|
//
|
|
|
|
// ComposeViewModel.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-3-11.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
|
|
|
|
final class ComposeViewModel {
|
|
|
|
|
2021-03-12 07:18:07 +01:00
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
2021-03-11 08:41:27 +01:00
|
|
|
// input
|
|
|
|
let context: AppContext
|
2021-03-12 07:18:07 +01:00
|
|
|
let composeKind: ComposeStatusSection.ComposeKind
|
|
|
|
let composeTootAttribute = ComposeStatusItem.ComposeTootAttribute()
|
|
|
|
let composeContent = CurrentValueSubject<String, Never>("")
|
|
|
|
let activeAuthentication: CurrentValueSubject<MastodonAuthentication?, Never>
|
2021-03-11 08:41:27 +01:00
|
|
|
|
|
|
|
// output
|
|
|
|
var diffableDataSource: UITableViewDiffableDataSource<ComposeStatusSection, ComposeStatusItem>!
|
2021-03-12 07:18:07 +01:00
|
|
|
|
|
|
|
// UI & UX
|
2021-03-11 08:41:27 +01:00
|
|
|
let title: CurrentValueSubject<String, Never>
|
|
|
|
let shouldDismiss = CurrentValueSubject<Bool, Never>(true)
|
2021-03-12 08:57:58 +01:00
|
|
|
let isComposeTootBarButtonItemEnabled = CurrentValueSubject<Bool, Never>(false)
|
2021-03-11 08:41:27 +01:00
|
|
|
|
|
|
|
init(
|
|
|
|
context: AppContext,
|
2021-03-12 07:18:07 +01:00
|
|
|
composeKind: ComposeStatusSection.ComposeKind
|
2021-03-11 08:41:27 +01:00
|
|
|
) {
|
|
|
|
self.context = context
|
|
|
|
self.composeKind = composeKind
|
|
|
|
switch composeKind {
|
2021-03-15 06:42:46 +01:00
|
|
|
case .post: self.title = CurrentValueSubject(L10n.Scene.Compose.Title.newPost)
|
|
|
|
case .reply: self.title = CurrentValueSubject(L10n.Scene.Compose.Title.newReply)
|
2021-03-11 08:41:27 +01:00
|
|
|
}
|
2021-03-12 07:18:07 +01:00
|
|
|
self.activeAuthentication = CurrentValueSubject(context.authenticationService.activeMastodonAuthentication.value)
|
2021-03-11 08:41:27 +01:00
|
|
|
// end init
|
2021-03-12 07:18:07 +01:00
|
|
|
|
|
|
|
// bind active authentication
|
|
|
|
context.authenticationService.activeMastodonAuthentication
|
|
|
|
.assign(to: \.value, on: activeAuthentication)
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
|
|
|
activeAuthentication
|
|
|
|
.sink { [weak self] mastodonAuthentication in
|
|
|
|
guard let self = self else { return }
|
|
|
|
let mastodonUser = mastodonAuthentication?.user
|
|
|
|
let username = mastodonUser?.username ?? " "
|
2021-03-11 08:41:27 +01:00
|
|
|
|
2021-03-12 07:18:07 +01:00
|
|
|
self.composeTootAttribute.avatarURL.value = mastodonUser?.avatarImageURL()
|
|
|
|
self.composeTootAttribute.displayName.value = {
|
|
|
|
guard let displayName = mastodonUser?.displayName, !displayName.isEmpty else {
|
|
|
|
return username
|
|
|
|
}
|
|
|
|
return displayName
|
|
|
|
}()
|
|
|
|
self.composeTootAttribute.username.value = username
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2021-03-12 08:57:58 +01:00
|
|
|
|
|
|
|
composeTootAttribute.composeContent
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.map { content in
|
|
|
|
let content = content?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
|
|
|
|
return !content.isEmpty
|
|
|
|
}
|
|
|
|
.assign(to: \.value, on: isComposeTootBarButtonItemEnabled)
|
|
|
|
.store(in: &disposeBag)
|
|
|
|
|
|
|
|
composeTootAttribute.composeContent
|
|
|
|
.receive(on: DispatchQueue.main)
|
|
|
|
.map { content in
|
|
|
|
let content = content ?? ""
|
|
|
|
return content.isEmpty
|
|
|
|
}
|
|
|
|
.assign(to: \.value, on: shouldDismiss)
|
|
|
|
.store(in: &disposeBag)
|
2021-03-11 08:41:27 +01:00
|
|
|
}
|
2021-03-12 07:18:07 +01:00
|
|
|
|
2021-03-11 08:41:27 +01:00
|
|
|
}
|