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, pushNotificationsKey: state.pushNotificationsKey,
onConfigured: state.rebuild, onConfigured: state.rebuild,
onChanged: state.rebuild, onChanged: state.rebuild,
visible: state.areNotificationsConfigured,
), ),
// Forez specific features // 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/independent_push_notifications_helper.dart';
import 'package:comunic/helpers/preferences_helper.dart'; import 'package:comunic/helpers/preferences_helper.dart';
import 'package:comunic/helpers/server_config_helper.dart'; import 'package:comunic/helpers/server_config_helper.dart';
import 'package:comunic/models/api_request.dart'; import 'package:comunic/models/api_request.dart';
import 'package:comunic/utils/flutter_utils.dart'; import 'package:comunic/utils/flutter_utils.dart';
import 'package:flutter/material.dart';
/// Push notifications helper /// Push notifications helper
/// ///
@ -58,6 +60,34 @@ class PushNotificationsHelper {
IndependentPushNotificationsHelper.disable(); 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 /// Set new push notification status on the server
static Future<void> setNewStatus( static Future<void> setNewStatus(
PushNotificationsStatus newStatus, { 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/push_notifications_helper.dart';
import 'package:comunic/helpers/server_config_helper.dart'; import 'package:comunic/helpers/server_config_helper.dart';
import 'package:comunic/models/config.dart'; import 'package:comunic/models/config.dart';
@ -162,35 +160,12 @@ class PushNotificationsConfigurationWidgetState
Future<bool> submit() async { Future<bool> submit() async {
try { try {
String firebaseToken = "";
if (currStatus == await PushNotificationsHelper.getLocalStatus()) { if (currStatus == await PushNotificationsHelper.getLocalStatus()) {
widget.onConfigured(); widget.onConfigured();
return true; return true;
} }
switch (currStatus) { await PushNotificationsHelper.configure(context, 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();
widget.onConfigured(); widget.onConfigured();
} catch (e, s) { } catch (e, s) {

View File

@ -1,4 +1,6 @@
import 'package:comunic/helpers/preferences_helper.dart'; 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/helpers/users_helper.dart';
import 'package:comunic/models/config.dart'; import 'package:comunic/models/config.dart';
import 'package:comunic/models/user.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/ui/widgets/tour/tour_notifications_pane.dart';
import 'package:comunic/utils/account_utils.dart'; import 'package:comunic/utils/account_utils.dart';
import 'package:comunic/utils/intl_utils.dart'; import 'package:comunic/utils/intl_utils.dart';
import 'package:comunic/utils/log_utils.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
/// Tour route /// Tour route
@ -41,9 +44,27 @@ class TourRouteState extends State<TourRoute> {
int _defaultIndex = 0; int _defaultIndex = 0;
bool areNotificationsConfigured = false;
Future<void> _init() async { Future<void> _init() async {
currUser = currUser =
await UsersHelper().getSingleWithThrow(userID(), forceDownload: true); 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(() {}); void rebuild() => setState(() {});
@ -53,7 +74,7 @@ class TourRouteState extends State<TourRoute> {
await _key.currentState.refresh(); await _key.currentState.refresh();
} }
List<Widget> get _list => config().toursEntriesBuilder != null List<Widget> get _list => (config().toursEntriesBuilder != null
? config().toursEntriesBuilder(this) ? config().toursEntriesBuilder(this)
: [ : [
FirstTourPane(), FirstTourPane(),
@ -69,6 +90,7 @@ class TourRouteState extends State<TourRoute> {
pushNotificationsKey: pushNotificationsKey, pushNotificationsKey: pushNotificationsKey,
onConfigured: () => setState(() {}), onConfigured: () => setState(() {}),
onChanged: () => setState(() {}), onChanged: () => setState(() {}),
visible: !areNotificationsConfigured,
), ),
PresentationPane( 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!")}", "${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(), LastTourPane(),
]; ])
..removeWhere((pane) {
if (pane is PresentationPane) {
PresentationPane p = pane;
return !(p.visible ?? true);
}
return false;
});
@override @override
Widget build(BuildContext context) => LoginRoutesTheme( Widget build(BuildContext context) => LoginRoutesTheme(

View File

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

View File

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