From 680c28b21f702178d76b21f848f270ffdc640c7b Mon Sep 17 00:00:00 2001 From: Rafa Date: Tue, 7 May 2024 11:06:03 -0400 Subject: [PATCH] Settings - Ollama Ping Interval (#102) * Settings - Ping Interval Adding a setting to set the interval in seconds when the app should check if the Ollama service is reachable. Default is 5 seconds. * fix: support no pinging at all --------- Co-authored-by: Augustinas Malinauskas --- Enchanted/Stores/AppStore.swift | 10 +++++++++- Enchanted/UI/Shared/Settings/Settings.swift | 2 ++ Enchanted/UI/Shared/Settings/SettingsView.swift | 5 +++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Enchanted/Stores/AppStore.swift b/Enchanted/Stores/AppStore.swift index d109857..45cd2a1 100644 --- a/Enchanted/Stores/AppStore.swift +++ b/Enchanted/Stores/AppStore.swift @@ -15,12 +15,20 @@ final class AppStore { private var cancellables = Set() private var timer: Timer? + private var pingInterval: TimeInterval = 5 @MainActor var isReachable: Bool = true @MainActor var notifications: [NotificationMessage] = [] @MainActor var menuBarIcon: String? = nil init() { - startCheckingReachability() + if let storedIntervalString = UserDefaults.standard.string(forKey: "pingInterval") { + pingInterval = Double(storedIntervalString) ?? 5 + + if pingInterval <= 0 { + pingInterval = .infinity + } + } + startCheckingReachability(interval: pingInterval) } deinit { diff --git a/Enchanted/UI/Shared/Settings/Settings.swift b/Enchanted/UI/Shared/Settings/Settings.swift index 8f6f114..85e3c64 100644 --- a/Enchanted/UI/Shared/Settings/Settings.swift +++ b/Enchanted/UI/Shared/Settings/Settings.swift @@ -18,6 +18,7 @@ struct Settings: View { @AppStorage("defaultOllamaModel") private var defaultOllamaModel: String = "" @AppStorage("ollamaBearerToken") private var ollamaBearerToken: String = "" @AppStorage("appUserInitials") private var appUserInitials: String = "" + @AppStorage("pingInterval") private var pingInterval: String = "5" @Environment(\.presentationMode) var presentationMode @@ -59,6 +60,7 @@ struct Settings: View { defaultOllamModel: $defaultOllamaModel, ollamaBearerToken: $ollamaBearerToken, appUserInitials: $appUserInitials, + pingInterval: $pingInterval, save: save, checkServer: checkServer, deleteAllConversations: conversationStore.deleteAllConversations, diff --git a/Enchanted/UI/Shared/Settings/SettingsView.swift b/Enchanted/UI/Shared/Settings/SettingsView.swift index 0189f7c..2ecfb7c 100644 --- a/Enchanted/UI/Shared/Settings/SettingsView.swift +++ b/Enchanted/UI/Shared/Settings/SettingsView.swift @@ -16,6 +16,7 @@ struct SettingsView: View { @Binding var defaultOllamModel: String @Binding var ollamaBearerToken: String @Binding var appUserInitials: String + @Binding var pingInterval: String @State var ollamaStatus: Bool? var save: () -> () var checkServer: () -> () @@ -101,6 +102,9 @@ struct SettingsView: View { #if os(iOS) .autocapitalization(.none) #endif + TextField("Ping Interval (seconds)", text: $pingInterval) + .disableAutocorrection(true) + .textFieldStyle(RoundedBorderTextFieldStyle()) Section(header: Text("APP").font(.headline).padding(.top, 20)) { @@ -164,6 +168,7 @@ struct SettingsView: View { defaultOllamModel: .constant("llama2"), ollamaBearerToken: .constant("x"), appUserInitials: .constant("AM"), + pingInterval: .constant("5"), save: {}, checkServer: {}, deleteAllConversations: {},