Cleanup and widget refactor

This commit is contained in:
Ricardo Markiewicz
2020-07-16 16:26:51 -03:00
parent 7f6dfa4fa3
commit a416359a43
8 changed files with 255 additions and 138 deletions
+9
View File
@@ -1,8 +1,11 @@
import 'package:como_gasto/states/login_state.dart'; import 'package:como_gasto/states/login_state.dart';
import 'package:firebase_auth/firebase_auth.dart'; import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/foundation.dart';
import 'package:get_it/get_it.dart'; import 'package:get_it/get_it.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'pages/notifications/local_notifications.dart';
final GetIt _l = GetIt.instance; final GetIt _l = GetIt.instance;
Future<void> init() async { Future<void> init() async {
@@ -17,4 +20,10 @@ Future<void> init() async {
firebaseAuth: _l(), firebaseAuth: _l(),
), ),
); );
if (kIsWeb) {
_l.registerSingleton<LocalNotifications>(WebLocalNotifications());
} else {
_l.registerSingleton<LocalNotifications>(MobileLocalNotifications());
}
} }
+28 -138
View File
@@ -2,14 +2,19 @@ import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:como_gasto/como_gasto_icons.dart'; import 'package:como_gasto/como_gasto_icons.dart';
import 'package:como_gasto/como_gasto_localizations.dart'; import 'package:como_gasto/como_gasto_localizations.dart';
import 'package:como_gasto/others/month_widget.dart'; import 'package:como_gasto/others/month_widget.dart';
import 'package:como_gasto/pages/notifications/local_notifications.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:rect_getter/rect_getter.dart'; import 'package:rect_getter/rect_getter.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import '../expenses_repository.dart'; import '../expenses_repository.dart';
import '../utils.dart'; import '../utils.dart';
import 'ui/loading_widget.dart';
import 'ui/month_selector_slider.dart';
import 'ui/no_data_widget.dart';
import 'ui/stream_widget.dart';
class HomePage extends StatefulWidget { class HomePage extends StatefulWidget {
@override @override
@@ -17,11 +22,9 @@ class HomePage extends StatefulWidget {
} }
class _HomePageState extends State<HomePage> { class _HomePageState extends State<HomePage> {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
var globalKey = RectGetter.createGlobalKey(); var globalKey = RectGetter.createGlobalKey();
Rect buttonRect; Rect buttonRect;
PageController _controller;
int currentPage = DateTime.now().month - 1; int currentPage = DateTime.now().month - 1;
Stream<QuerySnapshot> _query; Stream<QuerySnapshot> _query;
GraphType currentType = GraphType.LINES; GraphType currentType = GraphType.LINES;
@@ -30,11 +33,6 @@ class _HomePageState extends State<HomePage> {
void initState() { void initState() {
super.initState(); super.initState();
_controller = PageController(
initialPage: currentPage,
viewportFraction: 0.4,
);
setupNotificationPlugin(); setupNotificationPlugin();
} }
@@ -98,139 +96,43 @@ class _HomePageState extends State<HomePage> {
} }
Widget _body() { Widget _body() {
ComoGastoLocalizations localizations =
Localizations.of<ComoGastoLocalizations>(
context, ComoGastoLocalizations);
return SafeArea( return SafeArea(
child: Column( child: Column(
children: <Widget>[ children: <Widget>[
_selector(localizations), MonthSelectorSlider(
StreamBuilder<QuerySnapshot>( currentPage: currentPage,
stream: _query, onPageChanged: (newPage) {
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> data) { var db = Provider.of<ExpensesRepository>(context, listen: false);
if (data.connectionState == ConnectionState.active) {
if (data.data.documents.length > 0) {
return MonthWidget(
days: daysInMonth(currentPage + 1),
documents: data.data.documents,
graphType: currentType,
month: currentPage,
);
} else {
return Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.asset('assets/no_data.png'),
SizedBox(height: 80),
Text(
localizations.t('home.emptyList'),
style: Theme.of(context).textTheme.caption,
)
],
),
);
}
}
return Expanded( setState(() {
child: Center( currentPage = newPage;
child: CircularProgressIndicator(), _query = db.queryByMonth(currentPage + 1);
), });
);
}, },
), ),
StreamWidget(
stream: _query,
loading: (_) => LoadingWidget(),
empty: (_) => NoDataWidget(),
done: (_, data) => MonthWidget(
days: daysInMonth(currentPage + 1),
documents: data.documents,
graphType: currentType,
month: currentPage,
),
),
], ],
), ),
); );
} }
Widget _pageItem(String name, int position) {
var _alignment;
final selected = TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: Colors.blueGrey,
);
final unselected = TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.normal,
color: Colors.blueGrey.withOpacity(0.4),
);
if (position == currentPage) {
_alignment = Alignment.center;
} else if (position > currentPage) {
_alignment = Alignment.centerRight;
} else {
_alignment = Alignment.centerLeft;
}
return Align(
alignment: _alignment,
child: Text(
name,
style: position == currentPage ? selected : unselected,
),
);
}
Widget _selector(ComoGastoLocalizations localizations) {
return SizedBox.fromSize(
size: Size.fromHeight(70.0),
child: PageView(
onPageChanged: (newPage) {
var db = Provider.of<ExpensesRepository>(context);
setState(() {
currentPage = newPage;
_query = db.queryByMonth(currentPage + 1);
});
},
controller: _controller,
children: <Widget>[
_pageItem(localizations.t("months.jan"), 0),
_pageItem(localizations.t("months.feb"), 1),
_pageItem(localizations.t("months.mar"), 2),
_pageItem(localizations.t("months.apr"), 3),
_pageItem(localizations.t("months.may"), 4),
_pageItem(localizations.t("months.jun"), 5),
_pageItem(localizations.t("months.jul"), 6),
_pageItem(localizations.t("months.aug"), 7),
_pageItem(localizations.t("months.sep"), 8),
_pageItem(localizations.t("months.oct"), 9),
_pageItem(localizations.t("months.nov"), 10),
_pageItem(localizations.t("months.dec"), 11),
],
),
);
}
void setupNotificationPlugin() { void setupNotificationPlugin() {
if (kIsWeb) { LocalNotifications localNotifications = GetIt.I();
return;
}
flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
// initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project localNotifications.init(
var initializationSettingsAndroid = onSelectNotification: onSelectNotification,
new AndroidInitializationSettings('app_icon');
var initializationSettingsIOS = new IOSInitializationSettings(
onDidReceiveLocalNotification: onDidReceiveLocalNotification, onDidReceiveLocalNotification: onDidReceiveLocalNotification,
); );
var initializationSettings = new InitializationSettings(
initializationSettingsAndroid,
initializationSettingsIOS,
);
flutterLocalNotificationsPlugin
.initialize(
initializationSettings,
onSelectNotification: onSelectNotification,
)
.then((init) {
setupNotification();
});
} }
Future onSelectNotification(String payload) async { Future onSelectNotification(String payload) async {
@@ -260,16 +162,4 @@ class _HomePageState extends State<HomePage> {
], ],
)); ));
} }
void setupNotification() async {
var time = new Time(16, 11, 0);
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'daily-notifications', 'Daily Notifications', 'Daily Notifications');
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.showDailyAtTime(0, 'Spend-o-meter',
"Don't forget to add your expenses", time, platformChannelSpecifics);
}
} }
@@ -0,0 +1,61 @@
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
abstract class LocalNotifications {
void init({
@required SelectNotificationCallback onSelectNotification,
@required DidReceiveLocalNotificationCallback onDidReceiveLocalNotification,
});
}
class WebLocalNotifications extends LocalNotifications {
@override
void init({onSelectNotification, onDidReceiveLocalNotification}) {
// no-op
}
}
class MobileLocalNotifications extends LocalNotifications {
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
MobileLocalNotifications()
: flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
@override
void init({
@required SelectNotificationCallback onSelectNotification,
@required DidReceiveLocalNotificationCallback onDidReceiveLocalNotification,
}) {
// initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
var initializationSettingsAndroid =
new AndroidInitializationSettings('app_icon');
var initializationSettingsIOS = new IOSInitializationSettings(
onDidReceiveLocalNotification: onDidReceiveLocalNotification,
);
var initializationSettings = new InitializationSettings(
initializationSettingsAndroid,
initializationSettingsIOS,
);
flutterLocalNotificationsPlugin
.initialize(
initializationSettings,
onSelectNotification: onSelectNotification,
)
.then((init) {
_setupNotification();
});
}
void _setupNotification() async {
var time = new Time(16, 11, 0);
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'daily-notifications', 'Daily Notifications', 'Daily Notifications');
var iOSPlatformChannelSpecifics = new IOSNotificationDetails();
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.showDailyAtTime(0, 'Spend-o-meter',
"Don't forget to add your expenses", time, platformChannelSpecifics);
}
}
+14
View File
@@ -0,0 +1,14 @@
import 'package:flutter/material.dart';
class LoadingWidget extends StatelessWidget {
const LoadingWidget({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Expanded(
child: Center(
child: CircularProgressIndicator(),
),
);
}
}
+86
View File
@@ -0,0 +1,86 @@
import 'package:como_gasto/como_gasto_localizations.dart';
import 'package:flutter/material.dart';
class MonthSelectorSlider extends StatefulWidget {
final int currentPage;
final ValueChanged<int> onPageChanged;
const MonthSelectorSlider({Key key, this.currentPage, this.onPageChanged})
: super(key: key);
@override
_MonthSelectorSliderState createState() => _MonthSelectorSliderState();
}
class _MonthSelectorSliderState extends State<MonthSelectorSlider> {
PageController _controller;
@override
void initState() {
super.initState();
_controller = PageController(
initialPage: widget.currentPage,
viewportFraction: 0.4,
);
}
@override
Widget build(BuildContext context) {
ComoGastoLocalizations localizations =
Localizations.of<ComoGastoLocalizations>(
context, ComoGastoLocalizations);
return SizedBox.fromSize(
size: Size.fromHeight(70.0),
child: PageView(
onPageChanged: widget.onPageChanged,
controller: _controller,
children: <Widget>[
_pageItem(localizations.t("months.jan"), 0),
_pageItem(localizations.t("months.feb"), 1),
_pageItem(localizations.t("months.mar"), 2),
_pageItem(localizations.t("months.apr"), 3),
_pageItem(localizations.t("months.may"), 4),
_pageItem(localizations.t("months.jun"), 5),
_pageItem(localizations.t("months.jul"), 6),
_pageItem(localizations.t("months.aug"), 7),
_pageItem(localizations.t("months.sep"), 8),
_pageItem(localizations.t("months.oct"), 9),
_pageItem(localizations.t("months.nov"), 10),
_pageItem(localizations.t("months.dec"), 11),
],
),
);
}
Widget _pageItem(String name, int position) {
var _alignment;
final selected = TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: Colors.blueGrey,
);
final unselected = TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.normal,
color: Colors.blueGrey.withOpacity(0.4),
);
if (position == widget.currentPage) {
_alignment = Alignment.center;
} else if (position > widget.currentPage) {
_alignment = Alignment.centerRight;
} else {
_alignment = Alignment.centerLeft;
}
return Align(
alignment: _alignment,
child: Text(
name,
style: position == widget.currentPage ? selected : unselected,
),
);
}
}
+25
View File
@@ -0,0 +1,25 @@
import 'package:como_gasto/como_gasto_localizations.dart';
import 'package:flutter/material.dart';
class NoDataWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
ComoGastoLocalizations localizations =
Localizations.of<ComoGastoLocalizations>(
context, ComoGastoLocalizations);
return Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Image.asset('assets/no_data.png'),
SizedBox(height: 80),
Text(
localizations.t('home.emptyList'),
style: Theme.of(context).textTheme.caption,
)
],
),
);
}
}
+31
View File
@@ -0,0 +1,31 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
class StreamWidget extends StatelessWidget {
final Stream<QuerySnapshot> stream;
final Widget Function(BuildContext) empty;
final Widget Function(BuildContext) loading;
final Widget Function(BuildContext, QuerySnapshot) done;
const StreamWidget(
{Key key, this.stream, this.empty, this.loading, this.done})
: super(key: key);
@override
Widget build(BuildContext _) {
return StreamBuilder<QuerySnapshot>(
stream: stream,
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> data) {
if (data.connectionState == ConnectionState.active) {
if (data.data.documents.length > 0) {
return done(context, data.data);
} else {
return empty(context);
}
}
return loading(context);
},
);
}
}
+1
View File
@@ -64,6 +64,7 @@ class LoginState with ChangeNotifier {
_authenticationProvider.logout(); _authenticationProvider.logout();
_authenticationProvider = null; _authenticationProvider = null;
} }
_auth.signOut();
_loggedIn = false; _loggedIn = false;
notifyListeners(); notifyListeners();
} }