Skip to content

Commit

Permalink
save commit
Browse files Browse the repository at this point in the history
  • Loading branch information
praslnx8 committed Jan 19, 2024
1 parent f25d800 commit 869de94
Show file tree
Hide file tree
Showing 19 changed files with 145 additions and 188 deletions.
1 change: 0 additions & 1 deletion lib/domain/models/investment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ class Investment {
.then((sip) => sip
.deleteTransactions()
.then((_) => sip.performSipTransactions().then((_) => sip)));
;
}

Future<void> deleteSIP({required final int sipId}) async {
Expand Down
10 changes: 6 additions & 4 deletions lib/presentation/baskets_presenter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class BasketsViewState {
class BasketVO {
final int id;
final String name;
final String? description;
final String description;
final double totalInvestedAmount;
final int totalInvesments;

Expand All @@ -44,11 +44,13 @@ class BasketVO {
required this.description});

static Future<BasketVO> from({required final Basket basket}) async {
final double investedValue = await basket.getInvestedValue();
final int totalInvestments = await basket.getTotalInvestments();
return BasketVO(
id: basket.id,
name: basket.name,
description: basket.description,
totalInvestedAmount: await basket.getInvestedValue(),
totalInvesments: await basket.getTotalInvestments());
description: basket.description ?? '',
totalInvestedAmount: investedValue,
totalInvesments: totalInvestments);
}
}
12 changes: 6 additions & 6 deletions lib/presentation/create_basket_presenter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CreateBasketPresenter extends Presenter<CreateBasketViewState> {
_basketApi
.getBy(id: id)
.then((basketDO) => Basket.from(basketDO: basketDO))
.then((basket) => setBasket(basket));
.then((basket) => _setBasket(basket));
}

void createBasket({final int? basketId}) {
Expand All @@ -24,8 +24,8 @@ class CreateBasketPresenter extends Presenter<CreateBasketViewState> {
return;
}

final name = viewState.name;
final description = viewState.description;
final String name = viewState.name;
final String description = viewState.description;

if (basketId != null) {
_basketApi
Expand All @@ -47,17 +47,17 @@ class CreateBasketPresenter extends Presenter<CreateBasketViewState> {
updateViewState((viewState) => viewState.description = text);
}

void setBasket(Basket basketToUpdate) {
void _setBasket(Basket basketToUpdate) {
updateViewState((viewState) {
viewState.name = basketToUpdate.name;
viewState.description = basketToUpdate.description;
viewState.description = basketToUpdate.description ?? '';
});
}
}

class CreateBasketViewState {
String name = '';
String? description;
String description = '';
SingleEvent<void>? onBasketCreated;

bool isValid() {
Expand Down
60 changes: 23 additions & 37 deletions lib/presentation/create_goal_presenter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import 'package:wealth_wave/core/presenter.dart';
import 'package:wealth_wave/core/single_event.dart';
import 'package:wealth_wave/domain/models/goal.dart';
import 'package:wealth_wave/domain/services/goal_service.dart';
import 'package:wealth_wave/utils/ui_utils.dart';
import 'package:wealth_wave/utils/utils.dart';

class CreateGoalPresenter extends Presenter<CreateGoalViewState> {
final GoalService _goalService;
Expand All @@ -25,11 +23,10 @@ class CreateGoalPresenter extends Presenter<CreateGoalViewState> {

final String name = viewState.name;
final String description = viewState.description;
final double amount = double.tryParse(viewState.amount) ?? 0;
final DateTime date = parseDate(viewState.date) ?? DateTime.now();
final DateTime targetDate =
parseDate(viewState.targetDate) ?? DateTime.now();
final double inflation = double.tryParse(viewState.inflation) ?? 0;
final double amount = viewState.amount;
final DateTime date = viewState.date;
final DateTime targetDate = viewState.targetDate;
final double inflation = viewState.inflation;
final GoalImportance importance = viewState.importance;

if (goalIdToUpdate != null) {
Expand Down Expand Up @@ -68,20 +65,20 @@ class CreateGoalPresenter extends Presenter<CreateGoalViewState> {
updateViewState((viewState) => viewState.description = text);
}

void amountChanged(String text) {
updateViewState((viewState) => viewState.amount = text);
void amountChanged(double value) {
updateViewState((viewState) => viewState.amount = value);
}

void dateChanged(String date) {
void dateChanged(DateTime date) {
updateViewState((viewState) => viewState.date = date);
}

void targetDateChanged(String date) {
void targetDateChanged(DateTime date) {
updateViewState((viewState) => viewState.targetDate = date);
}

void inflationChanged(String text) {
updateViewState((viewState) => viewState.inflation = text);
void inflationChanged(double value) {
updateViewState((viewState) => viewState.inflation = value);
}

void importanceChanged(GoalImportance importance) {
Expand All @@ -92,11 +89,12 @@ class CreateGoalPresenter extends Presenter<CreateGoalViewState> {
updateViewState((viewState) {
viewState.name = goalToUpdate.name;
viewState.description = goalToUpdate.description ?? '';
viewState.amount = goalToUpdate.amount.toString();
viewState.inflation = goalToUpdate.inflation.toString();
viewState.amount = goalToUpdate.amount;
viewState.inflation = goalToUpdate.inflation;
viewState.importance = goalToUpdate.importance;
viewState.date = formatDate(goalToUpdate.amountUpdatedOn) ?? '';
viewState.targetDate = formatDate(goalToUpdate.maturityDate) ?? '';
viewState.date = goalToUpdate.amountUpdatedOn;
viewState.targetDate = goalToUpdate.maturityDate;
viewState.onDataLoaded = SingleEvent(null);
});
}

Expand All @@ -108,38 +106,26 @@ class CreateGoalPresenter extends Presenter<CreateGoalViewState> {
class CreateGoalViewState {
String name = '';
String description = '';
String amount = '';
String date = '';
String targetDate = '';
String inflation = '';
double amount = 0;
DateTime date = DateTime.now();
DateTime targetDate = DateTime.now().add(const Duration(days: 365));
double inflation = 0;
GoalImportance importance = GoalImportance.high;
SingleEvent<void>? onGoalCreated;
SingleEvent<void>? onDataLoaded;

double getTargetAmount() {
final amount = double.tryParse(this.amount) ?? 0;
final inflation = double.tryParse(this.inflation) ?? 0;
final date = parseDate(this.date) ?? DateTime.now();
final targetDate = parseDate(this.targetDate) ?? DateTime.now();
return amount *
pow(1 + inflation / 100, targetDate.difference(date).inDays / 365);
}

bool isValid() {
final double? amount = double.tryParse(this.amount);
final DateTime? date = parseDate(this.date);
final DateTime? targetDate = parseDate(this.targetDate);
final double? inflation = double.tryParse(this.inflation);

final bool isAmountValid = amount != null && amount > 0;
final bool isDateValid = date != null;
final bool isTargetDateValid =
targetDate != null && targetDate.isAfter(DateTime.now());
final bool isInflationValid =
inflation != null && inflation >= 0 && inflation <= 100;
final bool isAmountValid = amount > 0;
final bool isTargetDateValid = targetDate.isAfter(DateTime.now());
final bool isInflationValid = inflation >= 0 && inflation <= 100;

return name.isNotEmpty &&
isAmountValid &&
isDateValid &&
isTargetDateValid &&
isInflationValid;
}
Expand Down
80 changes: 32 additions & 48 deletions lib/presentation/create_investment_presenter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import 'package:wealth_wave/domain/models/basket.dart';
import 'package:wealth_wave/domain/models/investment.dart';
import 'package:wealth_wave/domain/services/basket_service.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 CreateInvestmentPresenter extends Presenter<CreateInvestmentViewState> {
final InvestmentService _investmentService;
Expand All @@ -31,26 +29,26 @@ class CreateInvestmentPresenter extends Presenter<CreateInvestmentViewState> {
return;
}

final name = viewState.name;
final description = viewState.description;
final value = viewState.value;
final valueUpdatedAt = viewState._getValueUpdatedAt();
final basketId = viewState.basketId;
final riskLevel = viewState.riskLevel;
final irr = viewState.irr;
final maturityDate = viewState.getMaturityDate();
final String name = viewState.name;
final String description = viewState.description;
final double? value = viewState.value;
final DateTime? valueUpdatedAt = viewState.valueUpdatedAt;
final int? basketId = viewState.basketId;
final RiskLevel riskLevel = viewState.riskLevel;
final double? irr = viewState.irr;
final DateTime? maturityDate = viewState.maturityDate;

if (investmentIdToUpdate != null) {
_investmentService
.update(
id: investmentIdToUpdate,
description: description,
name: name,
value: double.tryParse(value!),
value: value,
valueUpdatedOn: valueUpdatedAt,
basketId: basketId,
riskLevel: riskLevel,
irr: double.tryParse(irr!),
irr: irr,
maturityDate: maturityDate)
.then((_) => updateViewState((viewState) =>
viewState.onInvestmentCreated = SingleEvent(null)));
Expand All @@ -59,11 +57,11 @@ class CreateInvestmentPresenter extends Presenter<CreateInvestmentViewState> {
.create(
name: name,
description: description,
value: double.tryParse(value!),
value: value,
valueUpdatedOn: valueUpdatedAt,
basketId: basketId,
riskLevel: riskLevel,
irr: double.tryParse(irr!),
irr: irr,
maturityDate: maturityDate)
.then((investmentId) => updateViewState((viewState) =>
viewState.onInvestmentCreated = SingleEvent(null)));
Expand All @@ -78,20 +76,20 @@ class CreateInvestmentPresenter extends Presenter<CreateInvestmentViewState> {
updateViewState((viewState) => viewState.description = text);
}

void valueChanged(String text) {
void valueChanged(double? value) {
updateViewState((viewState) {
viewState.value = text;
if (text.isNotEmpty) {
viewState.value = value;
if (value != null && viewState.irr != null) {
viewState.irr = null;
viewState.onIRRCleared = SingleEvent(null);
}
});
}

void valueUpdatedDateChanged(String date) {
void valueUpdatedDateChanged(DateTime? date) {
updateViewState((viewState) {
viewState.valueUpdatedAt = date;
if (date.isNotEmpty && viewState.irr != null) {
if (date != null && viewState.irr != null) {
viewState.irr = null;
viewState.onIRRCleared = SingleEvent(null);
}
Expand All @@ -106,12 +104,12 @@ class CreateInvestmentPresenter extends Presenter<CreateInvestmentViewState> {
updateViewState((viewState) => viewState.riskLevel = riskLevel);
}

void irrChanged(String irr) {
void irrChanged(double? irr) {
updateViewState((viewState) {
viewState.irr = irr;
if (irr.isNotEmpty &&
if (irr != null &&
(viewState.value != null || viewState.valueUpdatedAt != null)) {
viewState.valueUpdatedAt = '';
viewState.valueUpdatedAt = null;
viewState.value = null;
viewState.onValueCleared = SingleEvent(null);
}
Expand All @@ -120,14 +118,12 @@ class CreateInvestmentPresenter extends Presenter<CreateInvestmentViewState> {

void setInvestment(Investment investmentToUpdate) {
updateViewState((viewState) {
final DateTime? valueUpdatedOn = investmentToUpdate.valueUpdatedOn;
final double? value = investmentToUpdate.value;
viewState.name = investmentToUpdate.name;
viewState.basketId = investmentToUpdate.basketId;
viewState.value = investmentToUpdate.value != null
? formatDecimal(investmentToUpdate.value!)
: null;
viewState.valueUpdatedAt = investmentToUpdate.valueUpdatedOn != null
? formatDate(investmentToUpdate.valueUpdatedOn!)
: null;
viewState.value = value;
viewState.valueUpdatedAt = valueUpdatedOn;
viewState.riskLevel = investmentToUpdate.riskLevel;
viewState.onInvestmentFetched = SingleEvent(null);
});
Expand All @@ -144,11 +140,11 @@ class CreateInvestmentViewState {
String name = '';
String description = '';
int? basketId;
String? irr;
double? irr;
RiskLevel riskLevel = RiskLevel.medium;
String? value;
String? valueUpdatedAt;
String? maturityDate;
double? value;
DateTime? valueUpdatedAt;
DateTime? maturityDate;
SingleEvent<void>? onInvestmentCreated;
SingleEvent<void>? onInvestmentFetched;
SingleEvent<void>? onIRRCleared;
Expand All @@ -157,23 +153,11 @@ class CreateInvestmentViewState {
List<Basket> baskets = List.empty(growable: false);

bool isValid() {
final valueAsDouble = double.tryParse(value ?? '');
final irrAsDouble = double.tryParse(irr ?? '');
final valueUpdateAtAsDate = parseDate(valueUpdatedAt ?? '');

final containsValue = valueAsDouble != null &&
valueAsDouble > 0 &&
valueUpdateAtAsDate != null;
final containsIRR = irrAsDouble != null && irrAsDouble > 0;
final value = this.value;
final irr = this.irr;
final containsValue = value != null && value > 0 && valueUpdatedAt != null;
final containsIRR = irr != null && irr > 0;

return name.isNotEmpty && (containsValue || containsIRR);
}

DateTime? _getValueUpdatedAt() {
return valueUpdatedAt != null ? parseDate(valueUpdatedAt!) : null;
}

DateTime? getMaturityDate() {
return maturityDate != null ? parseDate(maturityDate!) : null;
}
}
Loading

0 comments on commit 869de94

Please sign in to comment.