More state cleanup

pull/88/head
Kevin Moore 5 years ago
parent 62ffd22505
commit cb5036305e

@ -2,27 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'core/puzzle_animator.dart'; import 'package:flutter_web/foundation.dart';
import 'flutter.dart';
import 'core/puzzle_proxy.dart';
abstract class AppState { abstract class AppState {
PuzzleProxy get puzzle; PuzzleProxy get puzzle;
bool get autoPlay;
void setAutoPlay(bool newValue);
Listenable get animationNotifier; Listenable get animationNotifier;
void clickOrShake(int tileValue) {
setAutoPlay(false);
puzzle.clickOrShake(tileValue);
}
void Function(bool newValue) get setAutoPlayFunction {
if (puzzle.solved) {
return null;
}
return setAutoPlay;
}
} }

@ -7,32 +7,7 @@ import 'dart:math' show Point, Random;
import 'body.dart'; import 'body.dart';
import 'puzzle.dart'; import 'puzzle.dart';
import 'puzzle_proxy.dart';
enum PuzzleEvent { click, reset, noop }
abstract class PuzzleProxy {
int get width;
int get height;
int get length;
bool get solved;
void reset();
void clickOrShake(int tileValue);
int get tileCount;
int get clickCount;
int get incorrectTiles;
Point<double> location(int index);
bool isCorrectPosition(int value);
}
class PuzzleAnimator implements PuzzleProxy { class PuzzleAnimator implements PuzzleProxy {
final _rnd = Random(); final _rnd = Random();
@ -61,13 +36,10 @@ class PuzzleAnimator implements PuzzleProxy {
@override @override
int get tileCount => _puzzle.tileCount; int get tileCount => _puzzle.tileCount;
@override
int get incorrectTiles => _puzzle.incorrectTiles; int get incorrectTiles => _puzzle.incorrectTiles;
@override
int get clickCount => _clickCount; int get clickCount => _clickCount;
@override
void reset() => _resetCore(); void reset() => _resetCore();
Stream<PuzzleEvent> get onEvent => _controller.stream; Stream<PuzzleEvent> get onEvent => _controller.stream;
@ -97,7 +69,7 @@ class PuzzleAnimator implements PuzzleProxy {
_puzzle = _puzzle.clickRandom(vertical: _nextRandomVertical); _puzzle = _puzzle.clickRandom(vertical: _nextRandomVertical);
_nextRandomVertical = !_nextRandomVertical; _nextRandomVertical = !_nextRandomVertical;
_clickCount++; _clickCount++;
_controller.add(PuzzleEvent.click); _controller.add(PuzzleEvent.random);
} }
@override @override

@ -0,0 +1,25 @@
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:math' show Point;
enum PuzzleEvent { click, random, reset, noop }
abstract class PuzzleProxy {
int get width;
int get height;
int get length;
bool get solved;
void clickOrShake(int tileValue);
int get tileCount;
Point<double> location(int index);
bool isCorrectPosition(int value);
}

@ -0,0 +1,17 @@
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter_web/foundation.dart';
abstract class PuzzleControls implements Listenable {
void reset();
int get clickCount;
int get incorrectTiles;
bool get autoPlay;
void Function(bool newValue) get setAutoPlayFunction;
}

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'core/puzzle_animator.dart'; import 'core/puzzle_proxy.dart';
import 'flutter.dart'; import 'flutter.dart';
class PuzzleFlowDelegate extends FlowDelegate { class PuzzleFlowDelegate extends FlowDelegate {

@ -8,12 +8,42 @@ import 'package:provider/provider.dart';
import 'app_state.dart'; import 'app_state.dart';
import 'core/puzzle_animator.dart'; import 'core/puzzle_animator.dart';
import 'core/puzzle_proxy.dart';
import 'flutter.dart'; import 'flutter.dart';
import 'puzzle_controls.dart';
import 'puzzle_flow_delegate.dart'; import 'puzzle_flow_delegate.dart';
import 'shared_theme.dart'; import 'shared_theme.dart';
import 'themes.dart'; import 'themes.dart';
import 'value_tab_controller.dart'; import 'value_tab_controller.dart';
class _PuzzleControls extends ChangeNotifier implements PuzzleControls {
final PuzzleHomeState _parent;
_PuzzleControls(this._parent);
@override
bool get autoPlay => _parent._autoPlay;
void _notify() => notifyListeners();
@override
void Function(bool newValue) get setAutoPlayFunction {
if (_parent.puzzle.solved) {
return null;
}
return _parent._setAutoPlay;
}
@override
int get clickCount => _parent.puzzle.clickCount;
@override
int get incorrectTiles => _parent.puzzle.incorrectTiles;
@override
void reset() => _parent.puzzle.reset();
}
class PuzzleHomeState extends State with TickerProviderStateMixin, AppState { class PuzzleHomeState extends State with TickerProviderStateMixin, AppState {
@override @override
final PuzzleAnimator puzzle; final PuzzleAnimator puzzle;
@ -26,8 +56,8 @@ class PuzzleHomeState extends State with TickerProviderStateMixin, AppState {
Duration _lastElapsed; Duration _lastElapsed;
StreamSubscription _puzzleEventSubscription; StreamSubscription _puzzleEventSubscription;
@override bool _autoPlay = false;
bool autoPlay = false; _PuzzleControls _autoPlayListsenable;
PuzzleHomeState(this.puzzle) { PuzzleHomeState(this.puzzle) {
_puzzleEventSubscription = puzzle.onEvent.listen(_onPuzzleEvent); _puzzleEventSubscription = puzzle.onEvent.listen(_onPuzzleEvent);
@ -36,37 +66,31 @@ class PuzzleHomeState extends State with TickerProviderStateMixin, AppState {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_autoPlayListsenable = _PuzzleControls(this);
_ticker ??= createTicker(_onTick); _ticker ??= createTicker(_onTick);
_ensureTicking(); _ensureTicking();
} }
@override void _setAutoPlay(bool newValue) {
void setAutoPlay(bool newValue) { if (newValue != _autoPlay) {
if (newValue != autoPlay) {
setState(() { setState(() {
// Only allow enabling autoPlay if the puzzle is not solved // Only allow enabling autoPlay if the puzzle is not solved
autoPlay = newValue && !puzzle.solved; _autoPlayListsenable._notify();
if (autoPlay) { _autoPlay = newValue && !puzzle.solved;
if (_autoPlay) {
_ensureTicking(); _ensureTicking();
} }
}); });
} }
} }
bool _badHack;
@override @override
Widget build(BuildContext context) => MultiProvider( Widget build(BuildContext context) => MultiProvider(
providers: [ providers: [
Provider<AppState>.value( Provider<AppState>.value(value: this),
value: this, ListenableProvider<PuzzleControls>.value(
updateShouldNotify: (p, c) { listenable: _autoPlayListsenable,
if (c.autoPlay != _badHack) { )
_badHack = c.autoPlay;
return true;
}
return false;
}),
], ],
child: Material( child: Material(
child: Stack( child: Stack(
@ -89,11 +113,16 @@ class PuzzleHomeState extends State with TickerProviderStateMixin, AppState {
void dispose() { void dispose() {
animationNotifier.dispose(); animationNotifier.dispose();
_ticker?.dispose(); _ticker?.dispose();
_autoPlayListsenable?.dispose();
_puzzleEventSubscription.cancel(); _puzzleEventSubscription.cancel();
super.dispose(); super.dispose();
} }
void _onPuzzleEvent(PuzzleEvent e) { void _onPuzzleEvent(PuzzleEvent e) {
_autoPlayListsenable._notify();
if (e != PuzzleEvent.random) {
_setAutoPlay(false);
}
_tickerTimeSinceLastEvent = Duration.zero; _tickerTimeSinceLastEvent = Duration.zero;
_ensureTicking(); _ensureTicking();
setState(() { setState(() {
@ -126,18 +155,18 @@ class PuzzleHomeState extends State with TickerProviderStateMixin, AppState {
if (!puzzle.stable) { if (!puzzle.stable) {
animationNotifier.animate(); animationNotifier.animate();
} else { } else {
if (!autoPlay) { if (!_autoPlay) {
_ticker.stop(); _ticker.stop();
_lastElapsed = null; _lastElapsed = null;
} }
} }
if (autoPlay && if (_autoPlay &&
_tickerTimeSinceLastEvent > const Duration(milliseconds: 200)) { _tickerTimeSinceLastEvent > const Duration(milliseconds: 200)) {
puzzle.playRandom(); puzzle.playRandom();
if (puzzle.solved) { if (puzzle.solved) {
setAutoPlay(false); _setAutoPlay(false);
} }
} }
} }
@ -225,7 +254,7 @@ Widget _doBuildCore(bool small) => ValueTabController<SharedTheme>(
children: List<Widget>.generate( children: List<Widget>.generate(
appState.puzzle.length, appState.puzzle.length,
(i) => theme.tileButtonCore( (i) => theme.tileButtonCore(
i, appState, small), i, appState.puzzle, small),
), ),
), ),
), ),
@ -242,8 +271,10 @@ Widget _doBuildCore(bool small) => ValueTabController<SharedTheme>(
top: 2, top: 2,
right: 10, right: 10,
), ),
child: Row( child: Consumer<PuzzleControls>(
children: theme.bottomControls(appState)), builder: (_, controls, __) => Row(
children: theme.bottomControls(controls)),
),
) )
], ],
), ),

@ -2,9 +2,9 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'app_state.dart'; import 'core/puzzle_proxy.dart';
import 'core/puzzle_animator.dart';
import 'flutter.dart'; import 'flutter.dart';
import 'puzzle_controls.dart';
import 'widgets/material_interior_alt.dart'; import 'widgets/material_interior_alt.dart';
final puzzleAnimationDuration = kThemeAnimationDuration * 3; final puzzleAnimationDuration = kThemeAnimationDuration * 3;
@ -24,7 +24,7 @@ abstract class SharedTheme {
EdgeInsetsGeometry tilePadding(PuzzleProxy puzzle) => const EdgeInsets.all(6); EdgeInsetsGeometry tilePadding(PuzzleProxy puzzle) => const EdgeInsets.all(6);
Widget tileButton(int i, AppState appState, bool small); Widget tileButton(int i, PuzzleProxy puzzle, bool small);
Ink createInk( Ink createInk(
Widget child, { Widget child, {
@ -40,7 +40,7 @@ abstract class SharedTheme {
); );
Widget createButton( Widget createButton(
AppState appState, PuzzleProxy puzzle,
bool small, bool small,
int tileValue, int tileValue,
Widget content, { Widget content, {
@ -49,12 +49,12 @@ abstract class SharedTheme {
}) => }) =>
AnimatedContainer( AnimatedContainer(
duration: puzzleAnimationDuration, duration: puzzleAnimationDuration,
padding: tilePadding(appState.puzzle), padding: tilePadding(puzzle),
child: RaisedButton( child: RaisedButton(
elevation: 4, elevation: 4,
clipBehavior: Clip.hardEdge, clipBehavior: Clip.hardEdge,
animationDuration: puzzleAnimationDuration, animationDuration: puzzleAnimationDuration,
onPressed: () => appState.clickOrShake(tileValue), onPressed: () => puzzle.clickOrShake(tileValue),
shape: shape ?? puzzleBorder(small), shape: shape ?? puzzleBorder(small),
padding: const EdgeInsets.symmetric(), padding: const EdgeInsets.symmetric(),
child: content, child: content,
@ -76,22 +76,21 @@ abstract class SharedTheme {
fontWeight: FontWeight.bold, fontWeight: FontWeight.bold,
); );
List<Widget> bottomControls(AppState appState) => <Widget>[ List<Widget> bottomControls(PuzzleControls controls) => <Widget>[
IconButton( IconButton(
onPressed: appState.puzzle.reset, onPressed: controls.reset,
icon: Icon(Icons.refresh, color: puzzleAccentColor), icon: Icon(Icons.refresh, color: puzzleAccentColor),
//Icons.refresh,
), ),
Checkbox( Checkbox(
value: appState.autoPlay, value: controls.autoPlay,
onChanged: appState.setAutoPlayFunction, onChanged: controls.setAutoPlayFunction,
activeColor: puzzleAccentColor, activeColor: puzzleAccentColor,
), ),
Expanded( Expanded(
child: Container(), child: Container(),
), ),
Text( Text(
appState.puzzle.clickCount.toString(), controls.clickCount.toString(),
textAlign: TextAlign.right, textAlign: TextAlign.right,
style: _infoStyle, style: _infoStyle,
), ),
@ -99,7 +98,7 @@ abstract class SharedTheme {
SizedBox( SizedBox(
width: 28, width: 28,
child: Text( child: Text(
appState.puzzle.incorrectTiles.toString(), controls.incorrectTiles.toString(),
textAlign: TextAlign.right, textAlign: TextAlign.right,
style: _infoStyle, style: _infoStyle,
), ),
@ -107,11 +106,11 @@ abstract class SharedTheme {
const Text(' Tiles left ') const Text(' Tiles left ')
]; ];
Widget tileButtonCore(int i, AppState appState, bool small) { Widget tileButtonCore(int i, PuzzleProxy puzzle, bool small) {
if (i == appState.puzzle.tileCount && !appState.puzzle.solved) { if (i == puzzle.tileCount && !puzzle.solved) {
return const Center(); return const Center();
} }
return tileButton(i, appState, small); return tileButton(i, puzzle, small);
} }
} }

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'app_state.dart'; import 'core/puzzle_proxy.dart';
import 'flutter.dart'; import 'flutter.dart';
import 'shared_theme.dart'; import 'shared_theme.dart';
@ -37,14 +37,14 @@ class ThemePlaster extends SharedTheme {
); );
@override @override
Widget tileButton(int i, AppState appState, bool small) { Widget tileButton(int i, PuzzleProxy puzzle, bool small) {
final correctColumn = i % appState.puzzle.width; final correctColumn = i % puzzle.width;
final correctRow = i ~/ appState.puzzle.width; final correctRow = i ~/ puzzle.width;
final primary = (correctColumn + correctRow).isEven; final primary = (correctColumn + correctRow).isEven;
if (i == appState.puzzle.tileCount) { if (i == puzzle.tileCount) {
assert(appState.puzzle.solved); assert(puzzle.solved);
return Center( return Center(
child: Icon( child: Icon(
Icons.thumb_up, Icons.thumb_up,
@ -64,7 +64,7 @@ class ThemePlaster extends SharedTheme {
); );
return createButton( return createButton(
appState, puzzle,
small, small,
i, i,
content, content,

@ -2,8 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'app_state.dart'; import 'core/puzzle_proxy.dart';
import 'core/puzzle_animator.dart';
import 'flutter.dart'; import 'flutter.dart';
import 'shared_theme.dart'; import 'shared_theme.dart';
import 'widgets/decoration_image_plus.dart'; import 'widgets/decoration_image_plus.dart';
@ -36,8 +35,7 @@ class ThemeSeattle extends SharedTheme {
puzzle.solved ? const EdgeInsets.all(1) : const EdgeInsets.all(4); puzzle.solved ? const EdgeInsets.all(1) : const EdgeInsets.all(4);
@override @override
Widget tileButton(int i, AppState appState, bool small) { Widget tileButton(int i, PuzzleProxy puzzle, bool small) {
final puzzle = appState.puzzle;
if (i == puzzle.tileCount && !puzzle.solved) { if (i == puzzle.tileCount && !puzzle.solved) {
assert(puzzle.solved); assert(puzzle.solved);
} }
@ -72,6 +70,6 @@ class ThemeSeattle extends SharedTheme {
padding: EdgeInsets.all(small ? 20 : 32), padding: EdgeInsets.all(small ? 20 : 32),
); );
return createButton(appState, small, i, content); return createButton(puzzle, small, i, content);
} }
} }

@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'app_state.dart'; import 'core/puzzle_proxy.dart';
import 'flutter.dart'; import 'flutter.dart';
import 'shared_theme.dart'; import 'shared_theme.dart';
@ -33,9 +33,9 @@ class ThemeSimple extends SharedTheme {
); );
@override @override
Widget tileButton(int i, AppState appState, bool small) { Widget tileButton(int i, PuzzleProxy puzzle, bool small) {
if (i == appState.puzzle.tileCount) { if (i == puzzle.tileCount) {
assert(appState.puzzle.solved); assert(puzzle.solved);
return const Center( return const Center(
child: Icon( child: Icon(
Icons.thumb_up, Icons.thumb_up,
@ -45,7 +45,7 @@ class ThemeSimple extends SharedTheme {
); );
} }
final correctPosition = appState.puzzle.isCorrectPosition(i); final correctPosition = puzzle.isCorrectPosition(i);
final content = createInk( final content = createInk(
Center( Center(
@ -61,7 +61,7 @@ class ThemeSimple extends SharedTheme {
); );
return createButton( return createButton(
appState, puzzle,
small, small,
i, i,
content, content,

Loading…
Cancel
Save