Skip to content

Commit 77b451a

Browse files
committed
feat: add factory constructors for notifications
1 parent 3d1320c commit 77b451a

File tree

3 files changed

+88
-5
lines changed

3 files changed

+88
-5
lines changed

lib/interop/js_notification/js_notification.dart

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,21 @@ class JSNotification implements Serializable {
1010
/// See: https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification#options
1111
final JSNotificationOptions? options;
1212

13-
JSNotification(this.title, [this.options]);
13+
const JSNotification(this.title, [this.options]);
14+
15+
JSNotification copyWithSelf(JSNotification other) {
16+
return JSNotification(
17+
other.title,
18+
other.options,
19+
);
20+
}
21+
22+
JSNotification copyWith({String? title, JSNotificationOptions? options}) {
23+
return JSNotification(
24+
title ?? this.title,
25+
options ?? this.options,
26+
);
27+
}
1428

1529
@override
1630
Map<String, dynamic> toMap() {

lib/interop/js_notification/js_notification_action.dart

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,35 @@ class JSNotificationAction implements Serializable {
1414

1515
const JSNotificationAction({required this.action, required this.title, this.icon});
1616

17-
factory JSNotificationAction.fromTitle(String title, {bool actionToLowerCase = true}) {
18-
final action = actionToLowerCase ? title.toLowerCase() : title;
17+
factory JSNotificationAction.fromTitle(String title, {bool transformToLowerCase = true}) {
18+
final action = transformToLowerCase ? title.toLowerCase() : title;
1919
return JSNotificationAction(action: action, title: title);
2020
}
2121

22-
factory JSNotificationAction.fromAction(String action, {bool capatlizeTitle = true}) {
23-
final title = capatlizeTitle ? action.capitalize() : action;
22+
factory JSNotificationAction.fromAction(String action, {bool capitalize = true}) {
23+
final title = capitalize ? action.capitalize() : action;
2424
return JSNotificationAction(action: action, title: title);
2525
}
2626

2727
factory JSNotificationAction.simpleWithIcon(String title, String icon) =>
2828
JSNotificationAction(action: title, title: title, icon: icon);
2929

30+
JSNotificationAction copyWithSelf(JSNotificationAction other) {
31+
return JSNotificationAction(
32+
action: other.action,
33+
title: other.title,
34+
icon: other.icon,
35+
);
36+
}
37+
38+
JSNotificationAction copyWith({String? action, String? title, String? icon}) {
39+
return JSNotificationAction(
40+
action: action ?? this.action,
41+
title: title ?? this.title,
42+
icon: icon ?? this.icon,
43+
);
44+
}
45+
3046
@override
3147
Map<String, dynamic> toMap() {
3248
final map = {

lib/interop/js_notification/js_notification_options.dart

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,59 @@ class JSNotificationOptions implements Serializable {
6868
this.vibrate,
6969
});
7070

71+
JSNotificationOptions copyWithSelf({JSNotificationOptions? options}) {
72+
return JSNotificationOptions(
73+
actions: options?.actions ?? actions,
74+
badge: options?.badge ?? badge,
75+
body: options?.body ?? body,
76+
data: options?.data ?? data,
77+
dir: options?.dir ?? dir,
78+
icon: options?.icon ?? icon,
79+
image: options?.image ?? image,
80+
lang: options?.lang ?? lang,
81+
renotify: options?.renotify ?? renotify,
82+
requireInteraction: options?.requireInteraction ?? requireInteraction,
83+
silent: options?.silent ?? silent,
84+
tag: options?.tag ?? tag,
85+
timestamp: options?.timestamp ?? timestamp,
86+
vibrate: options?.vibrate ?? vibrate,
87+
);
88+
}
89+
90+
JSNotificationOptions copyWith({
91+
List<JSNotificationAction>? actions,
92+
int? badge,
93+
String? body,
94+
Map<String, dynamic>? data,
95+
JSNotificationDirection? dir,
96+
String? icon,
97+
String? image,
98+
String? lang,
99+
bool? renotify,
100+
bool? requireInteraction,
101+
bool? silent,
102+
String? tag,
103+
int? timestamp,
104+
VibratePattern? vibrate,
105+
}) {
106+
return JSNotificationOptions(
107+
actions: actions ?? this.actions,
108+
badge: badge ?? this.badge,
109+
body: body ?? this.body,
110+
data: data ?? this.data,
111+
dir: dir ?? this.dir,
112+
icon: icon ?? this.icon,
113+
image: image ?? this.image,
114+
lang: lang ?? this.lang,
115+
renotify: renotify ?? this.renotify,
116+
requireInteraction: requireInteraction ?? this.requireInteraction,
117+
silent: silent ?? this.silent,
118+
tag: tag ?? this.tag,
119+
timestamp: timestamp ?? this.timestamp,
120+
vibrate: vibrate ?? this.vibrate,
121+
);
122+
}
123+
71124
@override
72125
Map<String, dynamic> toMap() {
73126
final Map<String, dynamic> map = {};

0 commit comments

Comments
 (0)