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 0d11a91 commit 82af735
Show file tree
Hide file tree
Showing 17 changed files with 322 additions and 151 deletions.
7 changes: 3 additions & 4 deletions lib/api/apis/goal_investment_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,11 @@ class GoalInvestmentApi {
}

Future<int> update(
{required final int goalId,
{required final int id,
required final int goalId,
required final int investmentId,
required final double split}) async {
return (_db.update(_db.goalInvestmentTable)
..where((t) =>
t.goalId.equals(goalId) & t.investmentId.equals(investmentId)))
return (_db.update(_db.goalInvestmentTable)..where((t) => t.id.equals(id)))
.write(GoalInvestmentTableCompanion(
investmentId: Value(investmentId),
goalId: Value(goalId),
Expand Down
22 changes: 11 additions & 11 deletions lib/domain/models/goal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,25 +85,25 @@ class Goal {
.then((amounts) => amounts.reduce((value, element) => value + element));
}

Future<MapEntry<Investment, double>> tagInvestment(
{required final Investment investment,
required final double split}) async {
Future<void> tagInvestment(
{required final int investmentId, required final double split}) async {
return _goalInvestmentApi
.create(goalId: id, investmentId: investment.id, split: split)
.then((goalInvestmentDO) => MapEntry(investment, split));
.create(goalId: id, investmentId: investmentId, split: split)
.then((goalInvestmentDO) => Void);
}

Future<MapEntry<Investment, double>> updateTaggedInvestment(
{required final Investment investment,
Future<void> updateTaggedInvestment(
{required final int id,
required final int investmentId,
required final double split}) async {
return _goalInvestmentApi
.update(goalId: id, investmentId: investment.id, split: split)
.then((goalInvestmentDO) => MapEntry(investment, split));
.update(id: id, goalId: id, investmentId: investmentId, split: split)
.then((goalInvestmentDO) => Void);
}

Future<void> deleteTaggedInvestment({required final Investment investment}) {
Future<void> deleteTaggedInvestment({required final int investmentId}) {
return _goalInvestmentApi
.deleteBy(goalId: id, investmentId: investment.id)
.deleteBy(goalId: id, investmentId: investmentId)
.then((count) => Void);
}

Expand Down
26 changes: 14 additions & 12 deletions lib/domain/models/investment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -202,27 +202,29 @@ class Investment {
}

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

Future<MapEntry<Goal, double>> tagGoal(
{required final Goal goal, required final double split}) async {
Future<void> tagGoal(
{required final int goalId, required final double split}) async {
return _goalInvestmentApi
.create(goalId: goal.id, investmentId: id, split: split)
.then((goalInvestmentDO) => MapEntry(goal, split));
.create(goalId: goalId, investmentId: id, split: split)
.then((goalInvestmentDO) => Void);
}

Future<MapEntry<Goal, double>> updateTaggedGoal(
{required final Goal goal, required final double split}) async {
Future<void> updateTaggedGoal(
{required final int id,
required final int goalId,
required final double split}) async {
return _goalInvestmentApi
.update(goalId: goal.id, investmentId: id, split: split)
.then((goalInvestmentDO) => MapEntry(goal, split));
.update(id: id, goalId: goalId, investmentId: id, split: split)
.then((goalInvestmentDO) => Void);
}

Future<void> deleteTaggedGoal({required final Goal goal}) {
Future<void> deleteTaggedGoal({required final int goalId}) {
return _goalInvestmentApi
.deleteBy(goalId: goal.id, investmentId: id)
.deleteBy(goalId: goalId, investmentId: id)
.then((count) => Void);
}

Expand Down
16 changes: 13 additions & 3 deletions lib/presentation/create_investment_transaction_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/core/presenter.dart';
import 'package:wealth_wave/core/single_event.dart';
import 'package:wealth_wave/domain/models/transaction.dart';
import 'package:wealth_wave/domain/services/investment_service.dart';
import 'package:wealth_wave/domain/services/transaction_service.dart';
import 'package:wealth_wave/utils/ui_utils.dart';
import 'package:wealth_wave/utils/utils.dart';

class CreateInvestmentTransactionPresenter
extends Presenter<CreateTransactionViewState> {
final int _investmentId;
final InvestmentService _investmentService;
final TransactionService _transactionService;

CreateInvestmentTransactionPresenter(
{required final int investmentId,
final InvestmentService? investmentService})
final InvestmentService? investmentService,
final TransactionService? transactionService})
: _investmentId = investmentId,
_investmentService = investmentService ?? InvestmentService(),
_transactionService = transactionService ?? TransactionService(),
super(CreateTransactionViewState());

void createTransaction({final int? transactionIdToUpdate}) {
Expand Down Expand Up @@ -63,12 +67,18 @@ class CreateInvestmentTransactionPresenter
updateViewState((viewState) => viewState.investedDate = date);
}

void setTransaction(TransactionDO transactionToUpdate) {
void setTransaction(Transaction transactionToUpdate) {
updateViewState((viewState) {
viewState.amount = transactionToUpdate.amount;
viewState.investedDate = formatDate(transactionToUpdate.createdOn);
});
}

void fetchTransaction({required int id}) {
_transactionService
.getBy(id: id)
.then((transaction) => setTransaction(transaction));
}
}

class CreateTransactionViewState {
Expand Down
44 changes: 30 additions & 14 deletions lib/presentation/tag_goal_presenter.dart
Original file line number Diff line number Diff line change
@@ -1,34 +1,50 @@
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/goal.dart';
import 'package:wealth_wave/domain/models/investment.dart';
import 'package:wealth_wave/domain/services/investment_service.dart';

class TagGoalPresenter extends Presenter<TagGoalViewState> {
final Investment _investment;
final int _investmentId;
final InvestmentService _investmentService;

TagGoalPresenter({required final Investment investment})
: _investment = investment,
TagGoalPresenter(
{required final int investmentId,
final InvestmentService? investmentService})
: _investmentId = investmentId,
_investmentService = investmentService ?? InvestmentService(),
super(TagGoalViewState());

void onGoalSelected(int? goalId) {
updateViewState((viewState) => viewState.goalId = goalId);
}

void tagGoal({required final Goal goal}) {
final double sharePercentage = getViewState().sharePercentage;
void fetchGoals() {}

_investment.tagGoal(goal: goal, split: sharePercentage).then((_) =>
updateViewState(
(viewState) => viewState.onGoalTagged = SingleEvent(null)));
void tagGoal() {
final double sharePercentage = getViewState().sharePercentage;
final int? goalId = getViewState().goalId;
if (goalId != null && sharePercentage > 0) {
_investmentService
.getBy(id: _investmentId)
.then((investment) =>
investment.tagGoal(goalId: goalId, split: sharePercentage))
.then((_) => updateViewState(
(viewState) => viewState.onGoalTagged = SingleEvent(null)));
}
}

void updateTaggedGoal({required final Goal goal}) {
void updateTaggedGoal({required final int id}) {
final double sharePercentage = getViewState().sharePercentage;
final int? goalId = getViewState().goalId;

_investment.updateTaggedGoal(goal: goal, split: sharePercentage).then((_) =>
updateViewState(
(viewState) => viewState.onGoalTagged = SingleEvent(null)));
if (goalId != null && sharePercentage > 0) {
_investmentService
.getBy(id: _investmentId)
.then((investment) => investment.updateTaggedGoal(
id: id, goalId: goalId, split: sharePercentage))
.then((_) => updateViewState(
(viewState) => viewState.onGoalTagged = SingleEvent(null)));
}
}

void onPercentageChanged(String text) {
Expand Down
51 changes: 37 additions & 14 deletions lib/presentation/tag_investment_presenter.dart
Original file line number Diff line number Diff line change
@@ -1,33 +1,56 @@
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/goal.dart';
import 'package:wealth_wave/domain/models/investment.dart';
import 'package:wealth_wave/domain/services/goal_service.dart';
import 'package:wealth_wave/domain/services/investment_service.dart';

class TagInvestmentPresenter extends Presenter<TagInvestmentViewState> {
final Goal _goal;
final int _goalId;
final GoalService _goalService;
final InvestmentService _investmentService;

TagInvestmentPresenter({required final Goal goal})
: _goal = goal,
TagInvestmentPresenter(
{required final int goalId,
final GoalService? goalService,
final InvestmentService? investmentService})
: _goalId = goalId,
_goalService = goalService ?? GoalService(),
_investmentService = investmentService ?? InvestmentService(),
super(TagInvestmentViewState());

void onInvestmentSelected(int? investmentId) {
updateViewState((viewState) => viewState.investmentId = investmentId);
}

void tagInvestment({required final Investment investment}) {
void fetchInvesments() {}

void tagInvestment() {
final double sharePercentage = getViewState().sharePercentage;
_goal.tagInvestment(investment: investment, split: sharePercentage).then(
(_) => updateViewState(
(viewState) => viewState.onInvestmentTagged = SingleEvent(null)));
final int? investmentId = getViewState().investmentId;

if (investmentId != null && sharePercentage > 0) {
_goalService
.getBy(id: _goalId)
.then((goal) => goal.tagInvestment(
investmentId: investmentId, split: sharePercentage))
.then((_) => updateViewState(
(viewState) => viewState.onInvestmentTagged = SingleEvent(null)));
}
}

void updateTaggedInvestment({required final Investment investment}) {
void updateTaggedInvestment(
{required final int id, required final int investmentId}) {
final double sharePercentage = getViewState().sharePercentage;
_goal
.updateTaggedInvestment(investment: investment, split: sharePercentage)
.then((_) => updateViewState(
(viewState) => viewState.onInvestmentTagged = SingleEvent(null)));
final int? investmentId = getViewState().investmentId;

if (investmentId != null && sharePercentage > 0) {
_goalService
.getBy(id: _goalId)
.then((goal) => goal.updateTaggedInvestment(
id: id, investmentId: investmentId, split: sharePercentage))
.then((_) => updateViewState(
(viewState) => viewState.onInvestmentTagged = SingleEvent(null)));
}
}

void onPercentageChanged(String text) {
Expand Down
73 changes: 61 additions & 12 deletions lib/presentation/tagged_goals_presenter.dart
Original file line number Diff line number Diff line change
@@ -1,29 +1,78 @@
import 'package:wealth_wave/contract/goal_importance.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/investment_service.dart';

class TaggedGoalsPresenter extends Presenter<TaggedGoalsViewState> {
final Investment _investment;
final int _investmentId;
final InvestmentService _investmentService;

TaggedGoalsPresenter({required final Investment investment})
: _investment = investment,
TaggedGoalsPresenter(
{required final int investmentId,
final InvestmentService? investmentService})
: _investmentId = investmentId,
_investmentService = investmentService ?? InvestmentService(),
super(TaggedGoalsViewState());

void fetchTaggedInvestment() {
_investment.getGoals().then((value) => updateViewState((viewState) {
viewState.taggedGoals = value;
}));
_investmentService
.getBy(id: _investmentId)
.then((investment) => investment.getGoals())
.then((taggedGoals) => Future.wait(taggedGoals.entries.toList().map(
(taggedGoal) => TagggedGoalVO.from(
goal: taggedGoal.key, split: taggedGoal.value))))
.then((taggedGoalVOs) => updateViewState((viewState) {
viewState.taggedGoalVOs = taggedGoalVOs;
}));
}

void deleteTaggedInvestment({required final Goal goal}) {
_investment.deleteTaggedGoal(goal: goal).then((_) {
fetchTaggedInvestment();
});
void deleteTaggedInvestment({required final int goalId}) {
_investmentService
.getBy(id: _investmentId)
.then((investment) => investment.deleteTaggedGoal(goalId: goalId))
.then((value) => fetchTaggedInvestment());
}
}

class TaggedGoalsViewState {
Map<Goal, double> taggedGoals = {};
List<TagggedGoalVO> taggedGoalVOs = [];

TaggedGoalsViewState();
}

class TagggedGoalVO {
final int id;
final String name;
final String? description;
final double amount;
final DateTime amountUpdatedOn;
final DateTime maturityDate;
final double inflation;
final GoalImportance importance;
final double split;

TagggedGoalVO(
{required this.id,
required this.name,
required this.description,
required this.amount,
required this.amountUpdatedOn,
required this.maturityDate,
required this.inflation,
required this.importance,
required this.split});

static Future<TagggedGoalVO> from(
{required final Goal goal, required final double split}) async {
return Future.value(TagggedGoalVO(
id: goal.id,
name: goal.name,
description: goal.description,
amount: goal.amount,
amountUpdatedOn: goal.amountUpdatedOn,
maturityDate: goal.maturityDate,
inflation: goal.inflation,
importance: goal.importance,
split: split));
}
}
Loading

0 comments on commit 82af735

Please sign in to comment.