This commit is contained in:
fmodf 2024-08-18 11:05:43 +02:00
parent 062292bb11
commit 3c9e8a1384
11 changed files with 38 additions and 33 deletions

View file

@ -9,13 +9,13 @@ final class AttachmentsStore: ObservableObject {
@Published var galleryAccessGranted = false @Published var galleryAccessGranted = false
@Published var galleryItems: [GalleryItem] = [] @Published var galleryItems: [GalleryItem] = []
// private let client: Client private let client: Client
// private let roster: Roster private let roster: Roster
//
// init(roster: Roster, client: Client) { init(roster: Roster, client: Client) {
// self.client = client self.client = client
// self.roster = roster self.roster = roster
// } }
} }
extension AttachmentsStore { extension AttachmentsStore {

View file

@ -141,7 +141,7 @@ extension ClientsStore {
} }
extension ClientsStore { extension ClientsStore {
func conversation(for roster: Roster) async throws -> ConversationStore { func conversationStores(for roster: Roster) async throws -> (ConversationStore, AttachmentsStore) {
while !ready { while !ready {
await Task.yield() await Task.yield()
} }
@ -150,10 +150,12 @@ extension ClientsStore {
throw ClientStoreError.clientNotFound 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 { while !ready {
await Task.yield() await Task.yield()
} }
@ -163,6 +165,8 @@ extension ClientsStore {
} }
let roster = try await chat.fetchRoster() 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)
} }
} }

View file

@ -40,10 +40,6 @@ extension ConversationStore {
} }
extension ConversationStore { extension ConversationStore {
var attachmentsStore: AttachmentsStore {
AttachmentsStore()
}
func sendMedia(_ items: [GalleryItem]) async { func sendMedia(_ items: [GalleryItem]) async {
for item in items { for item in items {
Task { Task {

View file

@ -60,9 +60,9 @@ private struct ChatsRow: View {
} }
do { do {
let conversation = try await clientsStore.conversation(for: chat) let (conversation, attachments) = try await clientsStore.conversationStores(for: chat)
router.showScreen(.push) { _ in router.showScreen(.push) { _ in
ConversationScreen(conversation: conversation) ConversationScreen(conversation: conversation, attachments: attachments)
.navigationBarHidden(true) .navigationBarHidden(true)
} }
} catch { } catch {

View file

@ -158,9 +158,9 @@ private struct ContactsScreenRow: View {
} }
do { do {
let conversation = try await clientsStore.conversation(for: roster) let (conversation, attachments) = try await clientsStore.conversationStores(for: roster)
router.showScreen(.push) { _ in router.showScreen(.push) { _ in
ConversationScreen(conversation: conversation) ConversationScreen(conversation: conversation, attachments: attachments)
.navigationBarHidden(true) .navigationBarHidden(true)
} }
} catch { } catch {

View file

@ -9,7 +9,7 @@ enum AttachmentTab: Int, CaseIterable {
struct AttachmentPickerScreen: View { struct AttachmentPickerScreen: View {
@Environment(\.router) var router @Environment(\.router) var router
@StateObject var attachmentsStore: AttachmentsStore @EnvironmentObject var attachments: AttachmentsStore
@State private var selectedTab: AttachmentTab = .media @State private var selectedTab: AttachmentTab = .media
@ -36,7 +36,7 @@ struct AttachmentPickerScreen: View {
switch selectedTab { switch selectedTab {
case .media: case .media:
MediaPickerView() MediaPickerView()
.environmentObject(attachmentsStore) .environmentObject(attachments)
case .files: case .files:
FilesPickerView() FilesPickerView()

View file

@ -4,11 +4,11 @@ import SwiftUI
struct CameraCellPreview: View { struct CameraCellPreview: View {
@Environment(\.router) var router @Environment(\.router) var router
@EnvironmentObject var conversation: ConversationStore @EnvironmentObject var conversation: ConversationStore
@EnvironmentObject var store: AttachmentsStore @EnvironmentObject var attachments: AttachmentsStore
var body: some View { var body: some View {
Group { Group {
if store.cameraAccessGranted { if attachments.cameraAccessGranted {
ZStack { ZStack {
CameraView() CameraView()
.aspectRatio(1, contentMode: .fit) .aspectRatio(1, contentMode: .fit)
@ -57,7 +57,7 @@ struct CameraCellPreview: View {
} }
} }
.task { .task {
await store.checkCameraAuthorization() await attachments.checkCameraAuthorization()
} }
} }
} }

View file

@ -1,13 +1,13 @@
import SwiftUI import SwiftUI
struct GalleryView: View { struct GalleryView: View {
@EnvironmentObject var store: AttachmentsStore @EnvironmentObject var attachments: AttachmentsStore
@Binding var selectedItems: [String] @Binding var selectedItems: [String]
var body: some View { var body: some View {
Group { Group {
if store.galleryAccessGranted { if attachments.galleryAccessGranted {
ForEach(store.galleryItems) { item in ForEach(attachments.galleryItems) { item in
GridViewItem(item: item, selected: $selectedItems) GridViewItem(item: item, selected: $selectedItems)
} }
} else { } else {
@ -33,12 +33,12 @@ struct GalleryView: View {
} }
} }
.task { .task {
await store.checkGalleryAuthorization() await attachments.checkGalleryAuthorization()
} }
.onChange(of: store.galleryAccessGranted) { flag in .onChange(of: attachments.galleryAccessGranted) { flag in
if flag { if flag {
Task { Task {
await store.fetchGalleryItems() await attachments.fetchGalleryItems()
} }
} }
} }

View file

@ -6,7 +6,7 @@ import SwiftUI
struct MediaPickerView: View { struct MediaPickerView: View {
@Environment(\.router) var router @Environment(\.router) var router
@EnvironmentObject var conversation: ConversationStore @EnvironmentObject var conversation: ConversationStore
@EnvironmentObject var store: AttachmentsStore @EnvironmentObject var attachments: AttachmentsStore
@State private var selectedItems: [String] = [] @State private var selectedItems: [String] = []
@ -45,7 +45,7 @@ struct MediaPickerView: View {
.clipped() .clipped()
.onTapGesture { .onTapGesture {
Task { Task {
let items = store.galleryItems.filter { selectedItems.contains($0.id) } let items = attachments.galleryItems.filter { selectedItems.contains($0.id) }
await conversation.sendMedia(items) await conversation.sendMedia(items)
} }
router.dismissEnvironment() router.dismissEnvironment()

View file

@ -6,6 +6,7 @@ import SwiftUI
struct ConversationScreen: View { struct ConversationScreen: View {
@Environment(\.router) var router @Environment(\.router) var router
@StateObject var conversation: ConversationStore @StateObject var conversation: ConversationStore
@StateObject var attachments: AttachmentsStore
@State private var autoScroll = true @State private var autoScroll = true
@State private var firstIsVisible = true @State private var firstIsVisible = true
@ -98,9 +99,11 @@ struct ConversationScreen: View {
} }
} }
.environmentObject(conversation) .environmentObject(conversation)
.environmentObject(attachments)
.safeAreaInset(edge: .bottom, spacing: 0) { .safeAreaInset(edge: .bottom, spacing: 0) {
ConversationTextInput(autoScroll: $autoScroll) ConversationTextInput(autoScroll: $autoScroll)
.environmentObject(conversation) .environmentObject(conversation)
.environmentObject(attachments)
} }
} }
} }

View file

@ -4,6 +4,7 @@ import UIKit
struct ConversationTextInput: View { struct ConversationTextInput: View {
@Environment(\.router) var router @Environment(\.router) var router
@EnvironmentObject var conversation: ConversationStore @EnvironmentObject var conversation: ConversationStore
@EnvironmentObject var attachments: AttachmentsStore
@State private var messageStr = "" @State private var messageStr = ""
@FocusState private var isFocused: Bool @FocusState private var isFocused: Bool
@ -51,8 +52,9 @@ struct ConversationTextInput: View {
.padding(.leading, 8) .padding(.leading, 8)
.tappablePadding(.symmetric(8)) { .tappablePadding(.symmetric(8)) {
router.showScreen(.fullScreenCover) { _ in router.showScreen(.fullScreenCover) { _ in
AttachmentPickerScreen(attachmentsStore: conversation.attachmentsStore) AttachmentPickerScreen()
.environmentObject(conversation) .environmentObject(conversation)
.environmentObject(attachments)
} }
} }
TextField("", text: $messageStr, prompt: Text(L10n.Chat.textfieldPrompt).foregroundColor(.Material.Shape.separator)) TextField("", text: $messageStr, prompt: Text(L10n.Chat.textfieldPrompt).foregroundColor(.Material.Shape.separator))