diff --git a/trackeroo/lib/frontend/utils/transaction_listtile.dart b/trackeroo/lib/frontend/utils/transaction_listtile.dart index 0dba454dc31b973ea8a6fe1e57fe9706ff0d3d3c..b6828a5b604d6942e16fa61e16105c0bc17a0c13 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 0e21fcd131011b1afdbdcafce4a10394ba5ef812..8361e8829e72216bd8390a3c0938824929c50055 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 d1cd4289f1740168dd67cee4edd484f1a7a3b451..9b1b7028077ea37a90e28280dcb3c2aaa730cca4 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 dea47d6726f651c689d9050e4ec568940ad6d7d2..f68cace88983e4fcfa4c61c1cb14d3bea2495f52 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 7fb8b2a0eb55e0d64ac8aeba3191d59a73b08c0e..4402829d30058147a02296c605aacea17e9c34a0 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 c502c2b4bcef0292c53f275da9ac94b0091b93da..55170d53e207adf6a7f7cecc4b086da30b439910 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 fc6cfbe9f1ad79d5c93d812c5b06d73d7dcef22c..227a0e9a385c7c1dda2be059a0b74f0854933082 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 21241ca2b84111223714a515e22d1a6f0d421c9d..6033d41b903192a10a6052c800685ec3c8a3b1f6 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 d39aa6d41b85e562601183e5fcb7d5e0535b0762..70b733cce7e59a8352dc85f986a33cd21634be7d 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 0000000000000000000000000000000000000000..934fbba73cc9f631fc06268b35ec3563e79b1297 --- /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); + } +}