From f8b6ea6ba78cec430ebe716b3f14898c3d58f75b Mon Sep 17 00:00:00 2001 From: kingbri Date: Mon, 27 Feb 2023 13:05:46 -0500 Subject: [PATCH] Search: Add random text to the searchbar It's redundant to have the navgiation title of the Search view as "Search" and the searchbar to have the same title. Make the searchbar have randomized quotes (from a fixed set) that doesn't repeat cases. Maybe make this customizable in the future? Signed-off-by: kingbri --- Ferrite/Views/ContentView.swift | 41 +++++++++++++++++++++++++++++++- Ferrite/Views/SettingsView.swift | 5 ++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/Ferrite/Views/ContentView.swift b/Ferrite/Views/ContentView.swift index d4e40c6..7089b7b 100644 --- a/Ferrite/Views/ContentView.swift +++ b/Ferrite/Views/ContentView.swift @@ -15,10 +15,23 @@ struct ContentView: View { @EnvironmentObject var pluginManager: PluginManager @EnvironmentObject var toastModel: ToastViewModel + @AppStorage("Behavior.UsesRandomSearchText") var usesRandomSearchText: Bool = false + @State private var isEditingSearch = false @State private var isSearching = false @State private var searchText: String = "" + @State private var lastSearchTextIndex: Int = -1 + @State private var searchBarText: String = "Search" + let searchBarTextArray: [String] = [ + "What's on your mind?", + "Discover something interesting", + "Find an engaging show", + "Feeling adventurous?", + "Look for something new", + "The classics are a good idea" + ] + var body: some View { NavView { List { @@ -35,6 +48,11 @@ struct ContentView: View { Text("No results found") } } + .onChange(of: searchText) { newText in + if newText.isEmpty && isSearching { + searchBarText = getSearchBarText() + } + } .onChange(of: scrapingModel.searchResults) { _ in // Cleans up any leftover search results in the event of an abrupt cancellation if !isSearching { @@ -54,7 +72,7 @@ struct ContentView: View { .navigationTitle("Search") .navigationSearchBar { SearchBar( - "Search", + searchBarText, text: $searchText, isEditing: $isEditingSearch, onCommit: { @@ -88,6 +106,7 @@ struct ContentView: View { scrapingModel.runningSearchTask = nil isSearching = false searchText = "" + searchBarText = getSearchBarText() } } .navigationSearchBarHiddenWhenScrolling(false) @@ -97,6 +116,26 @@ struct ContentView: View { .environmentObject(scrapingModel) .environmentObject(debridManager) } + .backport.onAppear { + searchBarText = getSearchBarText() + } + } + + // Fetches random searchbar text if enabled, otherwise deinit the last case value + func getSearchBarText() -> String { + if usesRandomSearchText { + let num = Int.random(in: 0..