Skip to content

Commit

Permalink
Isolating domain
Browse files Browse the repository at this point in the history
  • Loading branch information
artberri committed Oct 20, 2018
1 parent d7e1967 commit 56d7b0e
Show file tree
Hide file tree
Showing 44 changed files with 192 additions and 109 deletions.
7 changes: 7 additions & 0 deletions lib/domain/collections/colors.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright (C) 2018 Alberto Varela Sánchez <alberto@berriart.com>
// Use of this source code is governed by the version 3 of the
// GNU General Public License that can be found in the LICENSE file.

enum Color {
blue, green, red, yellow
}
8 changes: 8 additions & 0 deletions lib/domain/collections/index.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (C) 2018 Alberto Varela Sánchez <alberto@berriart.com>
// Use of this source code is governed by the version 3 of the
// GNU General Public License that can be found in the LICENSE file.

export 'colors.dart';
export 'locales.dart';
export 'numbers.dart';
export 'routes.dart';
File renamed without changes.
4 changes: 0 additions & 4 deletions lib/domain/enums.dart → lib/domain/collections/numbers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
// Use of this source code is governed by the version 3 of the
// GNU General Public License that can be found in the LICENSE file.

enum Color {
blue, green, red, yellow
}

enum Number {
one, two, three, four, five, six, seven, eight, nine
}
8 changes: 2 additions & 6 deletions lib/routes.dart → lib/domain/collections/routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
// Use of this source code is governed by the version 3 of the
// GNU General Public License that can be found in the LICENSE file.

class Routes {
static final home = "/";
static final game = "/game";
static final countdown = "/countdown";
static final gameover = "/gameover";
static final settings = "/settings";
enum Routes {
home, game, countdown, gameover, settings
}
8 changes: 8 additions & 0 deletions lib/domain/contracts/animator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (C) 2018 Alberto Varela Sánchez <alberto@berriart.com>
// Use of this source code is governed by the version 3 of the
// GNU General Public License that can be found in the LICENSE file.

abstract class AnimatorContract {
void forward({ double from });
void stop();
}
8 changes: 8 additions & 0 deletions lib/domain/contracts/index.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (C) 2018 Alberto Varela Sánchez <alberto@berriart.com>
// Use of this source code is governed by the version 3 of the
// GNU General Public License that can be found in the LICENSE file.

export 'animator.dart';
export 'navigator.dart';
export 'storage.dart';
export 'timer.dart';
14 changes: 14 additions & 0 deletions lib/domain/contracts/navigator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (C) 2018 Alberto Varela Sánchez <alberto@berriart.com>
// Use of this source code is governed by the version 3 of the
// GNU General Public License that can be found in the LICENSE file.

import '../collections/index.dart';

abstract class NavigatorContract {
void navigateTo(Routes route);
void redirectTo(Routes route);
void navigateBack();
}



10 changes: 10 additions & 0 deletions lib/domain/contracts/storage.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (C) 2018 Alberto Varela Sánchez <alberto@berriart.com>
// Use of this source code is governed by the version 3 of the
// GNU General Public License that can be found in the LICENSE file.

abstract class StorageContract {
Future<String> getLanguage();
Future<bool> setLanguage(String language);
Future<int> getTopScore();
Future<bool> setTopScore(int topScore);
}
2 changes: 1 addition & 1 deletion lib/domain/timer.dart → lib/domain/contracts/timer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by the version 3 of the
// GNU General Public License that can be found in the LICENSE file.

abstract class Timer {
abstract class TimerContract {
int get elapsedInMilliseconds;
int get maxTimeInMilliseconds;
void start(int maxTimeInMilliseconds);
Expand Down
9 changes: 4 additions & 5 deletions lib/domain/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// Use of this source code is governed by the version 3 of the
// GNU General Public License that can be found in the LICENSE file.

export 'answer.dart';
export 'enums.dart';
export 'game.dart';
export 'game_timer.dart';
export 'timer.dart';
export 'collections/index.dart';
export 'model/index.dart';
export 'services/index.dart';
export 'contracts/index.dart';
2 changes: 1 addition & 1 deletion lib/domain/answer.dart → lib/domain/model/answer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by the version 3 of the
// GNU General Public License that can be found in the LICENSE file.

import 'enums.dart';
import '../collections/index.dart';

class Answer {
final Color color;
Expand Down
5 changes: 3 additions & 2 deletions lib/domain/game.dart → lib/domain/model/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
// Use of this source code is governed by the version 3 of the
// GNU General Public License that can be found in the LICENSE file.

import 'answer.dart';
import 'dart:math';
import 'enums.dart';

import 'answer.dart';
import '../collections/index.dart';

const colors = Color.values;
const numbers = Number.values;
Expand Down
6 changes: 6 additions & 0 deletions lib/domain/model/index.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (C) 2018 Alberto Varela Sánchez <alberto@berriart.com>
// Use of this source code is governed by the version 3 of the
// GNU General Public License that can be found in the LICENSE file.

export 'answer.dart';
export 'game.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by the version 3 of the
// GNU General Public License that can be found in the LICENSE file.

import 'timer.dart';
import '../contracts/timer.dart';

class GameTimer {
GameTimer(
Expand All @@ -12,11 +12,11 @@ class GameTimer {
this._timeAdditionByAnswerInMilliseconds,
);

final Timer _timer;
final TimerContract _timer;
final double _timePenaltyMultiplier;
final int _timeAdditionByAnswerInMilliseconds;

Timer get timer => _timer;
TimerContract get timer => _timer;

int _initialMaxTimeInMilliseconds;
int get maxTimeInMilliseconds => _timer.maxTimeInMilliseconds;
Expand Down
6 changes: 6 additions & 0 deletions lib/domain/services/index.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (C) 2018 Alberto Varela Sánchez <alberto@berriart.com>
// Use of this source code is governed by the version 3 of the
// GNU General Public License that can be found in the LICENSE file.

export 'game_timer.dart';
export 'locale_helper.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// Use of this source code is governed by the version 3 of the
// GNU General Public License that can be found in the LICENSE file.

import 'locales.dart';
import '../collections/index.dart';

class TranslationsHelper {
class LocaleHelper {

static final _codes = {
Locales.english: 'en',
Expand Down
4 changes: 2 additions & 2 deletions lib/i18n/delegate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import 'package:flutter/material.dart';

import 'translations.dart';
import 'translations_helper.dart';
import '../domain/index.dart';

class TranslationsDelegate extends LocalizationsDelegate<Translations> {
final Locale newLocale;

const TranslationsDelegate({this.newLocale});

@override
bool isSupported(Locale locale) => TranslationsHelper.supportedLanguages.contains(locale.languageCode);
bool isSupported(Locale locale) => LocaleHelper.supportedLanguages.contains(locale.languageCode);

@override
Future<Translations> load(Locale locale) {
Expand Down
2 changes: 0 additions & 2 deletions lib/i18n/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@

export 'delegate.dart';
export 'locale_changer.dart';
export 'locales.dart';
export 'translations.dart';
export 'translations_helper.dart';
4 changes: 2 additions & 2 deletions lib/i18n/translations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart' show rootBundle;

import 'translations_helper.dart';
import '../domain/index.dart';

class Translations {
Translations(Locale locale) {
Expand All @@ -33,5 +33,5 @@ class Translations {

get currentLanguage => locale.languageCode;

static Iterable<Locale> supportedLocales() => TranslationsHelper.supportedLanguages.map<Locale>((lang) => Locale(lang, ''));
static Iterable<Locale> supportedLocales() => LocaleHelper.supportedLanguages.map<Locale>((lang) => Locale(lang, ''));
}
6 changes: 4 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ void main() {
timeAdditionByAnswerInMilliseconds: 1800,
gameStartCountdownSeconds: 4,
child: Injector(
storage: Storage(),
animatorFactory: AnimatorFactory(),
dependencies: [
Storage(),
AnimatorFactory(),
],
child: Numcol(),
),
),
Expand Down
29 changes: 22 additions & 7 deletions lib/numcol.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

import 'view/index.dart';
import 'domain/index.dart';
import 'i18n/index.dart';
import 'services/index.dart';
import 'routes.dart';
import 'screens/index.dart';

class Numcol extends StatefulWidget {
Expand Down Expand Up @@ -40,25 +41,38 @@ class _NumcolState extends State<Numcol> {
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
Injector.of(context).storage.getLanguage()
Injector.of(context).inject<StorageContract>().getLanguage()
.then((chosenLocale) {
if (chosenLocale != null && chosenLocale != _chosenLocale) {
onLocaleChange(new Locale(chosenLocale, ''));
}
});
}
/*
Route<dynamic> _onGenerateRoute(RouteSettings settings) {
var path = settings.name.split('/');
if (path[0] == "myRoute") {
final foo = path.length > 1 ? int.parse(path[1]) : null;
return new MaterialPageRoute(
builder: (context) => new MyPage(foo: foo),
settings: settings,
);
}
return null;
}*/

@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'NumCol',
initialRoute: '/',
routes: {
Routes.home: (context) => HomeScreen(),
Routes.game: (context) => GameScreen(),
Routes.countdown: (context) => CountdownScreen(),
Routes.gameover: (context) => GameoverScreen(),
Routes.settings: (context) => SettingsScreen(),
routes[Routes.home]: (context) => HomeScreen(),
routes[Routes.game]: (context) => GameScreen(),
routes[Routes.countdown]: (context) => CountdownScreen(),
routes[Routes.gameover]: (context) => GameoverScreen(),
routes[Routes.settings]: (context) => SettingsScreen(),
},
localizationsDelegates: [
_newLocaleDelegate,
Expand All @@ -67,6 +81,7 @@ class _NumcolState extends State<Numcol> {
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: Translations.supportedLocales(),
/*onGenerateRoute: _onGenerateRoute,*/
);
}
}
2 changes: 1 addition & 1 deletion lib/screens/countdown/countdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class _CountdownScreenState extends State<CountdownScreen>
void didChangeDependencies() {
super.didChangeDependencies();
final configuration = Configuration.of(context);
_animator = Injector.of(context).animatorFactory.createCountdownAnimator(
_animator = Injector.of(context).inject<AnimatorFactory>().createCountdownAnimator(
vsync: this,
seconds: configuration.gameStartCountdownSeconds,
begin: configuration.gameStartCountdownSeconds,
Expand Down
9 changes: 3 additions & 6 deletions lib/screens/countdown/countdown_presenter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
// Use of this source code is governed by the version 3 of the
// GNU General Public License that can be found in the LICENSE file.

import '../../view/index.dart';
import '../../routes.dart';
import '../../domain/index.dart';

abstract class CountdownScreenViewContract {
void redirectTo(String route);
}
abstract class CountdownScreenViewContract implements NavigatorContract {}

class CountdownScreenPresenter {
CountdownScreenPresenter(this._view, this._animator);

final Animator _animator;
final AnimatorContract _animator;
final CountdownScreenViewContract _view;

void onLoad() => _animator.forward();
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/game/game.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class _GameScreenState extends State<GameScreen>
void didChangeDependencies() {
super.didChangeDependencies();
final configuration = Configuration.of(context);
_animator = Injector.of(context).animatorFactory.createGameAnimator(
_animator = Injector.of(context).inject<AnimatorFactory>().createGameAnimator(
vsync: this,
milliseconds: configuration.initialTimeInMilliseconds,
onCompleted: _gameover
Expand Down
5 changes: 1 addition & 4 deletions lib/screens/game/game_presenter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
// GNU General Public License that can be found in the LICENSE file.

import '../../domain/index.dart';
import '../../routes.dart';

abstract class GameScreenViewContract {
void redirectTo(String route);
}
abstract class GameScreenViewContract implements NavigatorContract {}

class GameScreenPresenter {
GameScreenPresenter(this._view, this._timer, this._game);
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/game/widgets/question/question.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class _QuestionWidgetState extends State<QuestionWidget> with TickerProviderStat
bool get isColorOk => widget.isColorOk.value;

QuestionAnimator _createAnimator(DismissAnimationCallback dismissAnimationCallbak) {
return Injector.of(context).animatorFactory.createQuestionAnimator(
return Injector.of(context).inject<AnimatorFactory>().createQuestionAnimator(
vsync: this,
milliseconds: 400,
onDismissed: dismissAnimationCallbak,
Expand Down
6 changes: 3 additions & 3 deletions lib/screens/game/widgets/question/question_presenter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed by the version 3 of the
// GNU General Public License that can be found in the LICENSE file.

import '../../../../view/index.dart';
import '../../../../domain/index.dart';

abstract class QuestionViewContract {
bool get isColorOk;
Expand All @@ -12,8 +12,8 @@ abstract class QuestionViewContract {
class QuestionPresenter {
QuestionPresenter(this._view, this._colorAnimator, this._numberAnimator);

final QuestionAnimator _colorAnimator;
final QuestionAnimator _numberAnimator;
final AnimatorContract _colorAnimator;
final AnimatorContract _numberAnimator;
final QuestionViewContract _view;

void onIsColorOkValueChanged() {
Expand Down
7 changes: 2 additions & 5 deletions lib/screens/gameover/gameover_presenter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
// Use of this source code is governed by the version 3 of the
// GNU General Public License that can be found in the LICENSE file.

import '../../routes.dart';
import '../../domain/index.dart';

abstract class GameoverScreenViewContract {
void redirectTo(String route);
void navigateBack();
}
abstract class GameoverScreenViewContract implements NavigatorContract {}

class GameoverScreenPresenter {
GameoverScreenPresenter(this._view);
Expand Down
Loading

0 comments on commit 56d7b0e

Please sign in to comment.