Skip to content

Commit

Permalink
save commit
Browse files Browse the repository at this point in the history
  • Loading branch information
praslnx8 committed Jan 18, 2024
1 parent c71d50e commit bb11dfa
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 62 deletions.
21 changes: 11 additions & 10 deletions lib/presentation/baskets_presenter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,7 @@ class BasketsPresenter extends Presenter<BasketsViewState> {
.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));
}
Expand All @@ -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<BasketVO> 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());
}
}
11 changes: 9 additions & 2 deletions lib/presentation/create_basket_presenter.dart
Original file line number Diff line number Diff line change
@@ -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<CreateBasketViewState> {
final BasketApi _basketApi;
Expand All @@ -10,6 +10,13 @@ class CreateBasketPresenter extends Presenter<CreateBasketViewState> {
: _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();

Expand Down Expand Up @@ -40,7 +47,7 @@ class CreateBasketPresenter extends Presenter<CreateBasketViewState> {
updateViewState((viewState) => viewState.description = text);
}

void setBasket(BasketVO basketToUpdate) {
void setBasket(Basket basketToUpdate) {
updateViewState((viewState) {
viewState.name = basketToUpdate.name;
viewState.description = basketToUpdate.description;
Expand Down
60 changes: 33 additions & 27 deletions lib/presentation/create_sip_presenter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<CreateSipViewState> {
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}) {
Expand All @@ -26,28 +30,30 @@ class CreateSipPresenter extends Presenter<CreateSipViewState> {
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) {
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/pages/baskets_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
21 changes: 9 additions & 12 deletions lib/ui/widgets/create_basket_dialog.dart
Original file line number Diff line number Diff line change
@@ -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<void> 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();
Expand All @@ -30,11 +29,9 @@ class _CreateBasketPage extends PageState<CreateBasketViewState,
void initState() {
super.initState();

Basket? basketToUpdate = widget.basketToUpdate;
if (basketToUpdate != null) {
_nameController.text = basketToUpdate.name;
_descriptionController.text = basketToUpdate.description ?? '';
presenter.setBasket(basketToUpdate);
int? basketIdToUpdate = widget.basketIdTOUpdate;
if (basketIdToUpdate != null) {
presenter.fetchBasket(id: basketIdToUpdate);
}

_nameController.addListener(() {
Expand Down Expand Up @@ -66,10 +63,10 @@ class _CreateBasketPage extends PageState<CreateBasketViewState,
ElevatedButton(
onPressed: snapshot.isValid()
? () {
presenter.createBasket(basketId: widget.basketToUpdate?.id);
presenter.createBasket(basketId: widget.basketIdTOUpdate);
}
: null,
child: widget.basketToUpdate != null
child: widget.basketIdTOUpdate != null
? const Text('Update')
: const Text('Create'),
),
Expand Down
4 changes: 2 additions & 2 deletions lib/ui/widgets/create_goal_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class _CreateGoalPage extends PageState<CreateGoalViewState, _CreateGoalDialog,
_nameController.text = goalToUpdate.name;
_descriptionController.text = goalToUpdate.description ?? '';
_amountController.text = goalToUpdate.amount.toString();
_currentDateController.text = formatDate(goalToUpdate.createdDate);
_targetDateController.text = formatDate(goalToUpdate.targetDate);
_currentDateController.text = formatDate(goalToUpdate.amountUpdatedOn);
_targetDateController.text = formatDate(goalToUpdate.maturityDate);
_inflationController.text = goalToUpdate.inflation.toString();

presenter.setGoal(goalToUpdate);
Expand Down
6 changes: 3 additions & 3 deletions lib/ui/widgets/create_investment_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ class _CreateInvestmentPage extends PageState<CreateInvestmentViewState,
if (investmentToUpdate != null) {
_nameController.text = investmentToUpdate.name;
_descriptionController.text = investmentToUpdate.description ?? '';
_valueController.text = investmentToUpdate.currentValue.toString();
_valueController.text = investmentToUpdate.value.toString();
_valueUpdatedDateController.text =
investmentToUpdate.currentValueUpdatedOn != null
? formatDate(investmentToUpdate.currentValueUpdatedOn!)
investmentToUpdate.valueUpdatedOn != null
? formatDate(investmentToUpdate.valueUpdatedOn!)
: '';
presenter.setInvestment(investmentToUpdate);
} else {
Expand Down
5 changes: 3 additions & 2 deletions lib/ui/widgets/create_sip_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ class _CreateTransactionPage extends PageState<CreateSipViewState,
_descriptionController.text = sipToUpdate.description ?? '';
_valueController.text = sipToUpdate.amount.toString();
_startDateController.text = formatDate(sipToUpdate.startDate);
_endDateController.text = formatDate(sipToUpdate.endDate);
final endDate = sipToUpdate.endDate;
_endDateController.text = endDate != null ? formatDate(endDate) : '';
presenter.setSip(sipToUpdate);
} else {
_startDateController.text = formatDate(DateTime.now());
Expand Down Expand Up @@ -137,6 +138,6 @@ class _CreateTransactionPage extends PageState<CreateSipViewState,

@override
CreateSipPresenter initializePresenter() {
return CreateSipPresenter();
return CreateSipPresenter(investmentId: widget.investmentId);
}
}
5 changes: 2 additions & 3 deletions lib/ui/widgets/sips_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class _SipsPage extends PageState<SipsViewState, _SipsDialog, SipsPresenter> {
void initState() {
super.initState();

presenter.getSips(investmentId: widget.investmentId);
presenter.getSips();
}

@override
Expand Down Expand Up @@ -68,8 +68,7 @@ class _SipsPage extends PageState<SipsViewState, _SipsDialog, SipsPresenter> {
context: context,
investmentId: widget.investmentId,
sipToUpdate: sip)
.then((value) => presenter.getSips(
investmentId: widget.investmentId));
.then((value) => presenter.getSips());
},
),
IconButton(
Expand Down

0 comments on commit bb11dfa

Please sign in to comment.