Cleanup and widget refactor
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import 'package:como_gasto/states/login_state.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'pages/notifications/local_notifications.dart';
|
||||
|
||||
final GetIt _l = GetIt.instance;
|
||||
|
||||
Future<void> init() async {
|
||||
@@ -17,4 +20,10 @@ Future<void> init() async {
|
||||
firebaseAuth: _l(),
|
||||
),
|
||||
);
|
||||
|
||||
if (kIsWeb) {
|
||||
_l.registerSingleton<LocalNotifications>(WebLocalNotifications());
|
||||
} else {
|
||||
_l.registerSingleton<LocalNotifications>(MobileLocalNotifications());
|
||||
}
|
||||
}
|
||||
|
||||
+28
-138
@@ -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_localizations.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/material.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:rect_getter/rect_getter.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
|
||||
import '../expenses_repository.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 {
|
||||
@override
|
||||
@@ -17,11 +22,9 @@ class HomePage extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _HomePageState extends State<HomePage> {
|
||||
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
|
||||
var globalKey = RectGetter.createGlobalKey();
|
||||
Rect buttonRect;
|
||||
|
||||
PageController _controller;
|
||||
int currentPage = DateTime.now().month - 1;
|
||||
Stream<QuerySnapshot> _query;
|
||||
GraphType currentType = GraphType.LINES;
|
||||
@@ -30,11 +33,6 @@ class _HomePageState extends State<HomePage> {
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
_controller = PageController(
|
||||
initialPage: currentPage,
|
||||
viewportFraction: 0.4,
|
||||
);
|
||||
|
||||
setupNotificationPlugin();
|
||||
}
|
||||
|
||||
@@ -98,139 +96,43 @@ class _HomePageState extends State<HomePage> {
|
||||
}
|
||||
|
||||
Widget _body() {
|
||||
ComoGastoLocalizations localizations =
|
||||
Localizations.of<ComoGastoLocalizations>(
|
||||
context, ComoGastoLocalizations);
|
||||
return SafeArea(
|
||||
child: Column(
|
||||
children: <Widget>[
|
||||
_selector(localizations),
|
||||
StreamBuilder<QuerySnapshot>(
|
||||
stream: _query,
|
||||
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> data) {
|
||||
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,
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
MonthSelectorSlider(
|
||||
currentPage: currentPage,
|
||||
onPageChanged: (newPage) {
|
||||
var db = Provider.of<ExpensesRepository>(context, listen: false);
|
||||
|
||||
return Expanded(
|
||||
child: Center(
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
);
|
||||
setState(() {
|
||||
currentPage = newPage;
|
||||
_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() {
|
||||
if (kIsWeb) {
|
||||
return;
|
||||
}
|
||||
flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
|
||||
LocalNotifications localNotifications = GetIt.I();
|
||||
|
||||
// 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(
|
||||
localNotifications.init(
|
||||
onSelectNotification: onSelectNotification,
|
||||
onDidReceiveLocalNotification: onDidReceiveLocalNotification,
|
||||
);
|
||||
var initializationSettings = new InitializationSettings(
|
||||
initializationSettingsAndroid,
|
||||
initializationSettingsIOS,
|
||||
);
|
||||
|
||||
flutterLocalNotificationsPlugin
|
||||
.initialize(
|
||||
initializationSettings,
|
||||
onSelectNotification: onSelectNotification,
|
||||
)
|
||||
.then((init) {
|
||||
setupNotification();
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -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(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -64,6 +64,7 @@ class LoginState with ChangeNotifier {
|
||||
_authenticationProvider.logout();
|
||||
_authenticationProvider = null;
|
||||
}
|
||||
_auth.signOut();
|
||||
_loggedIn = false;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user