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(
height: 50,
),
Visibility(
child: CircularProgressIndicator(),
visible: github.isSyncInProgress),
Visibility(
child: buildSyncErrors(context),
visible: github.syncErrors.length > 0,
)
if (github.isSyncInProgress)
CircularProgressIndicator(),
if (github.syncErrors.length > 0)
buildSyncErrors(context),
]),
actions: <Widget>[
Visibility(
visible: !github.isSyncInProgress,
child: FlatButton(
if (!github.isSyncInProgress)
FlatButton(
child: Text('Close',
style: TextStyle(color: Colors.blue)),
onPressed: () {
@ -58,8 +54,7 @@ class SettingsScreen extends StatelessWidget {
Navigator.popAndPushNamed(context, '/');
}
},
),
)
)
]);
});
},
@ -72,14 +67,11 @@ class SettingsScreen extends StatelessWidget {
}
static Widget buildSyncErrors(BuildContext context) {
List<Widget> syncErrors = new List();
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,42 +37,37 @@ class WarbandRosterScreen extends StatelessWidget {
child: SingleChildScrollView(child: WarbandDrawerWidget())));
}
List<Widget> tiles = new List();
github.activeRoster.heros.forEach((hero) {
tiles.add(new ListTile(
title: Text(hero.name),
leading: CircleAvatar(
child: Text(hero.experience.toString()),
backgroundColor: Colors.green,
foregroundColor: Colors.greenAccent,
),
subtitle: Text(hero.type),
));
});
tiles.add(Divider());
github.activeRoster.henchmenGroups.forEach((henchmenGroup) {
tiles.add(new 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),
));
});
return Scaffold(
appBar: AppBar(
title: Text(github.activeRoster.name),
),
drawer:
Drawer(child: SingleChildScrollView(child: WarbandDrawerWidget())),
body: SingleChildScrollView(child: Column(children: tiles)),
body: SingleChildScrollView(
child: Column(children: [
for (var hero in github.activeRoster.heros)
ListTile(
title: Text(hero.name),
leading: CircleAvatar(
child: Text(hero.experience.toString()),
backgroundColor: Colors.green,
foregroundColor: Colors.greenAccent,
),
subtitle: Text(hero.type),
),
Divider(),
for (var henchmenGroup in github.activeRoster.henchmenGroups)
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

@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:badges/badges.dart';
import 'package:toolheim/data/github_adapter.dart';
import 'package:toolheim/data/warband_roster.dart';
import 'package:toolheim/screens/settings_screen.dart';
import 'package:url_launcher/url_launcher.dart';
@ -11,21 +10,16 @@ class WarbandDrawerWidget extends StatelessWidget {
Widget build(BuildContext 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) {
return Padding(
padding: const EdgeInsets.only(top: 100, left: 30, right: 30),
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(
onPressed: () {
Navigator.popAndPushNamed(context, '/settings');
@ -38,12 +32,7 @@ class WarbandDrawerWidget extends StatelessWidget {
Text(
'If you have no clue what this app is all about, open the help screen and read the introduction.'),
FlatButton(
onPressed: () async {
const url = '';
if (await canLaunch(url)) {
await launch(url);
}
},
onPressed: openHelpWebsite,
child: Text(
'Help',
style: TextStyle(color: Colors.blue),
@ -53,103 +42,78 @@ class WarbandDrawerWidget extends StatelessWidget {
);
}
return buildRosterList(context);
}
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),
otherAccountsPictures: <Widget>[
IconButton(
icon: Icon(Icons.refresh),
return Column(children: <Widget>[
UserAccountsDrawerHeader(
margin: const EdgeInsets.all(0),
otherAccountsPictures: <Widget>[
IconButton(
icon: Icon(Icons.refresh),
color: Colors.white,
highlightColor: Colors.brown,
tooltip: 'Refresh warbands',
onPressed: () async {
await github.update();
if (github.syncErrors.length > 0) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('We have some errors while updating.'),
content: SettingsScreen.buildSyncErrors(context),
actions: <Widget>[
FlatButton(child: Text('Ok'), onPressed: () {})
]);
});
}
}),
IconButton(
icon: Icon(Icons.settings),
color: Colors.white,
highlightColor: Colors.brown,
tooltip: 'Refresh warbands',
onPressed: () async {
await github.update();
if (github.syncErrors.length > 0) {
List<Widget> errors = github.syncErrors.map((error) {
return Text(error);
}).toList();
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('We have some errors while updating.'),
content: SettingsScreen.buildSyncErrors(context),
actions: <Widget>[
FlatButton(child: Text('Ok'), onPressed: () {})
]);
});
}
}),
IconButton(
icon: Icon(Icons.settings),
color: Colors.white,
highlightColor: Colors.brown,
onPressed: () {
Navigator.popAndPushNamed(context, '/settings');
onPressed: () {
Navigator.popAndPushNamed(context, '/settings');
},
),
IconButton(
icon: Icon(Icons.help),
color: Colors.white,
highlightColor: Colors.brown,
onPressed: openHelpWebsite),
],
accountName: Text(github.activeRoster.name),
accountEmail: Text(github.activeRoster.race),
),
if (github.isSyncInProgress) LinearProgressIndicator(),
for (var roster in github.rosters)
ListTile(
onTap: () {
roster.unseen = false;
github.activeRoster = roster;
Navigator.of(context).pop();
},
title: Text(roster.name + ' (' + roster.playerName + ')',
style: TextStyle(
color: !roster.active ? Colors.black45 : Colors.black,
fontWeight: roster.unseen ? FontWeight.bold : FontWeight.normal,
)),
subtitle: Text(roster.version.message),
isThreeLine: true,
trailing: Badge(
badgeColor: Colors.black12,
shape: BadgeShape.square,
borderRadius: 2,
badgeContent: Text(roster.campaignPoints.toString() + ' CP',
style: TextStyle(color: Colors.white)),
),
),
IconButton(
icon: Icon(Icons.help),
color: Colors.white,
highlightColor: Colors.brown,
onPressed: () {
Navigator.popAndPushNamed(context, '/help');
},
),
],
accountName: Text(activeroster.name),
accountEmail: Text(activeroster.race),
));
Divider()
]);
}
tiles.add(Visibility(
visible: github.isSyncInProgress, child: LinearProgressIndicator()));
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: () {
roster.unseen = false;
github.activeRoster = roster;
Navigator.of(context).pop();
},
title: Text(roster.name + ' (' + roster.playerName + ')',
style: TextStyle(color: textColor, fontWeight: fontWeight)),
subtitle: Text(roster.version.message),
isThreeLine: true,
trailing: Badge(
badgeColor: Colors.black12,
shape: BadgeShape.square,
borderRadius: 2,
badgeContent: Text(roster.campaignPoints.toString() + ' CP',
style: TextStyle(color: Colors.white)),
),
));
});
tiles.add(Divider());
return Column(children: tiles);
void openHelpWebsite() async {
const url = 'https://google.com/';
if (await canLaunch(url)) {
await launch(url);
}
}
}