2023-12-29 21:39:54 +01:00
|
|
|
// ignore_for_file: inference_failure_on_function_invocation
|
|
|
|
|
|
|
|
import 'dart:async';
|
2023-11-21 20:23:14 +01:00
|
|
|
import 'dart:math';
|
2023-12-29 21:39:54 +01:00
|
|
|
|
2023-11-21 20:23:14 +01:00
|
|
|
import 'package:flutter/foundation.dart';
|
2023-09-08 11:50:21 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2023-12-29 21:39:54 +01:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2023-09-14 19:44:34 +02:00
|
|
|
import 'package:flutter_slidable/flutter_slidable.dart';
|
2023-09-08 11:50:21 +02:00
|
|
|
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
|
2023-10-05 17:47:20 +02:00
|
|
|
import 'package:flutter_tesseract_ocr/flutter_tesseract_ocr.dart';
|
2023-09-08 11:50:21 +02:00
|
|
|
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';
|
2023-11-01 17:58:05 +01:00
|
|
|
import 'package:prasule/api/category.dart';
|
2023-10-09 17:28:05 +02:00
|
|
|
import 'package:prasule/api/entry_data.dart';
|
2023-09-08 11:50:21 +02:00
|
|
|
import 'package:prasule/api/wallet.dart';
|
2024-01-08 18:21:31 +01:00
|
|
|
import 'package:prasule/api/wallet_entry.dart';
|
|
|
|
import 'package:prasule/api/wallet_manager.dart';
|
2023-09-08 11:50:21 +02:00
|
|
|
import 'package:prasule/main.dart';
|
|
|
|
import 'package:prasule/network/tessdata.dart';
|
2023-09-14 19:44:34 +02:00
|
|
|
import 'package:prasule/pw/platformbutton.dart';
|
2023-09-08 11:50:21 +02:00
|
|
|
import 'package:prasule/pw/platformdialog.dart';
|
2023-11-01 17:58:05 +01:00
|
|
|
import 'package:prasule/pw/platformroute.dart';
|
2023-11-21 20:23:14 +01:00
|
|
|
import 'package:prasule/util/drawer.dart';
|
2024-01-08 15:38:31 +01:00
|
|
|
import 'package:prasule/util/text_color.dart';
|
2023-09-08 11:50:21 +02:00
|
|
|
import 'package:prasule/views/create_entry.dart';
|
|
|
|
import 'package:prasule/views/settings/settings.dart';
|
2023-10-05 17:47:20 +02:00
|
|
|
import 'package:prasule/views/settings/tessdata_list.dart';
|
2023-09-08 11:50:21 +02:00
|
|
|
import 'package:prasule/views/setup.dart';
|
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Main view, shows entries
|
2023-09-08 11:50:21 +02:00
|
|
|
class HomeView extends StatefulWidget {
|
2023-12-29 21:39:54 +01:00
|
|
|
/// Main view, shows entries
|
2023-09-08 11:50:21 +02:00
|
|
|
const HomeView({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<HomeView> createState() => _HomeViewState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _HomeViewState extends State<HomeView> {
|
|
|
|
Wallet? selectedWallet;
|
2023-11-01 18:39:21 +01:00
|
|
|
List<Wallet> wallets = [];
|
2023-09-08 11:50:21 +02:00
|
|
|
DateTime? prevDate;
|
|
|
|
late String locale;
|
|
|
|
@override
|
|
|
|
void didChangeDependencies() {
|
|
|
|
super.didChangeDependencies();
|
|
|
|
locale = Localizations.localeOf(context).languageCode;
|
|
|
|
initializeDateFormatting(Localizations.localeOf(context).languageCode);
|
2023-09-14 19:44:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2023-09-08 11:50:21 +02:00
|
|
|
loadWallet();
|
|
|
|
}
|
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
Future<void> loadWallet() async {
|
2023-11-01 18:39:21 +01:00
|
|
|
wallets = await WalletManager.listWallets();
|
2023-09-08 11:50:21 +02:00
|
|
|
if (wallets.isEmpty && mounted) {
|
2023-12-29 21:39:54 +01:00
|
|
|
unawaited(
|
|
|
|
Navigator.of(context)
|
|
|
|
.pushReplacement(platformRoute((c) => const SetupView())),
|
|
|
|
);
|
2023-09-08 11:50:21 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-11-01 18:39:21 +01:00
|
|
|
selectedWallet = wallets.first;
|
2023-09-08 11:50:21 +02:00
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
2023-11-21 20:23:14 +01:00
|
|
|
drawer: makeDrawer(context, 1),
|
2023-09-08 11:50:21 +02:00
|
|
|
floatingActionButton: SpeedDial(
|
|
|
|
icon: Icons.add,
|
|
|
|
activeIcon: Icons.close,
|
|
|
|
children: [
|
2023-11-21 20:23:14 +01:00
|
|
|
if (kDebugMode)
|
|
|
|
SpeedDialChild(
|
|
|
|
child: const Icon(Icons.bug_report),
|
2023-12-25 19:03:52 +01:00
|
|
|
label: AppLocalizations.of(context).createTestData,
|
2023-11-21 20:23:14 +01:00
|
|
|
onTap: () {
|
|
|
|
// debug option to quickly fill a wallet with data
|
|
|
|
if (selectedWallet == null) return;
|
|
|
|
selectedWallet!.entries.clear();
|
2023-12-29 21:39:54 +01:00
|
|
|
final random = Random();
|
2023-11-21 20:23:14 +01:00
|
|
|
for (var i = 0; i < 30; i++) {
|
|
|
|
selectedWallet!.entries.add(
|
|
|
|
WalletSingleEntry(
|
|
|
|
data: EntryData(
|
|
|
|
name: "Test Entry #${i + 1}",
|
|
|
|
amount: random.nextInt(20000).toDouble(),
|
|
|
|
),
|
2023-11-22 15:09:05 +01:00
|
|
|
type: (random.nextInt(3) > 0)
|
|
|
|
? EntryType.expense
|
|
|
|
: EntryType.income,
|
2023-11-21 20:23:14 +01:00
|
|
|
date: DateTime(
|
|
|
|
2023,
|
|
|
|
random.nextInt(12) + 1,
|
|
|
|
random.nextInt(28) + 1,
|
|
|
|
),
|
|
|
|
category: selectedWallet!.categories[
|
|
|
|
random.nextInt(selectedWallet!.categories.length)],
|
|
|
|
id: selectedWallet!.nextId,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.i(selectedWallet!.entries.length);
|
|
|
|
|
|
|
|
// save and reload
|
|
|
|
WalletManager.saveWallet(selectedWallet!).then((value) {
|
|
|
|
Navigator.of(context).pushReplacement(
|
|
|
|
platformRoute(
|
|
|
|
(p0) => const HomeView(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
),
|
2023-09-08 11:50:21 +02:00
|
|
|
SpeedDialChild(
|
2023-12-29 21:39:54 +01:00
|
|
|
child: const Icon(Icons.edit),
|
|
|
|
label: AppLocalizations.of(context).addNew,
|
|
|
|
onTap: () async {
|
|
|
|
final sw = await Navigator.of(context).push<Wallet>(
|
|
|
|
MaterialPageRoute(
|
2024-01-08 18:21:31 +01:00
|
|
|
builder: (c) => CreateSingleEntryView(w: selectedWallet!),
|
2023-12-29 21:39:54 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
if (sw != null) {
|
|
|
|
selectedWallet = sw;
|
|
|
|
}
|
|
|
|
setState(() {});
|
|
|
|
},
|
|
|
|
),
|
2023-09-08 11:50:21 +02:00
|
|
|
SpeedDialChild(
|
|
|
|
child: const Icon(Icons.camera_alt),
|
2023-12-25 19:03:52 +01:00
|
|
|
label: AppLocalizations.of(context).addCamera,
|
2023-09-08 11:50:21 +02:00
|
|
|
onTap: () async {
|
2023-12-29 21:39:54 +01:00
|
|
|
final picker = ImagePicker();
|
|
|
|
final media = await picker.pickImage(source: ImageSource.camera);
|
2023-09-08 11:50:21 +02:00
|
|
|
logger.i(media?.name);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
SpeedDialChild(
|
|
|
|
child: const Icon(Icons.image),
|
2023-12-25 19:03:52 +01:00
|
|
|
label: AppLocalizations.of(context).addGallery,
|
2023-10-05 17:47:20 +02:00
|
|
|
onTap: () {
|
|
|
|
startOcr(ImageSource.gallery);
|
2023-09-08 11:50:21 +02:00
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
appBar: AppBar(
|
2023-11-01 18:39:21 +01:00
|
|
|
title: DropdownButton<int>(
|
|
|
|
value:
|
|
|
|
(selectedWallet == null) ? -1 : wallets.indexOf(selectedWallet!),
|
|
|
|
items: [
|
|
|
|
...wallets.map(
|
|
|
|
(e) => DropdownMenuItem(
|
|
|
|
value: wallets.indexOf(
|
|
|
|
e,
|
|
|
|
),
|
|
|
|
child: Text(e.name),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
DropdownMenuItem(
|
|
|
|
value: -1,
|
2023-12-25 19:03:52 +01:00
|
|
|
child: Text(AppLocalizations.of(context).newWallet),
|
2023-12-29 21:39:54 +01:00
|
|
|
),
|
2023-11-01 18:39:21 +01:00
|
|
|
],
|
|
|
|
onChanged: (v) async {
|
|
|
|
if (v == null || v == -1) {
|
2023-11-01 18:42:50 +01:00
|
|
|
await Navigator.of(context).push(
|
2023-11-01 18:39:21 +01:00
|
|
|
platformRoute(
|
|
|
|
(c) => const SetupView(
|
|
|
|
newWallet: true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
wallets = await WalletManager.listWallets();
|
|
|
|
logger.i(wallets.length);
|
|
|
|
selectedWallet = wallets.last;
|
|
|
|
setState(() {});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
selectedWallet = wallets[v];
|
|
|
|
setState(() {});
|
|
|
|
},
|
|
|
|
),
|
2023-09-08 11:50:21 +02:00
|
|
|
actions: [
|
|
|
|
PopupMenuButton(
|
2023-10-11 18:37:18 +02:00
|
|
|
itemBuilder: (context) => [
|
2023-12-25 19:03:52 +01:00
|
|
|
AppLocalizations.of(context).settings,
|
2023-12-29 21:39:54 +01:00
|
|
|
AppLocalizations.of(context).about,
|
2023-10-11 18:37:18 +02:00
|
|
|
].map((e) => PopupMenuItem(value: e, child: Text(e))).toList(),
|
2023-09-08 11:50:21 +02:00
|
|
|
onSelected: (value) {
|
2023-12-25 19:03:52 +01:00
|
|
|
if (value == AppLocalizations.of(context).settings) {
|
2023-12-31 12:41:10 +01:00
|
|
|
Navigator.of(context)
|
|
|
|
.push(
|
2023-12-29 21:39:54 +01:00
|
|
|
platformRoute(
|
|
|
|
(context) => const SettingsView(),
|
2023-09-08 11:50:21 +02:00
|
|
|
),
|
2023-12-31 12:41:10 +01:00
|
|
|
)
|
|
|
|
.then((value) async {
|
|
|
|
selectedWallet =
|
|
|
|
await WalletManager.loadWallet(selectedWallet!.name);
|
|
|
|
});
|
2023-12-25 19:03:52 +01:00
|
|
|
} else if (value == AppLocalizations.of(context).about) {
|
2023-09-08 11:50:21 +02:00
|
|
|
showAboutDialog(
|
2023-12-29 21:39:54 +01:00
|
|
|
context: context,
|
|
|
|
applicationLegalese: AppLocalizations.of(context).license,
|
|
|
|
applicationName: "Prašule",
|
|
|
|
);
|
2023-09-08 11:50:21 +02:00
|
|
|
}
|
|
|
|
},
|
2023-12-29 21:39:54 +01:00
|
|
|
),
|
2023-09-08 11:50:21 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
body: Center(
|
|
|
|
child: SizedBox(
|
|
|
|
width: MediaQuery.of(context).size.width * 0.9,
|
|
|
|
height: MediaQuery.of(context).size.height,
|
|
|
|
child: (selectedWallet == null)
|
|
|
|
? const Column(
|
|
|
|
children: [
|
|
|
|
SizedBox(
|
|
|
|
width: 40,
|
|
|
|
height: 40,
|
|
|
|
child: CircularProgressIndicator(),
|
2023-12-29 21:39:54 +01:00
|
|
|
),
|
2023-09-08 11:50:21 +02:00
|
|
|
],
|
|
|
|
)
|
|
|
|
: (selectedWallet!.entries.isEmpty)
|
2023-10-11 18:37:18 +02:00
|
|
|
? Column(
|
2023-09-08 11:50:21 +02:00
|
|
|
children: [
|
|
|
|
Text(
|
2023-12-25 19:03:52 +01:00
|
|
|
AppLocalizations.of(context).noEntries,
|
2023-10-11 18:37:18 +02:00
|
|
|
style: const TextStyle(
|
2023-09-08 11:50:21 +02:00
|
|
|
fontSize: 20,
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Text(
|
2023-12-25 19:03:52 +01:00
|
|
|
AppLocalizations.of(context).noEntriesSub,
|
2023-12-29 21:39:54 +01:00
|
|
|
),
|
2023-09-08 11:50:21 +02:00
|
|
|
],
|
|
|
|
)
|
|
|
|
: GroupedListView(
|
|
|
|
groupHeaderBuilder: (element) => Text(
|
|
|
|
DateFormat.yMMMM(locale).format(element.date),
|
|
|
|
style: TextStyle(
|
2023-12-29 21:39:54 +01:00
|
|
|
color: Theme.of(context).colorScheme.primary,
|
|
|
|
),
|
2023-09-08 11:50:21 +02:00
|
|
|
),
|
2023-11-21 20:23:14 +01:00
|
|
|
elements: selectedWallet!.entries,
|
|
|
|
itemComparator: (a, b) => b.date.compareTo(a.date),
|
2023-09-08 11:50:21 +02:00
|
|
|
groupBy: (e) => DateFormat.yMMMM(locale).format(e.date),
|
2023-11-21 20:23:14 +01:00
|
|
|
groupComparator: (a, b) {
|
|
|
|
// TODO: better sorting algorithm lol
|
2023-12-29 21:39:54 +01:00
|
|
|
final yearA = RegExp(r'\d+').firstMatch(a);
|
2023-11-21 20:23:14 +01:00
|
|
|
if (yearA == null) return 0;
|
2023-12-29 21:39:54 +01:00
|
|
|
final yearB = RegExp(r'\d+').firstMatch(b);
|
2023-11-21 20:23:14 +01:00
|
|
|
if (yearB == null) return 0;
|
2023-12-29 21:39:54 +01:00
|
|
|
final compareYears = int.parse(yearA.group(0)!)
|
2023-11-21 20:23:14 +01:00
|
|
|
.compareTo(int.parse(yearB.group(0)!));
|
|
|
|
if (compareYears != 0) return compareYears;
|
2023-12-29 21:39:54 +01:00
|
|
|
final months = List<String>.generate(
|
2023-11-21 20:23:14 +01:00
|
|
|
12,
|
|
|
|
(index) => DateFormat.MMMM(locale).format(
|
|
|
|
DateTime(2023, index + 1),
|
|
|
|
),
|
|
|
|
);
|
2023-12-29 21:39:54 +01:00
|
|
|
final monthA = RegExp('[^0-9 ]+').firstMatch(a);
|
2023-11-21 20:23:14 +01:00
|
|
|
if (monthA == null) return 0;
|
2023-12-29 21:39:54 +01:00
|
|
|
final monthB = RegExp('[^0-9 ]+').firstMatch(b);
|
2023-11-21 20:23:14 +01:00
|
|
|
if (monthB == null) return 0;
|
|
|
|
return months.indexOf(monthB.group(0)!).compareTo(
|
|
|
|
months.indexOf(monthA.group(0)!),
|
|
|
|
);
|
|
|
|
},
|
2023-09-14 19:44:34 +02:00
|
|
|
itemBuilder: (context, element) => Slidable(
|
2023-12-29 21:39:54 +01:00
|
|
|
endActionPane: ActionPane(
|
|
|
|
motion: const ScrollMotion(),
|
|
|
|
children: [
|
|
|
|
SlidableAction(
|
|
|
|
onPressed: (c) {
|
|
|
|
Navigator.of(context)
|
|
|
|
.push<WalletSingleEntry>(
|
|
|
|
MaterialPageRoute(
|
2024-01-08 18:21:31 +01:00
|
|
|
builder: (c) => CreateSingleEntryView(
|
2023-12-29 21:39:54 +01:00
|
|
|
w: selectedWallet!,
|
|
|
|
editEntry: element,
|
2023-09-14 19:44:34 +02:00
|
|
|
),
|
2023-12-29 21:39:54 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.then(
|
|
|
|
(editedEntry) {
|
|
|
|
if (editedEntry == null) return;
|
|
|
|
selectedWallet!.entries.remove(element);
|
|
|
|
selectedWallet!.entries.add(editedEntry);
|
|
|
|
WalletManager.saveWallet(selectedWallet!);
|
|
|
|
setState(() {});
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
backgroundColor:
|
|
|
|
Theme.of(context).colorScheme.secondary,
|
|
|
|
foregroundColor:
|
|
|
|
Theme.of(context).colorScheme.onSecondary,
|
|
|
|
icon: Icons.edit,
|
|
|
|
),
|
|
|
|
SlidableAction(
|
|
|
|
backgroundColor:
|
|
|
|
Theme.of(context).colorScheme.error,
|
|
|
|
foregroundColor:
|
|
|
|
Theme.of(context).colorScheme.onError,
|
|
|
|
icon: Icons.delete,
|
|
|
|
onPressed: (c) {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (cx) => PlatformDialog(
|
|
|
|
title:
|
|
|
|
AppLocalizations.of(context).sureDialog,
|
|
|
|
content: Text(
|
|
|
|
AppLocalizations.of(context).deleteSure,
|
2023-09-14 19:44:34 +02:00
|
|
|
),
|
2023-12-29 21:39:54 +01:00
|
|
|
actions: [
|
|
|
|
PlatformButton(
|
|
|
|
text: AppLocalizations.of(context).yes,
|
|
|
|
onPressed: () {
|
|
|
|
selectedWallet?.entries.removeWhere(
|
|
|
|
(e) => e.id == element.id,
|
|
|
|
);
|
|
|
|
WalletManager.saveWallet(
|
|
|
|
selectedWallet!,
|
|
|
|
);
|
|
|
|
Navigator.of(cx).pop();
|
|
|
|
setState(() {});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
PlatformButton(
|
|
|
|
text: AppLocalizations.of(context).no,
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(cx).pop();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2023-09-14 19:44:34 +02:00
|
|
|
child: ListTile(
|
|
|
|
leading: Container(
|
|
|
|
decoration: BoxDecoration(
|
2023-12-29 21:39:54 +01:00
|
|
|
borderRadius: BorderRadius.circular(16),
|
2024-01-08 15:38:31 +01:00
|
|
|
color: element.category.color,
|
2023-12-29 21:39:54 +01:00
|
|
|
),
|
2023-09-14 19:44:34 +02:00
|
|
|
child: Padding(
|
2023-12-29 21:39:54 +01:00
|
|
|
padding: const EdgeInsets.all(8),
|
2023-09-14 19:44:34 +02:00
|
|
|
child: Icon(
|
|
|
|
element.category.icon,
|
|
|
|
color:
|
2024-01-08 15:38:31 +01:00
|
|
|
element.category.color.calculateTextColor(),
|
2023-09-14 19:44:34 +02:00
|
|
|
),
|
2023-09-08 11:50:21 +02:00
|
|
|
),
|
|
|
|
),
|
2023-10-09 17:28:05 +02:00
|
|
|
title: Text(element.data.name),
|
2023-09-14 19:44:34 +02:00
|
|
|
subtitle: Text(
|
2024-01-08 18:21:31 +01:00
|
|
|
NumberFormat.currency(
|
|
|
|
symbol: selectedWallet!.currency.symbol,
|
|
|
|
).format(element.data.amount),
|
2023-12-29 21:39:54 +01:00
|
|
|
),
|
2023-09-08 11:50:21 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-10-05 17:47:20 +02:00
|
|
|
Future<void> startOcr(ImageSource imgSrc) async {
|
2023-12-29 21:39:54 +01:00
|
|
|
final availableLanguages = await TessdataApi.getDownloadedData();
|
2023-10-05 17:47:20 +02:00
|
|
|
if (availableLanguages.isEmpty) {
|
|
|
|
if (!mounted) return;
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
SnackBar(
|
2023-12-25 19:03:52 +01:00
|
|
|
content: Text(AppLocalizations.of(context).missingOcr),
|
2023-10-05 17:47:20 +02:00
|
|
|
action: SnackBarAction(
|
2023-12-25 19:03:52 +01:00
|
|
|
label: AppLocalizations.of(context).download,
|
2023-10-05 17:47:20 +02:00
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).push(
|
2023-12-29 21:39:54 +01:00
|
|
|
platformRoute(
|
|
|
|
(c) => const TessdataListView(),
|
2023-10-05 17:47:20 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!mounted) return;
|
2023-12-29 21:39:54 +01:00
|
|
|
final selectedLanguages =
|
|
|
|
List<bool>.filled(availableLanguages.length, false);
|
2023-10-09 17:28:05 +02:00
|
|
|
selectedLanguages[0] = true;
|
|
|
|
|
2023-12-29 21:39:54 +01:00
|
|
|
await showDialog(
|
2023-10-05 17:47:20 +02:00
|
|
|
context: context,
|
2023-10-09 17:28:05 +02:00
|
|
|
builder: (c) => StatefulBuilder(
|
|
|
|
builder: (ctx, setState) => PlatformDialog(
|
|
|
|
actions: [
|
|
|
|
TextButton(
|
2023-11-01 18:39:21 +01:00
|
|
|
onPressed: () async {
|
2023-12-29 21:39:54 +01:00
|
|
|
final picker = ImagePicker();
|
|
|
|
final media = await picker.pickImage(source: imgSrc);
|
2023-11-01 18:39:21 +01:00
|
|
|
if (media == null) {
|
|
|
|
if (mounted) Navigator.of(context).pop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// get selected languages
|
2023-12-29 21:39:54 +01:00
|
|
|
final selected = availableLanguages
|
|
|
|
.where(
|
|
|
|
(element) => selectedLanguages[
|
|
|
|
availableLanguages.indexOf(element)],
|
|
|
|
)
|
2023-11-01 18:39:21 +01:00
|
|
|
.join("+")
|
|
|
|
.replaceAll(".traineddata", "");
|
|
|
|
logger.i(selected);
|
|
|
|
if (!mounted) return;
|
2023-12-29 21:39:54 +01:00
|
|
|
unawaited(
|
|
|
|
showDialog(
|
2023-11-01 18:39:21 +01:00
|
|
|
context: context,
|
|
|
|
builder: (c) => PlatformDialog(
|
2023-12-29 21:39:54 +01:00
|
|
|
title: AppLocalizations.of(context).ocrLoading,
|
|
|
|
),
|
|
|
|
barrierDismissible: false,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
final string = await FlutterTesseractOcr.extractText(
|
|
|
|
media.path,
|
|
|
|
language: selected,
|
|
|
|
args: {
|
|
|
|
"psm": "4",
|
|
|
|
"preserve_interword_spaces": "1",
|
|
|
|
},
|
|
|
|
);
|
2023-11-01 18:39:21 +01:00
|
|
|
if (!mounted) return;
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
logger.i(string);
|
|
|
|
if (!mounted) return;
|
2023-12-29 21:39:54 +01:00
|
|
|
final lines = string.split("\n")
|
2023-11-01 18:39:21 +01:00
|
|
|
..removeWhere((element) {
|
|
|
|
element.trim();
|
|
|
|
return element.isEmpty;
|
|
|
|
});
|
|
|
|
var price = 0.0;
|
2023-12-29 21:39:54 +01:00
|
|
|
final description = StringBuffer();
|
|
|
|
for (final line in lines) {
|
2023-11-01 18:39:21 +01:00
|
|
|
// find numbered prices on each line
|
2023-12-29 21:39:54 +01:00
|
|
|
final regex = RegExp(r'\d+(?:(?:\.|,) {0,}\d{0,})+');
|
|
|
|
for (final match in regex.allMatches(line)) {
|
2023-11-01 18:39:21 +01:00
|
|
|
price += double.tryParse(match.group(0).toString()) ?? 0;
|
2023-10-09 17:28:05 +02:00
|
|
|
}
|
2023-12-29 21:39:54 +01:00
|
|
|
description.write("${line.replaceAll(regex, "")}\n");
|
2023-11-01 18:39:21 +01:00
|
|
|
}
|
|
|
|
Navigator.of(ctx).pop();
|
|
|
|
// show edit
|
2023-12-29 21:39:54 +01:00
|
|
|
final newEntry =
|
|
|
|
await Navigator.of(context).push<WalletSingleEntry>(
|
2023-11-01 18:39:21 +01:00
|
|
|
platformRoute<WalletSingleEntry>(
|
2024-01-08 18:21:31 +01:00
|
|
|
(c) => CreateSingleEntryView(
|
2023-11-01 18:39:21 +01:00
|
|
|
w: selectedWallet!,
|
|
|
|
editEntry: WalletSingleEntry(
|
|
|
|
data: EntryData(
|
2023-12-29 21:39:54 +01:00
|
|
|
name: "",
|
|
|
|
amount: price,
|
|
|
|
description: description.toString(),
|
|
|
|
),
|
2023-11-01 18:39:21 +01:00
|
|
|
type: EntryType.expense,
|
|
|
|
date: DateTime.now(),
|
|
|
|
category: selectedWallet!.categories.first,
|
|
|
|
id: selectedWallet!.nextId,
|
2023-11-01 17:58:05 +01:00
|
|
|
),
|
|
|
|
),
|
2023-11-01 18:39:21 +01:00
|
|
|
),
|
|
|
|
);
|
2023-12-29 21:39:54 +01:00
|
|
|
if (newEntry == null) return;
|
|
|
|
selectedWallet!.entries.add(newEntry);
|
|
|
|
await WalletManager.saveWallet(selectedWallet!);
|
|
|
|
setState(() {});
|
2023-11-01 18:39:21 +01:00
|
|
|
},
|
|
|
|
child: const Text("Ok"),
|
|
|
|
),
|
2023-10-09 17:28:05 +02:00
|
|
|
TextButton(
|
2023-11-01 18:39:21 +01:00
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(c).pop();
|
|
|
|
},
|
|
|
|
child: const Text("Cancel"),
|
|
|
|
),
|
2023-10-05 17:47:20 +02:00
|
|
|
],
|
2023-12-25 19:03:52 +01:00
|
|
|
title: AppLocalizations.of(context).ocrSelect,
|
2023-10-09 17:28:05 +02:00
|
|
|
content: Column(
|
|
|
|
children: [
|
|
|
|
...List.generate(
|
|
|
|
availableLanguages.length,
|
|
|
|
(index) => Row(
|
|
|
|
children: [
|
|
|
|
Checkbox(
|
|
|
|
value: selectedLanguages[index],
|
|
|
|
onChanged: (value) {
|
|
|
|
if (value == null ||
|
|
|
|
(selectedLanguages
|
|
|
|
.where((element) => element)
|
|
|
|
.length <=
|
|
|
|
1 &&
|
|
|
|
!value)) return;
|
|
|
|
selectedLanguages[index] = value;
|
|
|
|
setState(() {});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
const SizedBox(
|
|
|
|
width: 10,
|
|
|
|
),
|
2023-12-29 21:39:54 +01:00
|
|
|
Text(availableLanguages[index].split(".").first),
|
2023-10-09 17:28:05 +02:00
|
|
|
],
|
|
|
|
),
|
2023-12-29 21:39:54 +01:00
|
|
|
),
|
2023-10-09 17:28:05 +02:00
|
|
|
],
|
|
|
|
),
|
2023-10-05 17:47:20 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-09-08 11:50:21 +02:00
|
|
|
Future<void> getLostData() async {
|
2023-12-29 21:39:54 +01:00
|
|
|
final picker = ImagePicker();
|
|
|
|
final response = await picker.retrieveLostData();
|
2023-09-08 11:50:21 +02:00
|
|
|
if (response.isEmpty) {
|
|
|
|
return;
|
|
|
|
}
|
2023-12-29 21:39:54 +01:00
|
|
|
final files = response.files;
|
2023-09-08 11:50:21 +02:00
|
|
|
if (files != null) {
|
2023-10-09 17:28:05 +02:00
|
|
|
logger.i("Found lost files");
|
2023-09-08 11:50:21 +02:00
|
|
|
_handleLostFiles(files);
|
|
|
|
} else {
|
|
|
|
logger.e(response.exception);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _handleLostFiles(List<XFile> files) {
|
|
|
|
// TODO: implement
|
|
|
|
}
|
|
|
|
}
|