Skip to content

Commit 687dbcd

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 95f2bac commit 687dbcd

File tree

5 files changed

+148
-100
lines changed

5 files changed

+148
-100
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.2
29
* Add missing await for async tasks
310
* Deprecate registerAppWithDialog, use unifiedpush_ui if needed

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 & 55 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,66 +13,30 @@ 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-
@Deprecated("Use UnifiedPushUI")
27-
static Future<void> registerAppWithDialog(BuildContext context,
28-
[String instance = defaultInstance, List<String>? features]) async {
29-
var distributor = await getDistributor();
30-
final prefs = await SharedPreferences.getInstance();
31-
String? picked;
32-
33-
if (distributor == null) {
34-
final distributors = await getDistributors(features = features);
35-
if (distributors.isEmpty) {
36-
if (!(prefs.getBool(noDistribAck) ?? false)) {
37-
return showDialog(
38-
context: context,
39-
builder: noDistributorDialog(onDismissed: () {
40-
prefs.setBool(noDistribAck, true);
41-
}));
42-
}
43-
} else if (distributors.length == 1) {
44-
picked = distributors.single;
45-
} else {
46-
picked = await showDialog<String>(
47-
context: context,
48-
builder: pickDistributorDialog(distributors),
49-
);
50-
}
51-
52-
if (picked != null) {
53-
await saveDistributor(picked);
54-
}
55-
}
56-
57-
await registerApp(instance = instance, features = features);
58-
}
59-
60-
static Future<void> removeNoDistributorDialogACK() async {
61-
final prefs = await SharedPreferences.getInstance();
62-
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+
);
6321
}
6422

65-
static Future<void> registerApp(
66-
[String instance = defaultInstance,
67-
List<String>? features = const []]) async {
68-
await 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
28+
.registerApp(instance, features ?? const []);
6929
}
7030

7131
static Future<void> unregister([String instance = defaultInstance]) async {
7232
await UnifiedPushPlatform.instance.unregister(instance);
7333
}
7434

75-
static Future<List<String>> getDistributors(
76-
[List<String>? features = const []]) async {
77-
return await UnifiedPushPlatform.instance.getDistributors(features ?? []);
35+
static Future<List<String>> getDistributors([
36+
List<String>? features = const [],
37+
]) async {
38+
return await UnifiedPushPlatform.instance
39+
.getDistributors(features ?? const []);
7840
}
7941

8042
static Future<String?> getDistributor() async {

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.2
3+
version: 6.0.0
44
homepage: "https://unifiedpush.org/developers/flutter"
55
documentation: "https://unifiedpush.org/developers/flutter"
66
repository: https://codeberg.org/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)