From 11aca7599c1d91a1ae5f55f13bda34f35de1168c Mon Sep 17 00:00:00 2001 From: fuwa Date: Tue, 25 Jun 2019 19:42:30 +0800 Subject: [PATCH] monitor app lifecycle state --- cyberwow/lib/controller/rpc.dart | 1 + cyberwow/lib/main.dart | 23 +++++++++++++++++++-- cyberwow/lib/state.dart | 35 ++++++++++++++++++-------------- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/cyberwow/lib/controller/rpc.dart b/cyberwow/lib/controller/rpc.dart index 4e61723..1bc15ea 100644 --- a/cyberwow/lib/controller/rpc.dart +++ b/cyberwow/lib/controller/rpc.dart @@ -59,6 +59,7 @@ Future rpc(String method) async { } Future syncInfo() async { + print('rpc sync_info'); return rpc('sync_info'); } diff --git a/cyberwow/lib/main.dart b/cyberwow/lib/main.dart index dc360e8..da39ac3 100644 --- a/cyberwow/lib/main.dart +++ b/cyberwow/lib/main.dart @@ -60,12 +60,25 @@ class MyHomePage extends StatefulWidget { _MyHomePageState createState() => _MyHomePageState(); } -class _MyHomePageState extends State +class _MyHomePageState extends State with WidgetsBindingObserver { int _counter = 0; // AppState _state = LoadingState("init..."); AppState _state; + AppLifecycleState _notification = AppLifecycleState.resumed; + + @override + void didChangeAppLifecycleState(AppLifecycleState state) { + // print('app cycle: ${state}'); + setState(() { _notification = state; }); + } + + @override + void dispose() { + WidgetsBinding.instance.removeObserver(this); + super.dispose(); + } void _setState(AppState newState) { setState @@ -74,6 +87,10 @@ class _MyHomePageState extends State ); } + AppLifecycleState _getNotification() { + return _notification; + } + AppState _getState() { return _state; } @@ -117,9 +134,11 @@ class _MyHomePageState extends State super.initState(); print("MyHomePageState initState"); + WidgetsBinding.instance.addObserver(this); + SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.bottom]); - BlankState _blankState = BlankState(_setState); + BlankState _blankState = BlankState(_setState, _getNotification); _state = _blankState; buildStateMachine(_blankState); diff --git a/cyberwow/lib/state.dart b/cyberwow/lib/state.dart index 55e4630..5a94736 100644 --- a/cyberwow/lib/state.dart +++ b/cyberwow/lib/state.dart @@ -62,17 +62,19 @@ abstract class AppState { } typedef SetStateFunc = void Function(AppState); +typedef GetNotificationFunc = AppLifecycleState Function(); class HookedState extends AppState { final SetStateFunc setState; - HookedState(this.setState); + final GetNotificationFunc getNotification; + HookedState(this.setState, this.getNotification); } class BlankState extends HookedState { - BlankState(f) : super (f); + BlankState(f, s) : super (f, s); Future next(String status) async { - LoadingState _next = LoadingState(setState, status); + LoadingState _next = LoadingState(setState, getNotification, status); setState(_next); return _next; @@ -83,7 +85,7 @@ class LoadingState extends HookedState { String banner; String status = ''; - LoadingState(f, this.banner) : super (f); + LoadingState(f, s, this.banner) : super (f, s); void append(String msg) { this.status += msg; @@ -123,7 +125,7 @@ class LoadingState extends HookedState { await Future.wait([load(), showBanner()]); } - SyncingState _next = SyncingState(setState, status); + SyncingState _next = SyncingState(setState, getNotification, status); setState(_next); return _next; } @@ -132,7 +134,7 @@ class LoadingState extends HookedState { class SyncingState extends HookedState { String stdout; - SyncingState(f, this.stdout) : super (f); + SyncingState(f, s, this.stdout) : super (f, s); void append(String msg) { this.stdout += msg; @@ -148,12 +150,14 @@ class SyncingState extends HookedState { final _targetHeight = await rpc.targetHeight(); final _height = await rpc.height(); + print('syncing: target height ${_targetHeight}'); + print('syncing: height ${_height}'); if (_targetHeight == 0 && _height > minimumHeight) break; } - SyncedState _next = SyncedState(setState, stdout, processOutput); + SyncedState _next = SyncedState(setState, getNotification, stdout, processOutput); _next.height = await rpc.height(); setState(_next); return _next; @@ -165,23 +169,24 @@ class SyncedState extends HookedState { int height; Stream processOutput; - SyncedState(f, this.stdout, this.processOutput) : super (f); + SyncedState(f, s, this.stdout, this.processOutput) : super (f, s); Future next() async { print("Synced next"); while (true) { - final _targetHeight = await rpc.targetHeight(); - if (_targetHeight > 0) break; - height = await rpc.height(); + if (getNotification() == AppLifecycleState.resumed) { + final _targetHeight = await rpc.targetHeight(); + if (_targetHeight > 0) break; + height = await rpc.height(); + } await Future.delayed(const Duration(seconds: 2), () => "1"); } // print('synced: loop exit'); - - ReSyncingState _next = ReSyncingState(setState, stdout, processOutput); + ReSyncingState _next = ReSyncingState(setState, getNotification, stdout, processOutput); setState(_next); return _next; } @@ -192,7 +197,7 @@ class ReSyncingState extends HookedState { String stdout; Stream processOutput; - ReSyncingState(f, this.stdout, this.processOutput) : super (f); + ReSyncingState(f, s, this.stdout, this.processOutput) : super (f, s); void append(String msg) { this.stdout += msg; @@ -212,7 +217,7 @@ class ReSyncingState extends HookedState { } print('resync: await exit'); - SyncedState _next = SyncedState(setState, stdout, processOutput); + SyncedState _next = SyncedState(setState, getNotification, stdout, processOutput); _next.height = await rpc.height(); setState(_next); return _next;