1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-24 22:09:21 +00:00

Attempt to auto-configure push notifications to remove one tour pane

This commit is contained in:
Pierre HUBERT 2021-04-26 10:49:33 +02:00
parent 5da8a64d3d
commit 8a581d158a
6 changed files with 67 additions and 28 deletions

View File

@ -45,6 +45,7 @@ List<Widget> buildTour(TourRouteState state) {
pushNotificationsKey: state.pushNotificationsKey,
onConfigured: state.rebuild,
onChanged: state.rebuild,
visible: state.areNotificationsConfigured,
),
// Forez specific features

View File

@ -1,8 +1,10 @@
import 'package:comunic/helpers/firebase_messaging_helper.dart';
import 'package:comunic/helpers/independent_push_notifications_helper.dart';
import 'package:comunic/helpers/preferences_helper.dart';
import 'package:comunic/helpers/server_config_helper.dart';
import 'package:comunic/models/api_request.dart';
import 'package:comunic/utils/flutter_utils.dart';
import 'package:flutter/material.dart';
/// Push notifications helper
///
@ -58,6 +60,34 @@ class PushNotificationsHelper {
IndependentPushNotificationsHelper.disable();
}
/// Configure push notifications
static Future<void> configure(
BuildContext context, PushNotificationsStatus newStatus) async {
String firebaseToken = "";
switch (newStatus) {
case PushNotificationsStatus.DISABLED:
break;
case PushNotificationsStatus.FIREBASE:
await FirebaseMessagingHelper.preConfigure();
firebaseToken = await FirebaseMessagingHelper.getToken();
break;
case PushNotificationsStatus.INDEPENDENT:
if (await IndependentPushNotificationsHelper.needPreConfiguration())
await IndependentPushNotificationsHelper.preConfigure(context);
break;
default:
throw new Exception("Unknown status!");
}
await PushNotificationsHelper.clearLocalStatus();
await PushNotificationsHelper.setNewStatus(newStatus,
firebaseToken: firebaseToken);
await PushNotificationsHelper.refreshLocalStatus();
}
/// Set new push notification status on the server
static Future<void> setNewStatus(
PushNotificationsStatus newStatus, {

View File

@ -1,5 +1,3 @@
import 'package:comunic/helpers/firebase_messaging_helper.dart';
import 'package:comunic/helpers/independent_push_notifications_helper.dart';
import 'package:comunic/helpers/push_notifications_helper.dart';
import 'package:comunic/helpers/server_config_helper.dart';
import 'package:comunic/models/config.dart';
@ -162,35 +160,12 @@ class PushNotificationsConfigurationWidgetState
Future<bool> submit() async {
try {
String firebaseToken = "";
if (currStatus == await PushNotificationsHelper.getLocalStatus()) {
widget.onConfigured();
return true;
}
switch (currStatus) {
case PushNotificationsStatus.DISABLED:
break;
case PushNotificationsStatus.FIREBASE:
await FirebaseMessagingHelper.preConfigure();
firebaseToken = await FirebaseMessagingHelper.getToken();
break;
case PushNotificationsStatus.INDEPENDENT:
if (await IndependentPushNotificationsHelper.needPreConfiguration())
await IndependentPushNotificationsHelper.preConfigure(context);
break;
default:
throw new Exception("Unknown status!");
}
await PushNotificationsHelper.clearLocalStatus();
await PushNotificationsHelper.setNewStatus(currStatus,
firebaseToken: firebaseToken);
await PushNotificationsHelper.refreshLocalStatus();
await PushNotificationsHelper.configure(context, currStatus);
widget.onConfigured();
} catch (e, s) {

View File

@ -1,4 +1,6 @@
import 'package:comunic/helpers/preferences_helper.dart';
import 'package:comunic/helpers/push_notifications_helper.dart';
import 'package:comunic/helpers/server_config_helper.dart';
import 'package:comunic/helpers/users_helper.dart';
import 'package:comunic/models/config.dart';
import 'package:comunic/models/user.dart';
@ -12,6 +14,7 @@ import 'package:comunic/ui/widgets/tour/presentation_pane.dart';
import 'package:comunic/ui/widgets/tour/tour_notifications_pane.dart';
import 'package:comunic/utils/account_utils.dart';
import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/log_utils.dart';
import 'package:flutter/material.dart';
/// Tour route
@ -41,9 +44,27 @@ class TourRouteState extends State<TourRoute> {
int _defaultIndex = 0;
bool areNotificationsConfigured = false;
Future<void> _init() async {
currUser =
await UsersHelper().getSingleWithThrow(userID(), forceDownload: true);
// Pre-configure notifications
final notificationsStatus = await PushNotificationsHelper.getLocalStatus();
if (!PushNotificationsHelper.arePushNotificationsAvailable ||
notificationsStatus != PushNotificationsStatus.UNDEFINED)
areNotificationsConfigured = true;
// Attempt to automatically register to FCM
else if (srvConfig.notificationsPolicy.hasFirebase) {
try {
await PushNotificationsHelper.configure(
context, PushNotificationsStatus.FIREBASE);
} catch (e, s) {
logError(e, s);
}
}
}
void rebuild() => setState(() {});
@ -53,7 +74,7 @@ class TourRouteState extends State<TourRoute> {
await _key.currentState.refresh();
}
List<Widget> get _list => config().toursEntriesBuilder != null
List<Widget> get _list => (config().toursEntriesBuilder != null
? config().toursEntriesBuilder(this)
: [
FirstTourPane(),
@ -69,6 +90,7 @@ class TourRouteState extends State<TourRoute> {
pushNotificationsKey: pushNotificationsKey,
onConfigured: () => setState(() {}),
onChanged: () => setState(() {}),
visible: !areNotificationsConfigured,
),
PresentationPane(
@ -96,7 +118,14 @@ class TourRouteState extends State<TourRoute> {
"${tr("Your data is YOUR DATA. We will never use it or sell it.")}\n\n${tr("If you do not trust us, you can always check out our source code to verify it!")}",
),
LastTourPane(),
];
])
..removeWhere((pane) {
if (pane is PresentationPane) {
PresentationPane p = pane;
return !(p.visible ?? true);
}
return false;
});
@override
Widget build(BuildContext context) => LoginRoutesTheme(

View File

@ -14,6 +14,7 @@ class PresentationPane extends StatelessWidget {
final Function(BuildContext) onActionTap;
final bool canGoNext;
final Future<bool> Function(BuildContext) onTapNext;
final bool visible;
const PresentationPane({
Key key,
@ -26,6 +27,7 @@ class PresentationPane extends StatelessWidget {
this.onActionTap,
this.canGoNext = true,
this.onTapNext,
this.visible = true,
}) : assert(icon != null || iconWidget != null),
assert(title != null),
assert(text != null || child != null),

View File

@ -17,6 +17,7 @@ class TourNotificationsPane extends PresentationPane {
pushNotificationsKey,
@required Function() onConfigured,
@required Function() onChanged,
@required bool visible,
}) : assert(pushNotificationsKey != null),
super(
icon: Icons.notifications,
@ -28,5 +29,6 @@ class TourNotificationsPane extends PresentationPane {
),
canGoNext: pushNotificationsKey?.currentState?.canSubmit ?? false,
onTapNext: (c) => pushNotificationsKey.currentState.submit(),
visible: visible,
);
}