From bb11dfae1b069169e2f5d483e3af208ffabd32af Mon Sep 17 00:00:00 2001 From: Prasanna Anbazhagan Date: Thu, 18 Jan 2024 09:35:28 +0530 Subject: [PATCH] save commit --- lib/presentation/baskets_presenter.dart | 21 +++---- lib/presentation/create_basket_presenter.dart | 11 +++- lib/presentation/create_sip_presenter.dart | 60 ++++++++++--------- lib/ui/pages/baskets_page.dart | 2 +- lib/ui/widgets/create_basket_dialog.dart | 21 +++---- lib/ui/widgets/create_goal_dialog.dart | 4 +- lib/ui/widgets/create_investment_dialog.dart | 6 +- lib/ui/widgets/create_sip_dialog.dart | 5 +- lib/ui/widgets/sips_dialog.dart | 5 +- 9 files changed, 73 insertions(+), 62 deletions(-) diff --git a/lib/presentation/baskets_presenter.dart b/lib/presentation/baskets_presenter.dart index 5ba474f..4dc16dd 100644 --- a/lib/presentation/baskets_presenter.dart +++ b/lib/presentation/baskets_presenter.dart @@ -15,13 +15,7 @@ class BasketsPresenter extends Presenter { .then((baskets) => Future.wait(baskets.map((basket) => Future.wait([ basket.getTotalInvestments(), basket.getInvestedValue(), - ]).then((results) => BasketVO( - id: basket.id, - name: basket.name, - description: basket.description, - totalInvestedAmount: results[1].toDouble(), - totalInvesments: results[0].toInt(), - basket: basket))))) + ]).then((results) => BasketVO.from(basket: basket))))) .then((basketVOs) => updateViewState((viewState) => viewState.baskets = basketVOs)); } @@ -41,13 +35,20 @@ class BasketVO { final String? description; final double totalInvestedAmount; final int totalInvesments; - final Basket basket; BasketVO( {required this.totalInvestedAmount, required this.totalInvesments, required this.id, required this.name, - required this.description, - required this.basket}); + required this.description}); + + static Future from({required final Basket basket}) async { + return BasketVO( + id: basket.id, + name: basket.name, + description: basket.description, + totalInvestedAmount: await basket.getInvestedValue(), + totalInvesments: await basket.getTotalInvestments()); + } } diff --git a/lib/presentation/create_basket_presenter.dart b/lib/presentation/create_basket_presenter.dart index 3ca7da8..c97ea0a 100644 --- a/lib/presentation/create_basket_presenter.dart +++ b/lib/presentation/create_basket_presenter.dart @@ -1,7 +1,7 @@ import 'package:wealth_wave/api/apis/basket_api.dart'; import 'package:wealth_wave/core/presenter.dart'; import 'package:wealth_wave/core/single_event.dart'; -import 'package:wealth_wave/presentation/baskets_presenter.dart'; +import 'package:wealth_wave/domain/models/basket.dart'; class CreateBasketPresenter extends Presenter { final BasketApi _basketApi; @@ -10,6 +10,13 @@ class CreateBasketPresenter extends Presenter { : _basketApi = basketApi ?? BasketApi(), super(CreateBasketViewState()); + void fetchBasket({required final int id}) { + _basketApi + .getBy(id: id) + .then((basketDO) => Basket.from(basketDO: basketDO)) + .then((basket) => setBasket(basket)); + } + void createBasket({final int? basketId}) { var viewState = getViewState(); @@ -40,7 +47,7 @@ class CreateBasketPresenter extends Presenter { updateViewState((viewState) => viewState.description = text); } - void setBasket(BasketVO basketToUpdate) { + void setBasket(Basket basketToUpdate) { updateViewState((viewState) { viewState.name = basketToUpdate.name; viewState.description = basketToUpdate.description; diff --git a/lib/presentation/create_sip_presenter.dart b/lib/presentation/create_sip_presenter.dart index e2b61c9..63d97d5 100644 --- a/lib/presentation/create_sip_presenter.dart +++ b/lib/presentation/create_sip_presenter.dart @@ -2,15 +2,19 @@ import 'package:wealth_wave/api/db/app_database.dart'; import 'package:wealth_wave/contract/sip_frequency.dart'; import 'package:wealth_wave/core/presenter.dart'; import 'package:wealth_wave/core/single_event.dart'; -import 'package:wealth_wave/domain/models/investment.dart'; +import 'package:wealth_wave/domain/services/investment_service.dart'; import 'package:wealth_wave/utils/ui_utils.dart'; import 'package:wealth_wave/utils/utils.dart'; class CreateSipPresenter extends Presenter { - final Investment _investment; - - CreateSipPresenter({required Investment investment}) - : _investment = investment, + final int _investmentId; + final InvestmentService _investmentService; + + CreateSipPresenter( + {required final int investmentId, + final InvestmentService? investmentService}) + : _investmentId = investmentId, + _investmentService = investmentService ?? InvestmentService(), super(CreateSipViewState()); void createSip({required final int investmentId, final int? sipIdToUpdate}) { @@ -26,28 +30,30 @@ class CreateSipPresenter extends Presenter { final endDate = viewState.getEndDate(); final frequency = viewState.frequency; - if (sipIdToUpdate != null) { - _investment - .updateSip( - sipId: sipIdToUpdate, - description: description, - amount: amount, - startDate: startDate, - endDate: endDate, - frequency: frequency) - .then((_) => updateViewState( - (viewState) => viewState.onSipCreated = SingleEvent(null))); - } else { - _investment - .createSip( - description: description, - amount: amount, - startDate: startDate, - endDate: endDate, - frequency: frequency) - .then((_) => updateViewState( - (viewState) => viewState.onSipCreated = SingleEvent(null))); - } + _investmentService.getBy(id: _investmentId).then((investment) { + if (sipIdToUpdate != null) { + investment + .updateSip( + sipId: sipIdToUpdate, + description: description, + amount: amount, + startDate: startDate, + endDate: endDate, + frequency: frequency) + .then((_) => updateViewState( + (viewState) => viewState.onSipCreated = SingleEvent(null))); + } else { + investment + .createSip( + description: description, + amount: amount, + startDate: startDate, + endDate: endDate, + frequency: frequency) + .then((_) => updateViewState( + (viewState) => viewState.onSipCreated = SingleEvent(null))); + } + }); } void onDescriptionChanged(String text) { diff --git a/lib/ui/pages/baskets_page.dart b/lib/ui/pages/baskets_page.dart index 68cbd34..4afec0a 100644 --- a/lib/ui/pages/baskets_page.dart +++ b/lib/ui/pages/baskets_page.dart @@ -70,7 +70,7 @@ class _BasketsPage onSelected: (value) { if (value == 1) { showCreateBasketDialog( - basketToUpdate: basketVO.basket, + basketIdTOUpdate: basketVO.id, context: context) .then((value) => presenter.fetchBaskets()); } else if (value == 2) { diff --git a/lib/ui/widgets/create_basket_dialog.dart b/lib/ui/widgets/create_basket_dialog.dart index 1bf442b..3d44c1c 100644 --- a/lib/ui/widgets/create_basket_dialog.dart +++ b/lib/ui/widgets/create_basket_dialog.dart @@ -1,21 +1,20 @@ import 'package:flutter/material.dart'; import 'package:wealth_wave/core/page_state.dart'; -import 'package:wealth_wave/domain/models/basket.dart'; import 'package:wealth_wave/presentation/create_basket_presenter.dart'; import 'package:wealth_wave/ui/app_dimen.dart'; Future showCreateBasketDialog( - {required final BuildContext context, final Basket? basketToUpdate}) { + {required final BuildContext context, final int? basketIdTOUpdate}) { return showDialog( context: context, builder: (context) => - _CreateBasketDialog(basketToUpdate: basketToUpdate)); + _CreateBasketDialog(basketIdTOUpdate: basketIdTOUpdate)); } class _CreateBasketDialog extends StatefulWidget { - final Basket? basketToUpdate; + final int? basketIdTOUpdate; - const _CreateBasketDialog({this.basketToUpdate}); + const _CreateBasketDialog({this.basketIdTOUpdate}); @override State<_CreateBasketDialog> createState() => _CreateBasketPage(); @@ -30,11 +29,9 @@ class _CreateBasketPage extends PageState { void initState() { super.initState(); - presenter.getSips(investmentId: widget.investmentId); + presenter.getSips(); } @override @@ -68,8 +68,7 @@ class _SipsPage extends PageState { context: context, investmentId: widget.investmentId, sipToUpdate: sip) - .then((value) => presenter.getSips( - investmentId: widget.investmentId)); + .then((value) => presenter.getSips()); }, ), IconButton(