Search: Fix searchbar and prompts

Make searchbars adhere to autocorrect and fix the random search prompts
not applying by moving functionality to a ViewModel.

Also add a searchbar in the batch choice sheet.

Signed-off-by: kingbri <bdashore3@proton.me>
This commit is contained in:
kingbri 2023-04-01 14:56:43 -04:00
parent 2cf6e46422
commit c8c7732575
6 changed files with 68 additions and 49 deletions

View file

@ -64,4 +64,31 @@ public class NavigationViewModel: ObservableObject {
@Published var libraryPickerSelection: LibraryPickerSegment = .bookmarks
@Published var pluginPickerSelection: PluginPickerSegment = .sources
@Published var searchPrompt: String = "Search"
@Published var lastSearchPromptIndex: Int = -1
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"
]
func getSearchPrompt() {
if UserDefaults.standard.bool(forKey: "Behavior.UsesRandomSearchText") {
let num = Int.random(in: 0 ..< searchBarTextArray.count - 1)
if num == lastSearchPromptIndex {
lastSearchPromptIndex = num + 1
searchPrompt = searchBarTextArray[safe: num + 1] ?? "Search"
} else {
lastSearchPromptIndex = num
searchPrompt = searchBarTextArray[safe: num] ?? "Search"
}
} else {
lastSearchPromptIndex = -1
searchPrompt = "Search"
}
}
}

View file

@ -16,29 +16,15 @@ struct SearchResultsView: View {
@AppStorage("Behavior.UsesRandomSearchText") var usesRandomSearchText: Bool = false
@Binding var searchPrompt: String
@State private var lastSearchPromptIndex: Int = -1
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 {
ForEach(scrapingModel.searchResults, id: \.self) { result in
if result.source == scrapingModel.filteredSource?.name || scrapingModel.filteredSource == nil {
SearchResultButtonView(result: result)
}
}
.onAppear {
searchPrompt = getSearchPrompt()
}
.onChange(of: scrapingModel.searchText) { newText in
if newText.isEmpty, isSearching {
searchPrompt = getSearchPrompt()
navModel.getSearchPrompt()
}
}
.onChange(of: navModel.selectedTab) { tab in
@ -73,21 +59,4 @@ struct SearchResultsView: View {
}
}
}
// Fetches random searchbar text if enabled, otherwise deinit the last case value
func getSearchPrompt() -> String {
if usesRandomSearchText {
let num = Int.random(in: 0 ..< searchBarTextArray.count - 1)
if num == lastSearchPromptIndex {
lastSearchPromptIndex = num + 1
return searchBarTextArray[safe: num + 1] ?? "Search"
} else {
lastSearchPromptIndex = num
return searchBarTextArray[safe: num] ?? "Search"
}
} else {
lastSearchPromptIndex = -1
return "Search"
}
}
}

View file

@ -16,18 +16,21 @@ struct ContentView: View {
@AppStorage("Behavior.AutocorrectSearch") var autocorrectSearch: Bool = false
// TODO: Fix searchPrompt updating
@State private var searchPrompt: String = "Search"
var body: some View {
NavView {
List {
SearchResultsView(searchPrompt: $searchPrompt)
SearchResultsView()
}
.listStyle(.insetGrouped)
.inlinedList(inset: 20)
.navigationTitle("Search")
.searchable(text: $scrapingModel.searchText, placement: .navigationBarDrawer(displayMode: .always), prompt: Text(searchPrompt))
.searchable(
text: $scrapingModel.searchText,
placement: .navigationBarDrawer(displayMode: .always),
prompt: Text(navModel.searchPrompt)
)
.autocorrectionDisabled(!autocorrectSearch)
.textInputAutocapitalization(autocorrectSearch ? .sentences : .never)
.onSubmit(of: .search) {
if let runningSearchTask = scrapingModel.runningSearchTask, runningSearchTask.isCancelled {
scrapingModel.runningSearchTask = nil
@ -48,6 +51,9 @@ struct ContentView: View {
.customScopeBar {
SearchFilterHeaderView()
}
.onAppear {
navModel.getSearchPrompt()
}
}
}
}

View file

@ -82,6 +82,8 @@ struct LibraryView: View {
}
}
.searchable(text: $searchText, placement: .navigationBarDrawer(displayMode: .always))
.autocorrectionDisabled(!autocorrectSearch)
.textInputAutocapitalization(autocorrectSearch ? .sentences : .never)
.customScopeBar {
LibraryPickerView()
}

View file

@ -75,6 +75,8 @@ struct PluginsView: View {
}
.navigationTitle("Plugins")
.searchable(text: $searchText, placement: .navigationBarDrawer(displayMode: .always))
.autocorrectionDisabled(!autocorrectSearch)
.textInputAutocapitalization(autocorrectSearch ? .sentences : .never)
.customScopeBar {
PluginPickerView()
}

View file

@ -15,33 +15,43 @@ struct BatchChoiceView: View {
let backgroundContext = PersistenceController.shared.backgroundContext
// TODO: Make this generic for IA(?) and add searchbar
@AppStorage("Behavior.AutocorrectSearch") var autocorrectSearch = true
@State private var searchText: String = ""
// TODO: Make this generic for an IA protocol
var body: some View {
NavView {
List {
switch debridManager.selectedDebridType {
case .realDebrid:
ForEach(debridManager.selectedRealDebridItem?.files ?? [], id: \.self) { file in
Button(file.name) {
debridManager.selectedRealDebridFile = file
if file.name.lowercased().contains(searchText.lowercased()) || searchText.isEmpty {
Button(file.name) {
debridManager.selectedRealDebridFile = file
queueCommonDownload(fileName: file.name)
queueCommonDownload(fileName: file.name)
}
}
}
case .allDebrid:
ForEach(debridManager.selectedAllDebridItem?.files ?? [], id: \.self) { file in
Button(file.fileName) {
debridManager.selectedAllDebridFile = file
queueCommonDownload(fileName: file.fileName)
if file.fileName.lowercased().contains(searchText.lowercased()) || searchText.isEmpty {
Button(file.fileName) {
debridManager.selectedAllDebridFile = file
queueCommonDownload(fileName: file.fileName)
}
}
}
case .premiumize:
ForEach(debridManager.selectedPremiumizeItem?.files ?? [], id: \.self) { file in
Button(file.name) {
debridManager.selectedPremiumizeFile = file
queueCommonDownload(fileName: file.name)
if file.name.lowercased().contains(searchText.lowercased()) || searchText.isEmpty {
Button(file.name) {
debridManager.selectedPremiumizeFile = file
queueCommonDownload(fileName: file.name)
}
}
}
case .none:
@ -51,6 +61,9 @@ struct BatchChoiceView: View {
.tint(.primary)
.listStyle(.insetGrouped)
.inlinedList(inset: -20)
.searchable(text: $searchText, placement: .navigationBarDrawer(displayMode: .always))
.autocorrectionDisabled(!autocorrectSearch)
.textInputAutocapitalization(autocorrectSearch ? .sentences : .never)
.navigationTitle("Select a file")
.navigationBarTitleDisplayMode(.inline)
.toolbar {