diff --git a/lib/app_component.dart b/lib/app_component.dart index e9b44cc..c9f05e1 100644 --- a/lib/app_component.dart +++ b/lib/app_component.dart @@ -5,6 +5,7 @@ import 'package:rules_of_living/components/controls_component.dart'; import 'package:rules_of_living/components/header_component.dart'; import 'package:rules_of_living/components/simulation_component.dart'; import 'package:rules_of_living/service/configuration_service.dart'; +import 'package:rules_of_living/service/control_service.dart'; import 'package:rules_of_living/service/engine_service.dart'; @Component( @@ -20,8 +21,16 @@ import 'package:rules_of_living/service/engine_service.dart'; ControlsComponent, ConfigurationComponent ], - providers: [materialProviders, ClassProvider(EngineService), ClassProvider(ConfigurationService)], - styleUrls: const ['package:angular_components/app_layout/layout.scss.css', 'app_component.css'], + providers: [ + materialProviders, + ClassProvider(EngineService), + ClassProvider(ConfigurationService), + ClassProvider(EngineService) + ], + styleUrls: const [ + 'package:angular_components/app_layout/layout.scss.css', + 'app_component.css' + ], ) class AppComponent { var name = "World"; diff --git a/lib/components/controls_component.dart b/lib/components/controls_component.dart index 511a7d4..954d94b 100644 --- a/lib/components/controls_component.dart +++ b/lib/components/controls_component.dart @@ -1,6 +1,6 @@ import 'package:angular/angular.dart'; import 'package:angular_components/angular_components.dart'; -import 'package:rules_of_living/service/engine_service.dart'; +import 'package:rules_of_living/service/control_service.dart'; @Component( selector: 'sim-controls', @@ -15,27 +15,27 @@ import 'package:rules_of_living/service/engine_service.dart'; styleUrls: const ["controls_component.css"], ) class ControlsComponent { - final EngineService engine; + final ControlService ctrl; - ControlsComponent(this.engine); + ControlsComponent(this.ctrl); void onStartClicked() { - engine.toggleRunning(); + ctrl.toggleRunning(); } void onStepClicked() { - engine.step(); + ctrl.step(); } void onResetClicked() { - engine.reset(); + ctrl.reset(); } void onRandomClicked() { - engine.addRandomPattern(); + ctrl.addRandomPattern(); } void onClearClicked() { - engine.clear(); + ctrl.clear(); } } diff --git a/lib/components/simulation_component.dart b/lib/components/simulation_component.dart index 12b7844..99b3f02 100644 --- a/lib/components/simulation_component.dart +++ b/lib/components/simulation_component.dart @@ -1,7 +1,7 @@ import 'dart:html' as html; import 'package:angular/angular.dart'; -import 'package:rules_of_living/service/engine_service.dart'; +import 'package:rules_of_living/service/control_service.dart'; @Component( selector: 'gol-simulation', @@ -10,9 +10,9 @@ import 'package:rules_of_living/service/engine_service.dart'; providers: [], ) class SimulationComponent implements OnInit { - final EngineService engineService; + final ControlService ctrl; - SimulationComponent(this.engineService); + SimulationComponent(this.ctrl); @override void ngOnInit() { @@ -29,6 +29,6 @@ class SimulationComponent implements OnInit { the canvas did not load correctly :( ''', canvas.width / 2 - 50, canvas.height / 2); - engineService.canvas = canvas; + ctrl.canvas = canvas; } } diff --git a/lib/service/configuration_service.dart b/lib/service/configuration_service.dart index 8ca89ec..edee10c 100644 --- a/lib/service/configuration_service.dart +++ b/lib/service/configuration_service.dart @@ -3,7 +3,7 @@ import 'dart:math'; import 'package:rules_of_living/service/engine_service.dart'; class ConfigurationService { - final EngineService engineService; + final EngineService _es; bool showGrid; @@ -17,10 +17,10 @@ class ConfigurationService { int get simSpeed => _simSpeed; void set simSpeed(int val) { _simSpeed = val; - engineService.engine.stepsPerSecond = simSpeed; + _es.engine.stepsPerSecond = simSpeed; } - ConfigurationService(this.engineService) { + ConfigurationService(this._es) { showGrid = false; simSpeed = 5; } @@ -30,8 +30,8 @@ class ConfigurationService { } void setGridSize({int x, int y}) { - x = x ?? engineService.engine.gridSize.x; - y = y ?? engineService.engine.gridSize.y; - engineService.engine.gridSize = Point(x, y); + x = x ?? _es.engine.gridSize.x; + y = y ?? _es.engine.gridSize.y; + _es.engine.gridSize = Point(x, y); } } diff --git a/lib/service/control_service.dart b/lib/service/control_service.dart new file mode 100644 index 0000000..abc1f57 --- /dev/null +++ b/lib/service/control_service.dart @@ -0,0 +1,43 @@ +import 'dart:html' as html; + +import 'package:rules_of_living/service/engine_service.dart'; + +class ControlService { + EngineService _es; + + ControlService(this._es); + + void set canvas(html.CanvasElement canvas) => _es.engine.canvas = canvas; + html.CanvasElement get canvas => _es.engine.canvas; + + void run() { + _es.engine.running = true; + } + + void stop() { + _es.engine.running = false; + } + + void toggleRunning() { + _es.engine.running = !_es.engine.running; + } + + void step() { + _es.engine.step(); + } + + void reset() { + _es.engine.reset(); + } + + void addRandomPattern() { + _es.engine.running = false; + _es.engine.addPattern(); + } + + void clear() { + _es.engine.clear(); + } + + bool get isRunning => _es.engine.running; +} diff --git a/lib/service/engine_service.dart b/lib/service/engine_service.dart index f43cd55..8168735 100644 --- a/lib/service/engine_service.dart +++ b/lib/service/engine_service.dart @@ -1,48 +1,15 @@ -import 'dart:html' as html; - import 'package:rules_of_living/src/Engine.dart'; class EngineService { - Engine _engine; + Engine _uncachedEngineAccess; - Engine get engine => _engine ?? getEngine(Engine()); - - Engine getEngine(Engine engine) { - _engine = engine; - return _engine; + Engine get engine => _uncachedEngineAccess ?? _setCachedAndReturn(Engine()); + void set engine(Engine newEngine) { + _uncachedEngineAccess = newEngine; } - void set canvas(html.CanvasElement canvas) => engine.canvas = canvas; - html.CanvasElement get canvas => engine.canvas; - - void run() { - engine.running = true; + Engine _setCachedAndReturn(Engine newEngine) { + engine = newEngine; + return newEngine; } - - void stop() { - engine.running = false; - } - - void toggleRunning() { - engine.running = !engine.running; - } - - void step() { - engine.step(); - } - - void reset() { - engine.reset(); - } - - void addRandomPattern() { - engine.running = false; - engine.addPattern(); - } - - void clear() { - engine.clear(); - } - - bool get isRunning => engine.running; } diff --git a/test/service/configuration_service_test.dart b/test/service/configuration_service_test.dart index f28f6e8..44e23b2 100644 --- a/test/service/configuration_service_test.dart +++ b/test/service/configuration_service_test.dart @@ -1,11 +1,11 @@ import 'dart:math'; +import 'package:mockito/mockito.dart'; +import 'package:rules_of_living/service/configuration_service.dart'; +import 'package:rules_of_living/service/engine_service.dart'; import 'package:rules_of_living/src/Engine.dart'; @TestOn('browser') import 'package:test/test.dart'; -import 'package:rules_of_living/service/configuration_service.dart'; -import 'package:rules_of_living/service/engine_service.dart'; -import 'package:mockito/mockito.dart'; class MockEngine extends Mock implements Engine {} @@ -16,7 +16,7 @@ void main() { setUp(() { me = MockEngine(); engineService = EngineService(); - engineService.getEngine(me); + engineService.engine = me; sut = ConfigurationService(engineService); }); diff --git a/test/service/engine_service_test.dart b/test/service/engine_service_test.dart index cbd9655..a792b93 100644 --- a/test/service/engine_service_test.dart +++ b/test/service/engine_service_test.dart @@ -14,8 +14,8 @@ void main() { sut = EngineService(); }); group("Dependency Injection", () { - test("EngineService accesses the Engine defined in getEngine", () { - sut.getEngine(me); + test("EngineService can be passed a custom Engine", () { + sut.engine = me; Engine result = sut.engine; expect(result, equals(me)); @@ -33,7 +33,7 @@ void main() { }); test("caching can be overriden by providing a custom engine", () { Engine first = sut.engine; - sut.getEngine(me); + sut.engine = me; Engine second = sut.engine; expect(second, isNot(equals(first))); });