toolheim/mobile-app/lib/main.dart
2019-06-27 00:16:58 +02:00

146 lines
4.9 KiB
Dart

import 'dart:collection';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:toolheim/WarbandRoaster.dart';
import 'package:yaml/yaml.dart';
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,
),
home: RoasterWidget(),
);
}
}
class RoasterWidget extends StatefulWidget {
@override
_RoasterWidgetState createState() => _RoasterWidgetState();
}
class _RoasterWidgetState extends State<RoasterWidget> {
Future<WarbandRoaster> roaster;
static Future<WarbandRoaster> fetchWarband(urlPath) async {
http.Response response = await http.get(urlPath);
YamlMap yamlObject = loadYaml(response.body);
return WarbandRoaster.fromJson(yamlObject);
}
void initState() {
super.initState();
roaster = fetchWarband(players()['Aaron']);
}
HashMap players() {
HashMap players = new HashMap();
players['Aaron'] = 'https://raw.githubusercontent.com/Labernator/Mordheim/master/Mordheim-BorderTownBurning/Warband%20Rosters/Aaron/aaron.mordheim.yml';
players['Kai'] = 'https://raw.githubusercontent.com/Labernator/Mordheim/master/Mordheim-BorderTownBurning/Warband%20Rosters/kai/kai.mordheim_post5.yml';
players['Fabian'] = 'https://raw.githubusercontent.com/Labernator/Mordheim/master/Mordheim-BorderTownBurning/Warband%20Rosters/Fabian/fabian.mordheim.yml';
players['Marius'] = 'https://raw.githubusercontent.com/Labernator/Mordheim/master/Mordheim-BorderTownBurning/Warband%20Rosters/Marius/Marius_Post_5.mordheim.yml';
players['Stefan'] = 'https://raw.githubusercontent.com/Labernator/Mordheim/master/Mordheim-BorderTownBurning/Warband%20Rosters/Stefan/Pit%20Fighter.yml';
return players;
}
List<Widget> playerList() {
List<Widget> tiles = new List();
// Show some stats for the own warband
tiles.add(UserAccountsDrawerHeader(
accountName: Text('The Revolting Dwarfs'),
accountEmail: Text('Dwarf Rangers'),
currentAccountPicture: CircleAvatar(
child: Text('Aa'),
),
));
// TODO: Order Players on CP or rating
players().forEach((player, url) {
tiles.add(ListTile(
title: Text(player.toString()),
leading: CircleAvatar(
child: Text('WB'),
),
trailing: Chip(
label: Text('326 XP')
)
));
});
tiles.add(Divider());
return tiles;
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: roaster,
builder: (BuildContext context, AsyncSnapshot snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.waiting:
case ConnectionState.active:
return Center(child: CircularProgressIndicator());
case ConnectionState.done:
WarbandRoaster roaster = snapshot.data;
return Scaffold(
appBar: AppBar(
title: Text(roaster.name),
),
drawer: Drawer(
child: Column(
children: this.playerList()
),
),
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),
);
}
}
)
);
}
}
);
}
}