diff --git a/lib/domain/services/sip_service.dart b/lib/domain/services/sip_service.dart new file mode 100644 index 0000000..369d569 --- /dev/null +++ b/lib/domain/services/sip_service.dart @@ -0,0 +1,14 @@ +import 'package:wealth_wave/api/apis/sip_api.dart'; +import 'package:wealth_wave/domain/models/sip.dart'; + +class SipService { + final SipApi _sipApi; + + SipService({final SipApi? sipApi}) : _sipApi = sipApi ?? SipApi(); + + Future getBy({required final int id}) async { + return _sipApi.getById(id: id).then((sipDO) { + return SIP.from(sipDO: sipDO); + }); + } +} diff --git a/lib/domain/services/transaction_service.dart b/lib/domain/services/transaction_service.dart new file mode 100644 index 0000000..365c9f2 --- /dev/null +++ b/lib/domain/services/transaction_service.dart @@ -0,0 +1,15 @@ +import 'package:wealth_wave/api/apis/transaction_api.dart'; +import 'package:wealth_wave/domain/models/transaction.dart'; + +class TransactionService { + final TransactionApi _transactionApi; + + TransactionService({final TransactionApi? transactionApi}) + : _transactionApi = transactionApi ?? TransactionApi(); + + Future getBy({required final int id}) async { + return _transactionApi.getById(id: id).then((transactionDO) { + return Transaction.from(transactionDO: transactionDO); + }); + } +} diff --git a/lib/presentation/create_investment_transaction_presenter.dart b/lib/presentation/create_investment_transaction_presenter.dart index 9183418..8d1a731 100644 --- a/lib/presentation/create_investment_transaction_presenter.dart +++ b/lib/presentation/create_investment_transaction_presenter.dart @@ -1,16 +1,20 @@ import 'package:wealth_wave/api/db/app_database.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 CreateInvestmentTransactionPresenter extends Presenter { - final Investment _investment; + final int _investmentId; + final InvestmentService _investmentService; - CreateInvestmentTransactionPresenter({required final Investment investment}) - : _investment = investment, + CreateInvestmentTransactionPresenter( + {required final int investmentId, + final InvestmentService? investmentService}) + : _investmentId = investmentId, + _investmentService = investmentService ?? InvestmentService(), super(CreateTransactionViewState()); void createTransaction({final int? transactionIdToUpdate}) { @@ -24,22 +28,26 @@ class CreateInvestmentTransactionPresenter final amount = viewState.amount; final investedDate = viewState.getInvestedDate(); - if (transactionIdToUpdate != null) { - _investment - .updateTransaction( - transactionId: transactionIdToUpdate, - description: description, - amount: amount, - createdOn: investedDate) - .then((_) => updateViewState((viewState) => - viewState.onTransactionCreated = SingleEvent(null))); - } else { - _investment - .createTransaction( - description: description, amount: amount, createdOn: investedDate) - .then((_) => updateViewState((viewState) => - viewState.onTransactionCreated = SingleEvent(null))); - } + _investmentService.getBy(id: _investmentId).then((investment) { + if (transactionIdToUpdate != null) { + investment + .updateTransaction( + transactionId: transactionIdToUpdate, + description: description, + amount: amount, + createdOn: investedDate) + .then((_) => updateViewState((viewState) => + viewState.onTransactionCreated = SingleEvent(null))); + } else { + investment + .createTransaction( + description: description, + amount: amount, + createdOn: investedDate) + .then((_) => updateViewState((viewState) => + viewState.onTransactionCreated = SingleEvent(null))); + } + }); } void onDescriptionChanged(String text) { diff --git a/lib/presentation/create_sip_presenter.dart b/lib/presentation/create_sip_presenter.dart index 63d97d5..8ca6e3d 100644 --- a/lib/presentation/create_sip_presenter.dart +++ b/lib/presentation/create_sip_presenter.dart @@ -1,20 +1,24 @@ -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/sip.dart'; import 'package:wealth_wave/domain/services/investment_service.dart'; +import 'package:wealth_wave/domain/services/sip_service.dart'; import 'package:wealth_wave/utils/ui_utils.dart'; import 'package:wealth_wave/utils/utils.dart'; class CreateSipPresenter extends Presenter { final int _investmentId; final InvestmentService _investmentService; + final SipService _sipService; CreateSipPresenter( {required final int investmentId, - final InvestmentService? investmentService}) + final InvestmentService? investmentService, + final SipService? sipService}) : _investmentId = investmentId, _investmentService = investmentService ?? InvestmentService(), + _sipService = sipService ?? SipService(), super(CreateSipViewState()); void createSip({required final int investmentId, final int? sipIdToUpdate}) { @@ -77,7 +81,7 @@ class CreateSipPresenter extends Presenter { updateViewState((viewState) => viewState.frequency = frequency); } - void setSip(SipDO sipToUpdate) { + void setSip(SIP sipToUpdate) { updateViewState((viewState) { viewState.description = sipToUpdate.description; viewState.amount = sipToUpdate.amount; @@ -87,6 +91,10 @@ class CreateSipPresenter extends Presenter { viewState.frequency = sipToUpdate.frequency; }); } + + void fetchSip({required int id}) { + _sipService.getBy(id: id).then((sip) => setSip(sip)); + } } class CreateSipViewState { diff --git a/lib/presentation/sips_presenter.dart b/lib/presentation/sips_presenter.dart index 55c6b27..56c5be6 100644 --- a/lib/presentation/sips_presenter.dart +++ b/lib/presentation/sips_presenter.dart @@ -1,25 +1,70 @@ +import 'package:wealth_wave/contract/sip_frequency.dart'; import 'package:wealth_wave/core/presenter.dart'; -import 'package:wealth_wave/domain/models/investment.dart'; import 'package:wealth_wave/domain/models/sip.dart'; +import 'package:wealth_wave/domain/services/investment_service.dart'; class SipsPresenter extends Presenter { - final Investment _investment; + final int _investmentId; + final InvestmentService _investmentService; - SipsPresenter({required final Investment investment}) - : _investment = investment, + SipsPresenter( + {required final int investmentId, + final InvestmentService? investmentService}) + : _investmentId = investmentId, + _investmentService = investmentService ?? InvestmentService(), super(SipsViewState()); void getSips() { - _investment.getSips().then((sips) => updateViewState((viewState) { - viewState.sips = sips; - })); + _investmentService + .getBy(id: _investmentId) + .then((investment) => investment.getSips()) + .then((sips) => Future.wait(sips.map((sip) => SIPVO.from(sip: sip)))) + .then((sipVOs) => updateViewState((viewState) { + viewState.sipVOs = sipVOs; + })); } void deleteSip({required final int id}) { - _investment.deleteSIP(sipId: id).then((_) => getSips()); + _investmentService + .getBy(id: _investmentId) + .then((investment) => investment.deleteSIP(sipId: id)) + .then((_) => getSips()); } } class SipsViewState { - List sips = []; + List sipVOs = []; +} + +class SIPVO { + final int id; + final int investmentId; + final String? description; + final double amount; + final DateTime startDate; + final DateTime? endDate; + final SipFrequency frequency; + final DateTime? executedTill; + + SIPVO( + {required this.id, + required this.investmentId, + required this.description, + required this.amount, + required this.startDate, + required this.endDate, + required this.frequency, + required this.executedTill}); + + static Future from({required final SIP sip}) async { + return SIPVO( + id: sip.id, + investmentId: sip.investmentId, + description: sip.description, + amount: sip.amount, + startDate: sip.startDate, + endDate: sip.endDate, + frequency: sip.frequency, + executedTill: sip.executedTill); + } } diff --git a/lib/ui/widgets/create_sip_dialog.dart b/lib/ui/widgets/create_sip_dialog.dart index 524c5d9..bc7c687 100644 --- a/lib/ui/widgets/create_sip_dialog.dart +++ b/lib/ui/widgets/create_sip_dialog.dart @@ -1,6 +1,5 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; -import 'package:wealth_wave/api/db/app_database.dart'; import 'package:wealth_wave/core/page_state.dart'; import 'package:wealth_wave/presentation/create_sip_presenter.dart'; import 'package:wealth_wave/ui/app_dimen.dart'; @@ -9,20 +8,20 @@ import 'package:wealth_wave/utils/ui_utils.dart'; Future showCreateSipDialog( {required final BuildContext context, required final int investmentId, - final SipDO? sipToUpdate}) { + final int? sipIdToUpdate}) { return showDialog( context: context, builder: (context) => _CreateSipDialog( - sipToUpdate: sipToUpdate, + sipIdToUpdate: sipIdToUpdate, investmentId: investmentId, )); } class _CreateSipDialog extends StatefulWidget { - final SipDO? sipToUpdate; + final int? sipIdToUpdate; final int investmentId; - const _CreateSipDialog({this.sipToUpdate, required this.investmentId}); + const _CreateSipDialog({this.sipIdToUpdate, required this.investmentId}); @override State<_CreateSipDialog> createState() => _CreateTransactionPage(); @@ -39,14 +38,9 @@ class _CreateTransactionPage extends PageState { width: double.maxFinite, child: ListView.builder( shrinkWrap: true, - itemCount: snapshot.sips.length, + itemCount: snapshot.sipVOs.length, itemBuilder: (context, index) { - SipDO sip = snapshot.sips[index]; + SIPVO sipVO = snapshot.sipVOs[index]; return ListTile( title: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ - Text(formatDate(sip.startDate)), + Text(formatDate(sipVO.startDate)), const Text(' | '), - Text(sip.description ?? ''), + Text(sipVO.description ?? ''), ], ), const SizedBox( height: AppDimen.minPadding), // Add some spacing - Text('Amount: ${formatToCurrency(sip.amount)}'), + Text('Amount: ${formatToCurrency(sipVO.amount)}'), ], ), trailing: Row(mainAxisSize: MainAxisSize.min, children: [