From 20fafc4da808854dce49d732aa188155d2470e67 Mon Sep 17 00:00:00 2001 From: Marcin Czachursk Date: Mon, 6 Mar 2023 16:29:37 +0100 Subject: [PATCH] Improve search view. --- Vernissage.xcodeproj/project.pbxproj | 4 +- Vernissage/Views/MainView.swift | 40 +++++++++++--------- Vernissage/Views/SearchView/SearchView.swift | 15 ++++---- 3 files changed, 32 insertions(+), 27 deletions(-) diff --git a/Vernissage.xcodeproj/project.pbxproj b/Vernissage.xcodeproj/project.pbxproj index 3678899..e1dc9f0 100644 --- a/Vernissage.xcodeproj/project.pbxproj +++ b/Vernissage.xcodeproj/project.pbxproj @@ -1137,7 +1137,7 @@ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_IDENTITY = "Apple Development"; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 53; + CURRENT_PROJECT_VERSION = 54; DEVELOPMENT_ASSET_PATHS = "\"Vernissage/Preview Content\""; DEVELOPMENT_TEAM = B2U9FEKYP8; ENABLE_PREVIEWS = YES; @@ -1174,7 +1174,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_STYLE = Automatic; - CURRENT_PROJECT_VERSION = 53; + CURRENT_PROJECT_VERSION = 54; DEVELOPMENT_ASSET_PATHS = "\"Vernissage/Preview Content\""; DEVELOPMENT_TEAM = B2U9FEKYP8; ENABLE_PREVIEWS = YES; diff --git a/Vernissage/Views/MainView.swift b/Vernissage/Views/MainView.swift index 744e5dd..43bf5aa 100644 --- a/Vernissage/Views/MainView.swift +++ b/Vernissage/Views/MainView.swift @@ -93,8 +93,7 @@ struct MainView: View { ToolbarItem(placement: .principal) { Menu { Button { - HapticService.shared.fireHaptic(of: .tabSelection) - viewMode = .home + self.switchView(to: .home) } label: { HStack { Text(self.getViewTitle(viewMode: .home)) @@ -103,8 +102,7 @@ struct MainView: View { } Button { - HapticService.shared.fireHaptic(of: .tabSelection) - viewMode = .local + self.switchView(to: .local) } label: { HStack { Text(self.getViewTitle(viewMode: .local)) @@ -113,8 +111,7 @@ struct MainView: View { } Button { - HapticService.shared.fireHaptic(of: .tabSelection) - viewMode = .federated + self.switchView(to: .federated) } label: { HStack { Text(self.getViewTitle(viewMode: .federated)) @@ -123,8 +120,7 @@ struct MainView: View { } Button { - HapticService.shared.fireHaptic(of: .tabSelection) - viewMode = .search + self.switchView(to: .search) } label: { HStack { Text(self.getViewTitle(viewMode: .search)) @@ -136,8 +132,7 @@ struct MainView: View { Menu { Button { - HapticService.shared.fireHaptic(of: .tabSelection) - viewMode = .trendingPhotos + self.switchView(to: .trendingPhotos) } label: { HStack { Text(self.getViewTitle(viewMode: .trendingPhotos)) @@ -146,8 +141,7 @@ struct MainView: View { } Button { - HapticService.shared.fireHaptic(of: .tabSelection) - viewMode = .trendingTags + self.switchView(to: .trendingTags) } label: { HStack { Text(self.getViewTitle(viewMode: .trendingTags)) @@ -156,8 +150,7 @@ struct MainView: View { } Button { - HapticService.shared.fireHaptic(of: .tabSelection) - viewMode = .trendingAccounts + self.switchView(to: .trendingAccounts) } label: { HStack { Text(self.getViewTitle(viewMode: .trendingAccounts)) @@ -174,8 +167,7 @@ struct MainView: View { Divider() Button { - HapticService.shared.fireHaptic(of: .tabSelection) - viewMode = .profile + self.switchView(to: .profile) } label: { HStack { Text(self.getViewTitle(viewMode: .profile)) @@ -184,8 +176,7 @@ struct MainView: View { } Button { - HapticService.shared.fireHaptic(of: .tabSelection) - viewMode = .notifications + self.switchView(to: .notifications) } label: { HStack { Text(self.getViewTitle(viewMode: .notifications)) @@ -293,6 +284,19 @@ struct MainView: View { } } + private func switchView(to newViewMode: ViewMode) { + HapticService.shared.fireHaptic(of: .tabSelection) + + if viewMode == .search { + hideKeyboard() + DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { + self.viewMode = newViewMode + } + } else { + self.viewMode = newViewMode + } + } + private func tryToSwitch(_ account: AccountData) { Task { // Verify access token correctness. diff --git a/Vernissage/Views/SearchView/SearchView.swift b/Vernissage/Views/SearchView/SearchView.swift index eb1993a..d7d0904 100644 --- a/Vernissage/Views/SearchView/SearchView.swift +++ b/Vernissage/Views/SearchView/SearchView.swift @@ -22,13 +22,13 @@ struct SearchView: View { HStack { TextField("Search...", text: $query) .padding(8) -// .focused($focusedField, equals: .search) + .focused($focusedField, equals: .search) .keyboardType(.default) .autocapitalization(.none) .autocorrectionDisabled() -// .onAppear() { -// self.focusedField = .search -// } + .onAppear() { + self.focusedField = .search + } Spacer() @@ -41,9 +41,6 @@ struct SearchView: View { .buttonStyle(.bordered) } } - .onDisappear { - self.focusedField = .unknown - } if self.query.isEmpty == false { Section { @@ -66,6 +63,10 @@ struct SearchView: View { } } } + } + .onTapGesture { + self.focusedField = .unknown + hideKeyboard() } .navigationTitle("Search") }