working update, but need more error checking/handling
This commit is contained in:
parent
05ca125f87
commit
82682b3d16
5 changed files with 68 additions and 34 deletions
|
@ -15,7 +15,7 @@ class GitHubAdapter extends ChangeNotifier {
|
||||||
DateTime _lastSync;
|
DateTime _lastSync;
|
||||||
|
|
||||||
List<WarbandRoster> _rosters = [];
|
List<WarbandRoster> _rosters = [];
|
||||||
WarbandRoster _activeRoster;
|
String _activeRosterFilePath;
|
||||||
|
|
||||||
String get repository => PrefService.getString('repository');
|
String get repository => PrefService.getString('repository');
|
||||||
String get path => PrefService.getString('path');
|
String get path => PrefService.getString('path');
|
||||||
|
@ -26,13 +26,23 @@ class GitHubAdapter extends ChangeNotifier {
|
||||||
DateTime get lastSync => _lastSync;
|
DateTime get lastSync => _lastSync;
|
||||||
List<String> get syncErrors => _syncErrors;
|
List<String> get syncErrors => _syncErrors;
|
||||||
|
|
||||||
UnmodifiableListView<WarbandRoster> get rosters =>
|
List<WarbandRoster> get rosters => _rosters;
|
||||||
UnmodifiableListView(_rosters);
|
|
||||||
|
|
||||||
WarbandRoster get activeRoster => _activeRoster;
|
WarbandRoster get activeRoster {
|
||||||
|
if (_rosters.length == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_activeRosterFilePath == null) {
|
||||||
|
return _rosters.first;
|
||||||
|
}
|
||||||
|
return _rosters.firstWhere(
|
||||||
|
(roster) => roster.filePath == _activeRosterFilePath,
|
||||||
|
orElse: () => _rosters.first);
|
||||||
|
}
|
||||||
|
|
||||||
set activeRoster(WarbandRoster roster) {
|
set activeRoster(WarbandRoster roster) {
|
||||||
_activeRoster = roster;
|
_activeRosterFilePath = roster.filePath;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,14 +99,11 @@ class GitHubAdapter extends ChangeNotifier {
|
||||||
if (_syncErrors.length == 0) {
|
if (_syncErrors.length == 0) {
|
||||||
await for (String filePath in warbandFileStream()) {
|
await for (String filePath in warbandFileStream()) {
|
||||||
WarbandRoster roster = await fetchWarband(filePath);
|
WarbandRoster roster = await fetchWarband(filePath);
|
||||||
Version latestVersion = await getLatestCommit(filePath);
|
Version latestVersion = await getLatestVersion(filePath);
|
||||||
|
|
||||||
roster.playerName = getPlayerNameFromFilePath(filePath);
|
roster.playerName = getPlayerNameFromFilePath(filePath);
|
||||||
roster.currentVersion = latestVersion;
|
roster.version = latestVersion;
|
||||||
|
roster.filePath = filePath;
|
||||||
// On a search, we drop all previous information about the warbands,
|
|
||||||
// So, lastSyncVersion is equal to the currentVersion.
|
|
||||||
roster.lastSyncVersion = roster.currentVersion;
|
|
||||||
|
|
||||||
_rosters.add(roster);
|
_rosters.add(roster);
|
||||||
|
|
||||||
|
@ -114,11 +121,6 @@ class GitHubAdapter extends ChangeNotifier {
|
||||||
// Sort by CP
|
// Sort by CP
|
||||||
_rosters.sort((a, b) => b.campaignPoints.compareTo(a.campaignPoints));
|
_rosters.sort((a, b) => b.campaignPoints.compareTo(a.campaignPoints));
|
||||||
|
|
||||||
// Select first as active player if no active player is selected
|
|
||||||
if (_rosters.length > 0) {
|
|
||||||
_activeRoster = _rosters.first;
|
|
||||||
}
|
|
||||||
|
|
||||||
_lastSync = DateTime.now();
|
_lastSync = DateTime.now();
|
||||||
_syncinProgress = false;
|
_syncinProgress = false;
|
||||||
storage.setItem('lastSync', _lastSync.toIso8601String());
|
storage.setItem('lastSync', _lastSync.toIso8601String());
|
||||||
|
@ -127,11 +129,40 @@ class GitHubAdapter extends ChangeNotifier {
|
||||||
|
|
||||||
void update() async {
|
void update() async {
|
||||||
_syncinProgress = true;
|
_syncinProgress = true;
|
||||||
|
_syncErrors.clear();
|
||||||
|
|
||||||
// TODO: Loop through the found warbands and update it.
|
notifyListeners();
|
||||||
|
|
||||||
|
for (var i = 0; i < rosters.length; i++) {
|
||||||
|
Version newVersion = await getLatestVersion(rosters[i].filePath);
|
||||||
|
|
||||||
|
if (newVersion == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newVersion.gitHash != rosters[i].version.gitHash) {
|
||||||
|
WarbandRoster newRoster = await fetchWarband(rosters[i].filePath);
|
||||||
|
|
||||||
|
if (newRoster == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
newRoster.playerName = rosters[i].playerName;
|
||||||
|
newRoster.version = newVersion;
|
||||||
|
newRoster.filePath = rosters[i].filePath;
|
||||||
|
rosters[i] = newRoster;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_rosters.sort((a, b) => b.campaignPoints.compareTo(a.campaignPoints));
|
||||||
|
|
||||||
_lastSync = DateTime.now();
|
_lastSync = DateTime.now();
|
||||||
_syncinProgress = false;
|
_syncinProgress = false;
|
||||||
|
|
||||||
|
if (_syncErrors.length != 0) {
|
||||||
|
// TODO: Show sync errors.
|
||||||
|
}
|
||||||
|
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,7 +179,7 @@ class GitHubAdapter extends ChangeNotifier {
|
||||||
return playerName;
|
return playerName;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Version> getLatestCommit(String filePath) async {
|
Future<Version> getLatestVersion(String filePath) async {
|
||||||
// Fetch last change and some metainformation of the file
|
// Fetch last change and some metainformation of the file
|
||||||
http.Response response = await http.get("https://api.github.com/repos/" +
|
http.Response response = await http.get("https://api.github.com/repos/" +
|
||||||
repository +
|
repository +
|
||||||
|
|
|
@ -212,10 +212,10 @@ class WarbandRoster {
|
||||||
String filePath;
|
String filePath;
|
||||||
|
|
||||||
@JsonKey(ignore: true)
|
@JsonKey(ignore: true)
|
||||||
Version lastSyncVersion;
|
Version version;
|
||||||
|
|
||||||
@JsonKey(ignore: true)
|
@JsonKey(ignore: true)
|
||||||
Version currentVersion;
|
bool unseen = true;
|
||||||
|
|
||||||
WarbandRoster(
|
WarbandRoster(
|
||||||
this.nameAndRace,
|
this.nameAndRace,
|
||||||
|
|
|
@ -2,16 +2,14 @@ import 'package:flutter/material.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
import 'package:toolheim/data/github_adapter.dart';
|
import 'package:toolheim/data/github_adapter.dart';
|
||||||
import 'package:toolheim/data/warband_roster.dart';
|
|
||||||
import 'package:toolheim/widgets/warband_drawer_widget.dart';
|
import 'package:toolheim/widgets/warband_drawer_widget.dart';
|
||||||
|
|
||||||
class WarbandRosterScreen extends StatelessWidget {
|
class WarbandRosterScreen extends StatelessWidget {
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
GitHubAdapter github = Provider.of<GitHubAdapter>(context);
|
GitHubAdapter github = Provider.of<GitHubAdapter>(context);
|
||||||
WarbandRoster roster = github.activeRoster;
|
|
||||||
|
|
||||||
if (roster == null) {
|
if (github.activeRoster == null) {
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(title: const Text('Toolheim')),
|
appBar: AppBar(title: const Text('Toolheim')),
|
||||||
body: Builder(builder: (BuildContext context) {
|
body: Builder(builder: (BuildContext context) {
|
||||||
|
@ -41,7 +39,7 @@ class WarbandRosterScreen extends StatelessWidget {
|
||||||
|
|
||||||
List<Widget> tiles = new List();
|
List<Widget> tiles = new List();
|
||||||
|
|
||||||
roster.heros.forEach((hero) {
|
github.activeRoster.heros.forEach((hero) {
|
||||||
tiles.add(new ListTile(
|
tiles.add(new ListTile(
|
||||||
title: Text(hero.name),
|
title: Text(hero.name),
|
||||||
leading: CircleAvatar(
|
leading: CircleAvatar(
|
||||||
|
@ -55,7 +53,7 @@ class WarbandRosterScreen extends StatelessWidget {
|
||||||
|
|
||||||
tiles.add(Divider());
|
tiles.add(Divider());
|
||||||
|
|
||||||
roster.henchmenGroups.forEach((henchmenGroup) {
|
github.activeRoster.henchmenGroups.forEach((henchmenGroup) {
|
||||||
tiles.add(new ListTile(
|
tiles.add(new ListTile(
|
||||||
title: Text(henchmenGroup.name),
|
title: Text(henchmenGroup.name),
|
||||||
trailing: Chip(label: Text(henchmenGroup.number.toString() + 'x')),
|
trailing: Chip(label: Text(henchmenGroup.number.toString() + 'x')),
|
||||||
|
@ -70,7 +68,7 @@ class WarbandRosterScreen extends StatelessWidget {
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: Text(roster.name),
|
title: Text(github.activeRoster.name),
|
||||||
),
|
),
|
||||||
drawer:
|
drawer:
|
||||||
Drawer(child: SingleChildScrollView(child: WarbandDrawerWidget())),
|
Drawer(child: SingleChildScrollView(child: WarbandDrawerWidget())),
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
import 'dart:math';
|
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
import 'package:badges/badges.dart';
|
import 'package:badges/badges.dart';
|
||||||
|
@ -61,11 +59,9 @@ class WarbandDrawerWidget extends StatelessWidget {
|
||||||
|
|
||||||
List<Widget> tiles = new List();
|
List<Widget> tiles = new List();
|
||||||
|
|
||||||
tiles.add(Visibility(
|
|
||||||
visible: github.isSyncInProgress, child: LinearProgressIndicator()));
|
|
||||||
|
|
||||||
// Show some stats for the own warband
|
// Show some stats for the own warband
|
||||||
tiles.add(UserAccountsDrawerHeader(
|
tiles.add(UserAccountsDrawerHeader(
|
||||||
|
margin: const EdgeInsets.all(0),
|
||||||
otherAccountsPictures: <Widget>[
|
otherAccountsPictures: <Widget>[
|
||||||
IconButton(
|
IconButton(
|
||||||
icon: Icon(Icons.refresh),
|
icon: Icon(Icons.refresh),
|
||||||
|
@ -87,6 +83,9 @@ class WarbandDrawerWidget extends StatelessWidget {
|
||||||
accountEmail: Text(activeroster.race),
|
accountEmail: Text(activeroster.race),
|
||||||
));
|
));
|
||||||
|
|
||||||
|
tiles.add(Visibility(
|
||||||
|
visible: github.isSyncInProgress, child: LinearProgressIndicator()));
|
||||||
|
|
||||||
rosters.forEach((roster) {
|
rosters.forEach((roster) {
|
||||||
// We mark inactive warbands with a gray acent
|
// We mark inactive warbands with a gray acent
|
||||||
var textColor = Colors.black;
|
var textColor = Colors.black;
|
||||||
|
@ -94,14 +93,20 @@ class WarbandDrawerWidget extends StatelessWidget {
|
||||||
textColor = Colors.black45;
|
textColor = Colors.black45;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var fontWeight = FontWeight.normal;
|
||||||
|
if (roster.unseen) {
|
||||||
|
fontWeight = FontWeight.bold;
|
||||||
|
}
|
||||||
|
|
||||||
tiles.add(ListTile(
|
tiles.add(ListTile(
|
||||||
onTap: () {
|
onTap: () {
|
||||||
|
roster.unseen = false;
|
||||||
github.activeRoster = roster;
|
github.activeRoster = roster;
|
||||||
Navigator.of(context).pop();
|
Navigator.of(context).pop();
|
||||||
},
|
},
|
||||||
title: Text(roster.name + ' (' + roster.playerName + ')',
|
title: Text(roster.name + ' (' + roster.playerName + ')',
|
||||||
style: TextStyle(color: textColor)),
|
style: TextStyle(color: textColor, fontWeight: fontWeight)),
|
||||||
subtitle: Text(roster.currentVersion.message),
|
subtitle: Text(roster.version.message),
|
||||||
isThreeLine: true,
|
isThreeLine: true,
|
||||||
trailing: Badge(
|
trailing: Badge(
|
||||||
badgeColor: Colors.black12,
|
badgeColor: Colors.black12,
|
||||||
|
|
|
@ -14,7 +14,7 @@ description: A new Flutter project.
|
||||||
version: 1.0.0+1
|
version: 1.0.0+1
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
sdk: ">=2.1.0 <3.0.0"
|
sdk: ">=2.2.2 <3.0.0"
|
||||||
|
|
||||||
dependencies:
|
dependencies:
|
||||||
flutter:
|
flutter:
|
||||||
|
|
Loading…
Reference in a new issue