working update, but need more error checking/handling

This commit is contained in:
Aaron Fischer 2019-07-30 00:30:39 +02:00
parent 05ca125f87
commit 82682b3d16
5 changed files with 68 additions and 34 deletions

View file

@ -15,7 +15,7 @@ class GitHubAdapter extends ChangeNotifier {
DateTime _lastSync;
List<WarbandRoster> _rosters = [];
WarbandRoster _activeRoster;
String _activeRosterFilePath;
String get repository => PrefService.getString('repository');
String get path => PrefService.getString('path');
@ -26,13 +26,23 @@ class GitHubAdapter extends ChangeNotifier {
DateTime get lastSync => _lastSync;
List<String> get syncErrors => _syncErrors;
UnmodifiableListView<WarbandRoster> get rosters =>
UnmodifiableListView(_rosters);
List<WarbandRoster> get rosters => _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) {
_activeRoster = roster;
_activeRosterFilePath = roster.filePath;
notifyListeners();
}
@ -89,14 +99,11 @@ class GitHubAdapter extends ChangeNotifier {
if (_syncErrors.length == 0) {
await for (String filePath in warbandFileStream()) {
WarbandRoster roster = await fetchWarband(filePath);
Version latestVersion = await getLatestCommit(filePath);
Version latestVersion = await getLatestVersion(filePath);
roster.playerName = getPlayerNameFromFilePath(filePath);
roster.currentVersion = latestVersion;
// On a search, we drop all previous information about the warbands,
// So, lastSyncVersion is equal to the currentVersion.
roster.lastSyncVersion = roster.currentVersion;
roster.version = latestVersion;
roster.filePath = filePath;
_rosters.add(roster);
@ -114,11 +121,6 @@ class GitHubAdapter extends ChangeNotifier {
// Sort by CP
_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();
_syncinProgress = false;
storage.setItem('lastSync', _lastSync.toIso8601String());
@ -127,11 +129,40 @@ class GitHubAdapter extends ChangeNotifier {
void update() async {
_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();
_syncinProgress = false;
if (_syncErrors.length != 0) {
// TODO: Show sync errors.
}
notifyListeners();
}
@ -148,7 +179,7 @@ class GitHubAdapter extends ChangeNotifier {
return playerName;
}
Future<Version> getLatestCommit(String filePath) async {
Future<Version> getLatestVersion(String filePath) async {
// Fetch last change and some metainformation of the file
http.Response response = await http.get("https://api.github.com/repos/" +
repository +

View file

@ -212,10 +212,10 @@ class WarbandRoster {
String filePath;
@JsonKey(ignore: true)
Version lastSyncVersion;
Version version;
@JsonKey(ignore: true)
Version currentVersion;
bool unseen = true;
WarbandRoster(
this.nameAndRace,

View file

@ -2,16 +2,14 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:toolheim/data/github_adapter.dart';
import 'package:toolheim/data/warband_roster.dart';
import 'package:toolheim/widgets/warband_drawer_widget.dart';
class WarbandRosterScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
GitHubAdapter github = Provider.of<GitHubAdapter>(context);
WarbandRoster roster = github.activeRoster;
if (roster == null) {
if (github.activeRoster == null) {
return Scaffold(
appBar: AppBar(title: const Text('Toolheim')),
body: Builder(builder: (BuildContext context) {
@ -41,7 +39,7 @@ class WarbandRosterScreen extends StatelessWidget {
List<Widget> tiles = new List();
roster.heros.forEach((hero) {
github.activeRoster.heros.forEach((hero) {
tiles.add(new ListTile(
title: Text(hero.name),
leading: CircleAvatar(
@ -55,7 +53,7 @@ class WarbandRosterScreen extends StatelessWidget {
tiles.add(Divider());
roster.henchmenGroups.forEach((henchmenGroup) {
github.activeRoster.henchmenGroups.forEach((henchmenGroup) {
tiles.add(new ListTile(
title: Text(henchmenGroup.name),
trailing: Chip(label: Text(henchmenGroup.number.toString() + 'x')),
@ -70,7 +68,7 @@ class WarbandRosterScreen extends StatelessWidget {
return Scaffold(
appBar: AppBar(
title: Text(roster.name),
title: Text(github.activeRoster.name),
),
drawer:
Drawer(child: SingleChildScrollView(child: WarbandDrawerWidget())),

View file

@ -1,5 +1,3 @@
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:badges/badges.dart';
@ -61,11 +59,9 @@ class WarbandDrawerWidget extends StatelessWidget {
List<Widget> tiles = new List();
tiles.add(Visibility(
visible: github.isSyncInProgress, child: LinearProgressIndicator()));
// Show some stats for the own warband
tiles.add(UserAccountsDrawerHeader(
margin: const EdgeInsets.all(0),
otherAccountsPictures: <Widget>[
IconButton(
icon: Icon(Icons.refresh),
@ -87,6 +83,9 @@ class WarbandDrawerWidget extends StatelessWidget {
accountEmail: Text(activeroster.race),
));
tiles.add(Visibility(
visible: github.isSyncInProgress, child: LinearProgressIndicator()));
rosters.forEach((roster) {
// We mark inactive warbands with a gray acent
var textColor = Colors.black;
@ -94,14 +93,20 @@ class WarbandDrawerWidget extends StatelessWidget {
textColor = Colors.black45;
}
var fontWeight = FontWeight.normal;
if (roster.unseen) {
fontWeight = FontWeight.bold;
}
tiles.add(ListTile(
onTap: () {
roster.unseen = false;
github.activeRoster = roster;
Navigator.of(context).pop();
},
title: Text(roster.name + ' (' + roster.playerName + ')',
style: TextStyle(color: textColor)),
subtitle: Text(roster.currentVersion.message),
style: TextStyle(color: textColor, fontWeight: fontWeight)),
subtitle: Text(roster.version.message),
isThreeLine: true,
trailing: Badge(
badgeColor: Colors.black12,

View file

@ -14,7 +14,7 @@ description: A new Flutter project.
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
sdk: ">=2.2.2 <3.0.0"
dependencies:
flutter: