Lift the whole app one level up

This commit is contained in:
Aaron Fischer 2019-07-10 14:16:13 +02:00
parent df61398054
commit 9ef1b30516
9 changed files with 234 additions and 187 deletions

View file

@ -1,26 +1,39 @@
import 'dart:collection';
import 'dart:convert'; import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import 'package:toolheim/warband_roaster.dart'; import 'package:toolheim/data/warband_roaster.dart';
import 'package:yaml/yaml.dart'; import 'package:yaml/yaml.dart';
enum SyncState { class GitHubAdapter extends ChangeNotifier {
unknown, String _repository = 'Labernator/Mordheim';
running, String _path = 'Mordheim-BorderTownBurning/Warband Rosters';
success,
error,
}
class GitHubAdapter { List<String> _syncErrors = new List<String>();
// TODO: Store this in the SharedPreferences DateTime _lastSync;
final String repository;
final String path;
static SyncState syncState = SyncState.unknown; List<WarbandRoaster> _roasters = [];
List<String> syncErrors = new List<String>(); String _activePlayerName = 'Aaron';
static DateTime lastSync;
GitHubAdapter(this.repository, this.path); String get repository => _repository;
String get path => _path;
DateTime get lastSync => _lastSync;
UnmodifiableListView<String> get syncErrors => _syncErrors;
UnmodifiableListView<WarbandRoaster> get roasters =>
UnmodifiableListView(_roasters);
WarbandRoaster get activeRoaster => _roasters.firstWhere((roaster) {
return roaster.playerName == _activePlayerName;
});
void changeActiveRoaster(String playerName) {
_activePlayerName = playerName;
notifyListeners();
}
// TODO: Add persistence layer here
/// Search for warband files in the GitHub repository /// Search for warband files in the GitHub repository
/// ///
@ -28,22 +41,22 @@ class GitHubAdapter {
/// 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.
Future<List<WarbandRoaster>> search() async { void search() async {
syncErrors.clear(); _syncErrors.clear();
Stream<Map<String, String>> roasterStream() async* { Stream<Map<String, String>> roasterStream() 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").
http.Response response = await http.get( http.Response response = await http.get(
"https://api.github.com/search/code?q=heros+repo:" + "https://api.github.com/search/code?q=heros+repo:" +
repository + _repository +
"+filename:mordheim.yml+path:\"" + "+filename:mordheim.yml+path:\"" +
path + _path +
"\""); "\"");
// GitHub is not reachable // GitHub is not reachable
if (response.statusCode != 200) { if (response.statusCode != 200) {
syncErrors.add('Could not find any warband roaster files'); _syncErrors.add('Could not find any warband roaster files');
yield {}; yield {};
return; return;
} }
@ -53,7 +66,7 @@ class GitHubAdapter {
try { try {
searchResults = jsonDecode(response.body); searchResults = jsonDecode(response.body);
} on FormatException catch (e) { } on FormatException catch (e) {
syncErrors.add('Could not parse GitHub response.' + e.toString()); _syncErrors.add('Could not parse GitHub response.' + e.toString());
yield {}; yield {};
return; return;
} }
@ -66,7 +79,7 @@ class GitHubAdapter {
// in which the file resists // in which the file resists
String completePath = searchResult['path']; String completePath = searchResult['path'];
List<String> pathParts = List<String> pathParts =
completePath.substring(path.length + 1).split('/'); completePath.substring(_path.length + 1).split('/');
String playerName; String playerName;
if (pathParts.length >= 2) { if (pathParts.length >= 2) {
@ -76,12 +89,12 @@ class GitHubAdapter {
// Fetch last change and some metainformation of the file // Fetch last change and some metainformation of the file
http.Response response = await http.get( http.Response response = await http.get(
"https://api.github.com/repos/" + "https://api.github.com/repos/" +
repository + _repository +
"/commits?path=" + "/commits?path=" +
completePath); completePath);
if (response.statusCode != 200) { if (response.statusCode != 200) {
syncErrors.add('Could not load the warband metadata from GitHub.'); _syncErrors.add('Could not load the warband metadata from GitHub.');
continue; continue;
} }
@ -90,7 +103,7 @@ class GitHubAdapter {
try { try {
commits = jsonDecode(response.body); commits = jsonDecode(response.body);
} on FormatException catch (e) { } on FormatException catch (e) {
syncErrors.add('Could not parse GitHub response.' + e.toString()); _syncErrors.add('Could not parse GitHub response.' + e.toString());
continue; continue;
} }
@ -112,12 +125,12 @@ class GitHubAdapter {
} }
} }
List<WarbandRoaster> roasters = new List<WarbandRoaster>(); _roasters.clear();
// TODO: Read params from share preferences notifyListeners();
await for (Map<String, String> player in roasterStream()) { await for (Map<String, String> player in roasterStream()) {
http.Response response = await http.get( http.Response response = await http.get(
"https://raw.githubusercontent.com/" + "https://raw.githubusercontent.com/" +
repository + _repository +
'/master/' + '/master/' +
player['filePath']); player['filePath']);
@ -135,21 +148,23 @@ class GitHubAdapter {
// Sp, lastSyncVersion is equal to the currentVersion. // Sp, lastSyncVersion is equal to the currentVersion.
roaster.lastSyncVersion = roaster.currentVersion; roaster.lastSyncVersion = roaster.currentVersion;
roasters.add(roaster); _roasters.add(roaster);
notifyListeners();
} catch (e) { } catch (e) {
syncErrors.add(e.toString()); _syncErrors.add(e.toString());
} }
} }
return roasters; _lastSync = DateTime.now();
notifyListeners();
} }
Future<SyncState> update() async { void update() async {
// TODO: Search for warband yml files // TODO: Search for warband yml files
// TODO: Check if it is in the right format // TODO: Check if it is in the right format
// TODO: Store it into the database if valid // TODO: Store it into the database if valid
lastSync = DateTime.now();
return SyncState.success; _lastSync = DateTime.now();
notifyListeners();
} }
} }

View file

@ -1,5 +1,6 @@
import 'dart:collection'; import 'dart:collection';
import 'package:flutter/widgets.dart';
import 'package:json_annotation/json_annotation.dart'; import 'package:json_annotation/json_annotation.dart';
part 'warband_roaster.g.dart'; part 'warband_roaster.g.dart';
@ -128,6 +129,7 @@ class Hero extends Unit {
} }
} }
@immutable
class Stats { class Stats {
final int movement; final int movement;
final int weaponSkill; final int weaponSkill;
@ -216,6 +218,11 @@ class WarbandRoaster {
this.race = this.nameAndRace['race']; this.race = this.nameAndRace['race'];
} }
int experience() {
// TODO: Calculate
return 1337;
}
static HashMap<String, String> _warbandNameAndRace(String nameAndRace) { static HashMap<String, String> _warbandNameAndRace(String nameAndRace) {
HashMap<String, String> nr = new HashMap(); HashMap<String, String> nr = new HashMap();
RegExp re = new RegExp(r"(.*) \((.*)\)"); RegExp re = new RegExp(r"(.*) \((.*)\)");

View file

@ -1,163 +1,30 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:toolheim/github_reader.dart'; import 'package:provider/provider.dart';
import 'package:toolheim/warband_roaster.dart'; import 'package:toolheim/data/github_adapter.dart';
import 'package:toolheim/screens/settings_screen.dart';
import 'package:toolheim/screens/warband_roaster_screen.dart';
void main() => runApp(Toolheim()); void main() {
runApp(Toolheim());
}
class Toolheim extends StatelessWidget { class Toolheim extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return MaterialApp( return ChangeNotifierProvider(
title: 'Toolheim', builder: (context) => GitHubAdapter(),
theme: ThemeData( child: MaterialApp(
primarySwatch: Colors.brown, title: 'Toolheim',
accentColor: Colors.grey, theme: ThemeData(
primarySwatch: Colors.brown,
accentColor: Colors.grey,
),
initialRoute: '/',
routes: {
'/': (context) => WarbandRoasterScreen(),
'/settings': (context) => SettingsScreen()
},
), ),
home: RoasterWidget(),
); );
} }
} }
class RoasterWidget extends StatefulWidget {
@override
_RoasterWidgetState createState() => _RoasterWidgetState();
}
class _RoasterWidgetState extends State<RoasterWidget> {
// TODO: Read this from SharedPreferences
String activePlayer = 'Aaron';
GitHubAdapter github = GitHubAdapter(
'Labernator/Mordheim', 'Mordheim-BorderTownBurning/Warband Rosters');
List<Widget> playerList(List<WarbandRoaster> roasters) {
List<Widget> tiles = new List();
WarbandRoaster myWarband = roasters.firstWhere((roaster) {
return roaster.playerName == activePlayer;
});
// Show some stats for the own warband
tiles.add(UserAccountsDrawerHeader(
otherAccountsPictures: <Widget>[
IconButton(
icon: Icon(Icons.refresh),
color: Colors.white,
tooltip: 'Refresh warbands',
onPressed: () {
setState(() {
//roasters = await github.update();
});
},
),
IconButton(
icon: Icon(Icons.search),
color: Colors.white,
tooltip: 'Read warbands',
onPressed: () {
setState(() {
//roasters = await github.search()
});
},
)
],
accountName: Text(myWarband.name),
accountEmail: Text(myWarband.race),
currentAccountPicture: CircleAvatar(
child: Text('Aa'),
),
));
// TODO: Order Players on CP or rating
roasters.forEach((roaster) {
// We mark inactive warbands with a gray acent
var textColor = Colors.black;
if (!roaster.active) {
textColor = Colors.black45;
}
tiles.add(ListTile(
title: Text(roaster.name + ' (' + roaster.playerName + ')',
style: TextStyle(color: textColor)),
subtitle: Text(roaster.currentVersion.message),
isThreeLine: true,
trailing: Chip(
label: Text(
'326 XP',
))));
});
tiles.add(Divider());
return tiles;
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: github.search(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.waiting:
case ConnectionState.active:
return Center(child: CircularProgressIndicator());
case ConnectionState.done:
List<WarbandRoaster> roasters = snapshot.data;
if (roasters.length == 0) {
return Text('No warbands found');
}
// TODO: Replace with router
WarbandRoaster warband = roasters.firstWhere((roaster) {
return roaster.playerName == this.activePlayer;
});
return Scaffold(
appBar: AppBar(
title: Text(warband.name),
),
drawer: Drawer(
child: SingleChildScrollView(
child: Column(children: playerList(roasters))),
),
body: ListView.builder(
itemCount:
warband.heros.length + warband.henchmenGroups.length,
itemBuilder: (BuildContext context, int index) {
// TODO: Sort by initiative
if (index < warband.heros.length) {
var hero = warband.heros[index];
return ListTile(
title: Text(hero.name),
leading: CircleAvatar(
child: Text(hero.experience.toString()),
backgroundColor: Colors.green,
foregroundColor: Colors.greenAccent,
),
subtitle: Text(hero.type),
);
} else {
var henchmenGroup = warband
.henchmenGroups[index - warband.heros.length];
return ListTile(
title: Text(henchmenGroup.name),
trailing: Chip(
label: Text(
henchmenGroup.number.toString() + 'x')),
leading: CircleAvatar(
child: Text(henchmenGroup.experience.toString()),
backgroundColor: Colors.orange,
foregroundColor: Colors.white,
),
subtitle: Text(henchmenGroup.type),
);
}
}));
}
});
}
}

View file

@ -0,0 +1,14 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:toolheim/data/github_adapter.dart';
class SettingsScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
GitHubAdapter github = Provider.of<GitHubAdapter>(context);
return Scaffold(
appBar: AppBar(title: Text('Settings')), body: Text(github.repository));
}
}

View file

@ -0,0 +1,68 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:toolheim/data/github_adapter.dart';
import 'package:toolheim/data/warband_roaster.dart';
import 'package:toolheim/widgets/warband_drawer_widget.dart';
class WarbandRoasterScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
GitHubAdapter github = Provider.of<GitHubAdapter>(context);
if (github.lastSync == null) {
return Scaffold(
appBar: AppBar(title: Text('Toolheim')),
body: Center(
child: IconButton(
icon: Icon(Icons.search),
color: Colors.black,
tooltip: 'Search for warbands',
onPressed: github.search,
),
));
}
WarbandRoaster roaster = github.activeRoaster;
return Scaffold(
appBar: AppBar(
title: Text(roaster.name),
),
drawer:
Drawer(child: SingleChildScrollView(child: WarbandDrawerWidget())),
body: ListView.builder(
itemCount: roaster.heros.length + roaster.henchmenGroups.length,
itemBuilder: (BuildContext context, int index) {
// TODO: Sort by initiative
if (index < roaster.heros.length) {
var hero = roaster.heros[index];
return ListTile(
title: Text(hero.name),
leading: CircleAvatar(
child: Text(hero.experience.toString()),
backgroundColor: Colors.green,
foregroundColor: Colors.greenAccent,
),
subtitle: Text(hero.type),
);
} else {
var henchmenGroup =
roaster.henchmenGroups[index - roaster.heros.length];
return ListTile(
title: Text(henchmenGroup.name),
trailing:
Chip(label: Text(henchmenGroup.number.toString() + 'x')),
leading: CircleAvatar(
child: Text(henchmenGroup.experience.toString()),
backgroundColor: Colors.orange,
foregroundColor: Colors.white,
),
subtitle: Text(henchmenGroup.type),
);
}
}));
}
}

View file

@ -0,0 +1,68 @@
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:toolheim/data/github_adapter.dart';
import 'package:toolheim/data/warband_roaster.dart';
class WarbandDrawerWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
GitHubAdapter github = Provider.of<GitHubAdapter>(context);
if (github.lastSync == null) {
// Add search button
return Column(children: <Widget>[]);
}
WarbandRoaster activeRoaster = github.activeRoaster;
List<WarbandRoaster> roasters = github.roasters;
List<Widget> tiles = new List();
// Show some stats for the own warband
tiles.add(UserAccountsDrawerHeader(
//otherAccountsPictures: <Widget>[
// IconButton(
// icon: Icon(Icons.refresh),
// color: Colors.white,
// highlightColor: Colors.brown,
// tooltip: 'Refresh warbands',
// onPressed: github.update,
// ),
// IconButton(
// icon: Icon(Icons.search),
// color: Colors.white,
// tooltip: 'Read warbands',
// onPressed: github.search,
// )
//],
accountName: Text(activeRoaster.name),
accountEmail: Text(activeRoaster.race),
));
// TODO: Order Players on CP or rating
roasters.forEach((roaster) {
// We mark inactive warbands with a gray acent
var textColor = Colors.black;
if (!roaster.active) {
textColor = Colors.black45;
}
tiles.add(ListTile(
onTap: () {
github.changeActiveRoaster(roaster.playerName);
Navigator.of(context).pop();
},
title: Text(roaster.name + ' (' + roaster.playerName + ')',
style: TextStyle(color: textColor)),
subtitle: Text(roaster.currentVersion.message),
isThreeLine: true,
trailing:
Chip(label: Text(roaster.campaignPoints.toString() + ' CP'))));
});
tiles.add(Divider());
return Column(children: tiles);
}
}

View file

@ -312,6 +312,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.4.0" version: "1.4.0"
provider:
dependency: "direct main"
description:
name: provider
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.0+1"
pub_semver: pub_semver:
dependency: transitive dependency: transitive
description: description:

View file

@ -22,6 +22,7 @@ dependencies:
yaml: yaml:
checked_yaml: checked_yaml:
http: http:
provider:
# The following adds the Cupertino Icons font to your application. # The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.