From baf0dfa99dc132e076c99a7d933ef50b99984cf7 Mon Sep 17 00:00:00 2001 From: "Alexander \"PapaTutuWawa" Date: Sun, 18 May 2025 15:09:05 +0200 Subject: [PATCH] Format --- lib/database/sqlite.dart | 32 +++++++++------------ lib/ui/pages/account/account.dart | 4 +-- lib/ui/pages/loans/add_loan_change.dart | 1 + lib/ui/pages/loans/loan_details.dart | 9 +++--- lib/ui/state/core.dart | 28 +++++++++--------- lib/ui/state/core.freezed.dart | 38 +++++++++++++++++++++++-- lib/ui/widgets/account_indicator.dart | 2 +- 7 files changed, 72 insertions(+), 42 deletions(-) diff --git a/lib/database/sqlite.dart b/lib/database/sqlite.dart index 6d171ac..425509e 100644 --- a/lib/database/sqlite.dart +++ b/lib/database/sqlite.dart @@ -75,13 +75,8 @@ class LoanChanges extends Table { class LoanDto { final Loan loan; final Beneficiary beneficiary; - final List changes; - LoanDto({ - required this.loan, - required this.beneficiary, - required this.changes, - }); + LoanDto({required this.loan, required this.beneficiary}); } class RecurringTransactions extends Table { @@ -454,12 +449,10 @@ class LoansDao extends DatabaseAccessor with _$LoansDaoMixin { .watch() .map((rows) { return rows.map((row) { - return ( - loan: row.readTable(loans), - beneficiary: row.readTable(beneficiaries), - changes: List.empty(), - ) - as LoanDto; + return LoanDto( + loan: row.readTable(loans), + beneficiary: row.readTable(beneficiaries), + ); }).toList(); }); } @@ -479,12 +472,10 @@ class LoansDao extends DatabaseAccessor with _$LoansDaoMixin { .get() .then((rows) { return rows.map((row) { - return ( - loan: row.readTable(loans), - beneficiary: row.readTable(beneficiaries), - changes: List.empty(), - ) - as LoanDto; + return LoanDto( + loan: row.readTable(loans), + beneficiary: row.readTable(beneficiaries), + ); }).toList(); }); } @@ -508,6 +499,11 @@ class LoansDao extends DatabaseAccessor with _$LoansDaoMixin { ).insertReturning(loanChange, mode: InsertMode.insertOrReplace); } + Stream> watchLoanChanges(Loan loan) { + return (select(loanChanges) + ..where((c) => c.loanId.equals(loan.id))).watch(); + } + Future deleteLoanChange(int id) { return (delete(loanChanges)..where((c) => c.id.equals(id))).go(); } diff --git a/lib/ui/pages/account/account.dart b/lib/ui/pages/account/account.dart index b2cad67..6644932 100644 --- a/lib/ui/pages/account/account.dart +++ b/lib/ui/pages/account/account.dart @@ -44,7 +44,7 @@ class AccountListPageState extends State { width: 150, height: 100, child: Card( - color: colorHash(state.accounts[index].name!), + color: colorHash(state.accounts[index].name), shape: index == state.activeAccountIndex ? RoundedRectangleBorder( @@ -61,7 +61,7 @@ class AccountListPageState extends State { child: Column( mainAxisSize: MainAxisSize.min, children: [ - Text(state.accounts[index].name!), + Text(state.accounts[index].name), FutureBuilder( future: GetIt.I .get() diff --git a/lib/ui/pages/loans/add_loan_change.dart b/lib/ui/pages/loans/add_loan_change.dart index 0242edc..492cd1d 100644 --- a/lib/ui/pages/loans/add_loan_change.dart +++ b/lib/ui/pages/loans/add_loan_change.dart @@ -91,6 +91,7 @@ class AddLoanPopupState extends State { sign * double.parse(_amountController.text).abs(), ), date: Value(DateTime.now()), + loanId: Value(widget.loan.loan.id), ), ); widget.onDone(); diff --git a/lib/ui/pages/loans/loan_details.dart b/lib/ui/pages/loans/loan_details.dart index e352c2c..d5328db 100644 --- a/lib/ui/pages/loans/loan_details.dart +++ b/lib/ui/pages/loans/loan_details.dart @@ -43,10 +43,9 @@ class LoanDetailsPage extends StatelessWidget { return Text("No loan selected"); } - final loanChanges = state.activeLoan!.changes.toList(); final loanSum = - loanChanges.isNotEmpty - ? loanChanges + state.loanChanges.isNotEmpty + ? state.loanChanges .map((c) => c.amount) .reduce((acc, val) => acc + val) : 0.0; @@ -94,9 +93,9 @@ class LoanDetailsPage extends StatelessWidget { SliverToBoxAdapter( child: - loanChanges.isNotEmpty + state.loanChanges.isNotEmpty ? GroupedListView( - elements: loanChanges, + elements: state.loanChanges, shrinkWrap: true, reverse: true, groupBy: diff --git a/lib/ui/state/core.dart b/lib/ui/state/core.dart index 5d5ab47..eff8457 100644 --- a/lib/ui/state/core.dart +++ b/lib/ui/state/core.dart @@ -23,6 +23,7 @@ abstract class CoreState with _$CoreState { @Default([]) List budgetItems, @Default(null) BudgetsDto? activeBudget, @Default([]) List loans, + @Default([]) List loanChanges, @Default(null) LoanDto? activeLoan, @Default(false) bool isDeletingAccount, }) = _CoreState; @@ -40,6 +41,7 @@ class CoreCubit extends Cubit { StreamSubscription? _budgetsStreamSubscription; StreamSubscription? _budgetItemsStreamSubscription; StreamSubscription? _loanStreamSubscription; + StreamSubscription? _loanChangesSubscription; void setupAccountStream() { _accountsStreamSubscription?.cancel(); @@ -95,7 +97,6 @@ class CoreCubit extends Cubit { _budgetsStreamSubscription = db.budgetsDao .budgetsStream(activeAccount!) .listen((budgets) async { - print("BUDGETS: $budgets"); emit(state.copyWith(budgets: budgets)); }); _loanStreamSubscription?.cancel(); @@ -125,17 +126,6 @@ class CoreCubit extends Cubit { state.copyWith( accounts: accounts, activeAccountIndex: accounts.isEmpty ? null : 0, - transactions: await db.transactionsDao.getTransactions(account), - beneficiaries: await db.beneficiariesDao.getBeneficiaries(), - transactionTemplates: await db.transactionTemplatesDao - .getTransactionTemplates(account), - recurringTransactions: await db.recurringTransactionsDao - .getRecurringTransactions(account), - expenseCategories: await db.expenseCategoriesDao.getExpenseCategories( - account, - ), - //budgets: await db.budgetsDao.getBudgets(account), - loans: await db.loansDao.getLoans(account), ), ); @@ -211,8 +201,18 @@ class CoreCubit extends Cubit { await init(); } - void setActiveLoan(LoanDto loan) { - emit(state.copyWith(activeLoan: loan)); + void setActiveLoan(LoanDto? loan) { + emit(state.copyWith(activeLoan: loan, loanChanges: [])); + _loanChangesSubscription?.cancel(); + if (loan != null) { + _loanChangesSubscription = GetIt.I + .get() + .loansDao + .watchLoanChanges(loan.loan) + .listen((changes) { + emit(state.copyWith(loanChanges: changes)); + }); + } } Account? get activeAccount => diff --git a/lib/ui/state/core.freezed.dart b/lib/ui/state/core.freezed.dart index 457d1c9..034ffd8 100644 --- a/lib/ui/state/core.freezed.dart +++ b/lib/ui/state/core.freezed.dart @@ -33,6 +33,7 @@ mixin _$CoreState { List get budgetItems => throw _privateConstructorUsedError; BudgetsDto? get activeBudget => throw _privateConstructorUsedError; List get loans => throw _privateConstructorUsedError; + List get loanChanges => throw _privateConstructorUsedError; LoanDto? get activeLoan => throw _privateConstructorUsedError; bool get isDeletingAccount => throw _privateConstructorUsedError; @@ -60,6 +61,7 @@ abstract class $CoreStateCopyWith<$Res> { List budgetItems, BudgetsDto? activeBudget, List loans, + List loanChanges, LoanDto? activeLoan, bool isDeletingAccount, }); @@ -91,6 +93,7 @@ class _$CoreStateCopyWithImpl<$Res, $Val extends CoreState> Object? budgetItems = null, Object? activeBudget = freezed, Object? loans = null, + Object? loanChanges = null, Object? activeLoan = freezed, Object? isDeletingAccount = null, }) { @@ -161,6 +164,11 @@ class _$CoreStateCopyWithImpl<$Res, $Val extends CoreState> ? _value.loans : loans // ignore: cast_nullable_to_non_nullable as List, + loanChanges: + null == loanChanges + ? _value.loanChanges + : loanChanges // ignore: cast_nullable_to_non_nullable + as List, activeLoan: freezed == activeLoan ? _value.activeLoan @@ -200,6 +208,7 @@ abstract class _$$CoreStateImplCopyWith<$Res> List budgetItems, BudgetsDto? activeBudget, List loans, + List loanChanges, LoanDto? activeLoan, bool isDeletingAccount, }); @@ -230,6 +239,7 @@ class __$$CoreStateImplCopyWithImpl<$Res> Object? budgetItems = null, Object? activeBudget = freezed, Object? loans = null, + Object? loanChanges = null, Object? activeLoan = freezed, Object? isDeletingAccount = null, }) { @@ -300,6 +310,11 @@ class __$$CoreStateImplCopyWithImpl<$Res> ? _value._loans : loans // ignore: cast_nullable_to_non_nullable as List, + loanChanges: + null == loanChanges + ? _value._loanChanges + : loanChanges // ignore: cast_nullable_to_non_nullable + as List, activeLoan: freezed == activeLoan ? _value.activeLoan @@ -332,6 +347,7 @@ class _$CoreStateImpl implements _CoreState { final List budgetItems = const [], this.activeBudget = null, final List loans = const [], + final List loanChanges = const [], this.activeLoan = null, this.isDeletingAccount = false, }) : _accounts = accounts, @@ -342,7 +358,8 @@ class _$CoreStateImpl implements _CoreState { _expenseCategories = expenseCategories, _budgets = budgets, _budgetItems = budgetItems, - _loans = loans; + _loans = loans, + _loanChanges = loanChanges; @override @JsonKey() @@ -439,6 +456,15 @@ class _$CoreStateImpl implements _CoreState { return EqualUnmodifiableListView(_loans); } + final List _loanChanges; + @override + @JsonKey() + List get loanChanges { + if (_loanChanges is EqualUnmodifiableListView) return _loanChanges; + // ignore: implicit_dynamic_type + return EqualUnmodifiableListView(_loanChanges); + } + @override @JsonKey() final LoanDto? activeLoan; @@ -448,7 +474,7 @@ class _$CoreStateImpl implements _CoreState { @override String toString() { - return 'CoreState(activePage: $activePage, activeAccountIndex: $activeAccountIndex, activeTransaction: $activeTransaction, accounts: $accounts, recurringTransactions: $recurringTransactions, transactions: $transactions, transactionTemplates: $transactionTemplates, beneficiaries: $beneficiaries, expenseCategories: $expenseCategories, budgets: $budgets, budgetItems: $budgetItems, activeBudget: $activeBudget, loans: $loans, activeLoan: $activeLoan, isDeletingAccount: $isDeletingAccount)'; + return 'CoreState(activePage: $activePage, activeAccountIndex: $activeAccountIndex, activeTransaction: $activeTransaction, accounts: $accounts, recurringTransactions: $recurringTransactions, transactions: $transactions, transactionTemplates: $transactionTemplates, beneficiaries: $beneficiaries, expenseCategories: $expenseCategories, budgets: $budgets, budgetItems: $budgetItems, activeBudget: $activeBudget, loans: $loans, loanChanges: $loanChanges, activeLoan: $activeLoan, isDeletingAccount: $isDeletingAccount)'; } @override @@ -491,6 +517,10 @@ class _$CoreStateImpl implements _CoreState { (identical(other.activeBudget, activeBudget) || other.activeBudget == activeBudget) && const DeepCollectionEquality().equals(other._loans, _loans) && + const DeepCollectionEquality().equals( + other._loanChanges, + _loanChanges, + ) && (identical(other.activeLoan, activeLoan) || other.activeLoan == activeLoan) && (identical(other.isDeletingAccount, isDeletingAccount) || @@ -513,6 +543,7 @@ class _$CoreStateImpl implements _CoreState { const DeepCollectionEquality().hash(_budgetItems), activeBudget, const DeepCollectionEquality().hash(_loans), + const DeepCollectionEquality().hash(_loanChanges), activeLoan, isDeletingAccount, ); @@ -539,6 +570,7 @@ abstract class _CoreState implements CoreState { final List budgetItems, final BudgetsDto? activeBudget, final List loans, + final List loanChanges, final LoanDto? activeLoan, final bool isDeletingAccount, }) = _$CoreStateImpl; @@ -570,6 +602,8 @@ abstract class _CoreState implements CoreState { @override List get loans; @override + List get loanChanges; + @override LoanDto? get activeLoan; @override bool get isDeletingAccount; diff --git a/lib/ui/widgets/account_indicator.dart b/lib/ui/widgets/account_indicator.dart index 6a2ba1a..dc5128f 100644 --- a/lib/ui/widgets/account_indicator.dart +++ b/lib/ui/widgets/account_indicator.dart @@ -25,7 +25,7 @@ class AccountSwitcher extends StatelessWidget { itemBuilder: (context, index) { final item = state.accounts[index]; return ListTile( - title: Text(item.name!), + title: Text(item.name), trailing: IconButton( icon: Icon(Icons.delete), color: Colors.red,