From 82682b3d164ca8fa8bf7416864fd43158706e301 Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Tue, 30 Jul 2019 00:30:39 +0200 Subject: [PATCH] working update, but need more error checking/handling --- mobile-app/lib/data/github_adapter.dart | 67 ++++++++++++++----- mobile-app/lib/data/warband_roster.dart | 4 +- .../lib/screens/warband_roster_screen.dart | 10 ++- .../lib/widgets/warband_drawer_widget.dart | 19 ++++-- mobile-app/pubspec.yaml | 2 +- 5 files changed, 68 insertions(+), 34 deletions(-) diff --git a/mobile-app/lib/data/github_adapter.dart b/mobile-app/lib/data/github_adapter.dart index 70a558f..217cc0b 100644 --- a/mobile-app/lib/data/github_adapter.dart +++ b/mobile-app/lib/data/github_adapter.dart @@ -15,7 +15,7 @@ class GitHubAdapter extends ChangeNotifier { DateTime _lastSync; List _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 get syncErrors => _syncErrors; - UnmodifiableListView get rosters => - UnmodifiableListView(_rosters); + List 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 getLatestCommit(String filePath) async { + Future 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 + diff --git a/mobile-app/lib/data/warband_roster.dart b/mobile-app/lib/data/warband_roster.dart index faa6f60..19b9156 100644 --- a/mobile-app/lib/data/warband_roster.dart +++ b/mobile-app/lib/data/warband_roster.dart @@ -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, diff --git a/mobile-app/lib/screens/warband_roster_screen.dart b/mobile-app/lib/screens/warband_roster_screen.dart index 5fef49a..4f21f8d 100644 --- a/mobile-app/lib/screens/warband_roster_screen.dart +++ b/mobile-app/lib/screens/warband_roster_screen.dart @@ -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(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 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())), diff --git a/mobile-app/lib/widgets/warband_drawer_widget.dart b/mobile-app/lib/widgets/warband_drawer_widget.dart index 1f9c3d3..45b51f6 100644 --- a/mobile-app/lib/widgets/warband_drawer_widget.dart +++ b/mobile-app/lib/widgets/warband_drawer_widget.dart @@ -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 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: [ 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, diff --git a/mobile-app/pubspec.yaml b/mobile-app/pubspec.yaml index dc4ccd7..518950c 100644 --- a/mobile-app/pubspec.yaml +++ b/mobile-app/pubspec.yaml @@ -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: