prasule/lib/views/home.dart

393 lines
15 KiB
Dart
Raw Normal View History

2023-09-08 11:50:21 +02:00
import 'package:flutter/material.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';
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';
import 'package:prasule/api/entry_data.dart';
import 'package:prasule/api/walletentry.dart';
2023-09-08 11:50:21 +02:00
import 'package:prasule/api/wallet.dart';
import 'package:prasule/api/walletmanager.dart';
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';
import 'package:prasule/views/create_entry.dart';
import 'package:prasule/views/settings/settings.dart';
import 'package:prasule/views/settings/tessdata_list.dart';
2023-09-08 11:50:21 +02:00
import 'package:prasule/views/setup.dart';
2023-10-11 18:37:18 +02:00
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2023-09-08 11:50:21 +02:00
class HomeView extends StatefulWidget {
const HomeView({super.key});
@override
State<HomeView> createState() => _HomeViewState();
}
class _HomeViewState extends State<HomeView> {
Wallet? selectedWallet;
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();
}
void loadWallet() async {
var wallets = await WalletManager.listWallets();
if (wallets.isEmpty && mounted) {
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (c) => const SetupView()));
return;
}
selectedWallet = await WalletManager.loadWallet(wallets.first);
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: SpeedDial(
icon: Icons.add,
activeIcon: Icons.close,
children: [
SpeedDialChild(
child: const Icon(Icons.edit),
2023-10-11 18:37:18 +02:00
label: AppLocalizations.of(context)!.addNew,
2023-09-08 11:50:21 +02:00
onTap: () async {
2023-09-14 19:44:34 +02:00
var sw = await Navigator.of(context).push<Wallet>(
2023-09-08 11:50:21 +02:00
MaterialPageRoute(
builder: (c) => CreateEntryView(w: selectedWallet!),
),
);
2023-09-14 19:44:34 +02:00
if (sw != null) {
selectedWallet = sw;
}
2023-09-08 11:50:21 +02:00
setState(() {});
}),
SpeedDialChild(
child: const Icon(Icons.camera_alt),
2023-10-11 18:37:18 +02:00
label: AppLocalizations.of(context)!.addCamera,
2023-09-08 11:50:21 +02:00
onTap: () async {
final ImagePicker picker = ImagePicker();
final XFile? media =
await picker.pickImage(source: ImageSource.camera);
logger.i(media?.name);
},
),
SpeedDialChild(
child: const Icon(Icons.image),
2023-10-11 18:37:18 +02:00
label: AppLocalizations.of(context)!.addGallery,
onTap: () {
startOcr(ImageSource.gallery);
2023-09-08 11:50:21 +02:00
},
),
],
),
appBar: AppBar(
2023-10-11 18:37:18 +02:00
title: Text(AppLocalizations.of(context)!.home),
2023-09-08 11:50:21 +02:00
actions: [
PopupMenuButton(
2023-10-11 18:37:18 +02:00
itemBuilder: (context) => [
AppLocalizations.of(context)!.settings,
AppLocalizations.of(context)!.about
].map((e) => PopupMenuItem(value: e, child: Text(e))).toList(),
2023-09-08 11:50:21 +02:00
onSelected: (value) {
2023-10-11 18:37:18 +02:00
if (value == AppLocalizations.of(context)!.settings) {
2023-09-08 11:50:21 +02:00
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => const SettingsView(),
),
);
2023-10-11 18:37:18 +02:00
} else if (value == AppLocalizations.of(context)!.about) {
2023-09-08 11:50:21 +02:00
showAboutDialog(
context: context,
2023-10-11 18:37:18 +02:00
applicationLegalese: AppLocalizations.of(context)!.license,
2023-09-08 11:50:21 +02:00
applicationName: "Prašule");
}
},
)
],
),
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(),
)
],
)
: (selectedWallet!.entries.isEmpty)
2023-10-11 18:37:18 +02:00
? Column(
2023-09-08 11:50:21 +02:00
children: [
Text(
2023-10-11 18:37:18 +02:00
AppLocalizations.of(context)!.noEntries,
style: const TextStyle(
2023-09-08 11:50:21 +02:00
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
Text(
2023-10-11 18:37:18 +02:00
AppLocalizations.of(context)!.noEntriesSub,
2023-09-08 11:50:21 +02:00
)
],
)
: GroupedListView(
groupHeaderBuilder: (element) => Text(
DateFormat.yMMMM(locale).format(element.date),
style: TextStyle(
color: Theme.of(context).colorScheme.primary),
),
elements: selectedWallet!.entries
..sort((a, b) => a.date.compareTo(b.date)),
groupBy: (e) => DateFormat.yMMMM(locale).format(e.date),
2023-09-14 19:44:34 +02:00
itemBuilder: (context, element) => Slidable(
endActionPane:
ActionPane(motion: const ScrollMotion(), children: [
SlidableAction(
onPressed: (c) {
Navigator.of(context)
.push<WalletSingleEntry>(
2023-09-14 19:44:34 +02:00
MaterialPageRoute(
builder: (c) => CreateEntryView(
w: selectedWallet!,
editEntry: element,
),
),
)
.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(
2023-10-11 18:37:18 +02:00
title:
AppLocalizations.of(context)!.sureDialog,
content: Text(
AppLocalizations.of(context)!.deleteSure),
2023-09-14 19:44:34 +02:00
actions: [
PlatformButton(
text: "Yes",
onPressed: () {
selectedWallet?.entries.removeWhere(
(e) => e.id == element.id);
WalletManager.saveWallet(
selectedWallet!);
Navigator.of(cx).pop();
setState(() {});
},
),
PlatformButton(
text: "No",
onPressed: () {
Navigator.of(cx).pop();
},
),
],
),
);
},
),
]),
child: ListTile(
leading: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16),
color: Theme.of(context).colorScheme.secondary),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Icon(
element.category.icon,
color:
Theme.of(context).colorScheme.onSecondary,
),
2023-09-08 11:50:21 +02:00
),
),
title: Text(element.data.name),
2023-09-14 19:44:34 +02:00
subtitle: Text(
"${element.data.amount} ${selectedWallet!.currency.symbol}"),
2023-09-08 11:50:21 +02:00
),
),
),
),
),
);
}
Future<void> startOcr(ImageSource imgSrc) async {
var availableLanguages = await TessdataApi.getDownloadedData();
if (availableLanguages.isEmpty) {
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
2023-10-11 18:37:18 +02:00
content: Text(AppLocalizations.of(context)!.missingOcr),
action: SnackBarAction(
2023-10-11 18:37:18 +02:00
label: AppLocalizations.of(context)!.download,
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (c) => const TessdataListView(),
),
);
},
),
),
);
return;
}
if (!mounted) return;
var selectedLanguages = List<bool>.filled(availableLanguages.length, false);
selectedLanguages[0] = true;
showDialog(
context: context,
builder: (c) => StatefulBuilder(
builder: (ctx, setState) => PlatformDialog(
actions: [
TextButton(
onPressed: () async {
final ImagePicker picker = ImagePicker();
final XFile? media = await picker.pickImage(source: imgSrc);
if (media == null) {
if (mounted) Navigator.of(context).pop();
return;
}
// get selected languages
var selected = availableLanguages
.where((element) => selectedLanguages[
availableLanguages.indexOf(element)])
.join("+")
.replaceAll(".traineddata", "");
logger.i(selected);
if (!mounted) return;
showDialog(
context: context,
2023-10-11 18:37:18 +02:00
builder: (c) => PlatformDialog(
title: AppLocalizations.of(context)!.ocrLoading),
barrierDismissible: false);
var string = await FlutterTesseractOcr.extractText(media.path,
language: selected,
args: {
"psm": "4",
"preserve_interword_spaces": "1",
});
if (!mounted) return;
Navigator.of(context).pop();
logger.i(string);
if (!mounted) return;
var lines = string.split("\n")
..removeWhere((element) {
element.trim();
return element.isEmpty;
});
var data = <EntryData>[];
for (var line in lines) {
2023-11-01 15:37:17 +01:00
var regex = RegExp(r'\d+(?:\.|,)\d+');
var price = 0.0;
for (var match in regex.allMatches(line)) {
price += double.tryParse(match.group(0).toString()) ?? 0;
}
data.add(EntryData(name: "Idk", amount: price));
}
Navigator.of(context).pop();
2023-11-01 15:37:17 +01:00
// TODO: send to create
},
child: const Text("Ok")),
TextButton(
onPressed: () {
Navigator.of(c).pop();
},
child: const Text("Cancel")),
],
2023-10-11 18:37:18 +02:00
title: AppLocalizations.of(context)!.ocrSelect,
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,
),
Text(availableLanguages[index].split(".").first)
],
),
)
],
),
),
),
);
}
2023-09-08 11:50:21 +02:00
Future<void> getLostData() async {
final ImagePicker picker = ImagePicker();
final LostDataResponse response = await picker.retrieveLostData();
if (response.isEmpty) {
return;
}
final List<XFile>? files = response.files;
if (files != null) {
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
}
}