fix: bio editor not stable issue. resolve #265

This commit is contained in:
CMK 2021-08-05 18:11:00 +08:00
parent 7f617758c1
commit d134178032
2 changed files with 32 additions and 26 deletions

View File

@ -195,43 +195,47 @@ extension ProfileHeaderViewController {
} }
.store(in: &disposeBag) .store(in: &disposeBag)
viewModel.isEditing let profileNote = Publishers.CombineLatest3(
.assign(to: \.isEditable, on: profileHeaderView.bioMetaText.textView) viewModel.isEditing.removeDuplicates(),
.store(in: &disposeBag) viewModel.displayProfileInfo.note.removeDuplicates(),
viewModel.editProfileInfoDidInitialized
viewModel.isEditing )
.removeDuplicates() .map { isEditing, displayNote, _ -> String? in
.receive(on: DispatchQueue.main) if isEditing {
.sink { [weak self] isEditing in return self.viewModel.editProfileInfo.note.value
guard let self = self else { return } } else {
guard isEditing else { return } return displayNote
// trigger once when toggle
// and use delegate to update text style
let initialNote = self.viewModel.editProfileInfo.note.value
let metaContent = PlaintextMetaContent(string: initialNote ?? "")
self.profileHeaderView.bioMetaText.configure(content: metaContent)
} }
.store(in: &disposeBag) }
.eraseToAnyPublisher()
Publishers.CombineLatest3( Publishers.CombineLatest3(
viewModel.isEditing.removeDuplicates(), viewModel.isEditing.removeDuplicates(),
viewModel.displayProfileInfo.note.removeDuplicates(), profileNote.removeDuplicates(),
viewModel.emojiMeta.removeDuplicates() viewModel.emojiMeta.removeDuplicates()
) )
.receive(on: DispatchQueue.main) .receive(on: DispatchQueue.main)
.sink { [weak self] isEditing, note, emojiMeta in .sink { [weak self] isEditing, note, emojiMeta in
guard let self = self else { return } guard let self = self else { return }
guard !isEditing else { return }
let mastodonContent = MastodonContent(content: note ?? "", emojis: emojiMeta) self.profileHeaderView.bioMetaText.textView.isEditable = isEditing
do {
let metaContent = try MastodonMetaContent.convert(document: mastodonContent) if isEditing {
let metaContent = PlaintextMetaContent(string: note ?? "")
self.profileHeaderView.bioMetaText.configure(content: metaContent) self.profileHeaderView.bioMetaText.configure(content: metaContent)
} catch { } else {
assertionFailure() let mastodonContent = MastodonContent(content: note ?? "", emojis: emojiMeta)
self.profileHeaderView.bioMetaText.reset() do {
let metaContent = try MastodonMetaContent.convert(document: mastodonContent)
self.profileHeaderView.bioMetaText.configure(content: metaContent)
} catch {
assertionFailure()
self.profileHeaderView.bioMetaText.reset()
}
} }
} }
.store(in: &disposeBag) .store(in: &disposeBag)
profileHeaderView.bioMetaText.delegate = self profileHeaderView.bioMetaText.delegate = self
NotificationCenter.default.publisher(for: UITextField.textDidChangeNotification, object: profileHeaderView.nameTextField) NotificationCenter.default.publisher(for: UITextField.textDidChangeNotification, object: profileHeaderView.nameTextField)

View File

@ -31,6 +31,7 @@ final class ProfileHeaderViewModel {
// output // output
let displayProfileInfo = ProfileInfo() let displayProfileInfo = ProfileInfo()
let editProfileInfo = ProfileInfo() let editProfileInfo = ProfileInfo()
let editProfileInfoDidInitialized = CurrentValueSubject<Void, Never>(Void()) // needs trigger initial event
let isTitleViewDisplaying = CurrentValueSubject<Bool, Never>(false) let isTitleViewDisplaying = CurrentValueSubject<Bool, Never>(false)
var fieldDiffableDataSource: UICollectionViewDiffableDataSource<ProfileFieldSection, ProfileFieldItem>! var fieldDiffableDataSource: UICollectionViewDiffableDataSource<ProfileFieldSection, ProfileFieldItem>!
@ -52,6 +53,7 @@ final class ProfileHeaderViewModel {
self.editProfileInfo.fields.value = account?.source?.fields?.compactMap { field in self.editProfileInfo.fields.value = account?.source?.fields?.compactMap { field in
ProfileFieldItem.FieldValue(name: field.name, value: field.value) ProfileFieldItem.FieldValue(name: field.name, value: field.value)
} ?? [] } ?? []
self.editProfileInfoDidInitialized.send()
} }
.store(in: &disposeBag) .store(in: &disposeBag)