monitor app lifecycle state

master
fuwa 5 years ago
parent 25efd122ec
commit 11aca7599c

@ -59,6 +59,7 @@ Future<http.Response> rpc(String method) async {
} }
Future<http.Response> syncInfo() async { Future<http.Response> syncInfo() async {
print('rpc sync_info');
return rpc('sync_info'); return rpc('sync_info');
} }

@ -60,12 +60,25 @@ class MyHomePage extends StatefulWidget {
_MyHomePageState createState() => _MyHomePageState(); _MyHomePageState createState() => _MyHomePageState();
} }
class _MyHomePageState extends State<MyHomePage> class _MyHomePageState extends State<MyHomePage> with WidgetsBindingObserver
{ {
int _counter = 0; int _counter = 0;
// AppState _state = LoadingState("init..."); // AppState _state = LoadingState("init...");
AppState _state; 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) { void _setState(AppState newState) {
setState setState
@ -74,6 +87,10 @@ class _MyHomePageState extends State<MyHomePage>
); );
} }
AppLifecycleState _getNotification() {
return _notification;
}
AppState _getState() { AppState _getState() {
return _state; return _state;
} }
@ -117,9 +134,11 @@ class _MyHomePageState extends State<MyHomePage>
super.initState(); super.initState();
print("MyHomePageState initState"); print("MyHomePageState initState");
WidgetsBinding.instance.addObserver(this);
SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.bottom]); SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.bottom]);
BlankState _blankState = BlankState(_setState); BlankState _blankState = BlankState(_setState, _getNotification);
_state = _blankState; _state = _blankState;
buildStateMachine(_blankState); buildStateMachine(_blankState);

@ -62,17 +62,19 @@ abstract class AppState {
} }
typedef SetStateFunc = void Function(AppState); typedef SetStateFunc = void Function(AppState);
typedef GetNotificationFunc = AppLifecycleState Function();
class HookedState extends AppState { class HookedState extends AppState {
final SetStateFunc setState; final SetStateFunc setState;
HookedState(this.setState); final GetNotificationFunc getNotification;
HookedState(this.setState, this.getNotification);
} }
class BlankState extends HookedState { class BlankState extends HookedState {
BlankState(f) : super (f); BlankState(f, s) : super (f, s);
Future<LoadingState> next(String status) async { Future<LoadingState> next(String status) async {
LoadingState _next = LoadingState(setState, status); LoadingState _next = LoadingState(setState, getNotification, status);
setState(_next); setState(_next);
return _next; return _next;
@ -83,7 +85,7 @@ class LoadingState extends HookedState {
String banner; String banner;
String status = ''; String status = '';
LoadingState(f, this.banner) : super (f); LoadingState(f, s, this.banner) : super (f, s);
void append(String msg) { void append(String msg) {
this.status += msg; this.status += msg;
@ -123,7 +125,7 @@ class LoadingState extends HookedState {
await Future.wait([load(), showBanner()]); await Future.wait([load(), showBanner()]);
} }
SyncingState _next = SyncingState(setState, status); SyncingState _next = SyncingState(setState, getNotification, status);
setState(_next); setState(_next);
return _next; return _next;
} }
@ -132,7 +134,7 @@ class LoadingState extends HookedState {
class SyncingState extends HookedState { class SyncingState extends HookedState {
String stdout; String stdout;
SyncingState(f, this.stdout) : super (f); SyncingState(f, s, this.stdout) : super (f, s);
void append(String msg) { void append(String msg) {
this.stdout += msg; this.stdout += msg;
@ -148,12 +150,14 @@ class SyncingState extends HookedState {
final _targetHeight = await rpc.targetHeight(); final _targetHeight = await rpc.targetHeight();
final _height = await rpc.height(); final _height = await rpc.height();
print('syncing: target height ${_targetHeight}');
print('syncing: height ${_height}');
if (_targetHeight == 0 && _height > minimumHeight) break; if (_targetHeight == 0 && _height > minimumHeight) break;
} }
SyncedState _next = SyncedState(setState, stdout, processOutput); SyncedState _next = SyncedState(setState, getNotification, stdout, processOutput);
_next.height = await rpc.height(); _next.height = await rpc.height();
setState(_next); setState(_next);
return _next; return _next;
@ -165,23 +169,24 @@ class SyncedState extends HookedState {
int height; int height;
Stream<String> processOutput; Stream<String> processOutput;
SyncedState(f, this.stdout, this.processOutput) : super (f); SyncedState(f, s, this.stdout, this.processOutput) : super (f, s);
Future<ReSyncingState> next() async { Future<ReSyncingState> next() async {
print("Synced next"); print("Synced next");
while (true) { while (true) {
final _targetHeight = await rpc.targetHeight(); if (getNotification() == AppLifecycleState.resumed) {
if (_targetHeight > 0) break; final _targetHeight = await rpc.targetHeight();
height = await rpc.height(); if (_targetHeight > 0) break;
height = await rpc.height();
}
await Future.delayed(const Duration(seconds: 2), () => "1"); await Future.delayed(const Duration(seconds: 2), () => "1");
} }
// print('synced: loop exit'); // print('synced: loop exit');
ReSyncingState _next = ReSyncingState(setState, getNotification, stdout, processOutput);
ReSyncingState _next = ReSyncingState(setState, stdout, processOutput);
setState(_next); setState(_next);
return _next; return _next;
} }
@ -192,7 +197,7 @@ class ReSyncingState extends HookedState {
String stdout; String stdout;
Stream<String> processOutput; Stream<String> processOutput;
ReSyncingState(f, this.stdout, this.processOutput) : super (f); ReSyncingState(f, s, this.stdout, this.processOutput) : super (f, s);
void append(String msg) { void append(String msg) {
this.stdout += msg; this.stdout += msg;
@ -212,7 +217,7 @@ class ReSyncingState extends HookedState {
} }
print('resync: await exit'); print('resync: await exit');
SyncedState _next = SyncedState(setState, stdout, processOutput); SyncedState _next = SyncedState(setState, getNotification, stdout, processOutput);
_next.height = await rpc.height(); _next.height = await rpc.height();
setState(_next); setState(_next);
return _next; return _next;

Loading…
Cancel
Save