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)
viewModel.isEditing
.assign(to: \.isEditable, on: profileHeaderView.bioMetaText.textView)
.store(in: &disposeBag)
viewModel.isEditing
.removeDuplicates()
.receive(on: DispatchQueue.main)
.sink { [weak self] isEditing in
guard let self = self else { return }
guard isEditing else { return }
// 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)
Publishers.CombineLatest3(
let profileNote = Publishers.CombineLatest3(
viewModel.isEditing.removeDuplicates(),
viewModel.displayProfileInfo.note.removeDuplicates(),
viewModel.editProfileInfoDidInitialized
)
.map { isEditing, displayNote, _ -> String? in
if isEditing {
return self.viewModel.editProfileInfo.note.value
} else {
return displayNote
}
}
.eraseToAnyPublisher()
Publishers.CombineLatest3(
viewModel.isEditing.removeDuplicates(),
profileNote.removeDuplicates(),
viewModel.emojiMeta.removeDuplicates()
)
.receive(on: DispatchQueue.main)
.sink { [weak self] isEditing, note, emojiMeta in
guard let self = self else { return }
guard !isEditing else { return }
let mastodonContent = MastodonContent(content: note ?? "", emojis: emojiMeta)
do {
let metaContent = try MastodonMetaContent.convert(document: mastodonContent)
self.profileHeaderView.bioMetaText.textView.isEditable = isEditing
if isEditing {
let metaContent = PlaintextMetaContent(string: note ?? "")
self.profileHeaderView.bioMetaText.configure(content: metaContent)
} catch {
assertionFailure()
self.profileHeaderView.bioMetaText.reset()
} else {
let mastodonContent = MastodonContent(content: note ?? "", emojis: emojiMeta)
do {
let metaContent = try MastodonMetaContent.convert(document: mastodonContent)
self.profileHeaderView.bioMetaText.configure(content: metaContent)
} catch {
assertionFailure()
self.profileHeaderView.bioMetaText.reset()
}
}
}
.store(in: &disposeBag)
profileHeaderView.bioMetaText.delegate = self
NotificationCenter.default.publisher(for: UITextField.textDidChangeNotification, object: profileHeaderView.nameTextField)

View File

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