You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
cyberwow/cyberwow/lib/main.dart

180 lines
4.5 KiB

5 years ago
/*
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 <https://www.gnu.org/licenses/>.
*/
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';
5 years ago
import 'dart:io';
import 'dart:async';
import 'state.dart';
import 'config.dart';
import 'controller/loading.dart';
import 'controller/syncing.dart';
5 years ago
import 'widget/loading.dart';
import 'widget/blank.dart';
import 'widget/syncing.dart';
5 years ago
import 'widget/synced.dart';
import 'widget/resyncing.dart';
5 years ago
void main() {
Logger.root.level = kReleaseMode ? Level.INFO : Level.FINE;
Logger.root.onRecord.listen((LogRecord rec) {
print('${rec.level.name}: ${rec.time}: ${rec.message}');
});
runApp(CyberWOW_App());
}
5 years ago
class CyberWOW_App extends StatelessWidget {
5 years ago
@override
Widget build(BuildContext context) {
return MaterialApp
(
5 years ago
title: 'CyberWOW',
5 years ago
theme: ThemeData
(
primarySwatch: Colors.purple,
),
home: CyberWOW_Page(title: 'CyberWOW'),
5 years ago
);
}
}
class CyberWOW_Page extends StatefulWidget {
CyberWOW_Page({Key key, this.title}) : super(key: key);
5 years ago
final String title;
@override
_CyberWOW_PageState createState() => _CyberWOW_PageState();
5 years ago
}
class _CyberWOW_PageState extends State<CyberWOW_Page> with WidgetsBindingObserver
5 years ago
{
int _counter = 0;
// AppState _state = LoadingState("init...");
AppState _state;
AppLifecycleState _notification = AppLifecycleState.resumed;
5 years ago
final syncedPageController = PageController(
initialPage: 0,
);
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
log.fine('app cycle: ${state}');
setState(() { _notification = state; });
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
5 years ago
void _setState(AppState newState) {
setState
(
() => _state = newState
);
}
AppLifecycleState _getNotification() {
return _notification;
}
AppState _getState() {
return _state;
}
5 years ago
void _updateLoading(LoadingState state, String msg) {
log.fine('updateLoading: ' + msg);
5 years ago
}
Future<void> buildStateMachine(BlankState _blankState) async {
final loadingText = config.splash;
5 years ago
LoadingState _loadingState = await _blankState.next(loadingText);
final binName = config.outputBin;
final resourcePath = 'native/output/' + arch + '/' + binName;
5 years ago
final bundle = DefaultAssetBundle.of(context);
final loading = deployBinary(bundle, resourcePath, binName);
SyncingState _syncingState = await _loadingState.next(loading, '');
5 years ago
final syncing = runBinary(binName).asBroadcastStream();
5 years ago
SyncedState _syncedState = await _syncingState.next(syncing);
5 years ago
await _syncedState.next();
var validState = true;
while (validState) {
await _getState().use
(
(s) => () => validState = false,
(s) => () => validState = false,
(s) => () => validState = false,
(s) => s.next(),
(s) => s.next(),
);
}
5 years ago
}
@override
void initState() {
super.initState();
log.fine("CyberWOW_PageState initState");
5 years ago
WidgetsBinding.instance.addObserver(this);
5 years ago
SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.bottom]);
BlankState _blankState = BlankState(_setState, _getNotification);
5 years ago
_state = _blankState;
buildStateMachine(_blankState);
}
Future<bool> _exitApp(BuildContext context) async {
log.info("CyberWOW_PageState _exitApp");
WidgetsBinding.instance.removeObserver(this);
5 years ago
exit(0);
}
@override
Widget build(BuildContext context) {
return new WillPopScope
(
onWillPop: () => _exitApp(context),
child: _state.use
(
(s) => buildBlank(context, s),
(s) => buildLoading(context, s),
(s) => buildSyncing(context, s),
5 years ago
(s) => buildSynced(context, s, syncedPageController),
(s) => buildReSyncing(context, s),
5 years ago
),
);
}
}