Skip to content
Snippets Groups Projects
Commit 0eff6bd3 authored by Florian Schindler's avatar Florian Schindler
Browse files

changed category id map keys and added transactions to locator

parent 1dcc7686
No related branches found
No related tags found
No related merge requests found
...@@ -19,7 +19,8 @@ class _TransactionListtileState extends State<TransactionListtile> { ...@@ -19,7 +19,8 @@ class _TransactionListtileState extends State<TransactionListtile> {
@override @override
void initState() { 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(); super.initState();
} }
...@@ -66,7 +67,7 @@ class _TransactionListtileState extends State<TransactionListtile> { ...@@ -66,7 +67,7 @@ class _TransactionListtileState extends State<TransactionListtile> {
), ),
), ),
Text( 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> { ...@@ -126,7 +127,7 @@ class _TransactionListtileState extends State<TransactionListtile> {
), ),
), ),
const SizedBox(height: 20.0), const SizedBox(height: 20.0),
Container( category.id != 'no_category' ? Container(
padding: const EdgeInsets.symmetric(horizontal: 15.0, vertical: 12.0), padding: const EdgeInsets.symmetric(horizontal: 15.0, vertical: 12.0),
decoration: BoxDecoration( decoration: BoxDecoration(
color: Color(category.colorValue), color: Color(category.colorValue),
...@@ -139,7 +140,7 @@ class _TransactionListtileState extends State<TransactionListtile> { ...@@ -139,7 +140,7 @@ class _TransactionListtileState extends State<TransactionListtile> {
Text(category.title) Text(category.title)
], ],
), ),
), ) : const SizedBox(),
const SizedBox(height: 20.0), const SizedBox(height: 20.0),
const Expanded(child: SizedBox()), // to be removed when more content is added const Expanded(child: SizedBox()), // to be removed when more content is added
Row( Row(
......
...@@ -17,8 +17,8 @@ class CategoryView extends StatelessWidget { ...@@ -17,8 +17,8 @@ class CategoryView extends StatelessWidget {
ListView.builder( ListView.builder(
physics: const NeverScrollableScrollPhysics(), physics: const NeverScrollableScrollPhysics(),
shrinkWrap: true, shrinkWrap: true,
itemCount: locator.get<CategoriesController>().categories.values.length, itemCount: locator.get<CategoriesController>().categories.length,
itemBuilder: (context, index) => CategoryListtile(category: locator.get<CategoriesController>().categories[index]), itemBuilder: (context, index) => CategoryListtile(category: locator.get<CategoriesController>().categories.values.elementAt(index)),
) )
], ],
), ),
......
...@@ -14,8 +14,8 @@ class HomeView extends StatefulWidget { ...@@ -14,8 +14,8 @@ class HomeView extends StatefulWidget {
} }
class _HomeViewState extends State<HomeView> { class _HomeViewState extends State<HomeView> {
Transaction trn = Transaction(title: "Bus Ticket VVS", amount: -5.32, categoryId: 1, 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: 5, dueDate: DateTime.now()); Transaction trp = Transaction(title: "Gehalt", amount: 1300, categoryId: 'leisure_and_entertainment', dueDate: DateTime.now());
Timespan timespanView = Timespan.monthly; Timespan timespanView = Timespan.monthly;
......
...@@ -5,7 +5,7 @@ part 'category.g.dart'; ...@@ -5,7 +5,7 @@ part 'category.g.dart';
@HiveType(typeId: 0) @HiveType(typeId: 0)
class Category { class Category {
@HiveField(0) @HiveField(0)
int id; String id;
@HiveField(1) @HiveField(1)
String title; String title;
...@@ -20,6 +20,9 @@ class Category { ...@@ -20,6 +20,9 @@ class Category {
int colorValue; int colorValue;
@HiveField(5) @HiveField(5)
double spendings;
@HiveField(6)
double budget; double budget;
Category({ Category({
...@@ -28,6 +31,7 @@ class Category { ...@@ -28,6 +31,7 @@ class Category {
required this.iconCodePoint, required this.iconCodePoint,
this.iconFontFamily = 'MaterialIcons', this.iconFontFamily = 'MaterialIcons',
required this.colorValue, required this.colorValue,
this.spendings = 0,
this.budget = -1 this.budget = -1
}); });
......
...@@ -17,19 +17,20 @@ class CategoryAdapter extends TypeAdapter<Category> { ...@@ -17,19 +17,20 @@ class CategoryAdapter extends TypeAdapter<Category> {
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(), for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
}; };
return Category( return Category(
id: fields[0] as int, id: fields[0] as String,
title: fields[1] as String, title: fields[1] as String,
iconCodePoint: fields[2] as int, iconCodePoint: fields[2] as int,
iconFontFamily: fields[3] as String, iconFontFamily: fields[3] as String,
colorValue: fields[4] as int, colorValue: fields[4] as int,
budget: fields[5] as double, spendings: fields[5] as double,
budget: fields[6] as double,
); );
} }
@override @override
void write(BinaryWriter writer, Category obj) { void write(BinaryWriter writer, Category obj) {
writer writer
..writeByte(6) ..writeByte(7)
..writeByte(0) ..writeByte(0)
..write(obj.id) ..write(obj.id)
..writeByte(1) ..writeByte(1)
...@@ -41,6 +42,8 @@ class CategoryAdapter extends TypeAdapter<Category> { ...@@ -41,6 +42,8 @@ class CategoryAdapter extends TypeAdapter<Category> {
..writeByte(4) ..writeByte(4)
..write(obj.colorValue) ..write(obj.colorValue)
..writeByte(5) ..writeByte(5)
..write(obj.spendings)
..writeByte(6)
..write(obj.budget); ..write(obj.budget);
} }
......
...@@ -4,21 +4,25 @@ part 'transaction.g.dart'; ...@@ -4,21 +4,25 @@ part 'transaction.g.dart';
@HiveType(typeId: 1) @HiveType(typeId: 1)
class Transaction { class Transaction {
@HiveField(0) @HiveField(0)
String title; int id;
@HiveField(1) @HiveField(1)
double amount; String title;
@HiveField(2) @HiveField(2)
int categoryId; double amount;
@HiveField(3) @HiveField(3)
DateTime createdAt = DateTime.now(); String categoryId;
@HiveField(4) @HiveField(4)
DateTime createdAt = DateTime.now();
@HiveField(5)
DateTime dueDate; DateTime dueDate;
Transaction({ Transaction({
this.id = -1,
required this.title, required this.title,
required this.amount, required this.amount,
required this.categoryId, required this.categoryId,
......
...@@ -17,26 +17,29 @@ class TransactionAdapter extends TypeAdapter<Transaction> { ...@@ -17,26 +17,29 @@ class TransactionAdapter extends TypeAdapter<Transaction> {
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(), for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
}; };
return Transaction( return Transaction(
title: fields[0] as String, id: fields[0] as int,
amount: fields[1] as double, title: fields[1] as String,
categoryId: fields[2] as int, amount: fields[2] as double,
dueDate: fields[4] as DateTime, categoryId: fields[3] as String,
)..createdAt = fields[3] as DateTime; dueDate: fields[5] as DateTime,
)..createdAt = fields[4] as DateTime;
} }
@override @override
void write(BinaryWriter writer, Transaction obj) { void write(BinaryWriter writer, Transaction obj) {
writer writer
..writeByte(5) ..writeByte(6)
..writeByte(0) ..writeByte(0)
..write(obj.title) ..write(obj.id)
..writeByte(1) ..writeByte(1)
..write(obj.amount) ..write(obj.title)
..writeByte(2) ..writeByte(2)
..write(obj.categoryId) ..write(obj.amount)
..writeByte(3) ..writeByte(3)
..write(obj.createdAt) ..write(obj.categoryId)
..writeByte(4) ..writeByte(4)
..write(obj.createdAt)
..writeByte(5)
..write(obj.dueDate); ..write(obj.dueDate);
} }
......
...@@ -4,16 +4,21 @@ import 'package:trackeroo/logic/models/category.dart'; ...@@ -4,16 +4,21 @@ import 'package:trackeroo/logic/models/category.dart';
class CategoriesController { class CategoriesController {
CategoriesController({required this.catBox, required this.categories}); CategoriesController({required this.catBox, required this.categories});
Box<dynamic> catBox; Box<Category> catBox;
Map<dynamic, dynamic> categories; Map<dynamic, Category> categories;
void saveAllCategories() { void saveCategory() {
// TODO: implement save categories function // TODO: implement save categories function
return; return;
} }
List<Category> readAllCategories() { void updateCategory() {
// TODO: implement update category
return;
}
void deleteCategory() {
// TODO: implement read categories function // TODO: implement read categories function
return []; return;
} }
} }
...@@ -5,8 +5,10 @@ import 'package:get_it/get_it.dart'; ...@@ -5,8 +5,10 @@ import 'package:get_it/get_it.dart';
import 'package:trackeroo/logic/models/category.dart'; import 'package:trackeroo/logic/models/category.dart';
import 'package:trackeroo/logic/models/app_state.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/app_state_controller.dart';
import 'package:trackeroo/logic/services/categories_controller.dart'; import 'package:trackeroo/logic/services/categories_controller.dart';
import 'package:trackeroo/logic/services/transactions_controller.dart';
final locator = GetIt.instance; final locator = GetIt.instance;
...@@ -21,28 +23,36 @@ Future<void> setupLocatorService() async { ...@@ -21,28 +23,36 @@ Future<void> setupLocatorService() async {
locator.registerLazySingleton<AppStateController>(() => appStateController); locator.registerLazySingleton<AppStateController>(() => appStateController);
// load categories from box and safe to list, make list available in get_it // 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) { if(appState.isFirstOpening) {
// create default categories // create default categories
categoriesBox.addAll([ categoriesBox.putAll({
Category(id: 0, title: 'Food & Groceries', iconCodePoint: Icons.kitchen.codePoint, colorValue: Colors.green.value), 'food_and_groceries': Category(id: 'food_and_groceries', 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), 'transport_and_car': Category(id: 'transport_and_car', 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), '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),
Category(id: 3, title: 'Shopping', iconCodePoint: Icons.shopping_cart_rounded.codePoint, colorValue: Colors.blue.value), 'shopping': Category(id: 'shopping', 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), 'bars_and_restaurants': Category(id: 'bars_and_restaurants', 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), 'family_and_friends': Category(id: 'family_and_friends', 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), 'leisure_and_entertainment': Category(id: 'leisure_and_entertainment', 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), 'media_and_electronics': Category(id: 'media_and_electronics', 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), 'education': Category(id: 'education', 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), 'household_and_utilities': Category(id: 'household_and_utilities', 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), 'travel_and_holidays': Category(id: 'travel_and_holidays', title: 'Travel & Holidays', iconCodePoint: Icons.flight_rounded.codePoint, colorValue: Colors.teal.value),
Category(id: 11, title: 'ATM', iconCodePoint: Icons.local_atm_rounded.codePoint, colorValue: Colors.deepPurple.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( CategoriesController categoriesController = CategoriesController(
catBox: categoriesBox, catBox: categoriesBox,
categories: categoriesMap categories: categoriesMap
); );
locator.registerLazySingleton<CategoriesController>(() => categoriesController); 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);
} }
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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment