Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.venv/
1 change: 1 addition & 0 deletions mobile/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
.buildlog/
.history
.svn/
.vscode/

# IntelliJ related
*.iml
Expand Down
116 changes: 3 additions & 113 deletions mobile/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,117 +1,7 @@
import 'package:flutter/material.dart';

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
// This makes the visual density adapt to the platform that you run
// the app on. For desktop platforms, the controls will be smaller and
// closer together (more dense) than on mobile platforms.
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.

// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
import 'src/app.dart';

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;

void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}

@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
void main() {
runApp(App());
}
29 changes: 29 additions & 0 deletions mobile/lib/src/app.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:flutter/material.dart';
Comment thread
abelfodil marked this conversation as resolved.
import 'package:flutter_bloc/flutter_bloc.dart';

import 'presentation/wallets/wallets_route.dart';
import 'locator.dart';
import 'theme.dart';

class App extends StatelessWidget {
App() {
registerServices();
}

@override
Widget build(BuildContext context) {
/// Cubits are provided globally down all of the context tree
return MultiBlocProvider(
providers: createBlocProviders(),
child: MaterialApp(
title: 'PolyDodo',
theme: theme,
home: WalletsRoute(),
initialRoute: WalletsRoute.name,
routes: {
WalletsRoute.name: (context) => WalletsRoute(),
},
),
);
}
}
63 changes: 63 additions & 0 deletions mobile/lib/src/application/wallets/wallets_cubit.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import 'dart:math';

import 'package:bloc/bloc.dart';

import 'package:polydodo/src/application/wallets/wallets_state.dart';
import 'package:polydodo/src/domain/unique_id.dart';
import 'package:polydodo/src/domain/wallet/i_wallet_repository.dart';
import 'package:polydodo/src/domain/wallet/money.dart';
import 'package:polydodo/src/domain/wallet/owner.dart';
import 'package:polydodo/src/domain/wallet/wallet.dart';

class WalletsCubit extends Cubit<WalletsState> {
final IWalletRepository _walletRepository;

WalletsCubit(this._walletRepository) : super(WalletsInitial()) {
createNewWallets();
emit(WalletsLoadInProgress());
_walletRepository
.watch()
.listen((wallets) => emit(WalletsLoadSuccess(wallets)))
.onError((e) => emit(WalletsLoadFailure(e)));
}

// Does not yield another state
void transfer(Wallet sender, Wallet receiver, Money amount) async {
try {
sender.transfer(amount, receiver);
} catch (e) {
emit(WalletsTransferFailure(e));
}
await Future.wait([
_walletRepository.store(sender),
_walletRepository.store(receiver),
]).catchError((e) => emit(WalletsTransferFailure(e)));
}

Future<List<void>> createNewWallets() => Future.wait([
_walletRepository.store(
Wallet(
UniqueId(),
Owner(
id: UniqueId.from('1'),
firstName: 'Bob',
lastName: 'Skiridovsky',
age: 25,
),
Money(Random().nextDouble() * 10, Currency.cad),
),
),
_walletRepository.store(
Wallet(
UniqueId(),
Owner(
id: UniqueId.from('2'),
firstName: 'Alice',
lastName: 'Pogo',
age: 35,
),
Money(Random().nextDouble() * 1000, Currency.cad),
),
)
]);
}
25 changes: 25 additions & 0 deletions mobile/lib/src/application/wallets/wallets_state.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:polydodo/src/domain/wallet/wallet.dart';

abstract class WalletsState {}

class WalletsInitial extends WalletsState {}

class WalletsLoadInProgress extends WalletsState {}

class WalletsLoadSuccess extends WalletsState {
final List<Wallet> wallets;

WalletsLoadSuccess(this.wallets);
}

class WalletsLoadFailure extends WalletsState {
final Exception cause;

WalletsLoadFailure(this.cause);
}

class WalletsTransferFailure extends WalletsState {
final Exception cause;

WalletsTransferFailure(this.cause);
}
20 changes: 20 additions & 0 deletions mobile/lib/src/domain/unique_id.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:equatable/equatable.dart';
import 'package:uuid/uuid.dart';

class UniqueId extends Equatable {
final String _id;

UniqueId() : _id = Uuid().v4();

UniqueId.from(String unique)
: assert(unique != null),
_id = unique;

String get() => _id;

@override
List<Object> get props => [_id];

@override
bool get stringify => true;
}
1 change: 1 addition & 0 deletions mobile/lib/src/domain/wallet/constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const cadToUsdRate = 1.32;
6 changes: 6 additions & 0 deletions mobile/lib/src/domain/wallet/i_wallet_repository.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import 'wallet.dart';

abstract class IWalletRepository {
Future<void> store(Wallet wallet);
Stream<List<Wallet>> watch();
}
57 changes: 57 additions & 0 deletions mobile/lib/src/domain/wallet/money.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import 'package:equatable/equatable.dart';

import 'constants.dart';

enum Currency { usd, cad }

Map<Currency, String> currencyToString = {
Currency.usd: 'USD',
Currency.cad: 'CAD',
};

// Value object usually extends equatable
class Money extends Equatable {
static const _min = 0;

final double value;
final Currency currency;

Money(this.value, this.currency)
: assert(value != null),
assert(currency != null) {
// Validation that money cannot be in an impossible state
if (value <= _min) {
Exception('Money amount cannot be inferior to $_min');
}
}

double get usd {
if (currency == Currency.cad) {
return value / cadToUsdRate;
}
return value;
}

double get cad {
if (currency == Currency.usd) {
return value * cadToUsdRate;
}
return value;
}

Money convertTo(Currency currency) {
if (currency == Currency.usd) {
return Money(usd, currency);
}
return Money(cad, currency);
}

/// Gives back result into left operand's currency
Money operator +(Money other) =>
Money(this.usd + other.usd, Currency.usd).convertTo(this.currency);
Money operator -(Money other) =>
Money(this.usd - other.usd, Currency.usd).convertTo(this.currency);

@override
List<Object> get props => [usd];
}
32 changes: 32 additions & 0 deletions mobile/lib/src/domain/wallet/owner.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'package:meta/meta.dart';

import 'package:polydodo/src/domain/unique_id.dart';

class Owner {
static const _legalAge = 16;

final UniqueId id;
final String firstName;
final String lastName;
final int age;

Owner({
@required this.id,
@required this.firstName,
@required this.lastName,
@required this.age,
}) : assert(id != null),
assert(firstName != null),
assert(lastName != null) {
if (firstName.length <= 0 || lastName.length <= 0) {
throw Exception('First and last name cannot be empty');
}
if (!_hasLegalAge()) {
throw Exception('This owner does not have the legal age');
}
}

bool _hasLegalAge() => age >= _legalAge;

// In a good model entity, we should have behavorial method...
}
Loading