diff --git a/Mastodon/Scene/Profile/Header/ProfileHeaderViewController.swift b/Mastodon/Scene/Profile/Header/ProfileHeaderViewController.swift index 8c526392..716b6230 100644 --- a/Mastodon/Scene/Profile/Header/ProfileHeaderViewController.swift +++ b/Mastodon/Scene/Profile/Header/ProfileHeaderViewController.swift @@ -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) diff --git a/Mastodon/Scene/Profile/Header/ProfileHeaderViewModel.swift b/Mastodon/Scene/Profile/Header/ProfileHeaderViewModel.swift index c9875858..e8405b6a 100644 --- a/Mastodon/Scene/Profile/Header/ProfileHeaderViewModel.swift +++ b/Mastodon/Scene/Profile/Header/ProfileHeaderViewModel.swift @@ -31,6 +31,7 @@ final class ProfileHeaderViewModel { // output let displayProfileInfo = ProfileInfo() let editProfileInfo = ProfileInfo() + let editProfileInfoDidInitialized = CurrentValueSubject(Void()) // needs trigger initial event let isTitleViewDisplaying = CurrentValueSubject(false) var fieldDiffableDataSource: UICollectionViewDiffableDataSource! @@ -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)