Refactor the code

This commit is contained in:
Aaron Fischer 2019-08-02 00:21:41 +02:00
parent d34a0d4b32
commit 73ce25ca52
3 changed files with 111 additions and 160 deletions

View file

@ -37,18 +37,14 @@ class SettingsScreen extends StatelessWidget {
SizedBox( SizedBox(
height: 50, height: 50,
), ),
Visibility( if (github.isSyncInProgress)
child: CircularProgressIndicator(), CircularProgressIndicator(),
visible: github.isSyncInProgress), if (github.syncErrors.length > 0)
Visibility( buildSyncErrors(context),
child: buildSyncErrors(context),
visible: github.syncErrors.length > 0,
)
]), ]),
actions: <Widget>[ actions: <Widget>[
Visibility( if (!github.isSyncInProgress)
visible: !github.isSyncInProgress, FlatButton(
child: FlatButton(
child: Text('Close', child: Text('Close',
style: TextStyle(color: Colors.blue)), style: TextStyle(color: Colors.blue)),
onPressed: () { onPressed: () {
@ -58,7 +54,6 @@ class SettingsScreen extends StatelessWidget {
Navigator.popAndPushNamed(context, '/'); Navigator.popAndPushNamed(context, '/');
} }
}, },
),
) )
]); ]);
}); });
@ -72,14 +67,11 @@ class SettingsScreen extends StatelessWidget {
} }
static Widget buildSyncErrors(BuildContext context) { static Widget buildSyncErrors(BuildContext context) {
List<Widget> syncErrors = new List();
GitHubAdapter github = Provider.of<GitHubAdapter>(context); GitHubAdapter github = Provider.of<GitHubAdapter>(context);
// TODO: Make it pretty
github.syncErrors.forEach((error) {
syncErrors.add(Text(error, style: TextStyle(color: Colors.red)));
});
return Column(children: syncErrors); return Column(children: [
for (var error in github.syncErrors)
Text(error, style: TextStyle(color: Colors.red))
]);
} }
} }

View file

@ -37,10 +37,16 @@ class WarbandRosterScreen extends StatelessWidget {
child: SingleChildScrollView(child: WarbandDrawerWidget()))); child: SingleChildScrollView(child: WarbandDrawerWidget())));
} }
List<Widget> tiles = new List(); return Scaffold(
appBar: AppBar(
github.activeRoster.heros.forEach((hero) { title: Text(github.activeRoster.name),
tiles.add(new ListTile( ),
drawer:
Drawer(child: SingleChildScrollView(child: WarbandDrawerWidget())),
body: SingleChildScrollView(
child: Column(children: [
for (var hero in github.activeRoster.heros)
ListTile(
title: Text(hero.name), title: Text(hero.name),
leading: CircleAvatar( leading: CircleAvatar(
child: Text(hero.experience.toString()), child: Text(hero.experience.toString()),
@ -48,13 +54,10 @@ class WarbandRosterScreen extends StatelessWidget {
foregroundColor: Colors.greenAccent, foregroundColor: Colors.greenAccent,
), ),
subtitle: Text(hero.type), subtitle: Text(hero.type),
)); ),
}); Divider(),
for (var henchmenGroup in github.activeRoster.henchmenGroups)
tiles.add(Divider()); ListTile(
github.activeRoster.henchmenGroups.forEach((henchmenGroup) {
tiles.add(new ListTile(
title: Text(henchmenGroup.name), title: Text(henchmenGroup.name),
trailing: Chip(label: Text(henchmenGroup.number.toString() + 'x')), trailing: Chip(label: Text(henchmenGroup.number.toString() + 'x')),
leading: CircleAvatar( leading: CircleAvatar(
@ -63,16 +66,8 @@ class WarbandRosterScreen extends StatelessWidget {
foregroundColor: Colors.white, foregroundColor: Colors.white,
), ),
subtitle: Text(henchmenGroup.type), subtitle: Text(henchmenGroup.type),
)); )
}); ])),
return Scaffold(
appBar: AppBar(
title: Text(github.activeRoster.name),
),
drawer:
Drawer(child: SingleChildScrollView(child: WarbandDrawerWidget())),
body: SingleChildScrollView(child: Column(children: tiles)),
); );
} }
} }

View file

@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:badges/badges.dart'; import 'package:badges/badges.dart';
import 'package:toolheim/data/github_adapter.dart'; import 'package:toolheim/data/github_adapter.dart';
import 'package:toolheim/data/warband_roster.dart';
import 'package:toolheim/screens/settings_screen.dart'; import 'package:toolheim/screens/settings_screen.dart';
import 'package:url_launcher/url_launcher.dart'; import 'package:url_launcher/url_launcher.dart';
@ -11,21 +10,16 @@ class WarbandDrawerWidget extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
GitHubAdapter github = Provider.of<GitHubAdapter>(context); GitHubAdapter github = Provider.of<GitHubAdapter>(context);
// No settings at all
String settingsText =
'There is no repository set up. Please open the settings and provide a valid GitHub repository.';
// Never fetched any data
if (github.activeRoster == null) {
settingsText =
'The repository is set, but no warbands are found. Try to search for warbands on the settings screen.';
}
if (github.repository == null || github.activeRoster == null) { if (github.repository == null || github.activeRoster == null) {
return Padding( return Padding(
padding: const EdgeInsets.only(top: 100, left: 30, right: 30), padding: const EdgeInsets.only(top: 100, left: 30, right: 30),
child: Column(children: <Widget>[ child: Column(children: <Widget>[
Text(settingsText), if (github.activeRoster == null)
Text(
'The repository is set, but no warbands are found. Try to search for warbands on the settings screen.')
else
Text(
'There is no repository set up. Please open the settings and provide a valid GitHub repository.'),
FlatButton( FlatButton(
onPressed: () { onPressed: () {
Navigator.popAndPushNamed(context, '/settings'); Navigator.popAndPushNamed(context, '/settings');
@ -38,12 +32,7 @@ class WarbandDrawerWidget extends StatelessWidget {
Text( Text(
'If you have no clue what this app is all about, open the help screen and read the introduction.'), 'If you have no clue what this app is all about, open the help screen and read the introduction.'),
FlatButton( FlatButton(
onPressed: () async { onPressed: openHelpWebsite,
const url = '';
if (await canLaunch(url)) {
await launch(url);
}
},
child: Text( child: Text(
'Help', 'Help',
style: TextStyle(color: Colors.blue), style: TextStyle(color: Colors.blue),
@ -53,18 +42,8 @@ class WarbandDrawerWidget extends StatelessWidget {
); );
} }
return buildRosterList(context); return Column(children: <Widget>[
} UserAccountsDrawerHeader(
Widget buildRosterList(BuildContext context) {
GitHubAdapter github = Provider.of<GitHubAdapter>(context);
WarbandRoster activeroster = github.activeRoster;
List<WarbandRoster> rosters = github.rosters;
List<Widget> tiles = [];
// Show some stats for the own warband
tiles.add(UserAccountsDrawerHeader(
margin: const EdgeInsets.all(0), margin: const EdgeInsets.all(0),
otherAccountsPictures: <Widget>[ otherAccountsPictures: <Widget>[
IconButton( IconButton(
@ -74,12 +53,7 @@ class WarbandDrawerWidget extends StatelessWidget {
tooltip: 'Refresh warbands', tooltip: 'Refresh warbands',
onPressed: () async { onPressed: () async {
await github.update(); await github.update();
if (github.syncErrors.length > 0) { if (github.syncErrors.length > 0) {
List<Widget> errors = github.syncErrors.map((error) {
return Text(error);
}).toList();
showDialog( showDialog(
context: context, context: context,
builder: (BuildContext context) { builder: (BuildContext context) {
@ -104,38 +78,24 @@ class WarbandDrawerWidget extends StatelessWidget {
icon: Icon(Icons.help), icon: Icon(Icons.help),
color: Colors.white, color: Colors.white,
highlightColor: Colors.brown, highlightColor: Colors.brown,
onPressed: () { onPressed: openHelpWebsite),
Navigator.popAndPushNamed(context, '/help');
},
),
], ],
accountName: Text(activeroster.name), accountName: Text(github.activeRoster.name),
accountEmail: Text(activeroster.race), accountEmail: Text(github.activeRoster.race),
)); ),
if (github.isSyncInProgress) LinearProgressIndicator(),
tiles.add(Visibility( for (var roster in github.rosters)
visible: github.isSyncInProgress, child: LinearProgressIndicator())); ListTile(
rosters.forEach((roster) {
// We mark inactive warbands with a gray acent
var textColor = Colors.black;
if (!roster.active) {
textColor = Colors.black45;
}
var fontWeight = FontWeight.normal;
if (roster.unseen) {
fontWeight = FontWeight.bold;
}
tiles.add(ListTile(
onTap: () { onTap: () {
roster.unseen = false; roster.unseen = false;
github.activeRoster = roster; github.activeRoster = roster;
Navigator.of(context).pop(); Navigator.of(context).pop();
}, },
title: Text(roster.name + ' (' + roster.playerName + ')', title: Text(roster.name + ' (' + roster.playerName + ')',
style: TextStyle(color: textColor, fontWeight: fontWeight)), style: TextStyle(
color: !roster.active ? Colors.black45 : Colors.black,
fontWeight: roster.unseen ? FontWeight.bold : FontWeight.normal,
)),
subtitle: Text(roster.version.message), subtitle: Text(roster.version.message),
isThreeLine: true, isThreeLine: true,
trailing: Badge( trailing: Badge(
@ -145,11 +105,15 @@ class WarbandDrawerWidget extends StatelessWidget {
badgeContent: Text(roster.campaignPoints.toString() + ' CP', badgeContent: Text(roster.campaignPoints.toString() + ' CP',
style: TextStyle(color: Colors.white)), style: TextStyle(color: Colors.white)),
), ),
)); ),
}); Divider()
]);
}
tiles.add(Divider()); void openHelpWebsite() async {
const url = 'https://google.com/';
return Column(children: tiles); if (await canLaunch(url)) {
await launch(url);
}
} }
} }