Skip to content

Commit

Permalink
Add Dashboard Page
Browse files Browse the repository at this point in the history
  • Loading branch information
praslnx8 committed Jan 27, 2024
1 parent 58b0e23 commit b3d54b1
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 2 deletions.
36 changes: 35 additions & 1 deletion lib/presentation/dashboard_presenter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,41 @@ class DashboardPresenter extends Presenter<DashboardViewState> {
: _investmentService = investmentService ?? InvestmentService(),
super(DashboardViewState());

void fetchDashboard() {}
void fetchDashboard() {
_investmentService.get().then((investments) {
double invested = 0;
double currentValue = 0;
Map<RiskLevel, double> riskComposition = {};
Map<String, double> basketComposition = {};
Map<double, double> irrComposition = {};

for (var investment in investments) {
double investmentValue = investment.getValueOn(date: DateTime.now());
invested += investment.getTotalInvestedAmount(till: DateTime.now());
currentValue += investmentValue;
riskComposition.update(
investment.riskLevel, (value) => value + investmentValue,
ifAbsent: () => investmentValue);
basketComposition.update(
investment.basketName ?? '-', (value) => value + investmentValue,
ifAbsent: () => investmentValue);
irrComposition.update((investment.getIRR()).roundToDouble(),
(value) => value + investmentValue,
ifAbsent: () => investmentValue);
}

updateViewState((viewState) {
viewState.invested = invested;
viewState.currentValue = currentValue;
viewState.riskComposition = riskComposition
.map((key, value) => MapEntry(key, value / currentValue));
viewState.basketComposition = basketComposition
.map((key, value) => MapEntry(key, value / currentValue));
viewState.irrComposition = irrComposition
.map((key, value) => MapEntry(key, value / currentValue));
});
});
}
}

class DashboardViewState {
Expand Down
97 changes: 96 additions & 1 deletion lib/ui/pages/dashboard_page.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:primer_progress_bar/primer_progress_bar.dart';
import 'package:wealth_wave/contract/risk_level.dart';
import 'package:wealth_wave/core/page_state.dart';
import 'package:wealth_wave/presentation/dashboard_presenter.dart';
import 'package:wealth_wave/ui/app_dimen.dart';
import 'package:wealth_wave/utils/ui_utils.dart';

class DashboardPage extends StatefulWidget {
const DashboardPage({super.key});
Expand All @@ -20,7 +25,97 @@ class _DashboardPage
@override
Widget buildWidget(
final BuildContext context, final DashboardViewState snapshot) {
return Scaffold(body: Center());
final irrComposition = snapshot.irrComposition.entries.toList();
irrComposition.sort((a, b) => a.key.compareTo(b.key));

return Scaffold(
appBar: AppBar(
title: const Text('Investment Portfolio Dashboard'),
),
body: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(AppDimen.defaultPadding),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
Text(
'Total Invested Amount: ${formatToCurrency(snapshot.invested)}',
style: Theme.of(context).textTheme.bodyLarge),
const SizedBox(height: AppDimen.minPadding),
Text(
'Current Value of Investment: ${formatToCurrency(snapshot.currentValue)}',
style: Theme.of(context).textTheme.bodyLarge),
const SizedBox(height: AppDimen.minPadding),
Text('Risk Composition:',
style: Theme.of(context).textTheme.titleMedium),
const SizedBox(height: AppDimen.minPadding),
_buildPieChart(snapshot.riskComposition),
const SizedBox(height: AppDimen.minPadding),
Text('Basket Composition:',
style: Theme.of(context).textTheme.titleMedium),
const SizedBox(height: AppDimen.minPadding),
_buildBarChart(snapshot.basketComposition.entries.toList()),
const SizedBox(height: AppDimen.minPadding),
Text('IRR Composition:',
style: Theme.of(context).textTheme.titleMedium),
const SizedBox(height: AppDimen.minPadding),
_buildBarChart(irrComposition
.map((e) => MapEntry(formatToPercentage(e.key), e.value))
.toList()),
],
),
),
),
);
}

Widget _buildPieChart(Map<RiskLevel, double> data) {
return SizedBox(
height: 200,
child: PieChart(PieChartData(
sections: data.entries
.map((e) => PieChartSectionData(
value: e.value,
color: _getColorForRiskLevel(e.key),
title: _getRiskLevelName(e.key),
))
.toList())));
}

Widget _buildBarChart(List<MapEntry<String, double>> data) {
return PrimerProgressBar(
segments: data
.map((e) => Segment(
value: (e.value * 100).toInt(),
valueLabel: Text(formatToPercentage(e.value * 100)),
label: Text(e.key),
color:
Colors.primaries[e.key.hashCode % Colors.primaries.length]))
.toList(),
);
}

String _getRiskLevelName(RiskLevel riskLevel) {
switch (riskLevel) {
case RiskLevel.low:
return 'Low Risk';
case RiskLevel.medium:
return 'Medium Risk';
case RiskLevel.high:
return 'High Risk';
}
}

Color _getColorForRiskLevel(RiskLevel key) {
switch (key) {
case RiskLevel.low:
return Colors.green;
case RiskLevel.medium:
return Colors.orange;
case RiskLevel.high:
return Colors.red;
}
}

@override
Expand Down

0 comments on commit b3d54b1

Please sign in to comment.