code tidy

This commit is contained in:
Aaron Fischer 2019-08-02 00:56:04 +02:00
parent 17b19350a7
commit ec2a340a5d

View file

@ -19,8 +19,8 @@ class GitHubAdapter extends ChangeNotifier {
String get repository => PrefService.getString('repository'); String get repository => PrefService.getString('repository');
String get path => PrefService.getString('path'); String get path => PrefService.getString('path');
bool _syncinProgress = false; bool _syncInProgress = false;
bool get isSyncInProgress => _syncinProgress; bool get isSyncInProgress => _syncInProgress;
DateTime get lastSync => _lastSync; DateTime get lastSync => _lastSync;
List<String> get syncErrors => _syncErrors; List<String> get syncErrors => _syncErrors;
@ -51,10 +51,7 @@ class GitHubAdapter extends ChangeNotifier {
/// subfolder (see fields [repository] and [path]). If the file /// subfolder (see fields [repository] and [path]). If the file
/// contain errors or can't read, a sync error message will be written into /// contain errors or can't read, a sync error message will be written into
/// the [syncErrors] list. /// the [syncErrors] list.
void search() async { Future<void> search() async {
_syncErrors.clear();
_syncinProgress = true;
Stream<String> warbandFileStream() async* { Stream<String> warbandFileStream() async* {
// Get all files which could be potential warband files (end with // Get all files which could be potential warband files (end with
// mordheim.yml and contain the word "heros"). // mordheim.yml and contain the word "heros").
@ -94,45 +91,47 @@ class GitHubAdapter extends ChangeNotifier {
} }
} }
storage.clear(); //storage.clear();
_syncErrors.clear();
_rosters.clear(); _rosters.clear();
_syncInProgress = true;
notifyListeners(); notifyListeners();
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 getLatestVersion(filePath);
Version latestVersion = await getLatestVersion(filePath);
if (roster != null && latestVersion != null) { if (roster != null && latestVersion != null) {
roster.playerName = getPlayerNameFromFilePath(filePath); roster.playerName = getPlayerNameFromFilePath(filePath);
roster.version = latestVersion; roster.version = latestVersion;
roster.filePath = filePath; roster.filePath = filePath;
_rosters.add(roster); _rosters.add(roster);
notifyListeners(); notifyListeners();
}
//https://github.com/lesnitsky/flutter_localstorage/blob/master/example/lib/main.dart
// FIXME: store it correctly
//storage.setItem(player['player'] + '-warband-yaml', response.body);
//storage.setItem(
// player['player'] + '-current-version', roster.currentVersion);
//storage.setItem(
// player['player'] + '-last-sync-version', roster.lastSyncVersion);
} }
//https://github.com/lesnitsky/flutter_localstorage/blob/master/example/lib/main.dart
// FIXME: store it correctly
//storage.setItem(player['player'] + '-warband-yaml', response.body);
//storage.setItem(
// player['player'] + '-current-version', roster.currentVersion);
//storage.setItem(
// player['player'] + '-last-sync-version', roster.lastSyncVersion);
} }
// Sort by CP // Sort by CP
_rosters.sort((a, b) => b.campaignPoints.compareTo(a.campaignPoints)); _rosters.sort((a, b) => b.campaignPoints.compareTo(a.campaignPoints));
_lastSync = DateTime.now(); _lastSync = DateTime.now();
_syncinProgress = false; _syncInProgress = false;
storage.setItem('lastSync', _lastSync.toIso8601String()); //storage.setItem('lastSync', _lastSync.toIso8601String());
notifyListeners(); notifyListeners();
} }
Future<void> update() async { Future<void> update() async {
_syncinProgress = true; _syncInProgress = true;
_syncErrors.clear(); _syncErrors.clear();
notifyListeners(); notifyListeners();
@ -143,6 +142,7 @@ class GitHubAdapter extends ChangeNotifier {
// File does not exist any more, we remove the roster // File does not exist any more, we remove the roster
if (newVersion == null) { if (newVersion == null) {
rosters.removeAt(i); rosters.removeAt(i);
notifyListeners();
continue; continue;
} }
@ -158,13 +158,14 @@ class GitHubAdapter extends ChangeNotifier {
newRoster.version = newVersion; newRoster.version = newVersion;
newRoster.filePath = rosters[i].filePath; newRoster.filePath = rosters[i].filePath;
rosters[i] = newRoster; rosters[i] = newRoster;
notifyListeners();
} }
} }
_rosters.sort((a, b) => b.campaignPoints.compareTo(a.campaignPoints)); _rosters.sort((a, b) => b.campaignPoints.compareTo(a.campaignPoints));
_lastSync = DateTime.now(); _lastSync = DateTime.now();
_syncinProgress = false; _syncInProgress = false;
notifyListeners(); notifyListeners();
} }
@ -192,6 +193,7 @@ class GitHubAdapter extends ChangeNotifier {
if (response.statusCode != 200) { if (response.statusCode != 200) {
_syncErrors _syncErrors
.add(filePath + ': Could not load the warband metadata from GitHub.'); .add(filePath + ': Could not load the warband metadata from GitHub.');
notifyListeners();
return null; return null;
} }
@ -202,6 +204,7 @@ class GitHubAdapter extends ChangeNotifier {
} on FormatException catch (e) { } on FormatException catch (e) {
_syncErrors _syncErrors
.add(filePath + ': Could not parse GitHub response. ' + e.toString()); .add(filePath + ': Could not parse GitHub response. ' + e.toString());
notifyListeners();
return null; return null;
} }
@ -239,6 +242,7 @@ class GitHubAdapter extends ChangeNotifier {
return WarbandRoster.fromJson(yamlObject); return WarbandRoster.fromJson(yamlObject);
} catch (e) { } catch (e) {
_syncErrors.add(filePath + ': ' + e.message); _syncErrors.add(filePath + ': ' + e.message);
notifyListeners();
} }
return null; return null;