From a1ff96232487bb577a4d8fc3de08a71910cbd744 Mon Sep 17 00:00:00 2001 From: Mirshod Sobirov Date: Mon, 13 Apr 2026 13:56:39 -0400 Subject: [PATCH] added save chat function call into chatview --- LexAI_iOS/LexAI_iOS/Views/ChatView.swift | 34 +++++++++++++++++++++ LexAI_iOS/LexAI_iOS/Views/ContentView.swift | 7 +++-- LexAI_iOS/LexAI_iOS/Views/HomeView.swift | 2 ++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/LexAI_iOS/LexAI_iOS/Views/ChatView.swift b/LexAI_iOS/LexAI_iOS/Views/ChatView.swift index 30a15a2..bfc7fdf 100644 --- a/LexAI_iOS/LexAI_iOS/Views/ChatView.swift +++ b/LexAI_iOS/LexAI_iOS/Views/ChatView.swift @@ -15,6 +15,8 @@ struct ChatView: View { @State private var isAwaitingReply = false @Binding var selectedLanguage: String // language in conversation + @EnvironmentObject var firebaseManager: FirebaseManager + @Environment(/.scenePhase) private var scenePhase // to detect app exit private let functions = Functions.functions() @@ -58,6 +60,37 @@ struct ChatView: View { } #endif } + // triger 1: user leaves the view (new chat) + .onDisappear { + saveChatIfNeeded() + } + + // trigger 2: app goes to background or is killed + .onChange(of: scenePhase) { , newPhase in + if newPhase == .background || newPhase == .inactive { + saveChatIfNeeded() + } + } + } + + // MARK: - Save chat + private func saveChatIfNeeded() { + guard !messages.isEmpty else { return } + + // Build a readable transcript from the message array + let transcript = messages + .map { ($0.isFromUser ? "User" : "LexAI") + ": " + $0.text } + .joined(separator: "\n") + + let chatPrompt = FirebaseManager.ChatPrompt( + prompt: transcript, + documents: [], + location: "", + language: selectedLanguage, + user: firebaseManager.user?.uid ?? "anonymous" + ) + + firebaseManager.saveChat(prompt: chatPrompt) } private var messageList: some View { @@ -201,5 +234,6 @@ private struct MessageBubbleView: View { #Preview { @Previewable @State var selectedLanguage = "English" return ChatView(selectedLanguage: $selectedLanguage) + .environmentObject(FirebaseManager()) } diff --git a/LexAI_iOS/LexAI_iOS/Views/ContentView.swift b/LexAI_iOS/LexAI_iOS/Views/ContentView.swift index ff78857..64598c7 100644 --- a/LexAI_iOS/LexAI_iOS/Views/ContentView.swift +++ b/LexAI_iOS/LexAI_iOS/Views/ContentView.swift @@ -9,15 +9,16 @@ import SwiftUI struct ContentView: View { - @StateObject var authManager = AuthManager() + @StateObject var firebaseManager = FirebaseManager() var body: some View { Group { - if authManager.isAuthenticated { + if firebaseManager.isAuthenticated { HomeView() + .environmentObject(firebaseManager) } else { AuthView() - .environmentObject(authManager) + .environmentObject(firebaseManager) } } } diff --git a/LexAI_iOS/LexAI_iOS/Views/HomeView.swift b/LexAI_iOS/LexAI_iOS/Views/HomeView.swift index f94723c..5d0c4b0 100644 --- a/LexAI_iOS/LexAI_iOS/Views/HomeView.swift +++ b/LexAI_iOS/LexAI_iOS/Views/HomeView.swift @@ -12,6 +12,7 @@ struct HomeView: View { @State private var showToolbar = true @AppStorage("selectedLanguage") private var selectedLanguage: String = "English" @State private var showLanguageDropdown = false + @EnvironmentObject var firebaseManager: FirebaseManager private let languages = ["English", "Spanish", "French", "Arabic", "German"] @@ -20,6 +21,7 @@ struct HomeView: View { ZStack { VStack { ChatView(selectedLanguage: $selectedLanguage) + .environmentObject(firebaseManager) } if isSidebarOpen {