update CompositionViewModel.maxCharacters whenever the identity changes

This commit is contained in:
Amit Ron 2022-11-09 18:20:29 +02:00
parent 667e39554a
commit 9310d281ce
2 changed files with 16 additions and 3 deletions

View file

@ -30,7 +30,7 @@ public final class CompositionViewModel: AttachmentsRenderingViewModel, Observab
public let canRemoveAttachments = true
private let eventsSubject: PassthroughSubject<Event, Never>
private let maxCharacters: Int
@Published private var maxCharacters: Int
private var cancellables = Set<AnyCancellable>()
init(eventsSubject: PassthroughSubject<Event, Never>, maxCharacters: Int?) {
@ -62,8 +62,8 @@ public final class CompositionViewModel: AttachmentsRenderingViewModel, Observab
return tokens.map(\.countShorteningIfURL).reduce(tokens.count - 1, +)
}
.combineLatest($displayContentWarning, $contentWarning)
.map { (maxCharacters ?? Self.defaultMaxCharacters) - ($0 + ($1 ? $2.count : 0)) }
.combineLatest($displayContentWarning, $contentWarning, $maxCharacters)
.map { ($3) - ($0 + ($1 ? $2.count : 0)) }
.assign(to: &$remainingCharacters)
$displayContentWarning.filter { $0 }.assign(to: &$sensitive)
@ -86,6 +86,10 @@ public final class CompositionViewModel: AttachmentsRenderingViewModel, Observab
public func removeAttachment(viewModel: AttachmentViewModel) {
attachmentViewModels.removeAll { $0 === viewModel }
}
public func setMaxCharactersOrDefault(_ newMaxCharacters: Int?) {
maxCharacters = newMaxCharacters ?? Self.defaultMaxCharacters
}
}
public extension CompositionViewModel {

View file

@ -101,6 +101,15 @@ public final class NewStatusViewModel: ObservableObject {
.sink { [weak self] in self?.handle(event: $0) }
.store(in: &cancellables)
$identityContext
.map { $0.identity.instance?.maxTootChars }
.sink { [weak self] maxTootChars in
self?.compositionViewModels.forEach { cvm in
cvm.setMaxCharactersOrDefault(maxTootChars)
}
}
.store(in: &cancellables)
if let identity = identity {
setIdentity(identity)
}