Add search
This commit is contained in:
parent
9e16f9a693
commit
8a4196db3a
|
@ -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) {
|
||||||
|
|
|
@ -12,16 +12,20 @@ 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 {
|
||||||
|
if searchQuery.isEmpty {
|
||||||
KeysSection(codeHolder: entryHolder)
|
KeysSection(codeHolder: entryHolder)
|
||||||
|
|
||||||
Section {
|
Section {
|
||||||
|
@ -31,6 +35,9 @@ struct AppView: View {
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue