-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from VGVentures/feat/generate-random-data
feat: generate random data
- Loading branch information
Showing
11 changed files
with
192 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,64 @@ | ||
import 'dart:math' show Random; | ||
|
||
import 'package:bloc/bloc.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:financial_dashboard/l10n/l10n.dart'; | ||
import 'package:flutter/widgets.dart'; | ||
|
||
part 'financial_data_event.dart'; | ||
part 'financial_data_state.dart'; | ||
|
||
class FinancialDataBloc extends Bloc<FinancialDataEvent, FinancialDataState> { | ||
FinancialDataBloc() : super(const FinancialDataState()) { | ||
FinancialDataBloc({ | ||
Random? random, | ||
}) : _random = random ?? Random(), | ||
super(const FinancialDataState()) { | ||
on<FinancialDataRequested>(_onLoadFinancialData); | ||
} | ||
|
||
final Random _random; | ||
|
||
Future<void> _onLoadFinancialData( | ||
FinancialDataRequested event, | ||
Emitter<FinancialDataState> emit, | ||
) async { | ||
final savingsDataPoints = createSampleData(random: _random); | ||
final index = _random.nextInt(savingsDataPoints.length); | ||
final currentSavings = savingsDataPoints[index].value; | ||
final monthlySpendingLimitGoal = (_random.nextDouble() * 1000) + 3000; | ||
|
||
emit( | ||
state.copyWith( | ||
currentSavings: 234567.91, | ||
savingsDataPoints: createSampleData(), | ||
monthlySpendingLimitGoal: 3210.55, | ||
transactions: [ | ||
const Transaction(title: 'Paycheck', amount: 3000), | ||
const Transaction(title: 'Rent', amount: -1050.20), | ||
const Transaction(title: 'Food', amount: -670.50), | ||
], | ||
currentSavings: currentSavings, | ||
savingsDataPoints: savingsDataPoints, | ||
monthlySpendingLimitGoal: monthlySpendingLimitGoal, | ||
transactions: createSampleTransactions(random: _random), | ||
), | ||
); | ||
} | ||
} | ||
|
||
@visibleForTesting | ||
List<SavingsDataPoint> createSampleData() { | ||
List<SavingsDataPoint> createSampleData({Random? random}) { | ||
random ??= Random(); | ||
|
||
final data = <SavingsDataPoint>[]; | ||
var value = 100000.0; | ||
for (var age = 25; age <= 90; age++) { | ||
const toAdd = 10000; | ||
final toAdd = random.nextInt(12000); | ||
value += toAdd; | ||
data.add( | ||
SavingsDataPoint(age: age, value: value), | ||
); | ||
} | ||
return data; | ||
} | ||
|
||
@visibleForTesting | ||
List<Transaction> createSampleTransactions({Random? random}) { | ||
random ??= Random(); | ||
|
||
return TransactionType.values | ||
.map((type) => Transaction(type: type, amount: type.amount(random!))) | ||
.toList(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters