Skip to content

Commit a5986fe

Browse files
committed
feat: Add copyWith function for models reuse
1 parent d53b0cd commit a5986fe

File tree

4 files changed

+79
-6
lines changed

4 files changed

+79
-6
lines changed

lib/models/foreground_task_options.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,21 @@ class ForegroundTaskOptions {
4242
'allowWifiLock': allowWifiLock,
4343
};
4444
}
45+
46+
/// Creates a copy of the object replaced with new values.
47+
ForegroundTaskOptions copyWith({
48+
ForegroundTaskEventAction? eventAction,
49+
bool? autoRunOnBoot,
50+
bool? autoRunOnMyPackageReplaced,
51+
bool? allowWakeLock,
52+
bool? allowWifiLock,
53+
}) =>
54+
ForegroundTaskOptions(
55+
eventAction: eventAction ?? this.eventAction,
56+
autoRunOnBoot: autoRunOnBoot ?? this.autoRunOnBoot,
57+
autoRunOnMyPackageReplaced:
58+
autoRunOnMyPackageReplaced ?? this.autoRunOnMyPackageReplaced,
59+
allowWakeLock: allowWakeLock ?? this.allowWakeLock,
60+
allowWifiLock: allowWifiLock ?? this.allowWifiLock,
61+
);
4562
}

lib/models/notification_button.dart

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,22 @@ class NotificationButton {
2323

2424
/// Returns the data fields of [NotificationButton] in JSON format.
2525
Map<String, dynamic> toJson() {
26-
final String? textColorRgb = textColor?.toRgbString;
27-
2826
return {
2927
'id': id,
3028
'text': text,
31-
'textColorRgb': textColorRgb,
29+
'textColorRgb': textColor?.toRgbString,
3230
};
3331
}
32+
33+
/// Creates a copy of the object replaced with new values.
34+
NotificationButton copyWith({
35+
String? id,
36+
String? text,
37+
Color? textColor,
38+
}) =>
39+
NotificationButton(
40+
id: id ?? this.id,
41+
text: text ?? this.text,
42+
textColor: textColor ?? this.textColor,
43+
);
3444
}

lib/models/notification_icon.dart

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,19 @@ class NotificationIcon {
1818

1919
/// Returns the data fields of [NotificationIcon] in JSON format.
2020
Map<String, dynamic> toJson() {
21-
final String? backgroundColorRgb = backgroundColor?.toRgbString;
22-
2321
return {
2422
'metaDataName': metaDataName,
25-
'backgroundColorRgb': backgroundColorRgb,
23+
'backgroundColorRgb': backgroundColor?.toRgbString,
2624
};
2725
}
26+
27+
/// Creates a copy of the object replaced with new values.
28+
NotificationIcon copyWith({
29+
String? metaDataName,
30+
Color? backgroundColor,
31+
}) =>
32+
NotificationIcon(
33+
metaDataName: metaDataName ?? this.metaDataName,
34+
backgroundColor: backgroundColor ?? this.backgroundColor,
35+
);
2836
}

lib/models/notification_options.dart

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,34 @@ class AndroidNotificationOptions {
9696
'visibility': visibility.rawValue,
9797
};
9898
}
99+
100+
/// Creates a copy of the object replaced with new values.
101+
AndroidNotificationOptions copyWith({
102+
String? channelId,
103+
String? channelName,
104+
String? channelDescription,
105+
NotificationChannelImportance? channelImportance,
106+
NotificationPriority? priority,
107+
bool? enableVibration,
108+
bool? playSound,
109+
bool? showWhen,
110+
bool? showBadge,
111+
bool? onlyAlertOnce,
112+
NotificationVisibility? visibility,
113+
}) =>
114+
AndroidNotificationOptions(
115+
channelId: channelId ?? this.channelId,
116+
channelName: channelName ?? this.channelName,
117+
channelDescription: channelDescription ?? this.channelDescription,
118+
channelImportance: channelImportance ?? this.channelImportance,
119+
priority: priority ?? this.priority,
120+
enableVibration: enableVibration ?? this.enableVibration,
121+
playSound: playSound ?? this.playSound,
122+
showWhen: showWhen ?? this.showWhen,
123+
showBadge: showBadge ?? this.showBadge,
124+
onlyAlertOnce: onlyAlertOnce ?? this.onlyAlertOnce,
125+
visibility: visibility ?? this.visibility,
126+
);
99127
}
100128

101129
/// Notification options for iOS platform.
@@ -121,4 +149,14 @@ class IOSNotificationOptions {
121149
'playSound': playSound,
122150
};
123151
}
152+
153+
/// Creates a copy of the object replaced with new values.
154+
IOSNotificationOptions copyWith({
155+
bool? showNotification,
156+
bool? playSound,
157+
}) =>
158+
IOSNotificationOptions(
159+
showNotification: showNotification ?? this.showNotification,
160+
playSound: playSound ?? this.playSound,
161+
);
124162
}

0 commit comments

Comments
 (0)