Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working draft, fix #269 #278

Merged
merged 15 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
## Beta 0.11.0 (next)

* Reworked stats tab (ongoing)
* Enhanced search options (ongoing)
* Added partial and exact match mode
* Added option to include description, closes [#269](https://github.com/flow-mn/flow/issues/269)

## Beta 0.10.2

Expand Down
6 changes: 6 additions & 0 deletions assets/l10n/en_IN.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
"transactions.query.filter.keyword.all": "Search",
"transactions.query.filter.keyword.hint": "Search by title...",
"transactions.query.filter.keyword.clear": "Clear",
"transactions.query.filter.keyword.includeDescription": "Include description",
"transactions.query.filter.timeRange": "Time Range",
"transactions.query.filter.timeRange.all": "All Time",
"transactions.query.filter.accounts": "Accounts",
Expand Down Expand Up @@ -383,6 +384,11 @@
"enum.BackupEntryType@other": "Other backup",
"[email protected]": "Other backup",

"enum.TransactionSearchMode": "Search mode",
"enum.TransactionSearchMode@smart": "Smart",
"enum.TransactionSearchMode@substring": "Partial match",
"enum.TransactionSearchMode@exact": "Exact match",

"enum.TransactionGroupRange": "Group unit",
"enum.TransactionGroupRange@hour": "Hour",
"enum.TransactionGroupRange@day": "Day",
Expand Down
6 changes: 6 additions & 0 deletions assets/l10n/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
"transactions.query.filter.keyword.all": "Search",
"transactions.query.filter.keyword.hint": "Search by title...",
"transactions.query.filter.keyword.clear": "Clear",
"transactions.query.filter.keyword.includeDescription": "Include description",
"transactions.query.filter.timeRange": "Time Range",
"transactions.query.filter.timeRange.all": "All Time",
"transactions.query.filter.accounts": "Accounts",
Expand Down Expand Up @@ -383,6 +384,11 @@
"enum.BackupEntryType@other": "Other backup",
"[email protected]": "Other backup",

"enum.TransactionSearchMode": "Search mode",
"enum.TransactionSearchMode@smart": "Smart",
"enum.TransactionSearchMode@substring": "Partial match",
"enum.TransactionSearchMode@exact": "Exact match",

"enum.TransactionGroupRange": "Group unit",
"enum.TransactionGroupRange@hour": "Hour",
"enum.TransactionGroupRange@day": "Day",
Expand Down
6 changes: 6 additions & 0 deletions assets/l10n/it_IT.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
"transactions.query.filter.keyword.all": "Cerca",
"transactions.query.filter.keyword.hint": "Cerca per titolo...",
"transactions.query.filter.keyword.clear": "Cancella cerca",
"transactions.query.filter.keyword.includeDescription": "Includi note",
"transactions.query.filter.timeRange": "Periodo",
"transactions.query.filter.timeRange.all": "Tutto il tempo",
"transactions.query.filter.accounts": "Conti",
Expand Down Expand Up @@ -383,6 +384,11 @@
"enum.BackupEntryType@other": "Altro",
"[email protected]": "Altro tipo di backup",

"enum.TransactionSearchMode": "Modalità di ricerca",
"enum.TransactionSearchMode@smart": "Intelligente",
"enum.TransactionSearchMode@substring": "Corrispondenza parziale",
"enum.TransactionSearchMode@exact": "Corrispondenza esatta",

"enum.TransactionGroupRange": "Unità di gruppo",
"enum.TransactionGroupRange@hour": "Ora",
"enum.TransactionGroupRange@day": "Giorno",
Expand Down
6 changes: 6 additions & 0 deletions assets/l10n/mn_MN.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
"transactions.query.filter.keyword.all": "Хайх",
"transactions.query.filter.keyword.hint": "Гарчгаар хайх...",
"transactions.query.filter.keyword.clear": "Цэвэрлэх",
"transactions.query.filter.keyword.includeDescription": "Тэмдэглэлээс хайх",
"transactions.query.filter.timeRange": "Хугацаа",
"transactions.query.filter.timeRange.all": "Бүх цаг үе",
"transactions.query.filter.accounts": "Данс",
Expand Down Expand Up @@ -383,6 +384,11 @@
"enum.BackupEntryType@other": "Бусад нөөц",
"[email protected]": "Бусад нөөц",

"enum.TransactionSearchMode": "Хайх горим",
"enum.TransactionSearchMode@smart": "Ухаалаг",
"enum.TransactionSearchMode@substring": "Хэсэгчлэн таарах",
"enum.TransactionSearchMode@exact": "Яг таарах",

"enum.TransactionGroupRange": "Бүлэглэх нэгж",
"enum.TransactionGroupRange@hour": "Цаг",
"enum.TransactionGroupRange@day": "Өдөр",
Expand Down
6 changes: 6 additions & 0 deletions assets/l10n/tr_TR.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
"transactions.query.filter.keyword.all": "Aramak",
"transactions.query.filter.keyword.hint": "Başlığa göre ara...",
"transactions.query.filter.keyword.clear": "Temiz",
"transactions.query.filter.keyword.includeDescription": "Notlar ekle",
"transactions.query.filter.timeRange": "Zaman aralığı",
"transactions.query.filter.timeRange.all": "Tüm Zamanlar",
"transactions.query.filter.accounts": "Hesap",
Expand Down Expand Up @@ -383,6 +384,11 @@
"enum.BackupEntryType@other": "Diğer yedekleme",
"[email protected]": "Diğer yedekleme",

"enum.TransactionSearchMode": "Arama modu",
"enum.TransactionSearchMode@smart": "Akıllı",
"enum.TransactionSearchMode@substring": "Kısmi eşleşme",
"enum.TransactionSearchMode@exact": "Tam eşleşme",

"enum.TransactionGroupRange": "Grup birimi",
"enum.TransactionGroupRange@hour": "Saat",
"enum.TransactionGroupRange@day": "Gün",
Expand Down
140 changes: 97 additions & 43 deletions lib/data/transactions_filter/search_data.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
import "package:flow/entity/transaction.dart";
import "package:flow/l10n/named_enum.dart";
import "package:flow/objectbox/actions.dart";
import "package:flow/objectbox/objectbox.g.dart";
import "package:flow/utils/optional.dart";

enum TransactionSearchMode implements LocalizedEnum {
/// Fuzzy matching, allows for little error
smart,

/// Text must contain the keyword, no room for error
substring,

/// Text must be exactly the keyword
exact;

@override
String get localizationEnumValue => name;
@override
String get localizationEnumName => "TransactionSearchMode";
}

/// Fuzzy finding is case insensitive regardless of [caseInsensitive]
class TransactionSearchData {
/// Recomend using normalizedKeyword.
Expand All @@ -17,19 +34,12 @@ class TransactionSearchData {
return null;
}

if (caseInsensitive) {
return trimmed.toLowerCase();
}

return trimmed;
return trimmed.toLowerCase();
}

/// When [true], uses fuzzy matching
/// else, exact matching
final bool smartMatch;
final TransactionSearchMode mode;

/// Fuzzy finding is case insensitive regardless of this
final bool caseInsensitive;
final bool includeDescription;

/// Base score is [10.0]
///
Expand All @@ -40,72 +50,83 @@ class TransactionSearchData {

const TransactionSearchData({
this.keyword,
this.smartMatch = true,
this.caseInsensitive = true,
this.mode = TransactionSearchMode.smart,
this.smartMatchThreshold = 80.0,
this.includeDescription = true,
});

bool predicate(Transaction t) {
if (!smartMatch) {
return _stupidMatching(t);
if (includeDescription &&
normalizedKeyword != null &&
t.description?.toLowerCase().contains(normalizedKeyword!) == true) {
return true;
}

if (normalizedKeyword == null) return true;

final double score = t.titleSuggestionScore(
query: normalizedKeyword,
fuzzyPartial: true,
);

return score >= smartMatchThreshold;
return switch (mode) {
TransactionSearchMode.smart => _smartMatching(t),
TransactionSearchMode.substring => _substringMatching(t),
TransactionSearchMode.exact => _exactMatching(t),
};
}

bool _stupidMatching(Transaction t) {
if (normalizedKeyword == null) return true;

final String? normalizedTitle =
caseInsensitive ? t.title?.trim().toLowerCase() : t.title?.trim();
/// Filter is not available when smart match isn't enabled
Condition<Transaction>? get filter {
if (normalizedKeyword == null) {
return null;
}

if (normalizedTitle == null) return false;
if (!includeDescription) {
return _titleFilter;
}

return normalizedTitle.contains(
final Condition<Transaction> descriptionFilter =
Transaction_.description.contains(
normalizedKeyword!,
caseSensitive: false,
);

if (_titleFilter != null) {
return _titleFilter!.or(descriptionFilter);
}

return Transaction_.title.notNull().or(descriptionFilter);
}

/// Filter is not available when smart match isn't enabled
Condition<Transaction>? get filter {
if (smartMatch) {
Condition<Transaction>? get _titleFilter {
if (mode == TransactionSearchMode.smart) {
return null;
}

if (normalizedKeyword == null) {
return null;
if (mode == TransactionSearchMode.exact) {
return Transaction_.title.equals(
normalizedKeyword!,
caseSensitive: false,
);
}

return Transaction_.title.contains(
normalizedKeyword!,
caseSensitive: !caseInsensitive,
caseSensitive: false,
);
}

TransactionSearchData copyWithOptional({
Optional<String>? keyword,
bool? smartMatch,
bool? caseInsensitive,
TransactionSearchMode? mode,
bool? includeDescription,
double? smartMatchThreshold,
}) {
return TransactionSearchData(
keyword: keyword == null ? this.keyword : keyword.value,
smartMatch: smartMatch ?? this.smartMatch,
caseInsensitive: caseInsensitive ?? this.caseInsensitive,
mode: mode ?? this.mode,
includeDescription: includeDescription ?? this.includeDescription,
smartMatchThreshold: smartMatchThreshold ?? this.smartMatchThreshold,
);
}

@override
int get hashCode => Object.hashAll(
[keyword, smartMatch, caseInsensitive, smartMatchThreshold]);
int get hashCode =>
Object.hashAll([keyword, mode, includeDescription, smartMatchThreshold]);

@override
bool operator ==(Object other) {
Expand All @@ -118,8 +139,41 @@ class TransactionSearchData {
}

return keyword == other.keyword &&
smartMatch == other.smartMatch &&
caseInsensitive == other.caseInsensitive &&
mode == other.mode &&
includeDescription == other.includeDescription &&
smartMatchThreshold == other.smartMatchThreshold;
}

bool _smartMatching(Transaction t) {
if (normalizedKeyword == null) return true;

final double score = t.titleSuggestionScore(
query: normalizedKeyword,
fuzzyPartial: true,
);

return score >= smartMatchThreshold;
}

bool _substringMatching(Transaction t) {
if (normalizedKeyword == null) return true;

final String? normalizedTitle = t.title?.trim().toLowerCase();

if (normalizedTitle == null) return false;

return normalizedTitle.contains(
normalizedKeyword!,
);
}

bool _exactMatching(Transaction t) {
if (normalizedKeyword == null) return true;

final String? normalizedTitle = t.title?.trim().toLowerCase();

if (normalizedTitle == null) return false;

return normalizedTitle == normalizedKeyword!;
}
}
5 changes: 3 additions & 2 deletions lib/objectbox/actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,9 @@ extension TransactionListActions on Iterable<Transaction> {

final matches = where(data.predicate).toList();

if (data.smartMatch && matches.isEmpty) {
return search(data.copyWithOptional(smartMatch: false));
if (data.mode == TransactionSearchMode.smart && matches.isEmpty) {
return search(
data.copyWithOptional(mode: TransactionSearchMode.substring));
}

return matches;
Expand Down
Loading
Loading