2022-01-27 14:23:39 +01:00
|
|
|
//
|
|
|
|
// ProfileAboutViewModel.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK on 2022-1-22.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
2022-05-26 17:19:47 +02:00
|
|
|
import CoreDataStack
|
2022-01-27 14:23:39 +01:00
|
|
|
import MastodonSDK
|
|
|
|
import MastodonMeta
|
|
|
|
import Kanna
|
|
|
|
|
|
|
|
final class ProfileAboutViewModel {
|
|
|
|
|
|
|
|
var disposeBag = Set<AnyCancellable>()
|
|
|
|
|
|
|
|
// input
|
|
|
|
let context: AppContext
|
2022-05-26 17:19:47 +02:00
|
|
|
@Published var user: MastodonUser?
|
2022-01-27 14:23:39 +01:00
|
|
|
@Published var isEditing = false
|
|
|
|
@Published var accountForEdit: Mastodon.Entity.Account?
|
|
|
|
|
|
|
|
// output
|
|
|
|
var diffableDataSource: UICollectionViewDiffableDataSource<ProfileFieldSection, ProfileFieldItem>?
|
2022-05-26 17:19:47 +02:00
|
|
|
let profileInfo = ProfileInfo()
|
|
|
|
let profileInfoEditing = ProfileInfo()
|
2022-01-27 14:23:39 +01:00
|
|
|
|
2022-05-26 17:19:47 +02:00
|
|
|
@Published var fields: [MastodonField] = []
|
|
|
|
@Published var emojiMeta: MastodonContent.Emojis = [:]
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
init(context: AppContext) {
|
|
|
|
self.context = context
|
|
|
|
// end init
|
|
|
|
|
2022-05-26 17:19:47 +02:00
|
|
|
$user
|
|
|
|
.compactMap { $0 }
|
|
|
|
.flatMap { $0.publisher(for: \.emojis) }
|
|
|
|
.map { $0.asDictionary }
|
|
|
|
.assign(to: &$emojiMeta)
|
|
|
|
|
|
|
|
$user
|
|
|
|
.compactMap { $0 }
|
|
|
|
.flatMap { $0.publisher(for: \.fields) }
|
|
|
|
.assign(to: &$fields)
|
|
|
|
|
|
|
|
Publishers.CombineLatest(
|
|
|
|
$fields,
|
|
|
|
$emojiMeta
|
|
|
|
)
|
|
|
|
.map { fields, emojiMeta in
|
|
|
|
fields.map { ProfileFieldItem.FieldValue(name: $0.name, value: $0.value, emojiMeta: emojiMeta) }
|
|
|
|
}
|
|
|
|
.assign(to: &profileInfo.$fields)
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
Publishers.CombineLatest(
|
2022-05-26 17:19:47 +02:00
|
|
|
$accountForEdit,
|
|
|
|
$emojiMeta
|
2022-01-27 14:23:39 +01:00
|
|
|
)
|
|
|
|
.receive(on: DispatchQueue.main)
|
2022-05-26 17:19:47 +02:00
|
|
|
.sink { [weak self] account, emojiMeta in
|
2022-01-27 14:23:39 +01:00
|
|
|
guard let self = self else { return }
|
2022-05-26 17:19:47 +02:00
|
|
|
guard let account = account else { return }
|
2022-01-27 14:23:39 +01:00
|
|
|
|
2022-05-26 17:19:47 +02:00
|
|
|
self.profileInfo.fields = account.source?.fields?.compactMap { field in
|
|
|
|
ProfileFieldItem.FieldValue(
|
|
|
|
name: field.name,
|
|
|
|
value: field.value,
|
|
|
|
emojiMeta: emojiMeta
|
|
|
|
)
|
|
|
|
} ?? []
|
|
|
|
|
|
|
|
self.profileInfoEditing.fields = account.source?.fields?.compactMap { field in
|
2022-01-27 14:23:39 +01:00
|
|
|
ProfileFieldItem.FieldValue(
|
|
|
|
name: field.name,
|
|
|
|
value: field.value,
|
|
|
|
emojiMeta: [:] // no use for editing
|
|
|
|
)
|
|
|
|
} ?? []
|
|
|
|
}
|
|
|
|
.store(in: &disposeBag)
|
2022-05-26 17:19:47 +02:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ProfileAboutViewModel {
|
|
|
|
class ProfileInfo {
|
|
|
|
@Published var fields: [ProfileFieldItem.FieldValue] = []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ProfileAboutViewModel {
|
|
|
|
func appendFieldItem() {
|
2022-05-26 17:19:47 +02:00
|
|
|
var fields = profileInfoEditing.fields
|
2022-01-27 14:23:39 +01:00
|
|
|
guard fields.count < ProfileHeaderViewModel.maxProfileFieldCount else { return }
|
|
|
|
fields.append(ProfileFieldItem.FieldValue(name: "", value: "", emojiMeta: [:]))
|
2022-05-26 17:19:47 +02:00
|
|
|
profileInfoEditing.fields = fields
|
2022-01-27 14:23:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func removeFieldItem(item: ProfileFieldItem) {
|
2022-05-26 17:19:47 +02:00
|
|
|
var fields = profileInfoEditing.fields
|
2022-01-27 14:23:39 +01:00
|
|
|
guard case let .editField(field) = item else { return }
|
|
|
|
guard let removeIndex = fields.firstIndex(of: field) else { return }
|
|
|
|
fields.remove(at: removeIndex)
|
2022-05-26 17:19:47 +02:00
|
|
|
profileInfoEditing.fields = fields
|
2022-01-27 14:23:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - ProfileViewModelEditable
|
|
|
|
extension ProfileAboutViewModel: ProfileViewModelEditable {
|
2022-05-26 17:19:47 +02:00
|
|
|
var isEdited: Bool {
|
2022-01-27 14:23:39 +01:00
|
|
|
guard isEditing else { return false }
|
|
|
|
|
|
|
|
let isFieldsEqual: Bool = {
|
|
|
|
let originalFields = self.accountForEdit?.source?.fields?.compactMap { field in
|
|
|
|
ProfileFieldItem.FieldValue(name: field.name, value: field.value, emojiMeta: [:])
|
|
|
|
} ?? []
|
2022-05-26 17:19:47 +02:00
|
|
|
let editFields = profileInfoEditing.fields
|
2022-01-27 14:23:39 +01:00
|
|
|
guard editFields.count == originalFields.count else { return false }
|
|
|
|
for (editField, originalField) in zip(editFields, originalFields) {
|
|
|
|
guard editField.name.value == originalField.name.value,
|
|
|
|
editField.value.value == originalField.value.value else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}()
|
|
|
|
guard isFieldsEqual else { return true }
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|