From 855e7207e5bff1708d07fb75ee9170accc550d70 Mon Sep 17 00:00:00 2001 From: fuwa Date: Thu, 27 Jun 2019 00:43:31 +0800 Subject: [PATCH] add refresh indicator --- cyberwow/lib/config.dart | 2 + cyberwow/lib/controller/daemon.dart | 45 ++++++++++++++++++++ cyberwow/lib/controller/helper.dart | 1 - cyberwow/lib/controller/refresh.dart | 13 ++++++ cyberwow/lib/controller/rpc.dart | 16 ++++++- cyberwow/lib/state.dart | 63 +++++++++++++++------------- cyberwow/lib/widget/synced.dart | 26 +++++++++++- 7 files changed, 132 insertions(+), 34 deletions(-) create mode 100644 cyberwow/lib/controller/daemon.dart diff --git a/cyberwow/lib/config.dart b/cyberwow/lib/config.dart index 32adf5c..51050dd 100644 --- a/cyberwow/lib/config.dart +++ b/cyberwow/lib/config.dart @@ -27,3 +27,5 @@ const config = cryptoConfig.config; const arch = 'arm64'; // const arch = 'x86_64'; const minimumHeight = 118361; + +const isPC = arch == 'x86_64'; diff --git a/cyberwow/lib/controller/daemon.dart b/cyberwow/lib/controller/daemon.dart new file mode 100644 index 0000000..2b46db4 --- /dev/null +++ b/cyberwow/lib/controller/daemon.dart @@ -0,0 +1,45 @@ +/* + +Copyright 2019 fuwa + +This file is part of CyberWOW. + +CyberWOW is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +CyberWOW is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with CyberWOW. If not, see . + +*/ + +import 'dart:async'; +import 'dart:convert'; + +import 'rpc.dart' as rpc; + +Future isConnected() async { + final _outPeers = await rpc.outgoingConnectionsCount(); + final _inPeers = await rpc.incomingConnectionsCount(); + + // print('_outPeers: ${_outPeers}'); + return _outPeers + _inPeers > 0; +} + +Future isSynced() async { + final _targetHeight = await rpc.targetHeight(); + final _height = await rpc.height(); + return _targetHeight >= 0 && _targetHeight <= _height; +} + +Future isNotSynced() async { + final _targetHeight = await rpc.targetHeight(); + final _height = await rpc.height(); + return _targetHeight > _height; +} diff --git a/cyberwow/lib/controller/helper.dart b/cyberwow/lib/controller/helper.dart index 94fa3a6..e817faa 100644 --- a/cyberwow/lib/controller/helper.dart +++ b/cyberwow/lib/controller/helper.dart @@ -34,4 +34,3 @@ Future binaryExists(String name) async { final binPath = await getBinaryPath(name); return new File(binPath).exists(); } - diff --git a/cyberwow/lib/controller/refresh.dart b/cyberwow/lib/controller/refresh.dart index 53d168f..643ed42 100644 --- a/cyberwow/lib/controller/refresh.dart +++ b/cyberwow/lib/controller/refresh.dart @@ -38,3 +38,16 @@ Stream targetHeight(GetNotificationFunc getNotification) async* { await Future.delayed(const Duration(seconds: 2), () => "1"); } } + +Stream pull(GetNotificationFunc getNotification) async* { + while (true) { + final _appState = getNotification(); + // print('refresh targetHeight: app state: ${_appState}'); + + if (_appState == AppLifecycleState.resumed) { + yield null; + } + + await Future.delayed(const Duration(seconds: 2), () => "1"); + } +} diff --git a/cyberwow/lib/controller/rpc.dart b/cyberwow/lib/controller/rpc.dart index cda4f6f..9741945 100644 --- a/cyberwow/lib/controller/rpc.dart +++ b/cyberwow/lib/controller/rpc.dart @@ -112,7 +112,7 @@ Future offline() async { } } -Future outgoing_connections_count() async { +Future outgoingConnectionsCount() async { var response = await getInfo(); if (response == null) return -1; @@ -125,3 +125,17 @@ Future outgoing_connections_count() async { return responseBody["outgoing_connections_count"]; } } + +Future incomingConnectionsCount() async { + var response = await getInfo(); + + if (response == null) return -1; + + // print('Response status: ${response.statusCode}'); + if (response.statusCode != 200) { + return -1; + } else { + final responseBody = json.decode(response.body)['result']; + return responseBody["incoming_connections_count"]; + } +} diff --git a/cyberwow/lib/state.dart b/cyberwow/lib/state.dart index a86cbc3..07dc851 100644 --- a/cyberwow/lib/state.dart +++ b/cyberwow/lib/state.dart @@ -30,6 +30,7 @@ import 'package:path_provider/path_provider.dart'; import 'controller/helper.dart'; import 'controller/rpc.dart' as rpc; +import 'controller/daemon.dart' as daemon; import 'controller/refresh.dart' as refresh; import 'config.dart'; @@ -159,25 +160,25 @@ class SyncingState extends HookedState { } Future checkSync() async { - await for (var _targetHeight in refresh.targetHeight(getNotification)) { - print('syncing: target height ${_targetHeight}'); + await for (var _null in refresh.pull(getNotification)) { + // print('syncing: target height ${_targetHeight}'); - final _height = await rpc.height(); - // print('syncing: height ${_height}'); - - // final _offline = await rpc.offline(); - // print('syncing: offline ${_offline}'); - - final _out_peers = await rpc.outgoing_connections_count(); - // print('syncing: out_peers ${_out_peers}'); // here doc is wrong, targetHeight could match height when synced // potential bug, targetHeight could be smaller then height - if ((_targetHeight >= 0 && _targetHeight <= _height) && _out_peers > 0) { + final _isConnected = await daemon.isConnected(); + final _isSynced = await daemon.isSynced(); + + if (_isConnected && _isSynced) { synced = true; break; } // print('syncing: checkSync loop'); + + if (isPC) { + synced = true; + break; + } } } @@ -198,6 +199,7 @@ class SyncedState extends HookedState { int height; Stream processOutput; bool synced = true; + bool connected = true; SyncedState(f, s, this.stdout, this.processOutput) : super (f, s); @@ -208,6 +210,13 @@ class SyncedState extends HookedState { } } + void updateConnected(bool c) { + if (connected != c) { + connected = c; + setState(this); + } + } + Future next() async { print("Synced next"); @@ -222,22 +231,20 @@ class SyncedState extends HookedState { logStdout(); - await for (var _targetHeight in refresh.targetHeight(getNotification)) { - // print('re-sync: target height ${_targetHeight}'); - - final _height = await rpc.height(); - // print('re-sync: height ${_height}'); - - if (_targetHeight > _height) { - synced = false; - break; + Future checkSync() async { + await for (var _null in refresh.pull(getNotification)) { + if (await daemon.isNotSynced() && (!isPC)) { + synced = false; + break; + } + // print('synced loop'); + updateHeight(await rpc.height()); + updateConnected(await daemon.isConnected()); } - // print('synced loop'); - updateHeight(await rpc.height()); - // print('synced: targetheight: ${_targetHeight}'); - // print('synced: height: ${height}'); } + await checkSync(); + print('synced: loop exit'); ReSyncingState _next = ReSyncingState(setState, getNotification, stdout, processOutput); @@ -272,13 +279,9 @@ class ReSyncingState extends HookedState { } Future checkSync() async { - await for (var _targetHeight in refresh.targetHeight(getNotification)) { - // print('re-sync: target height ${_targetHeight}'); + await for (var _null in refresh.pull(getNotification)) { - final _height = await rpc.height(); - // print('re-sync: height ${_height}'); - - if (_targetHeight >= 0 && _targetHeight <= _height) { + if (await daemon.isSynced()) { synced = true; break; } diff --git a/cyberwow/lib/widget/synced.dart b/cyberwow/lib/widget/synced.dart index 8c1fabf..99ad6b9 100644 --- a/cyberwow/lib/widget/synced.dart +++ b/cyberwow/lib/widget/synced.dart @@ -39,12 +39,18 @@ Widget buildSynced(BuildContext context, SyncedState state) { mainAxisAlignment: MainAxisAlignment.center, children: [ - Spacer(), + Spacer + ( + flex: 3, + ), Image.asset ('assets/wownero_symbol.png', height: 200, ), - Spacer(), + Spacer + ( + flex: 2, + ), new Expanded ( flex: 1, @@ -63,6 +69,22 @@ Widget buildSynced(BuildContext context, SyncedState state) { ) ) ), + Spacer + ( + flex: 1, + ), + SizedBox + ( + height: 30.0, + width: 30.0, + child: (state.connected) ? + Container() : + CircularProgressIndicator(), + ), + Spacer + ( + flex: 1, + ), ], ), ),