diff --git a/cyberwow/lib/controller/helper.dart b/cyberwow/lib/controller/helper.dart index b1fb060..2ca3808 100644 --- a/cyberwow/lib/controller/helper.dart +++ b/cyberwow/lib/controller/helper.dart @@ -59,7 +59,7 @@ Future targetHeight() async { body: body ); - print('Response status: ${response.statusCode}'); + // print('Response status: ${response.statusCode}'); if (response.statusCode != 200) { return -1; } else { diff --git a/cyberwow/lib/main.dart b/cyberwow/lib/main.dart index 82edc32..b2be5b7 100644 --- a/cyberwow/lib/main.dart +++ b/cyberwow/lib/main.dart @@ -33,6 +33,7 @@ import 'widget/loading.dart'; import 'widget/blank.dart'; import 'widget/syncing.dart'; import 'widget/synced.dart'; +import 'widget/resyncing.dart'; void main() => runApp(MyApp()); @@ -73,6 +74,11 @@ class _MyHomePageState extends State ); } + AppState _getState() { + return _state; + } + + void _updateLoading(LoadingState state, String msg) { print('updateLoading: ' + msg); } @@ -88,10 +94,21 @@ class _MyHomePageState extends State SyncingState _syncingState = await _loadingState.next(loading, ''); - final syncing = runBinary(binName); - SyncedState _syncedState = await _syncingState.next(syncing); + final syncing = runBinary(binName).asBroadcastStream(); + SyncedState _syncedState = await _syncingState.next(syncing); await _syncedState.next(); + + while (true) { + await _getState().use + ( + (s) => () => 0, + (s) => () => 0, + (s) => () => 0, + (s) => s.next(), + (s) => s.next(), + ); + } } @override @@ -123,6 +140,7 @@ class _MyHomePageState extends State (s) => buildLoading(context, s), (s) => buildSyncing(context, s), (s) => buildSynced(context, s), + (s) => buildReSyncing(context, s), ), ); } diff --git a/cyberwow/lib/state.dart b/cyberwow/lib/state.dart index f77350c..9fc83b1 100644 --- a/cyberwow/lib/state.dart +++ b/cyberwow/lib/state.dart @@ -38,6 +38,7 @@ abstract class AppState { T Function(LoadingState) useLoadingState, T Function(SyncingState) useSyncingState, T Function(SyncedState) useSyncedState, + T Function(ReSyncingState) useReSyncingState, ) { if (this is BlankState) { @@ -52,6 +53,9 @@ abstract class AppState { if (this is SyncedState) { return useSyncedState(this); } + if (this is ReSyncingState) { + return useReSyncingState(this); + } throw Exception('Invalid state'); } } @@ -103,7 +107,7 @@ class LoadingState extends HookedState { } Future load() async { - print("LoadingState.next"); + print("Loading next"); await for (var line in loadingProgress) { // append(line); print(line); @@ -125,48 +129,87 @@ class LoadingState extends HookedState { } class SyncingState extends HookedState { - String status; + String stdout; - SyncingState(f, this.status) : super (f); + SyncingState(f, this.stdout) : super (f); void append(String msg) { - this.status += msg; + this.stdout += msg; setState(this); } Future next(Stream processOutput) async { - print("SyncingState.next"); + print("Syncing next"); await for (var line in processOutput) { append(line); print(line); - // print('hi'); final _targetHeight = await targetHeight(); - print('target_height: ${_targetHeight}'); if (_targetHeight == 0) break; } - SyncedState _next = SyncedState(setState, processOutput); + + SyncedState _next = SyncedState(setState, stdout, processOutput); setState(_next); return _next; } } class SyncedState extends HookedState { + String stdout; Stream processOutput; - SyncedState(f, this.processOutput) : super (f); + SyncedState(f, this.stdout, this.processOutput) : super (f); - Future next() async { - print("SyncedState.next"); + Future next() async { + print("Synced next"); while (true) { final _targetHeight = await targetHeight(); - print('target_height: ${_targetHeight}'); + if (_targetHeight != 0) break; await Future.delayed(const Duration(seconds: 2), () => "1"); } + + // print('synced: loop exit'); + + + ReSyncingState _next = ReSyncingState(setState, stdout, processOutput); + setState(_next); + return _next; + } +} + + +class ReSyncingState extends HookedState { + String stdout; + Stream processOutput; + + ReSyncingState(f, this.stdout, this.processOutput) : super (f); + + void append(String msg) { + this.stdout += msg; + setState(this); + } + + Future next() async { + print("ReSyncing next"); + + await for (var line in processOutput) { + append(line); + print(line); + + final _targetHeight = await targetHeight(); + + if (_targetHeight == 0) break; + } + + print('resync: await exit'); + + SyncedState _next = SyncedState(setState, stdout, processOutput); + setState(_next); + return _next; } } diff --git a/cyberwow/lib/widget/resyncing.dart b/cyberwow/lib/widget/resyncing.dart new file mode 100644 index 0000000..126f7e7 --- /dev/null +++ b/cyberwow/lib/widget/resyncing.dart @@ -0,0 +1,71 @@ +/* + +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 'package:flutter/material.dart'; + +import '../state.dart'; +import '../config.dart'; + +Widget buildReSyncing(BuildContext context, ReSyncingState state) { + return Scaffold + ( + // appBar: AppBar + // ( + // // title: Text(widget.title), + // title: Text('CyberWOW'), + // ), + body: new Container + ( + // padding: const EdgeInsets.all(10.0), + color: config.backgroundColor, + child: Align + ( + alignment: Alignment.topLeft, + child: Column + ( + mainAxisAlignment: MainAxisAlignment.center, + children: + [ + new Expanded + ( + flex: 1, + child: new SingleChildScrollView + ( + scrollDirection: Axis.vertical, + reverse: true, + child: Text + ( + state.stdout, + style: TextStyle + ( + fontFamily: 'RobotoMono', + fontSize: 11, + color: config.textColor, + ), + ) + ) + ) + ], + ), + ), + ), + ); +} diff --git a/cyberwow/lib/widget/syncing.dart b/cyberwow/lib/widget/syncing.dart index 439d749..eab269c 100644 --- a/cyberwow/lib/widget/syncing.dart +++ b/cyberwow/lib/widget/syncing.dart @@ -53,7 +53,7 @@ Widget buildSyncing(BuildContext context, SyncingState state) { reverse: true, child: Text ( - state.status, + state.stdout, style: TextStyle ( fontFamily: 'RobotoMono', diff --git a/cyberwow/native/hello.c b/cyberwow/native/hello.c index 569b40a..c36a60a 100644 --- a/cyberwow/native/hello.c +++ b/cyberwow/native/hello.c @@ -13,7 +13,7 @@ int main() i++; sleep(1); - if (i > 3) break; + if (i > 20) break; } return 0; }