From d86bbda4c224ad22603d31e8cd92352aa94e6e63 Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Thu, 17 Dec 2020 16:17:17 -0800 Subject: [PATCH] wip --- DB/Sources/DB/Content/CompositionRecord.swift | 9 ------ DB/Sources/DB/Entities/Composition.swift | 28 ------------------- Data Sources/NewStatusDataSource.swift | 4 +-- .../Entities/AppEnvironment.swift | 3 +- .../ServiceLayer/Entities/Composition.swift | 5 ---- .../Services/IdentityService.swift | 6 ++-- .../NewStatusViewController.swift | 10 +++---- .../ViewModels/CompositionViewModel.swift | 14 ++++++---- .../ViewModels/Entities/Composition.swift | 5 ---- .../ViewModels/NewStatusViewModel.swift | 17 ++++++----- Views/CompositionView.swift | 2 +- 11 files changed, 27 insertions(+), 76 deletions(-) delete mode 100644 DB/Sources/DB/Content/CompositionRecord.swift delete mode 100644 DB/Sources/DB/Entities/Composition.swift delete mode 100644 ServiceLayer/Sources/ServiceLayer/Entities/Composition.swift delete mode 100644 ViewModels/Sources/ViewModels/Entities/Composition.swift diff --git a/DB/Sources/DB/Content/CompositionRecord.swift b/DB/Sources/DB/Content/CompositionRecord.swift deleted file mode 100644 index cf3bc17..0000000 --- a/DB/Sources/DB/Content/CompositionRecord.swift +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright © 2020 Metabolist. All rights reserved. - -import Foundation -import GRDB - -struct CompositionRecord: Codable, FetchableRecord, PersistableRecord { - let id: Composition.Id - let text: String -} diff --git a/DB/Sources/DB/Entities/Composition.swift b/DB/Sources/DB/Entities/Composition.swift deleted file mode 100644 index 977cf0f..0000000 --- a/DB/Sources/DB/Entities/Composition.swift +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright © 2020 Metabolist. All rights reserved. - -import Combine -import Foundation -import GRDB -import Mastodon - -public class Composition { - public let id: Id - @Published public var text: String - @Published public var attachments: [Attachment] - - public init(id: Id, text: String) { - self.id = id - self.text = text - attachments = [] - } -} - -public extension Composition { - typealias Id = UUID -} - -extension Composition { - convenience init(record: CompositionRecord) { - self.init(id: record.id, text: record.text) - } -} diff --git a/Data Sources/NewStatusDataSource.swift b/Data Sources/NewStatusDataSource.swift index b44e8b0..f145e36 100644 --- a/Data Sources/NewStatusDataSource.swift +++ b/Data Sources/NewStatusDataSource.swift @@ -3,7 +3,7 @@ import UIKit import ViewModels -final class NewStatusDataSource: UICollectionViewDiffableDataSource { +final class NewStatusDataSource: UICollectionViewDiffableDataSource { private let updateQueue = DispatchQueue(label: "com.metabolist.metatext.new-status-data-source.update-queue") @@ -20,7 +20,7 @@ final class NewStatusDataSource: UICollectionViewDiffableDataSource, + override func apply(_ snapshot: NSDiffableDataSourceSnapshot, animatingDifferences: Bool = true, completion: (() -> Void)? = nil) { updateQueue.async { diff --git a/ServiceLayer/Sources/ServiceLayer/Entities/AppEnvironment.swift b/ServiceLayer/Sources/ServiceLayer/Entities/AppEnvironment.swift index e67ae52..c2bf3ad 100644 --- a/ServiceLayer/Sources/ServiceLayer/Entities/AppEnvironment.swift +++ b/ServiceLayer/Sources/ServiceLayer/Entities/AppEnvironment.swift @@ -8,14 +8,13 @@ import Mastodon import UserNotifications public struct AppEnvironment { - public let uuid: () -> UUID - let session: URLSession let webAuthSessionType: WebAuthSession.Type let keychain: Keychain.Type let userDefaults: UserDefaults let userNotificationClient: UserNotificationClient let reduceMotion: () -> Bool + let uuid: () -> UUID let inMemoryContent: Bool let fixtureDatabase: IdentityDatabase? diff --git a/ServiceLayer/Sources/ServiceLayer/Entities/Composition.swift b/ServiceLayer/Sources/ServiceLayer/Entities/Composition.swift deleted file mode 100644 index b4af700..0000000 --- a/ServiceLayer/Sources/ServiceLayer/Entities/Composition.swift +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright © 2020 Metabolist. All rights reserved. - -import DB - -public typealias Composition = DB.Composition diff --git a/ServiceLayer/Sources/ServiceLayer/Services/IdentityService.swift b/ServiceLayer/Sources/ServiceLayer/Services/IdentityService.swift index 474512c..b40523f 100644 --- a/ServiceLayer/Sources/ServiceLayer/Services/IdentityService.swift +++ b/ServiceLayer/Sources/ServiceLayer/Services/IdentityService.swift @@ -212,8 +212,8 @@ public extension IdentityService { progress: progress) } - func post(compositions: [Composition]) -> AnyPublisher { - fatalError() +// func post(compositions: [Composition]) -> AnyPublisher { +// fatalError() // guard let composition = compositions.first else { fatalError() } // guard let attachment = composition.attachments.first else { fatalError() } @@ -235,7 +235,7 @@ public extension IdentityService { // return mastodonAPIClient.request(StatusEndpoint.post(components)) // .ignoreOutput() // .eraseToAnyPublisher() - } +// } func service(timeline: Timeline) -> TimelineService { TimelineService(timeline: timeline, mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase) diff --git a/View Controllers/NewStatusViewController.swift b/View Controllers/NewStatusViewController.swift index 02b47bc..3ec57e3 100644 --- a/View Controllers/NewStatusViewController.swift +++ b/View Controllers/NewStatusViewController.swift @@ -61,7 +61,7 @@ class NewStatusViewController: UICollectionViewController { guard let self = self else { return } let oldSnapshot = self.dataSource.snapshot() - let newSnapshot = [$0.map(\.composition.id)].snapshot() + let newSnapshot = [$0.map(\.id)].snapshot() let diff = newSnapshot.itemIdentifiers.difference(from: oldSnapshot.itemIdentifiers) self.dataSource.apply(newSnapshot) { @@ -75,11 +75,8 @@ class NewStatusViewController: UICollectionViewController { // Invalidate the collection view layout on anything that could change the height of a cell viewModel.$compositionViewModels - .flatMap { Publishers.MergeMany($0.map(\.composition.$text)) } - .map { _ in () } - .merge(with: viewModel.$compositionViewModels - .flatMap { Publishers.MergeMany($0.map(\.objectWillChange)) } - .map { _ in () }) + .flatMap { Publishers.MergeMany($0.map(\.objectWillChange)) } + .receive(on: DispatchQueue.main) .sink { [weak self] in self?.collectionView.collectionViewLayout.invalidateLayout() } .store(in: &cancellables) @@ -89,6 +86,7 @@ class NewStatusViewController: UICollectionViewController { viewModel.$alertItem .compactMap { $0 } + .receive(on: DispatchQueue.main) .sink { [weak self] in self?.present(alertItem: $0) } .store(in: &cancellables) } diff --git a/ViewModels/Sources/ViewModels/CompositionViewModel.swift b/ViewModels/Sources/ViewModels/CompositionViewModel.swift index b2897c5..6f1ccc6 100644 --- a/ViewModels/Sources/ViewModels/CompositionViewModel.swift +++ b/ViewModels/Sources/ViewModels/CompositionViewModel.swift @@ -6,7 +6,9 @@ import Mastodon import ServiceLayer public final class CompositionViewModel: ObservableObject { - public let composition: Composition + public let id = Id() + @Published public var text = "" + @Published public private(set) var attachments = [Attachment]() @Published public private(set) var isPostable = false @Published public private(set) var identification: Identification @Published public private(set) var attachmentUpload: AttachmentUpload? @@ -14,19 +16,19 @@ public final class CompositionViewModel: ObservableObject { private let eventsSubject: PassthroughSubject private var cancellables = Set() - init(composition: Composition, - identification: Identification, + init(identification: Identification, identificationPublisher: AnyPublisher, eventsSubject: PassthroughSubject) { - self.composition = composition self.identification = identification self.eventsSubject = eventsSubject identificationPublisher.assign(to: &$identification) - composition.$text.map { !$0.isEmpty }.removeDuplicates().assign(to: &$isPostable) + $text.map { !$0.isEmpty }.removeDuplicates().assign(to: &$isPostable) } } public extension CompositionViewModel { + typealias Id = UUID + enum Event { case insertAfter(CompositionViewModel) case presentMediaPicker(CompositionViewModel) @@ -70,7 +72,7 @@ public extension CompositionViewModel { self?.eventsSubject.send(.error(error)) } } receiveValue: { [weak self] in - self?.composition.attachments.append($0) + self?.attachments.append($0) } .store(in: &cancellables) } diff --git a/ViewModels/Sources/ViewModels/Entities/Composition.swift b/ViewModels/Sources/ViewModels/Entities/Composition.swift deleted file mode 100644 index 092e4d4..0000000 --- a/ViewModels/Sources/ViewModels/Entities/Composition.swift +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright © 2020 Metabolist. All rights reserved. - -import ServiceLayer - -public typealias Composition = ServiceLayer.Composition diff --git a/ViewModels/Sources/ViewModels/NewStatusViewModel.swift b/ViewModels/Sources/ViewModels/NewStatusViewModel.swift index 867f78e..9d16de5 100644 --- a/ViewModels/Sources/ViewModels/NewStatusViewModel.swift +++ b/ViewModels/Sources/ViewModels/NewStatusViewModel.swift @@ -67,21 +67,20 @@ public extension NewStatusViewModel { } func post() { - identification.service.post(compositions: compositionViewModels.map(\.composition)) - .receive(on: DispatchQueue.main) - .handleEvents( - receiveSubscription: { [weak self] _ in self?.loading = true }, - receiveCompletion: { [weak self] _ in self?.loading = false }) - .assignErrorsToAlertItem(to: \.alertItem, on: self) - .sink { _ in } - .store(in: &cancellables) +// identification.service.post(compositions: compositionViewModels.map(\.composition)) +// .receive(on: DispatchQueue.main) +// .handleEvents( +// receiveSubscription: { [weak self] _ in self?.loading = true }, +// receiveCompletion: { [weak self] _ in self?.loading = false }) +// .assignErrorsToAlertItem(to: \.alertItem, on: self) +// .sink { _ in } +// .store(in: &cancellables) } } private extension NewStatusViewModel { func newCompositionViewModel() -> CompositionViewModel { CompositionViewModel( - composition: .init(id: environment.uuid(), text: ""), identification: identification, identificationPublisher: $identification.eraseToAnyPublisher(), eventsSubject: itemEventsSubject) diff --git a/Views/CompositionView.swift b/Views/CompositionView.swift index 357e8c5..ad9a3dc 100644 --- a/Views/CompositionView.swift +++ b/Views/CompositionView.swift @@ -43,7 +43,7 @@ extension CompositionView: UIContentView { extension CompositionView: UITextViewDelegate { func textViewDidChange(_ textView: UITextView) { - compositionConfiguration.viewModel.composition.text = textView.text + compositionConfiguration.viewModel.text = textView.text } }