Add search

This commit is contained in:
Shadowfacts 2021-08-25 13:04:52 -04:00
parent 9e16f9a693
commit 8a4196db3a
3 changed files with 33 additions and 11 deletions

View File

@ -10,6 +10,7 @@ import SwiftUI
struct AllKeysView: View { struct AllKeysView: View {
@ObservedObject private var store: KeyStore @ObservedObject private var store: KeyStore
@ObservedObject private var codeHolder: AppView.CodeHolder @ObservedObject private var codeHolder: AppView.CodeHolder
@State private var searchQuery = ""
init() { init() {
let store = KeyStore.shared let store = KeyStore.shared
@ -19,9 +20,10 @@ struct AllKeysView: View {
var body: some View { var body: some View {
List { List {
KeysSection(codeHolder: codeHolder) KeysSection(codeHolder: codeHolder, searchQuery: searchQuery)
} }
.listStyle(.insetGrouped) .listStyle(.insetGrouped)
.searchable(text: $searchQuery)
.navigationTitle("All Keys") .navigationTitle("All Keys")
.toolbar { .toolbar {
ToolbarItem(placement: .navigationBarTrailing) { ToolbarItem(placement: .navigationBarTrailing) {

View File

@ -12,25 +12,32 @@ import Combine
struct AppView: View { struct AppView: View {
@ObservedObject private var store: KeyStore @ObservedObject private var store: KeyStore
@ObservedObject private var entryHolder: CodeHolder @ObservedObject private var entryHolder: CodeHolder
@ObservedObject private var allEntriesHolder: CodeHolder
@State private var searchQuery = ""
@State private var isPresentingPreferences = false @State private var isPresentingPreferences = false
init() { init() {
self.store = .shared self.store = .shared
self.entryHolder = CodeHolder(store: .shared) { (entry) in entry.folderID == nil } self.entryHolder = CodeHolder(store: .shared) { (entry) in entry.folderID == nil }
self.allEntriesHolder = CodeHolder(store: .shared, entryFilter: nil)
} }
var body: some View { var body: some View {
NavigationView { NavigationView {
List { List {
KeysSection(codeHolder: entryHolder) if searchQuery.isEmpty {
KeysSection(codeHolder: entryHolder)
Section { Section {
NavigationLink("All Keys") { NavigationLink("All Keys") {
AllKeysView() AllKeysView()
}
} }
}
FoldersSection() FoldersSection()
} else {
KeysSection(codeHolder: allEntriesHolder, searchQuery: searchQuery)
}
} }
.listStyle(.insetGrouped) .listStyle(.insetGrouped)
.navigationTitle("OTP") .navigationTitle("OTP")
@ -48,6 +55,7 @@ struct AppView: View {
} }
} }
} }
.searchable(text: $searchQuery)
.sheet(isPresented: $isPresentingPreferences, content: self.preferencesSheet) .sheet(isPresented: $isPresentingPreferences, content: self.preferencesSheet)
} }

View File

@ -9,18 +9,30 @@ import SwiftUI
import UniformTypeIdentifiers import UniformTypeIdentifiers
struct KeysSection: View { struct KeysSection: View {
private let searchQuery: String?
@ObservedObject private var store: KeyStore = .shared @ObservedObject private var store: KeyStore = .shared
@ObservedObject private var entryHolder: AppView.CodeHolder @ObservedObject private var entryHolder: AppView.CodeHolder
@State private var editedEntry: AppView.CodeEntry? = nil @State private var editedEntry: AppView.CodeEntry? = nil
@State private var presentedQRCode: AppView.CodeEntry? = nil @State private var presentedQRCode: AppView.CodeEntry? = nil
init(codeHolder: AppView.CodeHolder) { init(codeHolder: AppView.CodeHolder, searchQuery: String? = nil) {
self.entryHolder = codeHolder self.entryHolder = codeHolder
self.searchQuery = searchQuery
}
var filteredEntries: [AppView.CodeEntry] {
if let query = searchQuery?.lowercased(), !query.isEmpty {
return entryHolder.sortedEntries.filter { (e) in
e.key.issuer.lowercased().contains(query) || (e.key.label?.lowercased().contains(query) ?? false)
}
} else {
return entryHolder.sortedEntries
}
} }
var body: some View { var body: some View {
Section { Section {
ForEach(entryHolder.sortedEntries) { (entry) in ForEach(filteredEntries) { (entry) in
KeyView(key: entry.key, currentCode: entry.code) KeyView(key: entry.key, currentCode: entry.code)
// disabled because dropping onto list rows does not work :/ // disabled because dropping onto list rows does not work :/
// .onDrag { // .onDrag {
@ -33,7 +45,7 @@ struct KeysSection: View {
.onDelete { (indices) in .onDelete { (indices) in
withAnimation(.default) { withAnimation(.default) {
for index in indices { for index in indices {
store.removeKey(entryID: entryHolder.sortedEntries[index].id) store.removeKey(entryID: filteredEntries[index].id)
} }
} }
} }