Merge pull request #1996 from famedly/krille/refactor-push-notification-object

refactor: Push Notification helper class make all fields optional and migrate dynamics to Object?
This commit is contained in:
Karthikeyan S 2025-01-08 14:39:52 +05:30 committed by GitHub
commit 32e051c439
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 45 additions and 38 deletions

View File

@ -2,9 +2,9 @@ import 'dart:convert';
/// Push Notification object from https://spec.matrix.org/v1.2/push-gateway-api/ /// Push Notification object from https://spec.matrix.org/v1.2/push-gateway-api/
class PushNotification { class PushNotification {
final Map<String, dynamic>? content; final Map<String, Object?>? content;
final PushNotificationCounts? counts; final PushNotificationCounts? counts;
final List<PushNotificationDevice> devices; final List<PushNotificationDevice>? devices;
final String? eventId; final String? eventId;
final String? prio; final String? prio;
final String? roomAlias; final String? roomAlias;
@ -17,7 +17,7 @@ class PushNotification {
const PushNotification({ const PushNotification({
this.content, this.content,
this.counts, this.counts,
required this.devices, this.devices,
this.eventId, this.eventId,
this.prio, this.prio,
this.roomAlias, this.roomAlias,
@ -30,39 +30,44 @@ class PushNotification {
/// Generate a Push Notification object from JSON. It also supports a /// Generate a Push Notification object from JSON. It also supports a
/// `map<String, String>` which usually comes from Firebase Cloud Messaging. /// `map<String, String>` which usually comes from Firebase Cloud Messaging.
factory PushNotification.fromJson(Map<String, dynamic> json) => factory PushNotification.fromJson(Map<String, Object?> json) =>
PushNotification( PushNotification(
content: json['content'] is Map content: json['content'] is Map
? Map<String, dynamic>.from(json['content']) ? Map<String, Object?>.from(json['content'] as Map)
: json['content'] is String : json['content'] is String
? jsonDecode(json['content']) ? jsonDecode(json['content'] as String)
: null, : null,
counts: json['counts'] is Map counts: json['counts'] is Map
? PushNotificationCounts.fromJson(json['counts']) ? PushNotificationCounts.fromJson(
json['counts'] as Map<String, Object?>,
)
: json['counts'] is String : json['counts'] is String
? PushNotificationCounts.fromJson(jsonDecode(json['counts'])) ? PushNotificationCounts.fromJson(
jsonDecode(json['counts'] as String),
)
: null, : null,
devices: json['devices'] is List devices: json['devices'] is List
? (json['devices'] as List) ? (json['devices'] as List)
.map((d) => PushNotificationDevice.fromJson(d)) .map((d) => PushNotificationDevice.fromJson(d))
.toList() .toList()
: (jsonDecode(json['devices']) as List) : (jsonDecode(json['devices'] as String) as List)
.map((d) => PushNotificationDevice.fromJson(d)) .map((d) => PushNotificationDevice.fromJson(d))
.toList(), .toList(),
eventId: json['event_id'], eventId: json['event_id'] as String?,
prio: json['prio'], prio: json['prio'] as String?,
roomAlias: json['room_alias'], roomAlias: json['room_alias'] as String?,
roomId: json['room_id'], roomId: json['room_id'] as String?,
roomName: json['room_name'], roomName: json['room_name'] as String?,
sender: json['sender'], sender: json['sender'] as String?,
senderDisplayName: json['sender_display_name'], senderDisplayName: json['sender_display_name'] as String?,
type: json['type'], type: json['type'] as String?,
); );
Map<String, dynamic> toJson() => { Map<String, Object?> toJson() => {
if (content != null) 'content': content, if (content != null) 'content': content,
if (counts != null) 'counts': counts?.toJson(), if (counts != null) 'counts': counts?.toJson(),
'devices': devices.map((i) => i.toJson()).toList(), if (devices != null)
'devices': devices?.map((i) => i.toJson()).toList(),
if (eventId != null) 'event_id': eventId, if (eventId != null) 'event_id': eventId,
if (prio != null) 'prio': prio, if (prio != null) 'prio': prio,
if (roomAlias != null) 'room_alias': roomAlias, if (roomAlias != null) 'room_alias': roomAlias,
@ -83,45 +88,47 @@ class PushNotificationCounts {
this.unread, this.unread,
}); });
factory PushNotificationCounts.fromJson(Map<String, dynamic> json) => factory PushNotificationCounts.fromJson(Map<String, Object?> json) =>
PushNotificationCounts( PushNotificationCounts(
missedCalls: json['missed_calls'], missedCalls: json['missed_calls'] as int?,
unread: json['unread'], unread: json['unread'] as int?,
); );
Map<String, dynamic> toJson() => { Map<String, Object?> toJson() => {
if (missedCalls != null) 'missed_calls': missedCalls, if (missedCalls != null) 'missed_calls': missedCalls,
if (unread != null) 'unread': unread, if (unread != null) 'unread': unread,
}; };
} }
class PushNotificationDevice { class PushNotificationDevice {
final String appId; final String? appId;
final Map<String, dynamic>? data; final Map<String, Object?>? data;
final String pushkey; final String? pushkey;
final int? pushkeyTs; final int? pushkeyTs;
final Tweaks? tweaks; final Tweaks? tweaks;
const PushNotificationDevice({ const PushNotificationDevice({
required this.appId, this.appId,
this.data, this.data,
required this.pushkey, this.pushkey,
this.pushkeyTs, this.pushkeyTs,
this.tweaks, this.tweaks,
}); });
factory PushNotificationDevice.fromJson(Map<String, dynamic> json) => factory PushNotificationDevice.fromJson(Map<String, Object?> json) =>
PushNotificationDevice( PushNotificationDevice(
appId: json['app_id'], appId: json['app_id'] as String?,
data: json['data'] == null data: json['data'] == null
? null ? null
: Map<String, dynamic>.from(json['data']), : Map<String, Object?>.from(json['data'] as Map),
pushkey: json['pushkey'], pushkey: json['pushkey'] as String?,
pushkeyTs: json['pushkey_ts'], pushkeyTs: json['pushkey_ts'] as int?,
tweaks: json['tweaks'] == null ? null : Tweaks.fromJson(json['tweaks']), tweaks: json['tweaks'] == null
? null
: Tweaks.fromJson(json['tweaks'] as Map<String, Object?>),
); );
Map<String, dynamic> toJson() => { Map<String, Object?> toJson() => {
'app_id': appId, 'app_id': appId,
if (data != null) 'data': data, if (data != null) 'data': data,
'pushkey': pushkey, 'pushkey': pushkey,
@ -137,11 +144,11 @@ class Tweaks {
this.sound, this.sound,
}); });
factory Tweaks.fromJson(Map<String, dynamic> json) => Tweaks( factory Tweaks.fromJson(Map<String, Object?> json) => Tweaks(
sound: json['sound'], sound: json['sound'] as String?,
); );
Map<String, dynamic> toJson() => { Map<String, Object?> toJson() => {
if (sound != null) 'sound': sound, if (sound != null) 'sound': sound,
}; };
} }