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 bb11dfa commit 0d11a91
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 53 deletions.
14 changes: 14 additions & 0 deletions lib/domain/services/sip_service.dart
Original file line number Diff line number Diff line change
@@ -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<SIP> getBy({required final int id}) async {
return _sipApi.getById(id: id).then((sipDO) {
return SIP.from(sipDO: sipDO);
});
}
}
15 changes: 15 additions & 0 deletions lib/domain/services/transaction_service.dart
Original file line number Diff line number Diff line change
@@ -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<Transaction> getBy({required final int id}) async {
return _transactionApi.getById(id: id).then((transactionDO) {
return Transaction.from(transactionDO: transactionDO);
});
}
}
48 changes: 28 additions & 20 deletions lib/presentation/create_investment_transaction_presenter.dart
Original file line number Diff line number Diff line change
@@ -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<CreateTransactionViewState> {
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}) {
Expand All @@ -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) {
Expand Down
14 changes: 11 additions & 3 deletions lib/presentation/create_sip_presenter.dart
Original file line number Diff line number Diff line change
@@ -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<CreateSipViewState> {
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}) {
Expand Down Expand Up @@ -77,7 +81,7 @@ class CreateSipPresenter extends Presenter<CreateSipViewState> {
updateViewState((viewState) => viewState.frequency = frequency);
}

void setSip(SipDO sipToUpdate) {
void setSip(SIP sipToUpdate) {
updateViewState((viewState) {
viewState.description = sipToUpdate.description;
viewState.amount = sipToUpdate.amount;
Expand All @@ -87,6 +91,10 @@ class CreateSipPresenter extends Presenter<CreateSipViewState> {
viewState.frequency = sipToUpdate.frequency;
});
}

void fetchSip({required int id}) {
_sipService.getBy(id: id).then((sip) => setSip(sip));
}
}

class CreateSipViewState {
Expand Down
63 changes: 54 additions & 9 deletions lib/presentation/sips_presenter.dart
Original file line number Diff line number Diff line change
@@ -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<SipsViewState> {
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<SIP> sips = [];
List<SIPVO> 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<SIPVO> 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);
}
}
20 changes: 7 additions & 13 deletions lib/ui/widgets/create_sip_dialog.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -9,20 +8,20 @@ import 'package:wealth_wave/utils/ui_utils.dart';
Future<void> 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();
Expand All @@ -39,14 +38,9 @@ class _CreateTransactionPage extends PageState<CreateSipViewState,
void initState() {
super.initState();

SipDO? sipToUpdate = widget.sipToUpdate;
if (sipToUpdate != null) {
_descriptionController.text = sipToUpdate.description ?? '';
_valueController.text = sipToUpdate.amount.toString();
_startDateController.text = formatDate(sipToUpdate.startDate);
final endDate = sipToUpdate.endDate;
_endDateController.text = endDate != null ? formatDate(endDate) : '';
presenter.setSip(sipToUpdate);
int? sipIdToUpdate = widget.sipIdToUpdate;
if (sipIdToUpdate != null) {
presenter.fetchSip(id: sipIdToUpdate);
} else {
_startDateController.text = formatDate(DateTime.now());
}
Expand Down
6 changes: 3 additions & 3 deletions lib/ui/widgets/create_transaction_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class _CreateTransactionPage extends PageState<CreateTransactionViewState,
_descriptionController.text = transactionToUpdate.description ?? '';
_valueController.text = transactionToUpdate.amount.toString();
_valueUpdatedDateController.text =
formatDate(transactionToUpdate.amountInvestedOn);
formatDate(transactionToUpdate.createdOn);
presenter.setTransaction(transactionToUpdate);
} else {
_valueUpdatedDateController.text = formatDate(DateTime.now());
Expand Down Expand Up @@ -85,7 +85,6 @@ class _CreateTransactionPage extends PageState<CreateTransactionViewState,
onPressed: snapshot.isValid()
? () {
presenter.createTransaction(
investmentId: widget.investmentId,
transactionIdToUpdate: widget.transactionToUpdate?.id);
}
: null,
Expand Down Expand Up @@ -126,6 +125,7 @@ class _CreateTransactionPage extends PageState<CreateTransactionViewState,

@override
CreateInvestmentTransactionPresenter initializePresenter() {
return CreateInvestmentTransactionPresenter();
return CreateInvestmentTransactionPresenter(
investmentId: widget.investmentId);
}
}
10 changes: 5 additions & 5 deletions lib/ui/widgets/sips_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,23 @@ class _SipsPage extends PageState<SipsViewState, _SipsDialog, SipsPresenter> {
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: [
Expand Down

0 comments on commit 0d11a91

Please sign in to comment.