From 3f62272162e3b5ac28511b0f1556a92609d861d1 Mon Sep 17 00:00:00 2001 From: ihugo Date: Sun, 25 Apr 2021 18:45:17 +0800 Subject: [PATCH] fix: lost comment if send without comment first --- .../Scene/Report/ReportViewModel+Data.swift | 2 +- Mastodon/Scene/Report/ReportViewModel.swift | 37 ++++++++++++++++--- .../MastodonSDK/API/Mastodon+API+Report.swift | 14 ------- 3 files changed, 32 insertions(+), 21 deletions(-) diff --git a/Mastodon/Scene/Report/ReportViewModel+Data.swift b/Mastodon/Scene/Report/ReportViewModel+Data.swift index 4dde72528..dcb715fba 100644 --- a/Mastodon/Scene/Report/ReportViewModel+Data.swift +++ b/Mastodon/Scene/Report/ReportViewModel+Data.swift @@ -88,7 +88,7 @@ extension ReportViewModel { } if status.id == self.statusId { attribute.isSelected = true - self.reportQuery.append(statusID: status.id) + self.append(statusID: status.id) self.continueEnableSubject.send(true) } } diff --git a/Mastodon/Scene/Report/ReportViewModel.swift b/Mastodon/Scene/Report/ReportViewModel.swift index 26c0f2f2a..4864145f5 100644 --- a/Mastodon/Scene/Report/ReportViewModel.swift +++ b/Mastodon/Scene/Report/ReportViewModel.swift @@ -26,6 +26,9 @@ class ReportViewModel: NSObject { var userId: String var statusId: String? + var statusIDs = [Mastodon.Entity.Status.ID]() + var comment: String? + var reportQuery: FileReportQuery var disposeBag = Set() let currentStep = CurrentValueSubject(.one) @@ -120,19 +123,19 @@ class ReportViewModel: NSObject { attribute.isSelected = !attribute.isSelected if attribute.isSelected { - self.reportQuery.append(statusID: status.id) + self.append(statusID: status.id) } else { - self.reportQuery.remove(statusID: status.id) + self.remove(statusID: status.id) } - let continueEnable = (self.reportQuery.statusIDs?.count ?? 0) > 0 + let continueEnable = self.statusIDs.count > 0 self.continueEnableSubject.send(continueEnable) } .store(in: &disposeBag) input.comment.assign( to: \.comment, - on: self.reportQuery + on: self ) .store(in: &disposeBag) input.comment.sink { [weak self] (comment) in @@ -150,7 +153,13 @@ class ReportViewModel: NSObject { return value } - Publishers.Merge(skip, input.step1Continue) + let step1Continue = input.step1Continue.map { [weak self] value -> Void in + guard let self = self else { return value } + self.reportQuery.statusIDs = self.statusIDs + return value + } + + Publishers.Merge(skip, step1Continue) .sink { [weak self] _ in self?.currentStep.value = .two self?.sendEnableSubject.send(false) @@ -164,8 +173,14 @@ class ReportViewModel: NSObject { self.reportQuery.comment = nil return value } + + let step2Continue = input.step2Continue.map { [weak self] value -> Void in + guard let self = self else { return value } + self.reportQuery.comment = self.comment + return value + } - return Publishers.Merge(skip, input.step2Continue) + return Publishers.Merge(skip, step2Continue) .flatMap { [weak self] (_) -> AnyPublisher<(Bool, Error?), Never> in guard let self = self else { return Empty(completeImmediately: true).eraseToAnyPublisher() @@ -189,4 +204,14 @@ class ReportViewModel: NSObject { } .eraseToAnyPublisher() } + + func append(statusID: Mastodon.Entity.Status.ID) { + guard self.statusIDs.contains(statusID) != true else { return } + self.statusIDs.append(statusID) + } + + func remove(statusID: String) { + guard let index = self.statusIDs.firstIndex(of: statusID) else { return } + self.statusIDs.remove(at: index) + } } diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Report.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Report.swift index fff746174..6ba8c3cf5 100644 --- a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Report.swift +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Report.swift @@ -87,19 +87,5 @@ public extension Mastodon.API.Reports { self.comment = comment self.forward = forward } - - public func append(statusID: Mastodon.Entity.Status.ID) { - guard self.statusIDs?.contains(statusID) != true else { return } - if self.statusIDs == nil { - self.statusIDs = [] - } - - self.statusIDs?.append(statusID) - } - - public func remove(statusID: String) { - guard let index = self.statusIDs?.firstIndex(of: statusID) else { return } - self.statusIDs?.remove(at: index) - } } }