Skip to content

Commit babf4da

Browse files
committed
feat: phoenix | user clicks after restore to reload the app
1 parent 1ae1116 commit babf4da

6 files changed

Lines changed: 40 additions & 35 deletions

File tree

lib/core/services/notification_service.dart

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
2+
import 'package:flutter_phoenix/flutter_phoenix.dart';
3+
import 'package:fuck_your_todos/main.dart';
24

35
class NotificationService {
46
static final NotificationService _instance = NotificationService._internal();
@@ -37,7 +39,13 @@ class NotificationService {
3739
await flutterLocalNotificationsPlugin.initialize(
3840
settings: initializationSettings,
3941
onDidReceiveNotificationResponse: (NotificationResponse response) async {
40-
// Handle notification interaction
42+
if (response.payload == 'restart_app' ||
43+
response.actionId == 'restart_app_action') {
44+
final context = navigatorKey.currentContext;
45+
if (context != null) {
46+
Phoenix.rebirth(context);
47+
}
48+
}
4149
},
4250
);
4351

@@ -65,23 +73,34 @@ class NotificationService {
6573
required String title,
6674
required String body,
6775
String? payload,
76+
bool showRestartButton = false,
6877
}) async {
6978
if (!_isInitialized) {
7079
await init();
7180
}
7281

73-
const AndroidNotificationDetails androidPlatformChannelSpecifics =
82+
final AndroidNotificationDetails androidPlatformChannelSpecifics =
7483
AndroidNotificationDetails(
7584
'instant_backup_and_restore_channel_id',
7685
'Backup and restore',
7786
importance: Importance.max,
7887
priority: Priority.high,
88+
actions: showRestartButton
89+
? <AndroidNotificationAction>[
90+
const AndroidNotificationAction(
91+
'restart_app_action',
92+
'Restart App',
93+
cancelNotification: true,
94+
showsUserInterface: true,
95+
),
96+
]
97+
: null,
7998
);
8099

81100
const DarwinNotificationDetails iosNotificationDetails =
82101
DarwinNotificationDetails();
83102

84-
const NotificationDetails platformChannelSpecifics = NotificationDetails(
103+
final NotificationDetails platformChannelSpecifics = NotificationDetails(
85104
android: androidPlatformChannelSpecifics,
86105
iOS: iosNotificationDetails,
87106
);
@@ -91,7 +110,7 @@ class NotificationService {
91110
title: title,
92111
body: body,
93112
notificationDetails: platformChannelSpecifics,
94-
payload: payload,
113+
payload: 'restart_app',
95114
);
96115
}
97116

lib/feature/error_screen/global_error_screen.dart

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:flutter/material.dart';
22
import 'package:url_launcher/url_launcher.dart';
3+
import 'package:flutter_phoenix/flutter_phoenix.dart';
34

45
class GlobalErrorScreen extends StatelessWidget {
56
final FlutterErrorDetails errorDetails;
@@ -90,7 +91,7 @@ class GlobalErrorScreen extends StatelessWidget {
9091
OutlinedButton.icon(
9192
onPressed: () {
9293
// Trigger the global restart routine at the root level!
93-
AppRestarter.restartApp(context);
94+
Phoenix.rebirth(context);
9495
},
9596
icon: const Icon(
9697
Icons.refresh_rounded,
@@ -119,32 +120,3 @@ class GlobalErrorScreen extends StatelessWidget {
119120
);
120121
}
121122
}
122-
123-
/// A wrapper to restart the entire app tree from scratch when instructed.
124-
class AppRestarter extends StatefulWidget {
125-
final Widget child;
126-
127-
const AppRestarter({super.key, required this.child});
128-
129-
static void restartApp(BuildContext context) {
130-
context.findAncestorStateOfType<_AppRestarterState>()?.restartApp();
131-
}
132-
133-
@override
134-
State<AppRestarter> createState() => _AppRestarterState();
135-
}
136-
137-
class _AppRestarterState extends State<AppRestarter> {
138-
Key _key = UniqueKey();
139-
140-
void restartApp() {
141-
setState(() {
142-
_key = UniqueKey(); // Re-creates the whole underlying tree!
143-
});
144-
}
145-
146-
@override
147-
Widget build(BuildContext context) {
148-
return KeyedSubtree(key: _key, child: widget.child);
149-
}
150-
}

lib/feature/settings_screen/Screens/data_and_privacy_screen/services/backup_and_restore.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ class BackupAndRestoreService {
196196
title: 'Backup restored',
197197
body:
198198
'Backup restored successfully. Please restart app to see effects.',
199+
showRestartButton: true,
199200
);
200201
}
201202
} catch (e) {

lib/main.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ import 'package:flutter/services.dart';
22
import 'package:dynamic_color/dynamic_color.dart';
33
import 'package:flutter/material.dart';
44
import 'package:flutter_riverpod/flutter_riverpod.dart';
5+
import 'package:flutter_phoenix/flutter_phoenix.dart';
56
import 'package:fuck_your_todos/core/theme/theme_provider.dart';
67
import 'package:fuck_your_todos/main_app_screen.dart';
78
import 'package:fuck_your_todos/feature/error_screen/global_error_screen.dart';
89
import 'package:fuck_your_todos/core/services/app_preferences.dart';
910

11+
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
12+
1013
void main() async {
1114
WidgetsFlutterBinding.ensureInitialized();
1215
await AppPreferences.init();
@@ -30,7 +33,7 @@ void main() async {
3033
return GlobalErrorScreen(errorDetails: details);
3134
};
3235

33-
runApp(AppRestarter(child: ProviderScope(child: MyApp())));
36+
runApp(Phoenix(child: ProviderScope(child: MyApp())));
3437
}
3538

3639
class MyApp extends ConsumerWidget {
@@ -61,6 +64,7 @@ class MyApp extends ConsumerWidget {
6164
}
6265

6366
return MaterialApp(
67+
navigatorKey: navigatorKey,
6468
theme: buildTheme(lightScheme, Brightness.light, false),
6569
darkTheme: buildTheme(darkScheme, Brightness.dark, pureDark),
6670
themeMode: themeMode,

pubspec.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,14 @@ packages:
390390
url: "https://pub.dev"
391391
source: hosted
392392
version: "2.0.1"
393+
flutter_phoenix:
394+
dependency: "direct main"
395+
description:
396+
name: flutter_phoenix
397+
sha256: "39589dac934ea476d0e43fb60c1ddfba58f14960743640c8250dea11c4333378"
398+
url: "https://pub.dev"
399+
source: hosted
400+
version: "1.1.1"
393401
flutter_plugin_android_lifecycle:
394402
dependency: transitive
395403
description:

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ dependencies:
3333
flutter_secure_storage: ^10.0.0
3434
flutter_local_notifications: ^20.1.0
3535
archive: ^4.0.9
36+
flutter_phoenix: ^1.1.1
3637

3738
dev_dependencies:
3839
flutter_test:

0 commit comments

Comments
 (0)