From 3c9e8a1384c96d2a0820b5fb707626aeb539db8d Mon Sep 17 00:00:00 2001 From: fmodf Date: Sun, 18 Aug 2024 11:05:43 +0200 Subject: [PATCH] wip --- .../AppData/Store/AttachmentsStore.swift | 14 +++++++------- .../AppData/Store/ClientsStore.swift | 12 ++++++++---- .../AppData/Store/ConversationStore.swift | 4 ---- .../View/Main/ChatList/ChatsListScreen.swift | 4 ++-- .../View/Main/Contacts/ContactsScreen.swift | 4 ++-- .../Attachments/AttachmentPickerScreen.swift | 4 ++-- .../MediaPicker/CameraCellPreview.swift | 6 +++--- .../Attachments/MediaPicker/GalleryView.swift | 12 ++++++------ .../Attachments/MediaPicker/MediaPickerView.swift | 4 ++-- .../Main/Conversation/ConversationScreen.swift | 3 +++ .../Main/Conversation/ConversationTextInput.swift | 4 +++- 11 files changed, 38 insertions(+), 33 deletions(-) diff --git a/ConversationsClassic/AppData/Store/AttachmentsStore.swift b/ConversationsClassic/AppData/Store/AttachmentsStore.swift index f883211..e11f0c3 100644 --- a/ConversationsClassic/AppData/Store/AttachmentsStore.swift +++ b/ConversationsClassic/AppData/Store/AttachmentsStore.swift @@ -9,13 +9,13 @@ final class AttachmentsStore: ObservableObject { @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 AttachmentsStore { diff --git a/ConversationsClassic/AppData/Store/ClientsStore.swift b/ConversationsClassic/AppData/Store/ClientsStore.swift index 78bc0c5..ce0bb67 100644 --- a/ConversationsClassic/AppData/Store/ClientsStore.swift +++ b/ConversationsClassic/AppData/Store/ClientsStore.swift @@ -141,7 +141,7 @@ extension ClientsStore { } extension ClientsStore { - func conversation(for roster: Roster) async throws -> ConversationStore { + func conversationStores(for roster: Roster) async throws -> (ConversationStore, AttachmentsStore) { while !ready { await Task.yield() } @@ -150,10 +150,12 @@ extension ClientsStore { throw ClientStoreError.clientNotFound } - return ConversationStore(roster: roster, client: client) + let conversationStore = ConversationStore(roster: roster, client: client) + let attachmentsStore = AttachmentsStore(roster: roster, client: client) + return (conversationStore, attachmentsStore) } - func conversation(for chat: Chat) async throws -> ConversationStore { + func conversationStores(for chat: Chat) async throws -> (ConversationStore, AttachmentsStore) { while !ready { await Task.yield() } @@ -163,6 +165,8 @@ extension ClientsStore { } let roster = try await chat.fetchRoster() - return ConversationStore(roster: roster, client: client) + let conversationStore = ConversationStore(roster: roster, client: client) + let attachmentsStore = AttachmentsStore(roster: roster, client: client) + return (conversationStore, attachmentsStore) } } diff --git a/ConversationsClassic/AppData/Store/ConversationStore.swift b/ConversationsClassic/AppData/Store/ConversationStore.swift index cf31770..68902b8 100644 --- a/ConversationsClassic/AppData/Store/ConversationStore.swift +++ b/ConversationsClassic/AppData/Store/ConversationStore.swift @@ -40,10 +40,6 @@ extension ConversationStore { } extension ConversationStore { - var attachmentsStore: AttachmentsStore { - AttachmentsStore() - } - func sendMedia(_ items: [GalleryItem]) async { for item in items { Task { diff --git a/ConversationsClassic/View/Main/ChatList/ChatsListScreen.swift b/ConversationsClassic/View/Main/ChatList/ChatsListScreen.swift index af6b874..8468603 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 = try await clientsStore.conversation(for: chat) + let (conversation, attachments) = try await clientsStore.conversationStores(for: chat) router.showScreen(.push) { _ in - ConversationScreen(conversation: conversation) + ConversationScreen(conversation: conversation, attachments: attachments) .navigationBarHidden(true) } } catch { diff --git a/ConversationsClassic/View/Main/Contacts/ContactsScreen.swift b/ConversationsClassic/View/Main/Contacts/ContactsScreen.swift index 920ac0d..d145036 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 = try await clientsStore.conversation(for: roster) + let (conversation, attachments) = try await clientsStore.conversationStores(for: roster) router.showScreen(.push) { _ in - ConversationScreen(conversation: conversation) + ConversationScreen(conversation: conversation, attachments: attachments) .navigationBarHidden(true) } } catch { diff --git a/ConversationsClassic/View/Main/Conversation/Attachments/AttachmentPickerScreen.swift b/ConversationsClassic/View/Main/Conversation/Attachments/AttachmentPickerScreen.swift index a6c23be..8f71c07 100644 --- a/ConversationsClassic/View/Main/Conversation/Attachments/AttachmentPickerScreen.swift +++ b/ConversationsClassic/View/Main/Conversation/Attachments/AttachmentPickerScreen.swift @@ -9,7 +9,7 @@ enum AttachmentTab: Int, CaseIterable { struct AttachmentPickerScreen: View { @Environment(\.router) var router - @StateObject var attachmentsStore: AttachmentsStore + @EnvironmentObject var attachments: AttachmentsStore @State private var selectedTab: AttachmentTab = .media @@ -36,7 +36,7 @@ struct AttachmentPickerScreen: View { switch selectedTab { case .media: MediaPickerView() - .environmentObject(attachmentsStore) + .environmentObject(attachments) case .files: FilesPickerView() diff --git a/ConversationsClassic/View/Main/Conversation/Attachments/MediaPicker/CameraCellPreview.swift b/ConversationsClassic/View/Main/Conversation/Attachments/MediaPicker/CameraCellPreview.swift index ec720b9..11fbef0 100644 --- a/ConversationsClassic/View/Main/Conversation/Attachments/MediaPicker/CameraCellPreview.swift +++ b/ConversationsClassic/View/Main/Conversation/Attachments/MediaPicker/CameraCellPreview.swift @@ -4,11 +4,11 @@ import SwiftUI struct CameraCellPreview: View { @Environment(\.router) var router @EnvironmentObject var conversation: ConversationStore - @EnvironmentObject var store: AttachmentsStore + @EnvironmentObject var attachments: AttachmentsStore var body: some View { Group { - if store.cameraAccessGranted { + if attachments.cameraAccessGranted { ZStack { CameraView() .aspectRatio(1, contentMode: .fit) @@ -57,7 +57,7 @@ struct CameraCellPreview: View { } } .task { - await store.checkCameraAuthorization() + await attachments.checkCameraAuthorization() } } } diff --git a/ConversationsClassic/View/Main/Conversation/Attachments/MediaPicker/GalleryView.swift b/ConversationsClassic/View/Main/Conversation/Attachments/MediaPicker/GalleryView.swift index 9c207d0..25aed9b 100644 --- a/ConversationsClassic/View/Main/Conversation/Attachments/MediaPicker/GalleryView.swift +++ b/ConversationsClassic/View/Main/Conversation/Attachments/MediaPicker/GalleryView.swift @@ -1,13 +1,13 @@ import SwiftUI struct GalleryView: View { - @EnvironmentObject var store: AttachmentsStore + @EnvironmentObject var attachments: AttachmentsStore @Binding var selectedItems: [String] var body: some View { Group { - if store.galleryAccessGranted { - ForEach(store.galleryItems) { item in + if attachments.galleryAccessGranted { + ForEach(attachments.galleryItems) { item in GridViewItem(item: item, selected: $selectedItems) } } else { @@ -33,12 +33,12 @@ struct GalleryView: View { } } .task { - await store.checkGalleryAuthorization() + await attachments.checkGalleryAuthorization() } - .onChange(of: store.galleryAccessGranted) { flag in + .onChange(of: attachments.galleryAccessGranted) { flag in if flag { Task { - await store.fetchGalleryItems() + await attachments.fetchGalleryItems() } } } diff --git a/ConversationsClassic/View/Main/Conversation/Attachments/MediaPicker/MediaPickerView.swift b/ConversationsClassic/View/Main/Conversation/Attachments/MediaPicker/MediaPickerView.swift index f26998b..f3b8daf 100644 --- a/ConversationsClassic/View/Main/Conversation/Attachments/MediaPicker/MediaPickerView.swift +++ b/ConversationsClassic/View/Main/Conversation/Attachments/MediaPicker/MediaPickerView.swift @@ -6,7 +6,7 @@ import SwiftUI struct MediaPickerView: View { @Environment(\.router) var router @EnvironmentObject var conversation: ConversationStore - @EnvironmentObject var store: AttachmentsStore + @EnvironmentObject var attachments: AttachmentsStore @State private var selectedItems: [String] = [] @@ -45,7 +45,7 @@ struct MediaPickerView: View { .clipped() .onTapGesture { Task { - let items = store.galleryItems.filter { selectedItems.contains($0.id) } + let items = attachments.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 88cb70b..401843d 100644 --- a/ConversationsClassic/View/Main/Conversation/ConversationScreen.swift +++ b/ConversationsClassic/View/Main/Conversation/ConversationScreen.swift @@ -6,6 +6,7 @@ import SwiftUI struct ConversationScreen: View { @Environment(\.router) var router @StateObject var conversation: ConversationStore + @StateObject var attachments: AttachmentsStore @State private var autoScroll = true @State private var firstIsVisible = true @@ -98,9 +99,11 @@ struct ConversationScreen: View { } } .environmentObject(conversation) + .environmentObject(attachments) .safeAreaInset(edge: .bottom, spacing: 0) { ConversationTextInput(autoScroll: $autoScroll) .environmentObject(conversation) + .environmentObject(attachments) } } } diff --git a/ConversationsClassic/View/Main/Conversation/ConversationTextInput.swift b/ConversationsClassic/View/Main/Conversation/ConversationTextInput.swift index d06347b..4c3f429 100644 --- a/ConversationsClassic/View/Main/Conversation/ConversationTextInput.swift +++ b/ConversationsClassic/View/Main/Conversation/ConversationTextInput.swift @@ -4,6 +4,7 @@ import UIKit struct ConversationTextInput: View { @Environment(\.router) var router @EnvironmentObject var conversation: ConversationStore + @EnvironmentObject var attachments: AttachmentsStore @State private var messageStr = "" @FocusState private var isFocused: Bool @@ -51,8 +52,9 @@ struct ConversationTextInput: View { .padding(.leading, 8) .tappablePadding(.symmetric(8)) { router.showScreen(.fullScreenCover) { _ in - AttachmentPickerScreen(attachmentsStore: conversation.attachmentsStore) + AttachmentPickerScreen() .environmentObject(conversation) + .environmentObject(attachments) } } TextField("", text: $messageStr, prompt: Text(L10n.Chat.textfieldPrompt).foregroundColor(.Material.Shape.separator))