Add search
This commit is contained in:
parent
9e16f9a693
commit
8a4196db3a
|
@ -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) {
|
||||
|
|
|
@ -12,16 +12,20 @@ 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 {
|
||||
if searchQuery.isEmpty {
|
||||
KeysSection(codeHolder: entryHolder)
|
||||
|
||||
Section {
|
||||
|
@ -31,6 +35,9 @@ struct AppView: View {
|
|||
}
|
||||
|
||||
FoldersSection()
|
||||
} else {
|
||||
KeysSection(codeHolder: allEntriesHolder, searchQuery: searchQuery)
|
||||
}
|
||||
}
|
||||
.listStyle(.insetGrouped)
|
||||
.navigationTitle("OTP")
|
||||
|
@ -48,6 +55,7 @@ struct AppView: View {
|
|||
}
|
||||
}
|
||||
}
|
||||
.searchable(text: $searchQuery)
|
||||
.sheet(isPresented: $isPresentingPreferences, content: self.preferencesSheet)
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue