diff --git a/cyberwow/lib/main.dart b/cyberwow/lib/main.dart index 19c8a3c..cc8d33e 100644 --- a/cyberwow/lib/main.dart +++ b/cyberwow/lib/main.dart @@ -177,7 +177,8 @@ class _CyberWOW_PageState extends State with WidgetsBindingObserv WidgetsBinding.instance.addObserver(this); - final BlankState _blankState = BlankState(_setState, _getNotification, _isExiting); + final AppHook _appHook = AppHook(_setState, _getNotification, _isExiting); + final BlankState _blankState = BlankState(_appHook); _state = _blankState; buildStateMachine(_blankState); diff --git a/cyberwow/lib/state.dart b/cyberwow/lib/state.dart index bb347d9..1cf6894 100644 --- a/cyberwow/lib/state.dart +++ b/cyberwow/lib/state.dart @@ -77,27 +77,32 @@ typedef SetStateFunc = void Function(AppState); typedef GetNotificationFunc = AppLifecycleState Function(); typedef IsExitingFunc = bool Function(); -class HookedState extends AppState { +class AppHook { final SetStateFunc setState; final GetNotificationFunc getNotification; final IsExitingFunc isExiting; - HookedState(this.setState, this.getNotification, this.isExiting); + AppHook(this.setState, this.getNotification, this.isExiting); +} + +class HookedState extends AppState { + final AppHook appHook; + HookedState(this.appHook); syncState() { - setState(this); + appHook.setState(this); } HookedState moveState(HookedState _next) { - setState(_next); + appHook.setState(_next); return _next; } } class BlankState extends HookedState { - BlankState(f1, f2, f3) : super (f1, f2, f3); + BlankState(appHook) : super (appHook); Future next(String status) async { - LoadingState _next = LoadingState(setState, getNotification, isExiting, status); + LoadingState _next = LoadingState(appHook, status); return moveState(_next); } } @@ -106,7 +111,7 @@ class LoadingState extends HookedState { final String banner; String status = ''; - LoadingState(f1, f2, f3, this.banner) : super (f1, f2, f3); + LoadingState(appHook, this.banner) : super (appHook); void append(final String msg) { this.status += msg; @@ -146,7 +151,7 @@ class LoadingState extends HookedState { await Future.wait([load(), showBanner()]); } - SyncingState _next = SyncingState(setState, getNotification, isExiting); + SyncingState _next = SyncingState(appHook); return moveState(_next); } } @@ -156,7 +161,7 @@ class SyncingState extends HookedState { bool synced = false; - SyncingState(f1, f2, f3) : super (f1, f2, f3); + SyncingState(appHook) : super (appHook); void append(final String msg) { stdout.addLast(msg); @@ -183,10 +188,10 @@ class SyncingState extends HookedState { } Future checkSync() async { - await for (final _null in refresh.pull(getNotification, 'syncingState')) { + await for (final _null in refresh.pull(appHook.getNotification, 'syncingState')) { log.finer('SyncingState: checkSync loop'); - if (isExiting()) { + if (appHook.isExiting()) { log.fine('Syncing state detected exiting'); break; } @@ -206,10 +211,10 @@ class SyncingState extends HookedState { printStdout(); await checkSync(); - if (isExiting()) { + if (appHook.isExiting()) { ExitingState _next = ExitingState ( - setState, getNotification, isExiting, stdout, processOutput + appHook, stdout, processOutput ); return moveState(_next); } @@ -221,7 +226,7 @@ class SyncingState extends HookedState { final _height = await rpc.height(); SyncedState _next = SyncedState ( - setState, getNotification, isExiting, stdout, processInput, processOutput, 1, + appHook, stdout, processInput, processOutput, 1, ); _next.height = _height; return moveState(_next); @@ -249,8 +254,8 @@ class SyncedState extends HookedState { String getConnectionsCache = ''; String getTransactionPoolCache = ''; - SyncedState(f1, f2, f3, this.stdout, this.processInput, this.processOutput, this.pageIndex) - : super (f1, f2, f3) { + SyncedState(appHook, this.stdout, this.processInput, this.processOutput, this.pageIndex) + : super (appHook) { pageController = PageController( initialPage: pageIndex ); } @@ -292,8 +297,8 @@ class SyncedState extends HookedState { logStdout(); Future checkSync() async { - await for (final _null in refresh.pull(getNotification, 'syncedState')) { - if (isExiting() || userExit) { + await for (final _null in refresh.pull(appHook.getNotification, 'syncedState')) { + if (appHook.isExiting() || userExit) { log.fine('Synced state detected exiting'); break; } @@ -329,10 +334,10 @@ class SyncedState extends HookedState { await checkSync(); - if (isExiting() || userExit) { + if (appHook.isExiting() || userExit) { ExitingState _next = ExitingState ( - setState, getNotification, isExiting, stdout, processOutput + appHook, stdout, processOutput ); return moveState(_next); } @@ -341,7 +346,7 @@ class SyncedState extends HookedState { ReSyncingState _next = ReSyncingState ( - setState, getNotification, isExiting, stdout, processInput, processOutput, pageIndex + appHook, stdout, processInput, processOutput, pageIndex ); return moveState(_next); } @@ -356,8 +361,8 @@ class ReSyncingState extends HookedState { bool synced = false; - ReSyncingState(f1, f2, f3, this.stdout, this.processInput, this.processOutput, this.pageIndex) - : super (f1, f2, f3); + ReSyncingState(appHook, this.stdout, this.processInput, this.processOutput, this.pageIndex) + : super (appHook); void append(final String msg) { stdout.addLast(msg); @@ -380,8 +385,8 @@ class ReSyncingState extends HookedState { } Future checkSync() async { - await for (final _null in refresh.pull(getNotification, 'ReSyncingState')) { - if (isExiting()) { + await for (final _null in refresh.pull(appHook.getNotification, 'ReSyncingState')) { + if (appHook.isExiting()) { log.fine('ReSyncing state detected exiting'); break; } @@ -397,10 +402,10 @@ class ReSyncingState extends HookedState { printStdout(); await checkSync(); - if (isExiting()) { + if (appHook.isExiting()) { ExitingState _next = ExitingState ( - setState, getNotification, isExiting, stdout, processOutput + appHook, stdout, processOutput ); return moveState(_next); } @@ -408,7 +413,7 @@ class ReSyncingState extends HookedState { log.fine('resync: await exit'); SyncedState _next = SyncedState ( - setState, getNotification, isExiting, stdout, processInput, processOutput, pageIndex + appHook, stdout, processInput, processOutput, pageIndex ); _next.height = await rpc.height(); return moveState(_next); @@ -419,7 +424,7 @@ class ExitingState extends HookedState { final Queue stdout; final Stream processOutput; - ExitingState(f1, f2, f3, this.stdout, this.processOutput) : super (f1, f2, f3); + ExitingState(appHook, this.stdout, this.processOutput) : super (appHook); void append(final String msg) { stdout.addLast(msg);