Can update scanned expenses entries
This commit is contained in:
@@ -7,6 +7,7 @@ import 'package:moneymgr_mobile/routes/login/manual_auth_screen.dart';
|
||||
import 'package:moneymgr_mobile/routes/login/qr_auth_screen.dart';
|
||||
import 'package:moneymgr_mobile/routes/profile/profile_screen.dart';
|
||||
import 'package:moneymgr_mobile/routes/scan/scan_screen.dart';
|
||||
import 'package:moneymgr_mobile/routes/scan_details/scan_details.dart';
|
||||
import 'package:moneymgr_mobile/routes/scans_list/scans_list_screen.dart';
|
||||
import 'package:moneymgr_mobile/routes/settings/settings_screen.dart';
|
||||
import 'package:moneymgr_mobile/services/router/routes_list.dart';
|
||||
@@ -36,7 +37,7 @@ GoRouter router(Ref ref) {
|
||||
// see [AuthState] enum.
|
||||
final navigationItems = [
|
||||
NavigationItem(
|
||||
path: scanPage,
|
||||
path: capturePage,
|
||||
body: (_) => ScanScreen(),
|
||||
icon: Icons.camera_alt_outlined,
|
||||
selectedIcon: Icons.camera_alt,
|
||||
@@ -48,6 +49,15 @@ GoRouter router(Ref ref) {
|
||||
icon: Icons.list,
|
||||
selectedIcon: Icons.list_alt,
|
||||
label: "List",
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: ":id",
|
||||
builder: (_, state) {
|
||||
final id = int.parse(state.pathParameters["id"]!);
|
||||
return ScanDetailScreen(id: id);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
NavigationItem(
|
||||
path: profilePage,
|
||||
|
@@ -14,7 +14,7 @@ const manualAuthPage = "/login/manual";
|
||||
const settingsPage = "/settings";
|
||||
|
||||
/// Scan path
|
||||
const scanPage = "/scan";
|
||||
const capturePage = "/scan";
|
||||
|
||||
/// Scans page
|
||||
const scansPage = "/scans";
|
||||
|
@@ -1,6 +1,7 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:math';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
@@ -8,17 +9,24 @@ import 'package:path/path.dart' as p;
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
part 'expenses.freezed.dart';part 'expenses.g.dart';
|
||||
part 'expenses.freezed.dart';
|
||||
|
||||
part 'expenses.g.dart';
|
||||
|
||||
typedef ExpensesList = List<Expense>;
|
||||
|
||||
@freezed
|
||||
abstract class BaseExpenseInfo with _$BaseExpenseInfo {
|
||||
const BaseExpenseInfo._();
|
||||
|
||||
const factory BaseExpenseInfo({
|
||||
required String? label,
|
||||
required double cost,
|
||||
required DateTime time,
|
||||
}) = _BaseExpenseInfo;
|
||||
|
||||
/// Get expense time as second
|
||||
int get timeAsSeconds => (time.millisecondsSinceEpoch / 1000).floor();
|
||||
}
|
||||
|
||||
@freezed
|
||||
@@ -45,6 +53,10 @@ abstract class Expense with _$Expense {
|
||||
factory Expense.fromJson(Map<String, dynamic> json) =>
|
||||
_$ExpenseFromJson(json);
|
||||
|
||||
/// Get base expense information
|
||||
BaseExpenseInfo get baseExpense =>
|
||||
BaseExpenseInfo(label: label, cost: cost, time: dateTime);
|
||||
|
||||
/// Get associated expense file name
|
||||
String get localFileName {
|
||||
if (mimeType == "application/pdf") return "$id.pdf";
|
||||
@@ -109,7 +121,7 @@ class ExpensesManager {
|
||||
id: (list.lastOrNull?.id ?? 0) + Random().nextInt(1000),
|
||||
label: info.label,
|
||||
cost: info.cost,
|
||||
time: (info.time.millisecondsSinceEpoch / 1000).floor(),
|
||||
time: info.timeAsSeconds,
|
||||
mimeType: fileMimeType,
|
||||
);
|
||||
|
||||
@@ -128,4 +140,28 @@ class ExpensesManager {
|
||||
list.add(exp);
|
||||
await saveList(list);
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a single expense information by its ID
|
||||
Future<Expense?> getById(int id) async {
|
||||
final list = await getList();
|
||||
return list.firstWhere((e) => e.id == id);
|
||||
}
|
||||
|
||||
/// Load the file associated with an expense
|
||||
Future<Uint8List> loadFile(Expense expense) async {
|
||||
final path = p.join(filesStoragePath.absolute.path, expense.localFileName);
|
||||
return File(path).readAsBytes();
|
||||
}
|
||||
|
||||
/// Update expense information
|
||||
Future<void> updateExpense(Expense expense, BaseExpenseInfo newInfo) async {
|
||||
final list = await getList();
|
||||
final entry = list.indexWhere((e) => e.id == expense.id);
|
||||
list[entry] = Expense(id: expense.id,
|
||||
label: newInfo.label,
|
||||
cost: newInfo.cost,
|
||||
time: newInfo.timeAsSeconds,
|
||||
mimeType: expense.mimeType);
|
||||
saveList(list);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user