From 73ce25ca52f6a25f457f1c567e3889e5c6dd9399 Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Fri, 2 Aug 2019 00:21:41 +0200 Subject: [PATCH] Refactor the code --- mobile-app/lib/screens/settings_screen.dart | 30 ++- .../lib/screens/warband_roster_screen.dart | 55 +++--- .../lib/widgets/warband_drawer_widget.dart | 186 +++++++----------- 3 files changed, 111 insertions(+), 160 deletions(-) diff --git a/mobile-app/lib/screens/settings_screen.dart b/mobile-app/lib/screens/settings_screen.dart index 94d7429..cfb7ea4 100644 --- a/mobile-app/lib/screens/settings_screen.dart +++ b/mobile-app/lib/screens/settings_screen.dart @@ -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: [ - 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 syncErrors = new List(); - GitHubAdapter github = Provider.of(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)) + ]); } } diff --git a/mobile-app/lib/screens/warband_roster_screen.dart b/mobile-app/lib/screens/warband_roster_screen.dart index 4f21f8d..c5ca1a5 100644 --- a/mobile-app/lib/screens/warband_roster_screen.dart +++ b/mobile-app/lib/screens/warband_roster_screen.dart @@ -37,42 +37,37 @@ class WarbandRosterScreen extends StatelessWidget { child: SingleChildScrollView(child: WarbandDrawerWidget()))); } - List 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), + ) + ])), ); } } diff --git a/mobile-app/lib/widgets/warband_drawer_widget.dart b/mobile-app/lib/widgets/warband_drawer_widget.dart index 946179f..7495edf 100644 --- a/mobile-app/lib/widgets/warband_drawer_widget.dart +++ b/mobile-app/lib/widgets/warband_drawer_widget.dart @@ -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(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: [ - 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(context); - WarbandRoster activeroster = github.activeRoster; - List rosters = github.rosters; - - List tiles = []; - - // Show some stats for the own warband - tiles.add(UserAccountsDrawerHeader( - margin: const EdgeInsets.all(0), - otherAccountsPictures: [ - IconButton( - icon: Icon(Icons.refresh), + return Column(children: [ + UserAccountsDrawerHeader( + margin: const EdgeInsets.all(0), + otherAccountsPictures: [ + 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: [ + 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 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: [ - 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); + } } }