Can update scanned expenses entries
This commit is contained in:
@@ -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