Make settings observable

This commit is contained in:
Selim Mustafaev 2025-04-15 11:43:24 +03:00
parent 41578579ef
commit 0ca4c232db
5 changed files with 20 additions and 61 deletions

View File

@ -25,7 +25,7 @@ class MainTabController: UITabBarController, UITabBarControllerDelegate {
addDummyTab() addDummyTab()
#endif #endif
addSearchTab() addSearchTab()
Task { await addSettings() } addSettings()
} }
func addHistoryTab() { func addHistoryTab() {
@ -62,11 +62,14 @@ class MainTabController: UITabBarController, UITabBarControllerDelegate {
viewControllers?.append(controller) viewControllers?.append(controller)
} }
func addSettings() async { func addSettings() {
let coordinator = SettingsCoordinator(tabController: self) let coordinator = SettingsCoordinator(tabController: self)
settingsCoordinator = coordinator settingsCoordinator = coordinator
try? await coordinator.start() let controller = coordinator.start()
controller.tabBarItem = UITabBarItem(title: NSLocalizedString("Settings", comment: ""),
image: UIImage(systemName: "gear"), tag: 0)
viewControllers?.append(controller)
} }
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool { func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {

View File

@ -11,44 +11,29 @@ import SwiftUI
import AutoCatCore import AutoCatCore
@MainActor @MainActor
class SettingsCoordinator: Coordinator { class SettingsCoordinator {
weak var viewController: UITabBarController? weak var tabController: UITabBarController?
var settingsController: UIViewController?
init(tabController: UITabBarController) { init(tabController: UITabBarController) {
self.viewController = tabController self.tabController = tabController
} }
func start() async throws { func start() -> UIViewController {
let viewModel = SettingsViewModel(settingsService: ServiceContainer.shared.resolve(SettingsServiceProtocol.self)) let viewModel = SettingsViewModel(settingsService: ServiceContainer.shared.resolve(SettingsServiceProtocol.self))
viewModel.coordinator = self viewModel.coordinator = self
let controller = UIHostingController(rootView: SettingsScreen(viewModel: viewModel)) let controller = UIHostingController(rootView: SettingsScreen(viewModel: viewModel))
settingsController = controller return UINavigationController(rootViewController: controller)
let navController = UINavigationController(rootViewController: controller)
navController.tabBarItem = UITabBarItem(title: NSLocalizedString("Settings", comment: ""),
image: UIImage(systemName: "gear"), tag: 0)
viewController?.viewControllers?.append(navController)
} }
func openAuthScreen() { func openAuthScreen() {
guard let window = viewController?.tabBar.window else { guard let window = tabController?.tabBar.window else {
return return
} }
let coordinator = AuthCoordinator(window: window) let coordinator = AuthCoordinator(window: window)
window.rootViewController = coordinator.start() window.rootViewController = coordinator.start()
} }
func openGoogleOauthPage() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let vc = storyboard.instantiateViewController(identifier: "GoogleSignInController") as? GoogleSignInController {
settingsController?.present(vc, animated: true)
}
}
} }

View File

@ -40,7 +40,7 @@ struct SettingsScreen: View {
} }
Section { Section {
Picker("Server", selection: $viewModel.backend) { Picker("Server", selection: $viewModel.settingService.backend) {
ForEach(Constants.Backend.allCases, id: \.self) { backend in ForEach(Constants.Backend.allCases, id: \.self) { backend in
Text(backend.name) Text(backend.name)
} }
@ -50,26 +50,26 @@ struct SettingsScreen: View {
Section("Plate number recognition") { Section("Plate number recognition") {
ToggleRowView(title: "Alternative order", ToggleRowView(title: "Alternative order",
description: "Recognize plate numbers in alternative form. For example 'ЕВА 123 777' instead of 'Е123ВА 777'", description: "Recognize plate numbers in alternative form. For example 'ЕВА 123 777' instead of 'Е123ВА 777'",
toggle: $viewModel.recognizeAlternativeOrder) toggle: $viewModel.settingService.recognizeAlternativeOrder)
ToggleRowView(title: "Shortened numbers", ToggleRowView(title: "Shortened numbers",
description: "If enabled, app will try to recognize shortened plate numbers (without region) and add default region", description: "If enabled, app will try to recognize shortened plate numbers (without region) and add default region",
toggle: $viewModel.recognizeShortenedNumbers) toggle: $viewModel.settingService.recognizeShortenedNumbers)
if viewModel.recognizeShortenedNumbers { if viewModel.settingService.recognizeShortenedNumbers {
LabeledContent("Default region") { LabeledContent("Default region") {
TextField("", text: $viewModel.defaultRegion) TextField("", text: $viewModel.settingService.defaultRegion)
.frame(width: 50) .frame(width: 50)
.multilineTextAlignment(.trailing) .multilineTextAlignment(.trailing)
} }
} }
ToggleRowView(title: "Beep before record", ToggleRowView(title: "Beep before record",
description: "When enabled, you will hear short sound before starting audio recording. This will only work when audio record is started via Siri", description: "When enabled, you will hear short sound before starting audio recording. This will only work when audio record is started via Siri",
toggle: $viewModel.recordBeep) toggle: $viewModel.settingService.recordBeep)
} }
Section("Debug") { Section("Debug") {
ToggleRowView(title: "Show debug info", ToggleRowView(title: "Show debug info",
description: nil, description: nil,
toggle: $viewModel.showDebugInfo) toggle: $viewModel.settingService.showDebugInfo)
} }
} }
.navigationTitle("Settings") .navigationTitle("Settings")

View File

@ -42,36 +42,6 @@ class SettingsViewModel {
return jwt.payload.email return jwt.payload.email
} }
var recognizeAlternativeOrder: Bool {
get { settingService.recognizeAlternativeOrder }
set { settingService.recognizeAlternativeOrder = newValue }
}
var recognizeShortenedNumbers: Bool {
get { settingService.recognizeShortenedNumbers }
set { settingService.recognizeShortenedNumbers = newValue }
}
var backend: Constants.Backend {
get { settingService.backend }
set { settingService.backend = newValue }
}
var recordBeep: Bool {
get { settingService.recordBeep }
set { settingService.recordBeep = newValue }
}
var showDebugInfo: Bool {
get { settingService.showDebugInfo }
set { settingService.showDebugInfo = newValue }
}
var defaultRegion: String {
get { settingService.defaultRegion }
set { settingService.defaultRegion = newValue }
}
init(settingsService: SettingsServiceProtocol) { init(settingsService: SettingsServiceProtocol) {
self.settingService = settingsService self.settingService = settingsService

View File

@ -20,6 +20,7 @@ enum SettingsKey: String {
} }
@MainActor @MainActor
@Observable
public final class SettingsService: SettingsServiceProtocol { public final class SettingsService: SettingsServiceProtocol {
let jsonEncoder = JSONEncoder() let jsonEncoder = JSONEncoder()