From c3bd783769c10ed60291573b4e6e90b654f69306 Mon Sep 17 00:00:00 2001 From: fmodf Date: Mon, 1 Jul 2024 11:29:31 +0200 Subject: [PATCH] wip --- .../AppCore/XMPP/XMPPService.swift | 17 +++--- .../Conversation/ConversationScreen.swift | 54 ++++++++++++++++--- 2 files changed, 55 insertions(+), 16 deletions(-) diff --git a/ConversationsClassic/AppCore/XMPP/XMPPService.swift b/ConversationsClassic/AppCore/XMPP/XMPPService.swift index 89a2b27..0f720fa 100644 --- a/ConversationsClassic/AppCore/XMPP/XMPPService.swift +++ b/ConversationsClassic/AppCore/XMPP/XMPPService.swift @@ -113,17 +113,14 @@ final class XMPPService: ObservableObject { message.to = JID(to) message.body = message.body - client.module(MessageModule.self) - .chatManager - .chat(for: client.context, with: JID(to).bareJid)? - .send(message: message) { res in - switch res { - case .success: - completion(true) + client.context.writer.write(message) { res in + switch res { + case .success: + completion(true) - case .failure: - completion(false) - } + case .failure: + completion(false) } + } } } diff --git a/ConversationsClassic/View/Screens/Conversation/ConversationScreen.swift b/ConversationsClassic/View/Screens/Conversation/ConversationScreen.swift index 4ff6cba..55f89ad 100644 --- a/ConversationsClassic/View/Screens/Conversation/ConversationScreen.swift +++ b/ConversationsClassic/View/Screens/Conversation/ConversationScreen.swift @@ -6,6 +6,8 @@ import SwiftUI struct ConversationScreen: View { @EnvironmentObject var store: AppStore + @State private var autoScroll = true + var body: some View { ZStack { // Background color @@ -20,14 +22,45 @@ struct ConversationScreen: View { // Msg list let messages = store.state.conversationsState.currentMessages if !messages.isEmpty { - List { - ForEach(messages) { message in - ConversationMessageRow(message: message) + ScrollViewReader { proxy in + List { + ForEach(messages) { message in + ConversationMessageRow(message: message) + .id(message.id) + .onAppear { + if message.id == messages.last?.id { + autoScroll = true + } + } + .onDisappear { + if message.id == messages.last?.id { + autoScroll = false + } + } + } + } + .listStyle(.plain) + .background(Color.Main.backgroundLight) + .scrollDismissesKeyboard(.immediately) + .scrollIndicators(.hidden) + .onChange(of: messages) { _ in + if autoScroll { + withAnimation { + proxy.scrollTo(messages.last?.id, anchor: .bottom) + } + } + } + .onChange(of: autoScroll) { new in + if new { + withAnimation { + proxy.scrollTo(messages.last?.id, anchor: .bottom) + } + } + } + .onAppear { + proxy.scrollTo(messages.last?.id, anchor: .bottom) } } - .listStyle(.plain) - .background(Color.Main.backgroundLight) - .scrollDismissesKeyboard(.immediately) } else { Spacer() } @@ -35,6 +68,15 @@ struct ConversationScreen: View { .onTapGesture { UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil) } + + // Jump to last button + if !autoScroll { + Button { + autoScroll = true + } label: { + Text("TEST") + } + } } .safeAreaInset(edge: .bottom, spacing: 0) { ConversationTextInput()