Skip to content

Commit

Permalink
save commit
Browse files Browse the repository at this point in the history
  • Loading branch information
praslnx8 committed Jan 17, 2024
1 parent d759df8 commit afb0613
Show file tree
Hide file tree
Showing 18 changed files with 241 additions and 255 deletions.
16 changes: 14 additions & 2 deletions lib/domain/models/goal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:wealth_wave/api/apis/investment_api.dart';
import 'package:wealth_wave/api/db/app_database.dart';
import 'package:wealth_wave/contract/goal_importance.dart';
import 'package:wealth_wave/domain/models/investment.dart';
import 'package:wealth_wave/domain/models/irr_calculator.dart';

class Goal {
final int id;
Expand All @@ -18,6 +19,7 @@ class Goal {

final GoalInvestmentApi _goalInvestmentApi;
final InvestmentApi _investmentApi;
final IRRCalculator _irrCalculator;

Goal(
{required this.id,
Expand All @@ -29,9 +31,11 @@ class Goal {
required this.amountUpdatedOn,
required this.importance,
final GoalInvestmentApi? goalInvestmentApi,
final InvestmentApi? investmentApi})
final InvestmentApi? investmentApi,
final IRRCalculator? irrCalculator})
: _goalInvestmentApi = goalInvestmentApi ?? GoalInvestmentApi(),
_investmentApi = investmentApi ?? InvestmentApi();
_investmentApi = investmentApi ?? InvestmentApi(),
_irrCalculator = irrCalculator ?? IRRCalculator();

Future<Map<Investment, double>> getInvestments() async {
return _goalInvestmentApi
Expand Down Expand Up @@ -59,6 +63,14 @@ class Goal {
.then((amounts) => amounts.reduce((value, element) => value + element));
}

Future<double> getMaturityAmount() {
return Future(() => _irrCalculator.calculateValueOnIRR(
irr: inflation,
date: maturityDate,
value: amount,
valueUpdatedOn: amountUpdatedOn));
}

Future<double> getValueOnMaturity() async {
return _goalInvestmentApi
.getBy(goalId: id)
Expand Down
17 changes: 9 additions & 8 deletions lib/domain/models/investment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class Investment {
return null;
}

Future<Map<Goal, double>> getInvestments() async {
Future<Map<Goal, double>> getGoals() async {
return _goalInvestmentApi
.getBy(goalId: id)
.then((goalInvestments) => Future.wait(goalInvestments.map(
Expand All @@ -131,7 +131,7 @@ class Investment {
}

Future<SIP> createSip(
{required final String description,
{required final String? description,
required final double amount,
required final DateTime startDate,
required final DateTime? endDate,
Expand All @@ -150,7 +150,7 @@ class Investment {

Future<SIP> updateSip(
{required final int sipId,
required final String description,
required final String? description,
required final double amount,
required final DateTime startDate,
required final DateTime? endDate,
Expand All @@ -173,7 +173,7 @@ class Investment {
}

Future<Transaction> createTransaction(
{required final String description,
{required final String? description,
required final double amount,
required final DateTime createdOn}) async {
return _transactionApi
Expand All @@ -187,9 +187,9 @@ class Investment {
(transactionDO) => Transaction.from(transactionDO: transactionDO));
}

Future<Transaction> updateTransactions(
Future<Transaction> updateTransaction(
{required final int transactionId,
required final String description,
required final String? description,
required final double amount,
required final DateTime createdOn}) async {
return _transactionApi
Expand All @@ -204,8 +204,9 @@ class Investment {
(transactionDO) => Transaction.from(transactionDO: transactionDO));
}

Future<void> deleteTransaction({required final int transactionId}) async {
return _transactionApi.deleteBy(id: transactionId).then((count) => Void);
Future<void> deleteTransaction(
{required final Transaction transaction}) async {
return _transactionApi.deleteBy(id: transaction.id).then((count) => Void);
}

Future<MapEntry<Goal, double>> tagGoal(
Expand Down
36 changes: 32 additions & 4 deletions lib/presentation/baskets_presenter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,20 @@ class BasketsPresenter extends Presenter<BasketsViewState> {
super(BasketsViewState());

void fetchBaskets() {
_basketService.get().then((baskets) => updateViewState((viewState) {
viewState.baskets = baskets;
}));
_basketService
.get()
.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((basketVOs) =>
updateViewState((viewState) => viewState.baskets = basketVOs));
}

void deleteBasket({required final int id}) {
Expand All @@ -21,5 +32,22 @@ class BasketsPresenter extends Presenter<BasketsViewState> {
}

class BasketsViewState {
List<Basket> baskets = [];
List<BasketVO> baskets = [];
}

class BasketVO {
final int id;
final String name;
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});
}
4 changes: 2 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/domain/models/basket.dart';
import 'package:wealth_wave/presentation/baskets_presenter.dart';

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

void setBasket(Basket basketToUpdate) {
void setBasket(BasketVO basketToUpdate) {
updateViewState((viewState) {
viewState.name = basketToUpdate.name;
viewState.description = basketToUpdate.description;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import 'package:wealth_wave/api/apis/basket_api.dart';
import 'package:wealth_wave/api/apis/investment_api.dart';
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/utils/ui_utils.dart';
import 'package:wealth_wave/utils/utils.dart';

class CreateTransactionPresenter extends Presenter<CreateTransactionViewState> {
final InvestmentApi _investmentApi;
class CreateInvestmentTransactionPresenter
extends Presenter<CreateTransactionViewState> {
final Investment _investment;

CreateTransactionPresenter(
{final InvestmentApi? investmentApi, final BasketApi? basketApi})
: _investmentApi = investmentApi ?? InvestmentApi(),
CreateInvestmentTransactionPresenter({required final Investment investment})
: _investment = investment,
super(CreateTransactionViewState());

void createTransaction(
{required final int investmentId, final int? transactionIdToUpdate}) {
void createTransaction({final int? transactionIdToUpdate}) {
var viewState = getViewState();

if (!viewState.isValid()) {
Expand All @@ -27,22 +25,18 @@ class CreateTransactionPresenter extends Presenter<CreateTransactionViewState> {
final investedDate = viewState.getInvestedDate();

if (transactionIdToUpdate != null) {
_investmentApi
_investment
.updateTransaction(
id: transactionIdToUpdate,
transactionId: transactionIdToUpdate,
description: description,
investmentId: investmentId,
amount: amount,
date: investedDate)
createdOn: investedDate)
.then((_) => updateViewState((viewState) =>
viewState.onTransactionCreated = SingleEvent(null)));
} else {
_investmentApi
_investment
.createTransaction(
investmentId: investmentId,
description: description,
amount: amount,
date: investedDate)
description: description, amount: amount, createdOn: investedDate)
.then((_) => updateViewState((viewState) =>
viewState.onTransactionCreated = SingleEvent(null)));
}
Expand All @@ -64,7 +58,7 @@ class CreateTransactionPresenter extends Presenter<CreateTransactionViewState> {
void setTransaction(TransactionDO transactionToUpdate) {
updateViewState((viewState) {
viewState.amount = transactionToUpdate.amount;
viewState.investedDate = formatDate(transactionToUpdate.amountInvestedOn);
viewState.investedDate = formatDate(transactionToUpdate.createdOn);
});
}
}
Expand Down
29 changes: 13 additions & 16 deletions lib/presentation/create_sip_presenter.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import 'package:wealth_wave/api/apis/basket_api.dart';
import 'package:wealth_wave/api/apis/investment_api.dart';
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/utils/ui_utils.dart';
import 'package:wealth_wave/utils/utils.dart';

class CreateSipPresenter extends Presenter<CreateSipViewState> {
final InvestmentApi _investmentApi;
final Investment _investment;

CreateSipPresenter(
{final InvestmentApi? investmentApi, final BasketApi? basketApi})
: _investmentApi = investmentApi ?? InvestmentApi(),
CreateSipPresenter({required Investment investment})
: _investment = investment,
super(CreateSipViewState());

void createSip({required final int investmentId, final int? sipIdToUpdate}) {
Expand All @@ -28,21 +27,19 @@ class CreateSipPresenter extends Presenter<CreateSipViewState> {
final frequency = viewState.frequency;

if (sipIdToUpdate != null) {
_investmentApi
_investment
.updateSip(
id: sipIdToUpdate,
sipId: sipIdToUpdate,
description: description,
investmentId: investmentId,
amount: amount,
startDate: startDate,
endDate: endDate,
frequency: frequency)
.then((_) => updateViewState(
(viewState) => viewState.onSipCreated = SingleEvent(null)));
} else {
_investmentApi
_investment
.createSip(
investmentId: investmentId,
description: description,
amount: amount,
startDate: startDate,
Expand Down Expand Up @@ -70,17 +67,17 @@ class CreateSipPresenter extends Presenter<CreateSipViewState> {
updateViewState((viewState) => viewState.endDate = date);
}

void onFrequencyChanged(String text) {
updateViewState(
(viewState) => viewState.frequency = double.tryParse(text) ?? 0);
void onFrequencyChanged(SipFrequency frequency) {
updateViewState((viewState) => viewState.frequency = frequency);
}

void setSip(SipDO sipToUpdate) {
updateViewState((viewState) {
viewState.description = sipToUpdate.description;
viewState.amount = sipToUpdate.amount;
viewState.startDate = formatDate(sipToUpdate.startDate);
viewState.endDate = formatDate(sipToUpdate.endDate);
viewState.endDate =
sipToUpdate.endDate != null ? formatDate(sipToUpdate.endDate!) : '';
viewState.frequency = sipToUpdate.frequency;
});
}
Expand All @@ -91,7 +88,7 @@ class CreateSipViewState {
double amount = 0.0;
String startDate = formatDate(DateTime.now());
String endDate = formatDate(DateTime.now().add(const Duration(days: 365)));
double frequency = 1.0;
SipFrequency frequency = SipFrequency.monthly;
SingleEvent<void>? onSipCreated;

bool isValid() {
Expand Down
61 changes: 47 additions & 14 deletions lib/presentation/goals_presenter.dart
Original file line number Diff line number Diff line change
@@ -1,30 +1,63 @@
import 'package:wealth_wave/api/apis/goal_api.dart';
import 'package:wealth_wave/core/presenter.dart';
import 'package:wealth_wave/domain/models/goal.dart';
import 'package:wealth_wave/domain/models/investment.dart';
import 'package:wealth_wave/domain/services/goal_service.dart';

class GoalsPresenter extends Presenter<GoalsViewState> {
final GoalApi _goalApi;
final FetchGoalsUseCase _fetchGoalsUseCase;

GoalsPresenter({
final GoalApi? goalApi,
final FetchGoalsUseCase? fetchGoalsUseCase,
}) : _goalApi = goalApi ?? GoalApi(),
_fetchGoalsUseCase = fetchGoalsUseCase ?? FetchGoalsUseCase(),
final GoalService _goalService;

GoalsPresenter({final GoalService? goalService})
: _goalService = goalService ?? GoalService(),
super(GoalsViewState());

void fetchGoals() {
_fetchGoalsUseCase.invoke().then((goals) => updateViewState((viewState) {
viewState.goals = goals;
}));
_goalService
.get()
.then((goals) => Future.wait(goals.map((goal) async {
final valueOnMaturity = await goal.getValueOnMaturity();
final investments = await goal.getInvestments();
final maturityAmount = await goal.getMaturityAmount();

return GoalVO(
id: goal.id,
name: goal.name,
description: goal.description,
goal: goal,
maturityAmount: maturityAmount,
maturityDate: goal.maturityDate,
valueOnMaturity: valueOnMaturity,
investments: investments);
})))
.then((goalVOs) =>
updateViewState((viewState) => viewState.goals = goalVOs));
}

void deleteGoal({required final int id}) {
_goalApi.deleteBy(id: id).then((_) => fetchGoals());
_goalService.deleteBy(id: id).then((_) => fetchGoals());
}
}

class GoalsViewState {
List<Goal> goals = [];
List<GoalVO> goals = [];
}

class GoalVO {
final int id;
final String name;
final String? description;
final double maturityAmount;
final DateTime maturityDate;
final double valueOnMaturity;
final Goal goal;
final Map<Investment, double> investments;

GoalVO(
{required this.id,
required this.name,
required this.description,
required this.maturityAmount,
required this.maturityDate,
required this.valueOnMaturity,
required this.goal,
required this.investments});
}
Loading

0 comments on commit afb0613

Please sign in to comment.