From f614c4dfac23c1eb1ecd70879cec9267b19b89c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maty=C3=A1=C5=A1=20Caras?= Date: Wed, 1 Nov 2023 17:58:05 +0100 Subject: [PATCH] feat: create basic function to load from ocr --- .vscode/settings.json | 3 ++- lib/api/entry_data.dart | 3 ++- lib/api/entry_data.g.dart | 2 ++ lib/api/multientry.dart | 25 --------------------- lib/api/multientry.g.dart | 32 --------------------------- lib/api/wallet.dart | 9 ++++++++ lib/l10n/app_cs.arb | 2 +- lib/pw/platformfield.dart | 8 +++++-- lib/pw/platformroute.dart | 9 ++++++++ lib/views/create_entry.dart | 34 ++++++++++++++++++++++------- lib/views/home.dart | 43 +++++++++++++++++++++++++++++++------ 11 files changed, 93 insertions(+), 77 deletions(-) delete mode 100644 lib/api/multientry.dart delete mode 100644 lib/api/multientry.g.dart create mode 100644 lib/pw/platformroute.dart diff --git a/.vscode/settings.json b/.vscode/settings.json index 1530c4d..4397789 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,7 @@ { "conventionalCommits.scopes": [ "ocr", - "ui" + "ui", + "translations" ] } \ No newline at end of file diff --git a/lib/api/entry_data.dart b/lib/api/entry_data.dart index 5044bfc..fedcec4 100644 --- a/lib/api/entry_data.dart +++ b/lib/api/entry_data.dart @@ -4,9 +4,10 @@ part 'entry_data.g.dart'; @JsonSerializable() class EntryData { String name; + String description; double amount; - EntryData({required this.name, required this.amount}); + EntryData({required this.name, required this.amount, this.description = ""}); factory EntryData.fromJson(Map json) => _$EntryDataFromJson(json); diff --git a/lib/api/entry_data.g.dart b/lib/api/entry_data.g.dart index 2be8617..48a4faa 100644 --- a/lib/api/entry_data.g.dart +++ b/lib/api/entry_data.g.dart @@ -9,9 +9,11 @@ part of 'entry_data.dart'; EntryData _$EntryDataFromJson(Map json) => EntryData( name: json['name'] as String, amount: (json['amount'] as num).toDouble(), + description: json['description'] as String? ?? "", ); Map _$EntryDataToJson(EntryData instance) => { 'name': instance.name, + 'description': instance.description, 'amount': instance.amount, }; diff --git a/lib/api/multientry.dart b/lib/api/multientry.dart deleted file mode 100644 index b53cdc8..0000000 --- a/lib/api/multientry.dart +++ /dev/null @@ -1,25 +0,0 @@ -import 'package:json_annotation/json_annotation.dart'; -import 'package:prasule/api/category.dart'; -import 'package:prasule/api/entry_data.dart'; -part 'multientry.g.dart'; - -@JsonSerializable() -class MultiEntry { - EntryType type; - List data; - DateTime date; - WalletCategory category; - int id; - - MultiEntry( - {required this.data, - required this.type, - required this.date, - required this.category, - required this.id}); - - factory MultiEntry.fromJson(Map json) => - _$MultiEntryFromJson(json); - - Map toJson() => _$MultiEntryToJson(this); -} diff --git a/lib/api/multientry.g.dart b/lib/api/multientry.g.dart deleted file mode 100644 index 3afafa6..0000000 --- a/lib/api/multientry.g.dart +++ /dev/null @@ -1,32 +0,0 @@ -// GENERATED CODE - DO NOT MODIFY BY HAND - -part of 'multientry.dart'; - -// ************************************************************************** -// JsonSerializableGenerator -// ************************************************************************** - -MultiEntry _$MultiEntryFromJson(Map json) => MultiEntry( - data: (json['data'] as List) - .map((e) => EntryData.fromJson(e as Map)) - .toList(), - type: $enumDecode(_$EntryTypeEnumMap, json['type']), - date: DateTime.parse(json['date'] as String), - category: - WalletCategory.fromJson(json['category'] as Map), - id: json['id'] as int, - ); - -Map _$MultiEntryToJson(MultiEntry instance) => - { - 'type': _$EntryTypeEnumMap[instance.type]!, - 'data': instance.data, - 'date': instance.date.toIso8601String(), - 'category': instance.category, - 'id': instance.id, - }; - -const _$EntryTypeEnumMap = { - EntryType.expense: 'expense', - EntryType.income: 'income', -}; diff --git a/lib/api/wallet.dart b/lib/api/wallet.dart index d4aa60b..6d88efd 100644 --- a/lib/api/wallet.dart +++ b/lib/api/wallet.dart @@ -29,4 +29,13 @@ class Wallet { /// Connect the generated [_$PersonToJson] function to the `toJson` method. Map toJson() => _$WalletToJson(this); + + /// Getter for the next unused unique number ID in the wallet's entry list + int get nextId { + var id = 1; + while (entries.where((element) => element.id == id).isNotEmpty) { + id++; // create unique ID + } + return id; + } } diff --git a/lib/l10n/app_cs.arb b/lib/l10n/app_cs.arb index 20f6afd..a06d8fa 100644 --- a/lib/l10n/app_cs.arb +++ b/lib/l10n/app_cs.arb @@ -26,7 +26,7 @@ "settings": "Nastavení", "about": "O aplikaci", "noEntries": "Žádné záznamy :(", - "noEntriesSub": "Přidejte nějaký skrz plovoucího tlačítka vpravo dole.", + "noEntriesSub": "Přidejte nějaký skrz plovoucí tlačítka vpravo dole.", "sureDialog": "Jste si jisti?", "deleteSure": "Opravdu chcete smazat tento záznam?", "missingOcr": "Nemáte stažené žádné jazyky pro OCR!", diff --git a/lib/pw/platformfield.dart b/lib/pw/platformfield.dart index dab58f3..eed5515 100644 --- a/lib/pw/platformfield.dart +++ b/lib/pw/platformfield.dart @@ -15,6 +15,7 @@ class PlatformField extends PlatformWidget { final List? autofillHints; final TextStyle? textStyle; final TextAlign textAlign; + final int? maxLines; const PlatformField( {super.key, this.controller, @@ -27,7 +28,8 @@ class PlatformField extends PlatformWidget { this.onChanged, this.autofillHints, this.textStyle, - this.textAlign = TextAlign.start}); + this.textAlign = TextAlign.start, + this.maxLines = 1}); @override TextField createAndroidWidget(BuildContext context) => TextField( @@ -44,6 +46,7 @@ class PlatformField extends PlatformWidget { inputFormatters: inputFormatters, onChanged: onChanged, autofillHints: autofillHints, + maxLines: maxLines, ); @override @@ -51,13 +54,14 @@ class PlatformField extends PlatformWidget { CupertinoTextField( textAlign: textAlign, controller: controller, - enabled: enabled, + enabled: enabled ?? true, obscureText: obscureText, prefix: (labelText == null) ? null : Text(labelText!), autocorrect: autocorrect, keyboardType: keyboardType, inputFormatters: inputFormatters, onChanged: onChanged, + maxLines: maxLines, style: textStyle, ); } diff --git a/lib/pw/platformroute.dart b/lib/pw/platformroute.dart new file mode 100644 index 0000000..fbcd8b5 --- /dev/null +++ b/lib/pw/platformroute.dart @@ -0,0 +1,9 @@ +import 'dart:io'; + +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +Route platformRoute(Widget Function(BuildContext) builder) => + (Platform.isIOS) + ? CupertinoPageRoute(builder: builder) + : MaterialPageRoute(builder: builder); diff --git a/lib/views/create_entry.dart b/lib/views/create_entry.dart index feaa4e2..c6ea3f2 100644 --- a/lib/views/create_entry.dart +++ b/lib/views/create_entry.dart @@ -26,12 +26,8 @@ class _CreateEntryViewState extends State { if (widget.editEntry != null) { newEntry = widget.editEntry!; } else { - var id = 1; - while (widget.w.entries.where((element) => element.id == id).isNotEmpty) { - id++; // create unique ID - } newEntry = WalletSingleEntry( - id: id, + id: widget.w.nextId, data: EntryData(amount: 0, name: ""), type: EntryType.expense, date: DateTime.now(), @@ -152,6 +148,28 @@ class _CreateEntryViewState extends State { }, ), ), + const SizedBox( + height: 20, + ), + Text(AppLocalizations.of(context)!.description), + const SizedBox( + height: 10, + ), + SizedBox( + height: 300, + width: MediaQuery.of(context).size.width * 0.8, + child: PlatformField( + keyboardType: TextInputType.multiline, + maxLines: null, + controller: TextEditingController( + text: newEntry.data.description, + ), + onChanged: (v) { + newEntry.data.description = v; + setState(() {}); + }, + ), + ), const SizedBox( height: 15, ), @@ -173,9 +191,9 @@ class _CreateEntryViewState extends State { return; } widget.w.entries.add(newEntry); - WalletManager.saveWallet(widget.w).then((value) => - Navigator.of(context) - .pop(widget.w)); // TODO loading circle? + WalletManager.saveWallet(widget.w).then( + (value) => Navigator.of(context).pop(widget.w), + ); // TODO loading circle? }, ) ], diff --git a/lib/views/home.dart b/lib/views/home.dart index df60c85..8fea23e 100644 --- a/lib/views/home.dart +++ b/lib/views/home.dart @@ -6,6 +6,7 @@ import 'package:grouped_list/grouped_list.dart'; import 'package:image_picker/image_picker.dart'; import 'package:intl/date_symbol_data_local.dart'; import 'package:intl/intl.dart'; +import 'package:prasule/api/category.dart'; import 'package:prasule/api/entry_data.dart'; import 'package:prasule/api/walletentry.dart'; import 'package:prasule/api/wallet.dart'; @@ -14,6 +15,7 @@ import 'package:prasule/main.dart'; import 'package:prasule/network/tessdata.dart'; import 'package:prasule/pw/platformbutton.dart'; import 'package:prasule/pw/platformdialog.dart'; +import 'package:prasule/pw/platformroute.dart'; import 'package:prasule/views/create_entry.dart'; import 'package:prasule/views/settings/settings.dart'; import 'package:prasule/views/settings/tessdata_list.dart'; @@ -317,18 +319,45 @@ class _HomeViewState extends State { element.trim(); return element.isEmpty; }); - - var data = []; + var price = 0.0; + var description = ""; for (var line in lines) { - var regex = RegExp(r'\d+(?:\.|,)\d+'); - var price = 0.0; + // find numbered prices on each line + var regex = RegExp(r'\d+(?:(?:\.|,) {0,}\d{0,})+'); for (var match in regex.allMatches(line)) { price += double.tryParse(match.group(0).toString()) ?? 0; } - data.add(EntryData(name: "Idk", amount: price)); + description += "${line.replaceAll(regex, "")}\n"; } - Navigator.of(context).pop(); - // TODO: send to create + Navigator.of(ctx).pop(); + // show edit + Navigator.of(context) + .push( + platformRoute( + (c) => CreateEntryView( + w: selectedWallet!, + editEntry: WalletSingleEntry( + data: EntryData( + name: "", + amount: price, + description: description), + type: EntryType.expense, + date: DateTime.now(), + category: selectedWallet!.categories.first, + id: selectedWallet!.nextId, + ), + ), + ), + ) + .then( + (newEntry) { + // save entry if we didn't return empty + if (newEntry == null) return; + selectedWallet!.entries.add(newEntry); + WalletManager.saveWallet(selectedWallet!); + setState(() {}); + }, + ); }, child: const Text("Ok")), TextButton(