fix: [WIP] bio note edit logic

This commit is contained in:
CMK 2021-08-05 17:20:03 +08:00
parent f7f248b6e9
commit 7f617758c1
1 changed files with 36 additions and 22 deletions

View File

@ -195,24 +195,33 @@ extension ProfileHeaderViewController {
} }
.store(in: &disposeBag) .store(in: &disposeBag)
Publishers.CombineLatest4( 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(
viewModel.isEditing.removeDuplicates(), viewModel.isEditing.removeDuplicates(),
viewModel.displayProfileInfo.note.removeDuplicates(), viewModel.displayProfileInfo.note.removeDuplicates(),
viewModel.editProfileInfo.note.removeDuplicates(),
viewModel.emojiMeta.removeDuplicates() viewModel.emojiMeta.removeDuplicates()
) )
.receive(on: DispatchQueue.main) .receive(on: DispatchQueue.main)
.sink { [weak self] isEditing, note, editingNote, 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 }
self.profileHeaderView.bioMetaText.textView.isEditable = isEditing
if isEditing {
if self.profileHeaderView.bioMetaText.backedString != note {
let metaContent = PlaintextMetaContent(string: editingNote ?? "")
self.profileHeaderView.bioMetaText.configure(content: metaContent)
}
} else {
let mastodonContent = MastodonContent(content: note ?? "", emojis: emojiMeta) let mastodonContent = MastodonContent(content: note ?? "", emojis: emojiMeta)
do { do {
let metaContent = try MastodonMetaContent.convert(document: mastodonContent) let metaContent = try MastodonMetaContent.convert(document: mastodonContent)
@ -222,7 +231,6 @@ extension ProfileHeaderViewController {
self.profileHeaderView.bioMetaText.reset() self.profileHeaderView.bioMetaText.reset()
} }
} }
}
.store(in: &disposeBag) .store(in: &disposeBag)
profileHeaderView.bioMetaText.delegate = self profileHeaderView.bioMetaText.delegate = self
@ -461,9 +469,15 @@ extension ProfileHeaderViewController {
extension ProfileHeaderViewController: MetaTextDelegate { extension ProfileHeaderViewController: MetaTextDelegate {
func metaText(_ metaText: MetaText, processEditing textStorage: MetaTextStorage) -> MetaContent? { func metaText(_ metaText: MetaText, processEditing textStorage: MetaTextStorage) -> MetaContent? {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: text: %s", ((#file as NSString).lastPathComponent), #line, #function, metaText.backedString) os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: text: %s", ((#file as NSString).lastPathComponent), #line, #function, metaText.backedString)
assert(metaText.textView === profileHeaderView.bioMetaText.textView)
if metaText.textView === profileHeaderView.bioMetaText.textView { switch metaText {
case profileHeaderView.bioMetaText:
guard viewModel.isEditing.value else { break }
viewModel.editProfileInfo.note.value = metaText.backedString viewModel.editProfileInfo.note.value = metaText.backedString
let metaContent = PlaintextMetaContent(string: metaText.backedString)
return metaContent
default:
assertionFailure()
} }
return nil return nil