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,8 +54,7 @@ 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,42 +37,37 @@ class WarbandRosterScreen extends StatelessWidget {
child: SingleChildScrollView(child: WarbandDrawerWidget()))); 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( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: Text(github.activeRoster.name), title: Text(github.activeRoster.name),
), ),
drawer: drawer:
Drawer(child: SingleChildScrollView(child: WarbandDrawerWidget())), 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: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,103 +42,78 @@ class WarbandDrawerWidget extends StatelessWidget {
); );
} }
return buildRosterList(context); return Column(children: <Widget>[
} UserAccountsDrawerHeader(
margin: const EdgeInsets.all(0),
Widget buildRosterList(BuildContext context) { otherAccountsPictures: <Widget>[
GitHubAdapter github = Provider.of<GitHubAdapter>(context); IconButton(
WarbandRoster activeroster = github.activeRoster; icon: Icon(Icons.refresh),
List<WarbandRoster> rosters = github.rosters; color: Colors.white,
highlightColor: Colors.brown,
List<Widget> tiles = []; tooltip: 'Refresh warbands',
onPressed: () async {
// Show some stats for the own warband await github.update();
tiles.add(UserAccountsDrawerHeader( if (github.syncErrors.length > 0) {
margin: const EdgeInsets.all(0), showDialog(
otherAccountsPictures: <Widget>[ context: context,
IconButton( builder: (BuildContext context) {
icon: Icon(Icons.refresh), 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, color: Colors.white,
highlightColor: Colors.brown, highlightColor: Colors.brown,
tooltip: 'Refresh warbands', onPressed: () {
onPressed: () async { Navigator.popAndPushNamed(context, '/settings');
await github.update(); },
),
if (github.syncErrors.length > 0) { IconButton(
List<Widget> errors = github.syncErrors.map((error) { icon: Icon(Icons.help),
return Text(error); color: Colors.white,
}).toList(); highlightColor: Colors.brown,
onPressed: openHelpWebsite),
showDialog( ],
context: context, accountName: Text(github.activeRoster.name),
builder: (BuildContext context) { accountEmail: Text(github.activeRoster.race),
return AlertDialog( ),
title: Text('We have some errors while updating.'), if (github.isSyncInProgress) LinearProgressIndicator(),
content: SettingsScreen.buildSyncErrors(context), for (var roster in github.rosters)
actions: <Widget>[ ListTile(
FlatButton(child: Text('Ok'), onPressed: () {}) onTap: () {
]); roster.unseen = false;
}); github.activeRoster = roster;
} Navigator.of(context).pop();
}),
IconButton(
icon: Icon(Icons.settings),
color: Colors.white,
highlightColor: Colors.brown,
onPressed: () {
Navigator.popAndPushNamed(context, '/settings');
}, },
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( Divider()
icon: Icon(Icons.help), ]);
color: Colors.white, }
highlightColor: Colors.brown,
onPressed: () {
Navigator.popAndPushNamed(context, '/help');
},
),
],
accountName: Text(activeroster.name),
accountEmail: Text(activeroster.race),
));
tiles.add(Visibility( void openHelpWebsite() async {
visible: github.isSyncInProgress, child: LinearProgressIndicator())); const url = 'https://google.com/';
if (await canLaunch(url)) {
rosters.forEach((roster) { await launch(url);
// 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);
} }
} }