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 'package:flutter/material.dart';
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';
enum SyncState {
unknown,
running,
success,
error,
}
class GitHubAdapter extends ChangeNotifier {
String _repository = 'Labernator/Mordheim';
String _path = 'Mordheim-BorderTownBurning/Warband Rosters';
class GitHubAdapter {
// TODO: Store this in the SharedPreferences
final String repository;
final String path;
List<String> _syncErrors = new List<String>();
DateTime _lastSync;
static SyncState syncState = SyncState.unknown;
List<String> syncErrors = new List<String>();
static DateTime lastSync;
List<WarbandRoaster> _roasters = [];
String _activePlayerName = 'Aaron';
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
///
@ -28,22 +41,22 @@ class GitHubAdapter {
/// subfolder (see fields [repository] and [path]). If the file
/// contain errors or can't read, a sync error message will be written into
/// the [syncErrors] list.
Future<List<WarbandRoaster>> search() async {
syncErrors.clear();
void search() async {
_syncErrors.clear();
Stream<Map<String, String>> roasterStream() async* {
// Get all files which could be potential warband files (end with
// mordheim.yml and contain the word "heros").
http.Response response = await http.get(
"https://api.github.com/search/code?q=heros+repo:" +
repository +
_repository +
"+filename:mordheim.yml+path:\"" +
path +
_path +
"\"");
// GitHub is not reachable
if (response.statusCode != 200) {
syncErrors.add('Could not find any warband roaster files');
_syncErrors.add('Could not find any warband roaster files');
yield {};
return;
}
@ -53,7 +66,7 @@ class GitHubAdapter {
try {
searchResults = jsonDecode(response.body);
} on FormatException catch (e) {
syncErrors.add('Could not parse GitHub response.' + e.toString());
_syncErrors.add('Could not parse GitHub response.' + e.toString());
yield {};
return;
}
@ -66,7 +79,7 @@ class GitHubAdapter {
// in which the file resists
String completePath = searchResult['path'];
List<String> pathParts =
completePath.substring(path.length + 1).split('/');
completePath.substring(_path.length + 1).split('/');
String playerName;
if (pathParts.length >= 2) {
@ -76,12 +89,12 @@ class GitHubAdapter {
// Fetch last change and some metainformation of the file
http.Response response = await http.get(
"https://api.github.com/repos/" +
repository +
_repository +
"/commits?path=" +
completePath);
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;
}
@ -90,7 +103,7 @@ class GitHubAdapter {
try {
commits = jsonDecode(response.body);
} on FormatException catch (e) {
syncErrors.add('Could not parse GitHub response.' + e.toString());
_syncErrors.add('Could not parse GitHub response.' + e.toString());
continue;
}
@ -112,12 +125,12 @@ class GitHubAdapter {
}
}
List<WarbandRoaster> roasters = new List<WarbandRoaster>();
// TODO: Read params from share preferences
_roasters.clear();
notifyListeners();
await for (Map<String, String> player in roasterStream()) {
http.Response response = await http.get(
"https://raw.githubusercontent.com/" +
repository +
_repository +
'/master/' +
player['filePath']);
@ -135,21 +148,23 @@ class GitHubAdapter {
// Sp, lastSyncVersion is equal to the currentVersion.
roaster.lastSyncVersion = roaster.currentVersion;
roasters.add(roaster);
_roasters.add(roaster);
notifyListeners();
} 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: Check if it is in the right format
// 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 'package:flutter/widgets.dart';
import 'package:json_annotation/json_annotation.dart';
part 'warband_roaster.g.dart';
@ -128,6 +129,7 @@ class Hero extends Unit {
}
}
@immutable
class Stats {
final int movement;
final int weaponSkill;
@ -216,6 +218,11 @@ class WarbandRoaster {
this.race = this.nameAndRace['race'];
}
int experience() {
// TODO: Calculate
return 1337;
}
static HashMap<String, String> _warbandNameAndRace(String nameAndRace) {
HashMap<String, String> nr = new HashMap();
RegExp re = new RegExp(r"(.*) \((.*)\)");

View file

@ -1,163 +1,30 @@
import 'package:flutter/material.dart';
import 'package:toolheim/github_reader.dart';
import 'package:toolheim/warband_roaster.dart';
import 'package:provider/provider.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 {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Toolheim',
theme: ThemeData(
primarySwatch: Colors.brown,
accentColor: Colors.grey,
return ChangeNotifierProvider(
builder: (context) => GitHubAdapter(),
child: MaterialApp(
title: 'Toolheim',
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"
source: hosted
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:
dependency: transitive
description:

View file

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