Redux in home

This commit is contained in:
Vicente J. Ruiz Jurado 2018-08-13 08:25:53 +02:00
parent cb71bb24b3
commit 3982bb525a

View file

@ -7,9 +7,11 @@ import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:fires_flutter/models/fireNotification.dart'; import 'package:fires_flutter/models/fireNotification.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:flutter_simple_dependency_injection/injector.dart'; import 'package:flutter_simple_dependency_injection/injector.dart';
import 'package:redux/redux.dart'; import 'package:redux/redux.dart';
import 'firesSpinner.dart';
import 'activeFires.dart'; import 'activeFires.dart';
import 'colors.dart'; import 'colors.dart';
import 'fireAlert.dart'; import 'fireAlert.dart';
@ -19,6 +21,23 @@ import 'mainDrawer.dart';
import 'models/appState.dart'; import 'models/appState.dart';
import 'redux/actions.dart'; import 'redux/actions.dart';
class _ViewModel {
final bool isLoaded;
_ViewModel({@required this.isLoaded});
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is _ViewModel &&
runtimeType == other.runtimeType &&
isLoaded == other.isLoaded;
@override
int get hashCode => isLoaded.hashCode;
}
class HomePage extends StatefulWidget { class HomePage extends StatefulWidget {
static const String routeName = '/home'; static const String routeName = '/home';
@ -31,8 +50,8 @@ class HomePage extends StatefulWidget {
class _HomePageState extends State<HomePage> { class _HomePageState extends State<HomePage> {
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>(); final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
final Store<AppState> store = Injector.getInjector().get<Store<AppState>>(); final Store<AppState> store = Injector.getInjector().get<Store<AppState>>();
final Connectivity _connectivity = new Connectivity(); final Connectivity _connectivity = new Connectivity();
final List<FireNotification> newNotifications = [];
// Platform messages are asynchronous, so we initialize in an async method. // Platform messages are asynchronous, so we initialize in an async method.
Future<Null> initConnectivity() async { Future<Null> initConnectivity() async {
@ -64,14 +83,14 @@ class _HomePageState extends State<HomePage> {
void initState() { void initState() {
super.initState(); super.initState();
_firebaseMessaging.configure(onMessage: (Map<String, dynamic> message) { _firebaseMessaging.configure(onMessage: (Map<String, dynamic> message) {
debugPrint("onMessage in fireApp: $message"); debugPrint("onMessage in fireApp (isLoaded: ${store.state.isLoaded}): $message");
_showItemDialog(message, _notifForMessage(message)); _showItemDialog(message, _notifForMessage(message));
}, onLaunch: (Map<String, dynamic> message) { }, onLaunch: (Map<String, dynamic> message) {
debugPrint("onLaunch: $message"); debugPrint("onLaunch (isLoaded: ${store.state.isLoaded}): $message");
_notifForMessage(message); _notifForMessage(message);
_navigateToItemDetail(message); _navigateToItemDetail(message);
}, onResume: (Map<String, dynamic> message) { }, onResume: (Map<String, dynamic> message) {
debugPrint("onResume: $message"); debugPrint("onResume (isLoaded: ${store.state.isLoaded}): $message");
_notifForMessage(message); _notifForMessage(message);
_navigateToItemDetail(message); _navigateToItemDetail(message);
}); });
@ -111,75 +130,95 @@ class _HomePageState extends State<HomePage> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return new Scaffold( return new StoreConnector<AppState, _ViewModel>(
key: _scaffoldKey, distinct: true,
drawer: new MainDrawer(context, HomePage.routeName), converter: (store) {
floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat, bool isLoaded = store.state.isLoaded;
floatingActionButton: if (isLoaded && newNotifications.isNotEmpty) {
Column(mainAxisAlignment: MainAxisAlignment.end, children: <Widget>[ newNotifications.forEach((notif) {
FloatingActionButton.extended( store.dispatch(new AddFireNotificationAction(notif));
elevation: 0.0, });
onPressed: () { newNotifications.clear();
Navigator.pushNamed(context, ActiveFiresPage.routeName); }
}, return new _ViewModel(
label: Text(S.of(context).activeFires, style: _btnFont), isLoaded: store.state.isLoaded
backgroundColor: fires600, );
heroTag: 'activeFires', },
icon: const Icon(Icons.whatshot, size: 32.0), builder: (context, view) {
), return new Scaffold(
Padding( key: _scaffoldKey,
padding: const EdgeInsets.only(top: 16.0, bottom: 26.0), drawer: new MainDrawer(context, HomePage.routeName),
child: FloatingActionButton.extended( floatingActionButtonLocation:
elevation: 0.0, FloatingActionButtonLocation.centerFloat,
onPressed: () { floatingActionButton: Column(
Navigator.pushNamed(context, FireAlert.routeName); mainAxisAlignment: MainAxisAlignment.end,
}, children: <Widget>[
heroTag: 'notifyFire', FloatingActionButton.extended(
backgroundColor: fires600, elevation: 0.0,
label: new Text(S.of(context).notifyAFire, style: _btnFont),
icon: const Icon(Icons.notifications_active, size: 32.0),
),
),
]),
body: new SafeArea(
child: Center(
child: new CenteredColumn(children: <Widget>[
new Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
new IconButton(
onPressed: () { onPressed: () {
_scaffoldKey.currentState.openDrawer(); Navigator.pushNamed(context, ActiveFiresPage.routeName);
}, },
icon: new Icon(Icons.menu, label: Text(S.of(context).activeFires, style: _btnFont),
size: 30.0, color: Colors.black38)), backgroundColor: fires600,
]), heroTag: 'activeFires',
new Expanded( icon: const Icon(Icons.whatshot, size: 32.0),
child: new FractionallySizedBox( ),
alignment: FractionalOffset.center, Padding(
heightFactor: 0.7, padding: const EdgeInsets.only(top: 16.0, bottom: 26.0),
child: new Image.asset('images/logo-200.png', child: FloatingActionButton.extended(
fit: BoxFit.fitHeight))), elevation: 0.0,
new Expanded( onPressed: () {
child: new FractionallySizedBox( Navigator.pushNamed(context, FireAlert.routeName);
alignment: FractionalOffset.topCenter, },
heightFactor: 1.0, heroTag: 'notifyFire',
child: new Column( backgroundColor: fires600,
label: new Text(S.of(context).notifyAFire,
style: _btnFont),
icon:
const Icon(Icons.notifications_active, size: 32.0),
),
),
]),
body: !view.isLoaded? new FiresSpinner(): new SafeArea(
child: Center(
child: new CenteredColumn(children: <Widget>[
new Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[ children: <Widget>[
new Padding( new IconButton(
padding: const EdgeInsets.symmetric( onPressed: () {
vertical: 10.0, horizontal: 20.0), _scaffoldKey.currentState.openDrawer();
child: FittedBox( },
child: new Text(S.of(context).appName, icon: new Icon(Icons.menu,
maxLines: 2, size: 30.0, color: Colors.black38)),
textAlign: TextAlign.center, ]),
style: _homeFont), new Expanded(
fit: BoxFit.scaleDown, child: new FractionallySizedBox(
)), alignment: FractionalOffset.center,
], heightFactor: 0.7,
))) child: new Image.asset('images/logo-200.png',
])), fit: BoxFit.fitHeight))),
)); new Expanded(
child: new FractionallySizedBox(
alignment: FractionalOffset.topCenter,
heightFactor: 1.0,
child: new Column(
children: <Widget>[
new Padding(
padding: const EdgeInsets.symmetric(
vertical: 10.0, horizontal: 20.0),
child: FittedBox(
child: new Text(S.of(context).appName,
maxLines: 2,
textAlign: TextAlign.center,
style: _homeFont),
fit: BoxFit.scaleDown,
)),
],
)))
])),
));
});
} }
void _showDialog(String message) { void _showDialog(String message) {
@ -259,7 +298,8 @@ class _HomePageState extends State<HomePage> {
} catch (e) { } catch (e) {
debugPrint(e.toString()); debugPrint(e.toString());
} }
if (notif != null) store.dispatch(new AddFireNotificationAction(notif)); if (notif != null)
newNotifications.add(notif);
return notif; return notif;
} }
} }