Skip to content

Commit

Permalink
✨ Fixed routing on notification click and fixed glitch on homepage at…
Browse files Browse the repository at this point in the history
… every refresh
  • Loading branch information
GiorgioBertolotti committed Oct 19, 2019
1 parent 1a9862a commit 9d20c52
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 243 deletions.
46 changes: 43 additions & 3 deletions pro_time/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:hive/hive.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pro_time/resources/activity_adapter.dart';
import 'package:pro_time/resources/application_state.dart';
import 'package:pro_time/resources/projects_adapter.dart';
import 'package:pro_time/resources/subactivity_adapter.dart';
import 'package:pro_time/ui/home.dart';
import 'package:pro_time/ui/project_page.dart';
import 'package:provider/provider.dart';

FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
Expand All @@ -14,6 +16,7 @@ void main() async {
Hive.registerAdapter(ProjectsAdapter(), 35);
Hive.registerAdapter(ActivityAdapter(), 36);
Hive.registerAdapter(SubActivityAdapter(), 37);
await _openBoxes();
flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
var initializationSettingsAndroid =
AndroidInitializationSettings('ic_notification');
Expand All @@ -22,22 +25,59 @@ void main() async {
initializationSettingsAndroid, initializationSettingsIOS);
flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: onSelectNotification);
var details =
await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
runApp(ProTime());
if (details.didNotificationLaunchApp) {
ProTime.navigatorKey.currentState.pushNamed("/projects/" + details.payload);
}
}

Future onSelectNotification(String id) async {
// TODO: Routing to project detail page
Future _openBoxes() async {
var dir = await getApplicationDocumentsDirectory();
Hive.init(dir.path);
return Future.wait([
Hive.openBox('projects'),
]);
}

Future onSelectNotification(String id) async {}

enum TimerState { STOPPED, STARTED, PAUSED }
final navigatorKey = new GlobalKey<NavigatorState>();

class ProTime extends StatelessWidget {
static final navigatorKey = GlobalKey<NavigatorState>();

Route routes(RouteSettings settings) {
if (settings.name == "/") {
return MaterialPageRoute(
builder: (_) => HomePage(),
);
} else if (settings.name.startsWith("/projects/")) {
try {
String id = settings.name.split("/")[2];
return MaterialPageRoute(
builder: (_) => ProjectPage(id),
);
} catch (e) {
return MaterialPageRoute(
builder: (_) => HomePage(),
);
}
} else {
return MaterialPageRoute(
builder: (_) => HomePage(),
);
}
}

@override
Widget build(BuildContext context) {
return Provider<ApplicationState>.value(
value: ApplicationState(),
child: MaterialApp(
initialRoute: "/",
onGenerateRoute: routes,
title: 'ProTime',
navigatorKey: navigatorKey,
theme: ThemeData(
Expand Down
9 changes: 5 additions & 4 deletions pro_time/lib/resources/new_project_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_colorpicker/flutter_colorpicker.dart';
import 'package:pro_time/main.dart';
import 'package:pro_time/model/project.dart';
import 'package:hive/hive.dart';

Expand Down Expand Up @@ -127,7 +128,7 @@ class _NewProjectDialogState extends State<NewProjectDialog> {
textColor: Colors.deepOrange,
child: Text("Cancel"),
onPressed: () {
Navigator.of(context).pop();
ProTime.navigatorKey.currentState.pop();
},
),
FlatButton(
Expand All @@ -152,7 +153,7 @@ class _NewProjectDialogState extends State<NewProjectDialog> {
);
}
Hive.box("projects").put(proj.id, proj);
Navigator.of(context).pop();
ProTime.navigatorKey.currentState.pop();
},
),
],
Expand Down Expand Up @@ -207,7 +208,7 @@ class _NewProjectDialogState extends State<NewProjectDialog> {
textColor: Colors.deepOrange,
child: Text("Cancel"),
onPressed: () {
Navigator.of(context).pop();
ProTime.navigatorKey.currentState.pop();
},
),
FlatButton(
Expand All @@ -218,7 +219,7 @@ class _NewProjectDialogState extends State<NewProjectDialog> {
if (_tmpMainColor != null) _selectedMainColor = _tmpMainColor;
if (_tmpTextColor != null) _selectedTextColor = _tmpTextColor;
});
Navigator.of(context).pop();
ProTime.navigatorKey.currentState.pop();
},
),
],
Expand Down
Loading

0 comments on commit 9d20c52

Please sign in to comment.