Skip to content

Commit ea425e4

Browse files
refactor: migrate project to Flutter best practice
* BREAKING: remove async gaps from `BuildContext` * See README for migration * BREAKING: remove built-in provider storage * See README for migration * Fix missing await statement for asynchronous tasks Signed-off-by: The one with the braid <[email protected]>
1 parent ca5ed6d commit ea425e4

File tree

5 files changed

+148
-99
lines changed

5 files changed

+148
-99
lines changed

unifiedpush/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 6.0.0
2+
* BREAKING: remove async gaps from `BuildContext`
3+
* See README for migration
4+
* BREAKING: remove built-in provider storage
5+
* See README for migration
6+
* Fix missing await statement for asynchronous tasks
7+
18
## 5.0.1
29
* Upgrade max sdk
310

unifiedpush/README.md

Lines changed: 123 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# UnifiedPush flutter-connector
22

3-
UnifiedPush is specifications and tools that let the user choose how push notifications are delivered. All in a free and open source way.
3+
UnifiedPush is specifications and tools that let the user choose how push notifications are delivered. All in a free and
4+
open source way.
45

56
## Getting Started
67

@@ -9,3 +10,124 @@ Check out the documentation here:
910
1. <https://unifiedpush.org/developers/flutter/>
1011
2. To have Firebase as a fallback, <https://unifiedpush.org/developers/embedded_fcm/>
1112
3. An [example app](../example) can be found here.
13+
14+
## Migrating from UnifiedPush 5.x.x
15+
16+
With version 6.0.0, UnifiedPush removes
17+
18+
- `UnifiedPush.registerAppWithDialog`
19+
- `UnifiedPush.removeNoDistributorDialogACK`
20+
21+
The reason is simple as:
22+
23+
The previous approach was using async gaps in the use of a `BuildContext`. With the new approach, we hand over the
24+
possibility for devs to properly handle async gaps of the registration dialog on their own. Additionally, this enables a
25+
custom registration dialog, localization and better integration into the embedding app. An additional advantage is a
26+
more minimal dependency chain since storage of the UnifiedPush state is now the apps responsibility.
27+
28+
<details>
29+
30+
<summary> Implementation 5.x.x </summary>
31+
32+
```dart
33+
Future<void> _myPushHandler() async {
34+
await UnifiedPush.registerAppWithDialog();
35+
}
36+
37+
Future<void> _myPushRemoveNoHandler() async {
38+
await UnifiedPush.removeNoDistributorDialogACK();
39+
}
40+
```
41+
42+
</details>
43+
44+
<details>
45+
46+
<summary> Migrated to 6.0.0 </summary>
47+
48+
```dart
49+
50+
static const noDistribAck = "noDistributorAck";
51+
52+
Future<void> _myPushHandler() async {
53+
final distributor = await UnifiedPush.getDistributor();
54+
final prefs = await SharedPreferences.getInstance();
55+
String? picked;
56+
57+
if (distributor == null) {
58+
final distributors = await getDistributors(features = features);
59+
if (distributors.isEmpty) {
60+
if (!(prefs.getBool(noDistribAck) ?? false)) {
61+
return showDialog(
62+
context: context,
63+
builder: noDistributorDialog(onDismissed: () {
64+
prefs.setBool(noDistribAck, true);
65+
}));
66+
}
67+
} else if (distributors.length == 1) {
68+
picked = distributors.single;
69+
} else {
70+
picked = await showDialog<String>(
71+
context: context,
72+
builder: pickDistributorDialog(distributors),
73+
);
74+
}
75+
76+
if (picked != null) {
77+
await saveDistributor(picked);
78+
}
79+
}
80+
81+
await registerApp(instance = instance, features = features);
82+
}
83+
84+
Future<void> _myPushRemoveNoHandler() async {
85+
final prefs = await SharedPreferences.getInstance();
86+
prefs.remove(noDistribAck);
87+
}
88+
89+
90+
noDistributorDialog({required Null Function() onDismissed}) {
91+
return (BuildContext context) {
92+
return AlertDialog(
93+
title: const Text('Push Notifications'),
94+
content: const SingleChildScrollView(
95+
child: SelectableText(
96+
"You need to install a distributor for push notifications to work.\nYou can find more information at: https://unifiedpush.org/users/intro/")),
97+
actions: [
98+
TextButton(
99+
child: const Text('Dismiss'),
100+
onPressed: () {
101+
onDismissed();
102+
Navigator.of(context).pop();
103+
},
104+
),
105+
TextButton(
106+
child: const Text('Close'),
107+
onPressed: Navigator.of(context).pop,
108+
),
109+
],
110+
);
111+
};
112+
}
113+
114+
pickDistributorDialog(distributors) {
115+
return (BuildContext context) {
116+
return SimpleDialog(
117+
title: const Text('Select push distributor'),
118+
children: distributors
119+
.map<Widget>(
120+
(d) => SimpleDialogOption(
121+
onPressed: () {
122+
Navigator.pop(context, d);
123+
},
124+
child: Text(d),
125+
),
126+
)
127+
.toList());
128+
};
129+
}
130+
131+
```
132+
133+
</details>

unifiedpush/lib/dialogs.dart

Lines changed: 0 additions & 42 deletions
This file was deleted.

unifiedpush/lib/unifiedpush.dart

Lines changed: 17 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import 'dart:async';
22
import 'dart:typed_data';
3-
import 'package:flutter/material.dart';
4-
import 'package:shared_preferences/shared_preferences.dart';
3+
54
import 'package:unifiedpush_platform_interface/unifiedpush_platform_interface.dart';
65

76
import 'constants.dart';
8-
import 'dialogs.dart';
97

108
class UnifiedPush {
119
static Future<void> initialize({
@@ -15,70 +13,35 @@ class UnifiedPush {
1513
void Function(Uint8List message, String instance)? onMessage,
1614
}) async {
1715
await UnifiedPushPlatform.instance.initializeCallback(
18-
onNewEndpoint: (String e, String i) async => onNewEndpoint?.call(e, i),
19-
onRegistrationFailed: (String i) async => onRegistrationFailed?.call(i),
20-
onUnregistered: (String i) async => onUnregistered?.call(i),
21-
onMessage: (Uint8List m, String i) async => onMessage?.call(m, i));
22-
}
23-
24-
static const noDistribAck = "noDistributorAck";
25-
26-
static Future<void> registerAppWithDialog(BuildContext context,
27-
[String instance = defaultInstance, List<String>? features]) async {
28-
var distributor = await getDistributor();
29-
final prefs = await SharedPreferences.getInstance();
30-
String? picked;
31-
32-
if (distributor == null) {
33-
final distributors = await getDistributors(features = features);
34-
if (distributors.isEmpty) {
35-
if (!(prefs.getBool(noDistribAck) ?? false)) {
36-
return showDialog(
37-
context: context,
38-
builder: noDistributorDialog(onDismissed: () {
39-
prefs.setBool(noDistribAck, true);
40-
}));
41-
}
42-
} else if (distributors.length == 1) {
43-
picked = distributors.single;
44-
} else {
45-
picked = await showDialog<String>(
46-
context: context,
47-
builder: pickDistributorDialog(distributors),
48-
);
49-
}
50-
51-
if (picked != null) {
52-
await saveDistributor(picked);
53-
}
54-
}
55-
56-
await registerApp(instance = instance, features = features);
57-
}
58-
59-
static Future<void> removeNoDistributorDialogACK() async {
60-
final prefs = await SharedPreferences.getInstance();
61-
prefs.remove(noDistribAck);
16+
onNewEndpoint: (String e, String i) async => onNewEndpoint?.call(e, i),
17+
onRegistrationFailed: (String i) async => onRegistrationFailed?.call(i),
18+
onUnregistered: (String i) async => onUnregistered?.call(i),
19+
onMessage: (Uint8List m, String i) async => onMessage?.call(m, i),
20+
);
6221
}
6322

64-
static Future<void> registerApp(
65-
[String instance = defaultInstance, List<String>? features]) async {
66-
UnifiedPushPlatform.instance.registerApp(instance, features ?? []);
23+
static Future<void> registerApp([
24+
String instance = defaultInstance,
25+
List<String> features = const [],
26+
]) async {
27+
await UnifiedPushPlatform.instance.registerApp(instance, features);
6728
}
6829

6930
static Future<void> unregister([String instance = defaultInstance]) async {
70-
UnifiedPushPlatform.instance.unregister(instance);
31+
await UnifiedPushPlatform.instance.unregister(instance);
7132
}
7233

73-
static Future<List<String>> getDistributors([List<String>? features]) async {
74-
return await UnifiedPushPlatform.instance.getDistributors(features ?? []);
34+
static Future<List<String>> getDistributors([
35+
List<String> features = const [],
36+
]) async {
37+
return await UnifiedPushPlatform.instance.getDistributors(features);
7538
}
7639

7740
static Future<String?> getDistributor() async {
7841
return await UnifiedPushPlatform.instance.getDistributor();
7942
}
8043

8144
static Future<void> saveDistributor(String distributor) async {
82-
UnifiedPushPlatform.instance.saveDistributor(distributor);
45+
await UnifiedPushPlatform.instance.saveDistributor(distributor);
8346
}
8447
}

unifiedpush/pubspec.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: unifiedpush
22
description: Push notifications with the provider chosen by the user.
3-
version: 5.0.1
3+
version: 6.0.0
44
homepage: "https://unifiedpush.org/developers/flutter"
55
documentation: "https://unifiedpush.org/developers/flutter"
66
repository: https://github.com/UnifiedPush/flutter-connector
@@ -13,7 +13,6 @@ environment:
1313
dependencies:
1414
flutter:
1515
sdk: flutter
16-
shared_preferences: ^2.0.11
1716
unifiedpush_platform_interface: ^2.0.0
1817
unifiedpush_android: ^2.0.0
1918

0 commit comments

Comments
 (0)