Ferrite-backup/Ferrite/Views/PluginsView.swift
kingbri c8c7732575 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>
2023-04-01 14:56:43 -04:00

91 lines
3 KiB
Swift

//
// PluginsView.swift
// Ferrite
//
// Created by Brian Dashore on 1/11/23.
//
import SwiftUI
struct PluginsView: View {
@EnvironmentObject var pluginManager: PluginManager
@EnvironmentObject var navModel: NavigationViewModel
@AppStorage("Behavior.AutocorrectSearch") var autocorrectSearch = true
@FetchRequest(
entity: Source.entity(),
sortDescriptors: []
) var installedSources: FetchedResults<Source>
@FetchRequest(
entity: Action.entity(),
sortDescriptors: []
) var installedActions: FetchedResults<Action>
@State private var checkedForPlugins = false
// Bound to the isSearching environment var
@State private var isSearching = false
@State private var searchText: String = ""
var body: some View {
NavView {
ZStack {
if checkedForPlugins {
switch navModel.pluginPickerSelection {
case .sources:
PluginAggregateView<Source, SourceJson>(
installedPlugins: installedSources,
searchText: $searchText
)
case .actions:
PluginAggregateView<Action, ActionJson>(
installedPlugins: installedActions,
searchText: $searchText
)
}
}
}
.searchListener(isSearching: $isSearching)
.overlay {
if !isSearching {
if checkedForPlugins {
switch navModel.pluginPickerSelection {
case .sources:
if installedSources.isEmpty, pluginManager.availableSources.isEmpty {
EmptyInstructionView(title: "No Sources", message: "Add a plugin list in Settings")
}
case .actions:
if installedActions.isEmpty, pluginManager.availableActions.isEmpty {
EmptyInstructionView(title: "No Actions", message: "Add a plugin list in Settings")
}
}
} else {
ProgressView()
}
}
}
.task {
await pluginManager.fetchPluginsFromUrl()
checkedForPlugins = true
}
.onDisappear {
checkedForPlugins = false
}
.navigationTitle("Plugins")
.searchable(text: $searchText, placement: .navigationBarDrawer(displayMode: .always))
.autocorrectionDisabled(!autocorrectSearch)
.textInputAutocapitalization(autocorrectSearch ? .sentences : .never)
.customScopeBar {
PluginPickerView()
}
}
}
}
struct PluginsView_Previews: PreviewProvider {
static var previews: some View {
PluginsView()
}
}