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

View File

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

View File

@ -9,18 +9,30 @@ import SwiftUI
import UniformTypeIdentifiers
struct KeysSection: View {
private let searchQuery: String?
@ObservedObject private var store: KeyStore = .shared
@ObservedObject private var entryHolder: AppView.CodeHolder
@State private var editedEntry: 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.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 {
Section {
ForEach(entryHolder.sortedEntries) { (entry) in
ForEach(filteredEntries) { (entry) in
KeyView(key: entry.key, currentCode: entry.code)
// disabled because dropping onto list rows does not work :/
// .onDrag {
@ -33,7 +45,7 @@ struct KeysSection: View {
.onDelete { (indices) in
withAnimation(.default) {
for index in indices {
store.removeKey(entryID: entryHolder.sortedEntries[index].id)
store.removeKey(entryID: filteredEntries[index].id)
}
}
}