From a52b2d99b6f7acbd3c4be58104fbaaa32bf641ae Mon Sep 17 00:00:00 2001 From: fmodf Date: Sat, 17 Aug 2024 18:15:05 +0200 Subject: [PATCH] wip --- ...{FileStore.swift => AttachmentsStore.swift} | 18 +++++++++--------- .../AppData/Store/ClientsStore.swift | 14 ++++---------- .../AppData/Store/ConversationStore.swift | 15 +++++++++++++++ .../View/Main/ChatList/ChatsListScreen.swift | 4 ++-- .../View/Main/Contacts/ContactsScreen.swift | 4 ++-- .../Attachments/AttachmentPickerScreen.swift | 2 ++ .../MediaPicker/CameraCellPreview.swift | 2 +- .../Attachments/MediaPicker/GalleryView.swift | 2 +- .../MediaPicker/MediaPickerView.swift | 11 +++++++++-- .../Main/Conversation/ConversationScreen.swift | 2 -- .../Conversation/ConversationTextInput.swift | 4 +--- 11 files changed, 46 insertions(+), 32 deletions(-) rename ConversationsClassic/AppData/Store/{FileStore.swift => AttachmentsStore.swift} (80%) diff --git a/ConversationsClassic/AppData/Store/FileStore.swift b/ConversationsClassic/AppData/Store/AttachmentsStore.swift similarity index 80% rename from ConversationsClassic/AppData/Store/FileStore.swift rename to ConversationsClassic/AppData/Store/AttachmentsStore.swift index ef3efac..f883211 100644 --- a/ConversationsClassic/AppData/Store/FileStore.swift +++ b/ConversationsClassic/AppData/Store/AttachmentsStore.swift @@ -4,21 +4,21 @@ import Photos import SwiftUI @MainActor -final class FileStore: ObservableObject { +final class AttachmentsStore: ObservableObject { @Published var cameraAccessGranted = false @Published var galleryAccessGranted = false @Published var galleryItems: [GalleryItem] = [] - private let client: Client - private let roster: Roster - - init(roster: Roster, client: Client) { - self.client = client - self.roster = roster - } + // private let client: Client + // private let roster: Roster + // + // init(roster: Roster, client: Client) { + // self.client = client + // self.roster = roster + // } } -extension FileStore { +extension AttachmentsStore { func checkCameraAuthorization() async { let status = AVCaptureDevice.authorizationStatus(for: .video) var isAuthorized = status == .authorized diff --git a/ConversationsClassic/AppData/Store/ClientsStore.swift b/ConversationsClassic/AppData/Store/ClientsStore.swift index 0ed4460..78bc0c5 100644 --- a/ConversationsClassic/AppData/Store/ClientsStore.swift +++ b/ConversationsClassic/AppData/Store/ClientsStore.swift @@ -141,7 +141,7 @@ extension ClientsStore { } extension ClientsStore { - func getStores(for roster: Roster) async throws -> (ConversationStore, FileStore) { + func conversation(for roster: Roster) async throws -> ConversationStore { while !ready { await Task.yield() } @@ -150,13 +150,10 @@ extension ClientsStore { throw ClientStoreError.clientNotFound } - let conversation = ConversationStore(roster: roster, client: client) - let fileStore = FileStore(roster: roster, client: client) - - return (conversation, fileStore) + return ConversationStore(roster: roster, client: client) } - func getStores(for chat: Chat) async throws -> (ConversationStore, FileStore) { + func conversation(for chat: Chat) async throws -> ConversationStore { while !ready { await Task.yield() } @@ -166,9 +163,6 @@ extension ClientsStore { } let roster = try await chat.fetchRoster() - let conversation = ConversationStore(roster: roster, client: client) - let fileStore = FileStore(roster: roster, client: client) - - return (conversation, fileStore) + return ConversationStore(roster: roster, client: client) } } diff --git a/ConversationsClassic/AppData/Store/ConversationStore.swift b/ConversationsClassic/AppData/Store/ConversationStore.swift index b008026..63ec66b 100644 --- a/ConversationsClassic/AppData/Store/ConversationStore.swift +++ b/ConversationsClassic/AppData/Store/ConversationStore.swift @@ -51,6 +51,21 @@ extension ConversationStore { } } +extension ConversationStore { + var attachmentsStore: AttachmentsStore { + AttachmentsStore() + } + + func sendMedia(_ items: [GalleryItem]) async { + print("media!", items) + // guard !ids.isEmpty else { return } + // let items = galleryItems.filter { ids.contains($0.id) } + // for item in items { + // await client.uploadMedia(item.url) + // } + } +} + private extension ConversationStore { func subscribe() { messagesCancellable = ValueObservation.tracking(Message diff --git a/ConversationsClassic/View/Main/ChatList/ChatsListScreen.swift b/ConversationsClassic/View/Main/ChatList/ChatsListScreen.swift index 4e22761..af6b874 100644 --- a/ConversationsClassic/View/Main/ChatList/ChatsListScreen.swift +++ b/ConversationsClassic/View/Main/ChatList/ChatsListScreen.swift @@ -60,9 +60,9 @@ private struct ChatsRow: View { } do { - let (conversation, fileStore) = try await clientsStore.getStores(for: chat) + let conversation = try await clientsStore.conversation(for: chat) router.showScreen(.push) { _ in - ConversationScreen(conversation: conversation, fileStore: fileStore) + ConversationScreen(conversation: conversation) .navigationBarHidden(true) } } catch { diff --git a/ConversationsClassic/View/Main/Contacts/ContactsScreen.swift b/ConversationsClassic/View/Main/Contacts/ContactsScreen.swift index 4427804..920ac0d 100644 --- a/ConversationsClassic/View/Main/Contacts/ContactsScreen.swift +++ b/ConversationsClassic/View/Main/Contacts/ContactsScreen.swift @@ -158,9 +158,9 @@ private struct ContactsScreenRow: View { } do { - let (conversation, fileStore) = try await clientsStore.getStores(for: roster) + let conversation = try await clientsStore.conversation(for: roster) router.showScreen(.push) { _ in - ConversationScreen(conversation: conversation, fileStore: fileStore) + ConversationScreen(conversation: conversation) .navigationBarHidden(true) } } catch { diff --git a/ConversationsClassic/View/Main/Conversation/Attachments/AttachmentPickerScreen.swift b/ConversationsClassic/View/Main/Conversation/Attachments/AttachmentPickerScreen.swift index 4d6ca1a..aa07b3b 100644 --- a/ConversationsClassic/View/Main/Conversation/Attachments/AttachmentPickerScreen.swift +++ b/ConversationsClassic/View/Main/Conversation/Attachments/AttachmentPickerScreen.swift @@ -9,6 +9,7 @@ enum AttachmentTab: Int, CaseIterable { struct AttachmentPickerScreen: View { @Environment(\.router) var router + @StateObject var attachmentsStore: AttachmentsStore @State private var selectedTab: AttachmentTab = .media @@ -35,6 +36,7 @@ struct AttachmentPickerScreen: View { switch selectedTab { case .media: MediaPickerView() + .environmentObject(attachmentsStore) case .files: Color.blue diff --git a/ConversationsClassic/View/Main/Conversation/Attachments/MediaPicker/CameraCellPreview.swift b/ConversationsClassic/View/Main/Conversation/Attachments/MediaPicker/CameraCellPreview.swift index 73b348f..1a0da8e 100644 --- a/ConversationsClassic/View/Main/Conversation/Attachments/MediaPicker/CameraCellPreview.swift +++ b/ConversationsClassic/View/Main/Conversation/Attachments/MediaPicker/CameraCellPreview.swift @@ -3,7 +3,7 @@ import SwiftUI struct CameraCellPreview: View { @Environment(\.router) var router - @EnvironmentObject var store: FileStore + @EnvironmentObject var store: AttachmentsStore var body: some View { Group { diff --git a/ConversationsClassic/View/Main/Conversation/Attachments/MediaPicker/GalleryView.swift b/ConversationsClassic/View/Main/Conversation/Attachments/MediaPicker/GalleryView.swift index d48fd52..9c207d0 100644 --- a/ConversationsClassic/View/Main/Conversation/Attachments/MediaPicker/GalleryView.swift +++ b/ConversationsClassic/View/Main/Conversation/Attachments/MediaPicker/GalleryView.swift @@ -1,7 +1,7 @@ import SwiftUI struct GalleryView: View { - @EnvironmentObject var store: FileStore + @EnvironmentObject var store: AttachmentsStore @Binding var selectedItems: [String] var body: some View { diff --git a/ConversationsClassic/View/Main/Conversation/Attachments/MediaPicker/MediaPickerView.swift b/ConversationsClassic/View/Main/Conversation/Attachments/MediaPicker/MediaPickerView.swift index ea21355..f26998b 100644 --- a/ConversationsClassic/View/Main/Conversation/Attachments/MediaPicker/MediaPickerView.swift +++ b/ConversationsClassic/View/Main/Conversation/Attachments/MediaPicker/MediaPickerView.swift @@ -4,6 +4,10 @@ import Photos import SwiftUI struct MediaPickerView: View { + @Environment(\.router) var router + @EnvironmentObject var conversation: ConversationStore + @EnvironmentObject var store: AttachmentsStore + @State private var selectedItems: [String] = [] var body: some View { @@ -40,8 +44,11 @@ struct MediaPickerView: View { } .clipped() .onTapGesture { - // store.dispatch(.sharingAction(.shareMedia(ids: selectedItems))) - // store.dispatch(.sharingAction(.showSharing(false))) + Task { + let items = store.galleryItems.filter { selectedItems.contains($0.id) } + await conversation.sendMedia(items) + } + router.dismissEnvironment() } } } diff --git a/ConversationsClassic/View/Main/Conversation/ConversationScreen.swift b/ConversationsClassic/View/Main/Conversation/ConversationScreen.swift index edfeb86..88cb70b 100644 --- a/ConversationsClassic/View/Main/Conversation/ConversationScreen.swift +++ b/ConversationsClassic/View/Main/Conversation/ConversationScreen.swift @@ -6,7 +6,6 @@ import SwiftUI struct ConversationScreen: View { @Environment(\.router) var router @StateObject var conversation: ConversationStore - @StateObject var fileStore: FileStore @State private var autoScroll = true @State private var firstIsVisible = true @@ -102,7 +101,6 @@ struct ConversationScreen: View { .safeAreaInset(edge: .bottom, spacing: 0) { ConversationTextInput(autoScroll: $autoScroll) .environmentObject(conversation) - .environmentObject(fileStore) } } } diff --git a/ConversationsClassic/View/Main/Conversation/ConversationTextInput.swift b/ConversationsClassic/View/Main/Conversation/ConversationTextInput.swift index cc07667..d06347b 100644 --- a/ConversationsClassic/View/Main/Conversation/ConversationTextInput.swift +++ b/ConversationsClassic/View/Main/Conversation/ConversationTextInput.swift @@ -4,7 +4,6 @@ import UIKit struct ConversationTextInput: View { @Environment(\.router) var router @EnvironmentObject var conversation: ConversationStore - @EnvironmentObject var fileStore: FileStore @State private var messageStr = "" @FocusState private var isFocused: Bool @@ -52,9 +51,8 @@ struct ConversationTextInput: View { .padding(.leading, 8) .tappablePadding(.symmetric(8)) { router.showScreen(.fullScreenCover) { _ in - AttachmentPickerScreen() + AttachmentPickerScreen(attachmentsStore: conversation.attachmentsStore) .environmentObject(conversation) - .environmentObject(fileStore) } } TextField("", text: $messageStr, prompt: Text(L10n.Chat.textfieldPrompt).foregroundColor(.Material.Shape.separator))