From 0eff6bd3e8bf781874c15fe914a8a740ea751165 Mon Sep 17 00:00:00 2001 From: Florian Schindler <florianschndlr@gmail.com> Date: Wed, 21 Jun 2023 07:35:58 +0200 Subject: [PATCH] changed category id map keys and added transactions to locator --- .../frontend/utils/transaction_listtile.dart | 9 ++-- .../lib/frontend/views/category_view.dart | 4 +- trackeroo/lib/frontend/views/home_view.dart | 4 +- trackeroo/lib/logic/models/category.dart | 6 ++- trackeroo/lib/logic/models/category.g.dart | 9 ++-- trackeroo/lib/logic/models/transaction.dart | 12 ++++-- trackeroo/lib/logic/models/transaction.g.dart | 23 +++++----- .../logic/services/categories_controller.dart | 15 ++++--- trackeroo/lib/logic/services/locator.dart | 42 ++++++++++++------- .../services/transactions_controller.dart | 29 +++++++++++++ 10 files changed, 106 insertions(+), 47 deletions(-) create mode 100644 trackeroo/lib/logic/services/transactions_controller.dart diff --git a/trackeroo/lib/frontend/utils/transaction_listtile.dart b/trackeroo/lib/frontend/utils/transaction_listtile.dart index 0dba454..b6828a5 100644 --- a/trackeroo/lib/frontend/utils/transaction_listtile.dart +++ b/trackeroo/lib/frontend/utils/transaction_listtile.dart @@ -19,7 +19,8 @@ class _TransactionListtileState extends State<TransactionListtile> { @override void initState() { - category = locator.get<CategoriesController>().categories[widget.transaction.categoryId]; + category = locator.get<CategoriesController>().categories[widget.transaction.categoryId] ?? + Category(id: 'no_category', title: '', iconCodePoint: Icons.attach_money_rounded.codePoint, colorValue: Colors.transparent.value); super.initState(); } @@ -66,7 +67,7 @@ class _TransactionListtileState extends State<TransactionListtile> { ), ), Text( - "${category.title} · ${widget.transaction.createdAt.day}.${monthsGer[widget.transaction.createdAt.month]} · ${widget.transaction.createdAt.hour < 10 ? 0 : ''}${widget.transaction.createdAt.hour}:${widget.transaction.createdAt.minute < 10 ? 0 : ''}${widget.transaction.createdAt.minute}" + "${widget.transaction.createdAt.day}.${monthsGer[widget.transaction.createdAt.month]} · ${widget.transaction.createdAt.hour < 10 ? 0 : ''}${widget.transaction.createdAt.hour}:${widget.transaction.createdAt.minute < 10 ? 0 : ''}${widget.transaction.createdAt.minute}" ) ], ), @@ -126,7 +127,7 @@ class _TransactionListtileState extends State<TransactionListtile> { ), ), const SizedBox(height: 20.0), - Container( + category.id != 'no_category' ? Container( padding: const EdgeInsets.symmetric(horizontal: 15.0, vertical: 12.0), decoration: BoxDecoration( color: Color(category.colorValue), @@ -139,7 +140,7 @@ class _TransactionListtileState extends State<TransactionListtile> { Text(category.title) ], ), - ), + ) : const SizedBox(), const SizedBox(height: 20.0), const Expanded(child: SizedBox()), // to be removed when more content is added Row( diff --git a/trackeroo/lib/frontend/views/category_view.dart b/trackeroo/lib/frontend/views/category_view.dart index 0e21fcd..8361e88 100644 --- a/trackeroo/lib/frontend/views/category_view.dart +++ b/trackeroo/lib/frontend/views/category_view.dart @@ -17,8 +17,8 @@ class CategoryView extends StatelessWidget { ListView.builder( physics: const NeverScrollableScrollPhysics(), shrinkWrap: true, - itemCount: locator.get<CategoriesController>().categories.values.length, - itemBuilder: (context, index) => CategoryListtile(category: locator.get<CategoriesController>().categories[index]), + itemCount: locator.get<CategoriesController>().categories.length, + itemBuilder: (context, index) => CategoryListtile(category: locator.get<CategoriesController>().categories.values.elementAt(index)), ) ], ), diff --git a/trackeroo/lib/frontend/views/home_view.dart b/trackeroo/lib/frontend/views/home_view.dart index d1cd428..9b1b702 100644 --- a/trackeroo/lib/frontend/views/home_view.dart +++ b/trackeroo/lib/frontend/views/home_view.dart @@ -14,8 +14,8 @@ class HomeView extends StatefulWidget { } class _HomeViewState extends State<HomeView> { - Transaction trn = Transaction(title: "Bus Ticket VVS", amount: -5.32, categoryId: 1, dueDate: DateTime.now()); - Transaction trp = Transaction(title: "Gehalt", amount: 1300, categoryId: 5, dueDate: DateTime.now()); + Transaction trn = Transaction(title: "Bus Ticket VVS", amount: -5.32, categoryId: 'food_and_groceries', dueDate: DateTime.now()); + Transaction trp = Transaction(title: "Gehalt", amount: 1300, categoryId: 'leisure_and_entertainment', dueDate: DateTime.now()); Timespan timespanView = Timespan.monthly; diff --git a/trackeroo/lib/logic/models/category.dart b/trackeroo/lib/logic/models/category.dart index dea47d6..f68cace 100644 --- a/trackeroo/lib/logic/models/category.dart +++ b/trackeroo/lib/logic/models/category.dart @@ -5,7 +5,7 @@ part 'category.g.dart'; @HiveType(typeId: 0) class Category { @HiveField(0) - int id; + String id; @HiveField(1) String title; @@ -20,6 +20,9 @@ class Category { int colorValue; @HiveField(5) + double spendings; + + @HiveField(6) double budget; Category({ @@ -28,6 +31,7 @@ class Category { required this.iconCodePoint, this.iconFontFamily = 'MaterialIcons', required this.colorValue, + this.spendings = 0, this.budget = -1 }); diff --git a/trackeroo/lib/logic/models/category.g.dart b/trackeroo/lib/logic/models/category.g.dart index 7fb8b2a..4402829 100644 --- a/trackeroo/lib/logic/models/category.g.dart +++ b/trackeroo/lib/logic/models/category.g.dart @@ -17,19 +17,20 @@ class CategoryAdapter extends TypeAdapter<Category> { for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(), }; return Category( - id: fields[0] as int, + id: fields[0] as String, title: fields[1] as String, iconCodePoint: fields[2] as int, iconFontFamily: fields[3] as String, colorValue: fields[4] as int, - budget: fields[5] as double, + spendings: fields[5] as double, + budget: fields[6] as double, ); } @override void write(BinaryWriter writer, Category obj) { writer - ..writeByte(6) + ..writeByte(7) ..writeByte(0) ..write(obj.id) ..writeByte(1) @@ -41,6 +42,8 @@ class CategoryAdapter extends TypeAdapter<Category> { ..writeByte(4) ..write(obj.colorValue) ..writeByte(5) + ..write(obj.spendings) + ..writeByte(6) ..write(obj.budget); } diff --git a/trackeroo/lib/logic/models/transaction.dart b/trackeroo/lib/logic/models/transaction.dart index c502c2b..55170d5 100644 --- a/trackeroo/lib/logic/models/transaction.dart +++ b/trackeroo/lib/logic/models/transaction.dart @@ -4,21 +4,25 @@ part 'transaction.g.dart'; @HiveType(typeId: 1) class Transaction { @HiveField(0) - String title; + int id; @HiveField(1) - double amount; + String title; @HiveField(2) - int categoryId; + double amount; @HiveField(3) - DateTime createdAt = DateTime.now(); + String categoryId; @HiveField(4) + DateTime createdAt = DateTime.now(); + + @HiveField(5) DateTime dueDate; Transaction({ + this.id = -1, required this.title, required this.amount, required this.categoryId, diff --git a/trackeroo/lib/logic/models/transaction.g.dart b/trackeroo/lib/logic/models/transaction.g.dart index fc6cfbe..227a0e9 100644 --- a/trackeroo/lib/logic/models/transaction.g.dart +++ b/trackeroo/lib/logic/models/transaction.g.dart @@ -17,26 +17,29 @@ class TransactionAdapter extends TypeAdapter<Transaction> { for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(), }; return Transaction( - title: fields[0] as String, - amount: fields[1] as double, - categoryId: fields[2] as int, - dueDate: fields[4] as DateTime, - )..createdAt = fields[3] as DateTime; + id: fields[0] as int, + title: fields[1] as String, + amount: fields[2] as double, + categoryId: fields[3] as String, + dueDate: fields[5] as DateTime, + )..createdAt = fields[4] as DateTime; } @override void write(BinaryWriter writer, Transaction obj) { writer - ..writeByte(5) + ..writeByte(6) ..writeByte(0) - ..write(obj.title) + ..write(obj.id) ..writeByte(1) - ..write(obj.amount) + ..write(obj.title) ..writeByte(2) - ..write(obj.categoryId) + ..write(obj.amount) ..writeByte(3) - ..write(obj.createdAt) + ..write(obj.categoryId) ..writeByte(4) + ..write(obj.createdAt) + ..writeByte(5) ..write(obj.dueDate); } diff --git a/trackeroo/lib/logic/services/categories_controller.dart b/trackeroo/lib/logic/services/categories_controller.dart index 21241ca..6033d41 100644 --- a/trackeroo/lib/logic/services/categories_controller.dart +++ b/trackeroo/lib/logic/services/categories_controller.dart @@ -4,16 +4,21 @@ import 'package:trackeroo/logic/models/category.dart'; class CategoriesController { CategoriesController({required this.catBox, required this.categories}); - Box<dynamic> catBox; - Map<dynamic, dynamic> categories; + Box<Category> catBox; + Map<dynamic, Category> categories; - void saveAllCategories() { + void saveCategory() { // TODO: implement save categories function return; } - List<Category> readAllCategories() { + void updateCategory() { + // TODO: implement update category + return; + } + + void deleteCategory() { // TODO: implement read categories function - return []; + return; } } diff --git a/trackeroo/lib/logic/services/locator.dart b/trackeroo/lib/logic/services/locator.dart index d39aa6d..70b733c 100644 --- a/trackeroo/lib/logic/services/locator.dart +++ b/trackeroo/lib/logic/services/locator.dart @@ -5,8 +5,10 @@ import 'package:get_it/get_it.dart'; import 'package:trackeroo/logic/models/category.dart'; import 'package:trackeroo/logic/models/app_state.dart'; +import 'package:trackeroo/logic/models/transaction.dart'; import 'package:trackeroo/logic/services/app_state_controller.dart'; import 'package:trackeroo/logic/services/categories_controller.dart'; +import 'package:trackeroo/logic/services/transactions_controller.dart'; final locator = GetIt.instance; @@ -21,28 +23,36 @@ Future<void> setupLocatorService() async { locator.registerLazySingleton<AppStateController>(() => appStateController); // load categories from box and safe to list, make list available in get_it - var categoriesBox = await Hive.openBox('categories_box'); + Box<Category> categoriesBox = await Hive.openBox<Category>('categories_box'); if(appState.isFirstOpening) { // create default categories - categoriesBox.addAll([ - Category(id: 0, title: 'Food & Groceries', iconCodePoint: Icons.kitchen.codePoint, colorValue: Colors.green.value), - Category(id: 1, title: 'Transport & Car', iconCodePoint: Icons.directions_bus_rounded.codePoint, colorValue: Colors.amber.value), - Category(id: 2, title: 'Healthcare & Drug Stores', iconCodePoint: Icons.health_and_safety_rounded.codePoint, colorValue: Colors.red.value), - Category(id: 3, title: 'Shopping', iconCodePoint: Icons.shopping_cart_rounded.codePoint, colorValue: Colors.blue.value), - Category(id: 4, title: 'Bars & Restaurants', iconCodePoint: Icons.local_bar_rounded.codePoint, colorValue: Colors.brown.value), - Category(id: 5, title: 'Family & Friends', iconCodePoint: Icons.people_rounded.codePoint, colorValue: Colors.cyan.value), - Category(id: 6, title: 'Leisure & Entertainment', iconCodePoint: Icons.local_activity_rounded.codePoint, colorValue: Colors.purple.value), - Category(id: 7, title: 'Media & Electronics', iconCodePoint: Icons.laptop_rounded.codePoint, colorValue: Colors.grey.value), - Category(id: 8, title: 'Education', iconCodePoint: Icons.book.codePoint, colorValue: Colors.lightGreen.value), - Category(id: 9, title: 'Household & Utilities', iconCodePoint: Icons.chair_rounded.codePoint, colorValue: Colors.orange.value), - Category(id: 10, title: 'Travel & Utilities', iconCodePoint: Icons.flight_rounded.codePoint, colorValue: Colors.teal.value), - Category(id: 11, title: 'ATM', iconCodePoint: Icons.local_atm_rounded.codePoint, colorValue: Colors.deepPurple.value) - ]); + categoriesBox.putAll({ + 'food_and_groceries': Category(id: 'food_and_groceries', title: 'Food & Groceries', iconCodePoint: Icons.kitchen.codePoint, colorValue: Colors.green.value), + 'transport_and_car': Category(id: 'transport_and_car', title: 'Transport & Car', iconCodePoint: Icons.directions_bus_rounded.codePoint, colorValue: Colors.amber.value), + 'healthcare_and_drug_stores': Category(id: 'healthcare_and_drug_stores', title: 'Healthcare & Drug Stores', iconCodePoint: Icons.health_and_safety_rounded.codePoint, colorValue: Colors.red.value), + 'shopping': Category(id: 'shopping', title: 'Shopping', iconCodePoint: Icons.shopping_cart_rounded.codePoint, colorValue: Colors.blue.value), + 'bars_and_restaurants': Category(id: 'bars_and_restaurants', title: 'Bars & Restaurants', iconCodePoint: Icons.local_bar_rounded.codePoint, colorValue: Colors.brown.value), + 'family_and_friends': Category(id: 'family_and_friends', title: 'Family & Friends', iconCodePoint: Icons.people_rounded.codePoint, colorValue: Colors.cyan.value), + 'leisure_and_entertainment': Category(id: 'leisure_and_entertainment', title: 'Leisure & Entertainment', iconCodePoint: Icons.local_activity_rounded.codePoint, colorValue: Colors.purple.value), + 'media_and_electronics': Category(id: 'media_and_electronics', title: 'Media & Electronics', iconCodePoint: Icons.laptop_rounded.codePoint, colorValue: Colors.grey.value), + 'education': Category(id: 'education', title: 'Education', iconCodePoint: Icons.book.codePoint, colorValue: Colors.lightGreen.value), + 'household_and_utilities': Category(id: 'household_and_utilities', title: 'Household & Utilities', iconCodePoint: Icons.chair_rounded.codePoint, colorValue: Colors.orange.value), + 'travel_and_holidays': Category(id: 'travel_and_holidays', title: 'Travel & Holidays', iconCodePoint: Icons.flight_rounded.codePoint, colorValue: Colors.teal.value), + 'atm': Category(id: 'atm', title: 'ATM', iconCodePoint: Icons.local_atm_rounded.codePoint, colorValue: Colors.deepPurple.value) + }); } - Map<dynamic, dynamic> categoriesMap = categoriesBox.toMap(); + Map<dynamic, Category> categoriesMap = categoriesBox.toMap(); CategoriesController categoriesController = CategoriesController( catBox: categoriesBox, categories: categoriesMap ); locator.registerLazySingleton<CategoriesController>(() => categoriesController); + + Box<Transaction> transactionsBox = await Hive.openBox<Transaction>('transactions_box'); + List<Transaction> transactionsList = HiveList(transactionsBox); + TransactionsController transactionsController = TransactionsController( + transactionsBox: transactionsBox, + transactions: transactionsList + ); + locator.registerLazySingleton<TransactionsController>(() => transactionsController); } diff --git a/trackeroo/lib/logic/services/transactions_controller.dart b/trackeroo/lib/logic/services/transactions_controller.dart new file mode 100644 index 0000000..934fbba --- /dev/null +++ b/trackeroo/lib/logic/services/transactions_controller.dart @@ -0,0 +1,29 @@ +import 'package:flutter/material.dart'; +import 'package:hive_flutter/hive_flutter.dart'; +import 'package:trackeroo/logic/models/transaction.dart'; +import 'package:trackeroo/logic/services/categories_controller.dart'; +import 'package:trackeroo/logic/services/locator.dart'; + +class TransactionsController { + TransactionsController({required this.transactionsBox, required this.transactions}); + + Box<Transaction> transactionsBox; + List<Transaction> transactions; + + void saveTransaction(Transaction transaction) { + locator.get<CategoriesController>().categories[transaction.categoryId]?.spendings += transaction.amount; + int id = UniqueKey().hashCode; + transaction.id = id; + transactionsBox.put(transaction.id, transaction); + transactions.add(transaction); + } + + void updateTransaction() { + // TODO: implement update transaction + } + + void deleteTransaction(Transaction transaction) { + transactionsBox.delete(transaction.id); + transactions.remove(transaction); + } +} -- GitLab