feat: edit & delete

This commit is contained in:
Matyáš Caras 2023-09-14 19:44:34 +02:00
parent 54599c7d37
commit ba04bd5a1c
Signed by untrusted user who does not match committer: hernik
GPG key ID: 2A3175F98820C5C6
9 changed files with 158 additions and 47 deletions

View file

@ -25,6 +25,11 @@ class WalletCategory {
/// Connect the generated [_$PersonToJson] function to the `toJson` method. /// Connect the generated [_$PersonToJson] function to the `toJson` method.
Map<String, dynamic> toJson() => _$WalletCategoryToJson(this); Map<String, dynamic> toJson() => _$WalletCategoryToJson(this);
@override
String toString() {
return name;
}
} }
Map<String, dynamic> _iconDataToJson(IconData icon) => Map<String, dynamic> _iconDataToJson(IconData icon) =>

View file

@ -9,19 +9,21 @@ class WalletEntry {
double amount; double amount;
DateTime date; DateTime date;
WalletCategory category; WalletCategory category;
int id;
WalletEntry( WalletEntry(
{required this.name, {required this.name,
required this.amount, required this.amount,
required this.type, required this.type,
required this.date, required this.date,
required this.category}); required this.category,
required this.id});
/// Connect the generated [_$WalletEntry] function to the `fromJson` /// Connect the generated [_$WalletEntry] function to the `fromJson`
/// factory. /// factory.
factory WalletEntry.fromJson(Map<String, dynamic> json) => factory WalletEntry.fromJson(Map<String, dynamic> json) =>
_$WalletEntryFromJson(json); _$WalletEntryFromJson(json);
/// Connect the generated [_$PersonToJson] function to the `toJson` method. /// Connect the generated [_$WalletEntryToJson] function to the `toJson` method.
Map<String, dynamic> toJson() => _$WalletEntryToJson(this); Map<String, dynamic> toJson() => _$WalletEntryToJson(this);
} }

View file

@ -13,6 +13,7 @@ WalletEntry _$WalletEntryFromJson(Map<String, dynamic> json) => WalletEntry(
date: DateTime.parse(json['date'] as String), date: DateTime.parse(json['date'] as String),
category: category:
WalletCategory.fromJson(json['category'] as Map<String, dynamic>), WalletCategory.fromJson(json['category'] as Map<String, dynamic>),
id: json['id'] as int,
); );
Map<String, dynamic> _$WalletEntryToJson(WalletEntry instance) => Map<String, dynamic> _$WalletEntryToJson(WalletEntry instance) =>
@ -22,6 +23,7 @@ Map<String, dynamic> _$WalletEntryToJson(WalletEntry instance) =>
'amount': instance.amount, 'amount': instance.amount,
'date': instance.date.toIso8601String(), 'date': instance.date.toIso8601String(),
'category': instance.category, 'category': instance.category,
'id': instance.id,
}; };
const _$EntryTypeEnumMap = { const _$EntryTypeEnumMap = {

View file

@ -27,7 +27,7 @@ class PlatformField extends PlatformWidget<TextField, CupertinoTextField> {
this.onChanged, this.onChanged,
this.autofillHints, this.autofillHints,
this.textStyle, this.textStyle,
this.textAlign = TextAlign.center}); this.textAlign = TextAlign.start});
@override @override
TextField createAndroidWidget(BuildContext context) => TextField( TextField createAndroidWidget(BuildContext context) => TextField(

View file

@ -9,7 +9,8 @@ import 'package:prasule/pw/platformfield.dart';
class CreateEntryView extends StatefulWidget { class CreateEntryView extends StatefulWidget {
final Wallet w; final Wallet w;
const CreateEntryView({super.key, required this.w}); final WalletEntry? editEntry;
const CreateEntryView({super.key, required this.w, this.editEntry});
@override @override
State<CreateEntryView> createState() => _CreateEntryViewState(); State<CreateEntryView> createState() => _CreateEntryViewState();
@ -20,12 +21,21 @@ class _CreateEntryViewState extends State<CreateEntryView> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
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 = WalletEntry( newEntry = WalletEntry(
id: id,
name: "", name: "",
amount: 0, amount: 0,
type: EntryType.expense, type: EntryType.expense,
date: DateTime.now(), date: DateTime.now(),
category: widget.w.categories.first); category: widget.w.categories.first);
}
setState(() {}); setState(() {});
} }
@ -36,7 +46,7 @@ class _CreateEntryViewState extends State<CreateEntryView> {
title: const Text("Create new entry"), title: const Text("Create new entry"),
), ),
body: SizedBox( body: SizedBox(
width: MediaQuery.of(context).size.width * 0.9, width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height, height: MediaQuery.of(context).size.height,
child: Center( child: Center(
child: SingleChildScrollView( child: SingleChildScrollView(
@ -45,8 +55,9 @@ class _CreateEntryViewState extends State<CreateEntryView> {
children: [ children: [
const Text("Name"), const Text("Name"),
SizedBox( SizedBox(
width: MediaQuery.of(context).size.width * 0.4, width: MediaQuery.of(context).size.width * 0.8,
child: PlatformField( child: PlatformField(
controller: TextEditingController(text: newEntry.name),
onChanged: (v) { onChanged: (v) {
newEntry.name = v; newEntry.name = v;
}, },
@ -57,13 +68,15 @@ class _CreateEntryViewState extends State<CreateEntryView> {
), ),
const Text("Amount"), const Text("Amount"),
SizedBox( SizedBox(
width: MediaQuery.of(context).size.width * 0.4, width: MediaQuery.of(context).size.width * 0.8,
child: PlatformField( child: PlatformField(
controller: TextEditingController(text: "0"), controller:
TextEditingController(text: newEntry.amount.toString()),
keyboardType: keyboardType:
const TextInputType.numberWithOptions(decimal: true), const TextInputType.numberWithOptions(decimal: true),
inputFormatters: [ inputFormatters: [
FilteringTextInputFormatter.allow(RegExp(r'\d+[\.,]\d+')) FilteringTextInputFormatter.allow(
RegExp(r'\d+[\.,]{0,1}\d{0,}'))
], ],
onChanged: (v) { onChanged: (v) {
newEntry.amount = double.parse(v); newEntry.amount = double.parse(v);
@ -78,24 +91,23 @@ class _CreateEntryViewState extends State<CreateEntryView> {
height: 10, height: 10,
), ),
SizedBox( SizedBox(
width: MediaQuery.of(context).size.width * 0.4, width: MediaQuery.of(context).size.width * 0.8,
child: DropdownButton<EntryType>( child: DropdownButton<EntryType>(
value: newEntry.type, value: newEntry.type,
items: [ items: [
DropdownMenuItem( DropdownMenuItem(
value: EntryType.expense, value: EntryType.expense,
child: SizedBox( child: SizedBox(
width: MediaQuery.of(context).size.width * 0.4 - 24, width: MediaQuery.of(context).size.width * 0.8 - 24,
child: const Text( child: const Text(
"Expense", "Expense",
textAlign: TextAlign.center,
), ),
), ),
), ),
DropdownMenuItem( DropdownMenuItem(
value: EntryType.income, value: EntryType.income,
child: SizedBox( child: SizedBox(
width: MediaQuery.of(context).size.width * 0.4 - 24, width: MediaQuery.of(context).size.width * 0.8 - 24,
child: const Text("Income"), child: const Text("Income"),
), ),
), ),
@ -115,26 +127,26 @@ class _CreateEntryViewState extends State<CreateEntryView> {
height: 10, height: 10,
), ),
SizedBox( SizedBox(
width: MediaQuery.of(context).size.width * 0.4, width: MediaQuery.of(context).size.width * 0.8,
child: DropdownButton<int>( child: DropdownButton<int>(
value: widget.w.categories.indexOf(newEntry.category), value: newEntry.category.id,
items: List.generate( items: List.generate(
widget.w.categories.length, widget.w.categories.length,
(index) => DropdownMenuItem( (index) => DropdownMenuItem(
value: widget.w.categories value: widget.w.categories[index].id,
.indexOf(widget.w.categories[index]),
child: SizedBox( child: SizedBox(
width: MediaQuery.of(context).size.width * 0.4 - 24, width: MediaQuery.of(context).size.width * 0.8 - 24,
child: Text( child: Text(
widget.w.categories[index].name, widget.w.categories[index].name,
textAlign: TextAlign.center,
), ),
), ),
), ),
), ),
onChanged: (v) { onChanged: (v) {
if (v == null) return; if (v == null) return;
newEntry.category = widget.w.categories[v]; newEntry.category = widget.w.categories
.where((element) => element.id == v)
.first;
setState(() {}); setState(() {});
}, },
), ),
@ -154,6 +166,10 @@ class _CreateEntryViewState extends State<CreateEntryView> {
); );
return; return;
} }
if (widget.editEntry != null) {
Navigator.of(context).pop(newEntry);
return;
}
widget.w.entries.add(newEntry); widget.w.entries.add(newEntry);
WalletManager.saveWallet(widget.w).then((value) => WalletManager.saveWallet(widget.w).then((value) =>
Navigator.of(context) Navigator.of(context)

View file

@ -1,16 +1,16 @@
import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart'; import 'package:flutter_speed_dial/flutter_speed_dial.dart';
import 'package:flutter_tesseract_ocr/flutter_tesseract_ocr.dart';
import 'package:grouped_list/grouped_list.dart'; import 'package:grouped_list/grouped_list.dart';
import 'package:image_picker/image_picker.dart'; import 'package:image_picker/image_picker.dart';
import 'package:intl/date_symbol_data_local.dart'; import 'package:intl/date_symbol_data_local.dart';
import 'package:intl/intl.dart'; import 'package:intl/intl.dart';
import 'package:prasule/api/entry.dart';
import 'package:prasule/api/wallet.dart'; import 'package:prasule/api/wallet.dart';
import 'package:prasule/api/walletmanager.dart'; import 'package:prasule/api/walletmanager.dart';
import 'package:prasule/main.dart'; import 'package:prasule/main.dart';
import 'package:prasule/network/tessdata.dart'; import 'package:prasule/network/tessdata.dart';
import 'package:prasule/pw/platformbutton.dart';
import 'package:prasule/pw/platformdialog.dart'; import 'package:prasule/pw/platformdialog.dart';
import 'package:prasule/views/create_entry.dart'; import 'package:prasule/views/create_entry.dart';
import 'package:prasule/views/settings/settings.dart'; import 'package:prasule/views/settings/settings.dart';
@ -32,6 +32,11 @@ class _HomeViewState extends State<HomeView> {
super.didChangeDependencies(); super.didChangeDependencies();
locale = Localizations.localeOf(context).languageCode; locale = Localizations.localeOf(context).languageCode;
initializeDateFormatting(Localizations.localeOf(context).languageCode); initializeDateFormatting(Localizations.localeOf(context).languageCode);
}
@override
void initState() {
super.initState();
loadWallet(); loadWallet();
} }
@ -57,11 +62,14 @@ class _HomeViewState extends State<HomeView> {
child: const Icon(Icons.edit), child: const Icon(Icons.edit),
label: "Add new", label: "Add new",
onTap: () async { onTap: () async {
selectedWallet = await Navigator.of(context).push<Wallet>( var sw = await Navigator.of(context).push<Wallet>(
MaterialPageRoute( MaterialPageRoute(
builder: (c) => CreateEntryView(w: selectedWallet!), builder: (c) => CreateEntryView(w: selectedWallet!),
), ),
); );
if (sw != null) {
selectedWallet = sw;
}
setState(() {}); setState(() {});
}), }),
SpeedDialChild( SpeedDialChild(
@ -186,7 +194,74 @@ class _HomeViewState extends State<HomeView> {
elements: selectedWallet!.entries elements: selectedWallet!.entries
..sort((a, b) => a.date.compareTo(b.date)), ..sort((a, b) => a.date.compareTo(b.date)),
groupBy: (e) => DateFormat.yMMMM(locale).format(e.date), groupBy: (e) => DateFormat.yMMMM(locale).format(e.date),
itemBuilder: (context, element) => ListTile( itemBuilder: (context, element) => Slidable(
endActionPane:
ActionPane(motion: const ScrollMotion(), children: [
SlidableAction(
onPressed: (c) {
Navigator.of(context)
.push<WalletEntry>(
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(
title: "Are you sure",
content: const Text(
"Do you really want to delete this entry?"),
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( leading: Container(
decoration: BoxDecoration( decoration: BoxDecoration(
borderRadius: BorderRadius.circular(16), borderRadius: BorderRadius.circular(16),
@ -195,7 +270,8 @@ class _HomeViewState extends State<HomeView> {
padding: const EdgeInsets.all(8.0), padding: const EdgeInsets.all(8.0),
child: Icon( child: Icon(
element.category.icon, element.category.icon,
color: Theme.of(context).colorScheme.onSecondary, color:
Theme.of(context).colorScheme.onSecondary,
), ),
), ),
), ),
@ -206,6 +282,7 @@ class _HomeViewState extends State<HomeView> {
), ),
), ),
), ),
),
); );
} }

View file

@ -42,19 +42,19 @@ class _SetupViewState extends State<SetupView> {
WalletCategory( WalletCategory(
name: "Car", name: "Car",
type: EntryType.expense, type: EntryType.expense,
id: 1, id: 2,
icon: IconData(Icons.car_repair.codePoint, fontFamily: 'MaterialIcons'), icon: IconData(Icons.car_repair.codePoint, fontFamily: 'MaterialIcons'),
), ),
WalletCategory( WalletCategory(
name: "Food", name: "Food",
type: EntryType.expense, type: EntryType.expense,
id: 1, id: 3,
icon: IconData(Icons.restaurant.codePoint, fontFamily: 'MaterialIcons'), icon: IconData(Icons.restaurant.codePoint, fontFamily: 'MaterialIcons'),
), ),
WalletCategory( WalletCategory(
name: "Travel", name: "Travel",
type: EntryType.expense, type: EntryType.expense,
id: 1, id: 4,
icon: IconData(Icons.train.codePoint, fontFamily: 'MaterialIcons'), icon: IconData(Icons.train.codePoint, fontFamily: 'MaterialIcons'),
), ),
]; ];

View file

@ -403,6 +403,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "2.0.15" version: "2.0.15"
flutter_slidable:
dependency: "direct main"
description:
name: flutter_slidable
sha256: cc4231579e3eae41ae166660df717f4bad1359c87f4a4322ad8ba1befeb3d2be
url: "https://pub.dev"
source: hosted
version: "3.0.0"
flutter_speed_dial: flutter_speed_dial:
dependency: "direct main" dependency: "direct main"
description: description:
@ -1094,4 +1102,4 @@ packages:
version: "3.1.2" version: "3.1.2"
sdks: sdks:
dart: ">=3.1.0-262.2.beta <3.3.0" dart: ">=3.1.0-262.2.beta <3.3.0"
flutter: ">=3.4.0-17.0.pre" flutter: ">=3.7.0"

View file

@ -50,6 +50,7 @@ dependencies:
flutter_speed_dial: ^7.0.0 flutter_speed_dial: ^7.0.0
image_picker: ^1.0.1 image_picker: ^1.0.1
flutter_tesseract_ocr: ^0.4.23 flutter_tesseract_ocr: ^0.4.23
flutter_slidable: ^3.0.0
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: