Move sync to settings

This commit is contained in:
Aaron Fischer 2019-07-25 16:44:51 +02:00
parent 90587cd30a
commit 761717d0ab
5 changed files with 113 additions and 72 deletions

View file

@ -169,7 +169,8 @@ class GitHubAdapter extends ChangeNotifier {
_rosters.add(roster); _rosters.add(roster);
//https://github.com/lesnitsky/flutter_localstorage/blob/master/example/lib/main.dart //https://github.com/lesnitsky/flutter_localstorage/blob/master/example/lib/main.dart
storage.setItem(player['player'], roster); // FIXME: store it correctly
//storage.setItem(player['player'], roster);
notifyListeners(); notifyListeners();
} }
} catch (e) { } catch (e) {
@ -188,7 +189,7 @@ class GitHubAdapter extends ChangeNotifier {
_lastSync = DateTime.now(); _lastSync = DateTime.now();
_syncinProgress = false; _syncinProgress = false;
storage.setItem('lastSync', _lastSync); storage.setItem('lastSync', _lastSync.toIso8601String());
notifyListeners(); notifyListeners();
} }

View file

@ -176,17 +176,28 @@ class WarbandRoster {
@JsonKey(ignore: true) @JsonKey(ignore: true)
String race; String race;
@JsonKey(name: 'active', defaultValue: true)
bool active; bool active;
@JsonKey(name: 'campaign') @JsonKey(name: 'campaign', defaultValue: 0)
final int campaignPoints; final int campaignPoints;
final String objective; final String objective;
final String alignment; final String alignment;
@JsonKey(defaultValue: '')
final String achievments; final String achievments;
@JsonKey(defaultValue: 0)
final int gc; final int gc;
@JsonKey(defaultValue: 0)
final int shards; final int shards;
@JsonKey(defaultValue: '')
final String equipment; final String equipment;
final List<Hero> heros; final List<Hero> heros;
@JsonKey(name: 'henchmen') @JsonKey(name: 'henchmen')

View file

@ -11,7 +11,8 @@ HenchmenGroup _$HenchmenGroupFromJson(Map json) {
HenchmenGroup._henchmenHeaderFromJson(json['group'] as String), HenchmenGroup._henchmenHeaderFromJson(json['group'] as String),
Unit._statsFromJson(json['stats'] as String), Unit._statsFromJson(json['stats'] as String),
Unit._splitListFromJson(json['weapons'] as String), Unit._splitListFromJson(json['weapons'] as String),
Unit._splitListFromJson(json['amour'] as String)); Unit._splitListFromJson(json['amour'] as String),
);
} }
Hero _$HeroFromJson(Map json) { Hero _$HeroFromJson(Map json) {
@ -22,24 +23,25 @@ Hero _$HeroFromJson(Map json) {
Unit._splitListFromJson(json['amour'] as String), Unit._splitListFromJson(json['amour'] as String),
Unit._splitListFromJson(json['rules'] as String), Unit._splitListFromJson(json['rules'] as String),
json['warbandaddition'] as int, json['warbandaddition'] as int,
Hero._heroHeaderFromJson(json['hero'] as String)); Hero._heroHeaderFromJson(json['hero'] as String),
);
} }
WarbandRoster _$WarbandRosterFromJson(Map json) { WarbandRoster _$WarbandRosterFromJson(Map json) {
return WarbandRoster( return WarbandRoster(
WarbandRoster._warbandNameAndRace(json['warband'] as String), WarbandRoster._warbandNameAndRace(json['warband'] as String),
json['campaign'] as int, json['campaign'] as int ?? 0,
json['objective'] as String, json['objective'] as String,
json['alignment'] as String, json['alignment'] as String,
json['gc'] as int, json['gc'] as int ?? 0,
json['shards'] as int, json['shards'] as int ?? 0,
json['equipment'] as String, json['equipment'] as String ?? '',
json['achievments'] as String, json['achievments'] as String ?? '',
(json['heros'] as List) (json['heros'] as List)
?.map((e) => e == null ? null : Hero.fromJson(e)) ?.map((e) => e == null ? null : Hero.fromJson(e))
?.toList(), ?.toList(),
(json['henchmen'] as List) (json['henchmen'] as List)
?.map((e) => e == null ? null : HenchmenGroup.fromJson(e)) ?.map((e) => e == null ? null : HenchmenGroup.fromJson(e))
?.toList()) ?.toList(),
..active = json['active'] as bool; )..active = json['active'] as bool ?? true;
} }

View file

@ -1,12 +1,15 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:preferences/preferences.dart'; import 'package:preferences/preferences.dart';
import 'package:provider/provider.dart';
import 'package:toolheim/data/github_adapter.dart';
// TODO: Add the search button here
// TODO: Display possible errors here // TODO: Display possible errors here
class SettingsScreen extends StatelessWidget { class SettingsScreen extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
GitHubAdapter github = Provider.of<GitHubAdapter>(context);
return Scaffold( return Scaffold(
appBar: AppBar(title: Text('Settings')), appBar: AppBar(title: Text('Settings')),
body: PreferencePage([ body: PreferencePage([
@ -16,10 +19,69 @@ class SettingsScreen extends StatelessWidget {
TextFieldPreference('Repository', 'repository'), TextFieldPreference('Repository', 'repository'),
PreferenceText( PreferenceText(
'If your warband folders are placed in a subfolder, you can specify it here.'), 'If your warband folders are placed in a subfolder, you can specify it here.'),
TextFieldPreference('Path', 'path', defaultVal: '/') TextFieldPreference('Path', 'path', defaultVal: '/'),
PreferenceTitle('Search for Warbands'),
PreferenceText(
'Search the given GitHub repository for valid Warband files (ending with .warband.yml). This step can be done at any time.'),
FlatButton(
onPressed: () {
github.search();
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Search Warbands ...'),
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
'Checking the GitHub repository for suitable files. This can take a while.'),
SizedBox(
height: 50,
),
Visibility(
child: CircularProgressIndicator(),
visible: github.isSyncInProgress),
Visibility(
child: buildSyncErrors(context),
visible: github.syncErrors.length > 0,
)
]),
actions: <Widget>[
Visibility(
visible: !github.isSyncInProgress,
child: FlatButton(
child: Text('Close',
style: TextStyle(color: Colors.blue)),
onPressed: () {
if (github.syncErrors.length > 0) {
Navigator.pop(context);
} else {
Navigator.popAndPushNamed(context, '/');
}
},
),
)
]);
});
},
child:
Text('Start search', style: TextStyle(color: Colors.blue))),
])); ]));
//String _repository = 'Labernator/Mordheim'; //String _repository = 'Labernator/Mordheim';
//String _path = 'Mordheim-BorderTownBurning/Warband Rosters'; //String _path = 'Mordheim-BorderTownBurning/Warband Rosters';
} }
Widget buildSyncErrors(BuildContext context) {
List<Widget> syncErrors = new List();
GitHubAdapter github = Provider.of<GitHubAdapter>(context);
// TODO: Make it pretty
github.syncErrors.forEach((error) {
syncErrors.add(Text(error, style: TextStyle(color: Colors.red)));
});
return Column(children: syncErrors);
}
} }

View file

@ -37,33 +37,16 @@ class WarbandDrawerWidget extends StatelessWidget {
padding: const EdgeInsets.only(top: 100, left: 30, right: 30), padding: const EdgeInsets.only(top: 100, left: 30, right: 30),
child: Column(children: <Widget>[ child: Column(children: <Widget>[
Text( Text(
'The repository is set. You can now search for warbands. This can take a while.'), 'The repository is set, but no warbands are found. Try to search for warbands on the settings screen.'),
FlatButton(
onPressed: () {
github.search();
},
child: Text(
'Search for warbands',
style: TextStyle(color: Colors.blue),
),
),
FlatButton( FlatButton(
onPressed: () { onPressed: () {
Navigator.popAndPushNamed(context, '/settings'); Navigator.popAndPushNamed(context, '/settings');
}, },
child: Text( child: Text(
'Change settings', 'Open Settings',
style: TextStyle(color: Colors.blue), style: TextStyle(color: Colors.blue),
), ),
), ),
Visibility(
visible: github.isSyncInProgress,
child: CircularProgressIndicator(),
),
Visibility(
visible: github.syncErrors.length > 0,
child: buildSyncErrors(context),
)
]), ]),
); );
} }
@ -71,18 +54,6 @@ class WarbandDrawerWidget extends StatelessWidget {
return buildRosterList(context); return buildRosterList(context);
} }
Widget buildSyncErrors(BuildContext context) {
List<Widget> syncErrors = new List();
GitHubAdapter github = Provider.of<GitHubAdapter>(context);
// TODO: Make it pretty
github.syncErrors.forEach((error) {
syncErrors.add(Text(error, style: TextStyle(color: Colors.red)));
});
return Column(children: syncErrors);
}
Widget buildRosterList(BuildContext context) { Widget buildRosterList(BuildContext context) {
GitHubAdapter github = Provider.of<GitHubAdapter>(context); GitHubAdapter github = Provider.of<GitHubAdapter>(context);
WarbandRoster activeroster = github.activeRoster(); WarbandRoster activeroster = github.activeRoster();
@ -96,18 +67,12 @@ class WarbandDrawerWidget extends StatelessWidget {
// Show some stats for the own warband // Show some stats for the own warband
tiles.add(UserAccountsDrawerHeader( tiles.add(UserAccountsDrawerHeader(
otherAccountsPictures: <Widget>[ otherAccountsPictures: <Widget>[
//IconButton(
// icon: Icon(Icons.refresh),
// color: Colors.white,
// highlightColor: Colors.brown,
// tooltip: 'Refresh warbands',
// onPressed: github.update,
//),
IconButton( IconButton(
icon: Icon(Icons.search), icon: Icon(Icons.refresh),
color: Colors.white, color: Colors.white,
tooltip: 'Read warbands', highlightColor: Colors.brown,
onPressed: github.search, tooltip: 'Refresh warbands',
onPressed: github.update,
), ),
IconButton( IconButton(
icon: Icon(Icons.settings), icon: Icon(Icons.settings),