Skip to content

Commit

Permalink
Merge pull request #198 from moneymanagerex/vm-13
Browse files Browse the repository at this point in the history
add search in aux data
  • Loading branch information
georgeef authored Nov 2, 2024
2 parents d44a666 + 35ecc52 commit 52083a4
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 43 deletions.
14 changes: 1 addition & 13 deletions MMEX/App/Cache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,6 @@
import Foundation
import SQLite

struct CurrencyInfo {
let name : String
let baseConvRate : Double
let formatter : CurrencyFormatter

init(_ data: CurrencyData) {
self.name = data.name
self.baseConvRate = data.baseConvRate
self.formatter = data.formatter
}
}

typealias CurrencyCache = [DataId: CurrencyInfo]

extension CurrencyCache {
Expand All @@ -28,7 +16,7 @@ extension CurrencyCache {
}

mutating func load(_ dict: [DataId: CurrencyData]) {
self = dict.mapValues { data in CurrencyInfo(data) }
self = dict.mapValues(CurrencyInfo.init)
}

mutating func update(id: DataId, data: CurrencyData) {
Expand Down
12 changes: 12 additions & 0 deletions MMEX/Data/CurrencyData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ extension CurrencyData {
}
}

struct CurrencyInfo {
let name : String
let baseConvRate : Double
let formatter : CurrencyFormatter

init(_ data: CurrencyData) {
self.name = data.name
self.baseConvRate = data.baseConvRate
self.formatter = data.formatter
}
}

extension CurrencyData {
static let sampleData: [CurrencyData] = [
CurrencyData(
Expand Down
3 changes: 2 additions & 1 deletion MMEX/ViewModel/Group/PayeeGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ extension ViewModel {
dict[$0.key] != nil ? ($0.key, $0.value) : nil
}.sorted { $0.1 < $1.1 }.map { $0.0 }
for g in payeeGroup.groupCategory {
let name = categoryPath.path[g]
var name = categoryPath.path[g]
if name != nil, name!.isEmpty { name = "(none)" }
payeeGroup.append(name, dict[g] ?? [], dict[g] != nil, true)
}
case .attachment:
Expand Down
2 changes: 1 addition & 1 deletion MMEX/ViewModel/Load/LoadMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ struct LoadMainUsed<MainRepository: RepositoryProtocol>: LoadFetchProtocol {
}

nonisolated func fetchValue(env: EnvironmentManager) async -> ValueType? {
MainRepository(env)?.selectId(from: self.table).map { Set($0) }
MainRepository(env)?.selectId(from: self.table).map(Set.init)
}
}

Expand Down
13 changes: 10 additions & 3 deletions MMEX/ViewModel/Search/AccountSearch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ import SwiftUI

struct AccountSearch: SearchProtocol {
var area: [SearchArea<AccountData>] = [
("Name", true, [ {$0.name} ], []),
("Notes", false, [ {$0.notes} ], []),
("Other", false, [ {$0.num}, {$0.heldAt}, {$0.website}, {$0.contactInfo}, {$0.accessInfo} ], []),
("Name", true, {[ $0.name ]}, nil),
("Currency", false, nil, { vm, data in [
vm.currencyList.name.readyValue?[data.currencyId] ?? ""
] } ),
("Notes", false, {[ $0.notes ]}, nil),
("Attachment", false, nil, { vm, data in
(vm.accountList.att.readyValue?[data.id]?.map { $0.description } ?? []) +
(vm.accountList.att.readyValue?[data.id]?.map { $0.filename } ?? [])
} ),
("Other", false, {[ $0.num, $0.heldAt, $0.website, $0.contactInfo, $0.accessInfo ]}, nil),
]
var key: String = ""
}
Expand Down
11 changes: 9 additions & 2 deletions MMEX/ViewModel/Search/AssetSearch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ import SwiftUI

struct AssetSearch: SearchProtocol {
var area: [SearchArea<AssetData>] = [
("Name", true, [ {$0.name} ], []),
("Notes", false, [ {$0.notes} ], []),
("Name", true, {[ $0.name ]}, nil),
("Currency", false, nil, { vm, data in [
vm.currencyList.name.readyValue?[data.currencyId] ?? ""
] } ),
("Attachment", false, nil, { vm, data in
(vm.accountList.att.readyValue?[data.id]?.map { $0.description } ?? []) +
(vm.accountList.att.readyValue?[data.id]?.map { $0.filename } ?? [])
} ),
("Notes", false, {[ $0.notes ]}, nil),
]
var key: String = ""
}
Expand Down
6 changes: 4 additions & 2 deletions MMEX/ViewModel/Search/CategorySearch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import SwiftUI

struct CategorySearch: SearchProtocol {
var area: [SearchArea<CategoryData>] = [
("Name", true, [ {$0.name} ], []),
//("Path", false, [ {$0.path} ]),
("Name", true, {[ $0.name ]}, nil),
("Path", false, nil, { vm, data in [
vm.categoryList.path.readyValue?.path[data.id] ?? ""
] } ),
]
var key: String = ""
}
Expand Down
10 changes: 5 additions & 5 deletions MMEX/ViewModel/Search/CurrencySearch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import SwiftUI

struct CurrencySearch: SearchProtocol {
var area: [SearchArea<CurrencyData>] = [
("Name", true, [ {$0.name} ], []),
("Symbol", false, [ {$0.symbol} ], []),
("Decimal point", false, [ {$0.decimalPoint} ], []),
("Group separator", false, [ {$0.groupSeparator} ], []),
("Other", false, [ {$0.unitName}, {$0.centName} ], []),
("Name", true, {[ $0.name ]}, nil),
("Symbol", false, {[ $0.symbol ]}, nil),
("Decimal point", false, {[ $0.decimalPoint ]}, nil),
("Group separator", false, {[ $0.groupSeparator ]}, nil),
("Other", false, {[ $0.unitName, $0.centName ]}, nil),
]
var key: String = ""
}
Expand Down
13 changes: 10 additions & 3 deletions MMEX/ViewModel/Search/PayeeSearch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ import SwiftUI

struct PayeeSearch: SearchProtocol {
var area: [SearchArea<PayeeData>] = [
("Name", true, [ {$0.name} ], []),
("Notes", false, [ {$0.notes} ], []),
("Other", false, [ {$0.number}, {$0.website}, {$0.pattern} ], []),
("Name", true, {[ $0.name ]}, nil),
("Category", false, nil, { vm, data in [
vm.categoryList.path.readyValue?.path[data.categoryId] ?? ""
] } ),
("Notes", false, {[ $0.notes ]}, nil),
("Attachment", false, nil, { vm, data in
(vm.accountList.att.readyValue?[data.id]?.map { $0.description } ?? []) +
(vm.accountList.att.readyValue?[data.id]?.map { $0.filename } ?? [])
} ),
("Other", false, {[ $0.number, $0.website, $0.pattern ]}, nil),
]
var key: String = ""
}
Expand Down
25 changes: 15 additions & 10 deletions MMEX/ViewModel/Search/SearchProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import SwiftUI
typealias SearchArea<MainData: DataProtocol> = (
name: String,
isSelected: Bool,
mainValues: [(MainData) -> String],
auxValues: [(ViewModel, MainData) -> String]
mainValues: ((MainData) -> [String])?,
auxValues: (@MainActor (ViewModel, MainData) -> [String])?
)

protocol SearchProtocol: Copyable {
Expand All @@ -27,19 +27,24 @@ extension SearchProtocol {

var isEmpty: Bool { key.isEmpty }

@MainActor
func match(_ vm: ViewModel, _ data: MainData) -> Bool {
if key.isEmpty { return true }
for i in 0 ..< area.count {
guard area[i].isSelected else { continue }
if area[i].mainValues.first(
where: { $0(data).localizedCaseInsensitiveContains(key) }
) != nil {
return true
if let mainValues = area[i].mainValues {
for value in mainValues(data) {
if value.localizedCaseInsensitiveContains(key) {
return true
}
}
}
if area[i].auxValues.first(
where: { $0(vm, data).localizedCaseInsensitiveContains(key) }
) != nil {
return true
if let auxValues = area[i].auxValues {
for value in auxValues(vm, data) {
if value.localizedCaseInsensitiveContains(key) {
return true
}
}
}
}
return false
Expand Down
13 changes: 10 additions & 3 deletions MMEX/ViewModel/Search/StockSearch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ import SwiftUI

struct StockSearch: SearchProtocol {
var area: [SearchArea<StockData>] = [
("Name", true, [ {$0.name} ], []),
("Symbol", false, [ {$0.symbol} ], []),
("Notes", false, [ {$0.notes} ], []),
("Name", true, {[ $0.name ]}, nil),
("Symbol", false, {[ $0.symbol ]}, nil),
("Account", false, nil, { vm, data in [
vm.accountList.data.readyValue?[data.accountId]?.name ?? ""
] } ),
("Attachment", false, nil, { vm, data in
(vm.accountList.att.readyValue?[data.id]?.map { $0.description } ?? []) +
(vm.accountList.att.readyValue?[data.id]?.map { $0.filename } ?? [])
} ),
("Notes", false, {[ $0.notes ]}, nil),
]
var key: String = ""
}
Expand Down

0 comments on commit 52083a4

Please sign in to comment.