22// ignore_for_file: lines_longer_than_80_chars
33
44import 'package:equatable/equatable.dart' ;
5- import 'package:json_annotation/json_annotation.dart' ;
6-
7- part 'notification_settings.g.dart' ;
5+ import 'package:meta/meta.dart' ;
86
97/// {@template notification_settings}
108/// Represents the user's notification settings.
119///
1210/// Stores lists of followed category, source, and country IDs for notifications.
1311/// Assumes notifications are primarily for breaking news within these follows.
1412/// {@endtemplate}
15- @JsonSerializable ()
13+ @immutable
1614class NotificationSettings extends Equatable {
1715 /// {@macro notification_settings}
1816 const NotificationSettings ({
@@ -23,8 +21,40 @@ class NotificationSettings extends Equatable {
2321 });
2422
2523 /// Creates a [NotificationSettings] instance from a JSON map.
26- factory NotificationSettings .fromJson (Map <String , dynamic > json) =>
27- _$NotificationSettingsFromJson (json);
24+ ///
25+ /// Throws a [FormatException] if the JSON is invalid.
26+ factory NotificationSettings .fromJson (Map <String , dynamic > json) {
27+ final enabled = json['enabled' ] as bool ? ;
28+ final categoryNotificationsRaw = json['categoryNotifications' ] as List ? ;
29+ final sourceNotificationsRaw = json['sourceNotifications' ] as List ? ;
30+ final followedEventCountryIdsRaw = json['followedEventCountryIds' ] as List ? ;
31+
32+ if (enabled == null ) {
33+ throw const FormatException (
34+ 'Missing required field "enabled" in NotificationSettings JSON.' ,
35+ );
36+ }
37+
38+ // Safely cast lists, defaulting to empty if null or invalid type
39+ final categoryNotifications =
40+ categoryNotificationsRaw? .whereType <String >().toList (growable: false ) ??
41+ const < String > [];
42+ final sourceNotifications =
43+ sourceNotificationsRaw? .whereType <String >().toList (growable: false ) ??
44+ const < String > [];
45+ final followedEventCountryIds =
46+ followedEventCountryIdsRaw? .whereType <String >().toList (
47+ growable: false ,
48+ ) ??
49+ const < String > [];
50+
51+ return NotificationSettings (
52+ enabled: enabled,
53+ categoryNotifications: categoryNotifications,
54+ sourceNotifications: sourceNotifications,
55+ followedEventCountryIds: followedEventCountryIds,
56+ );
57+ }
2858
2959 /// Whether notifications are enabled globally.
3060 final bool enabled;
@@ -39,7 +69,14 @@ class NotificationSettings extends Equatable {
3969 final List <String > followedEventCountryIds;
4070
4171 /// Converts this [NotificationSettings] instance to a JSON map.
42- Map <String , dynamic > toJson () => _$NotificationSettingsToJson (this );
72+ Map <String , dynamic > toJson () {
73+ return {
74+ 'enabled' : enabled,
75+ 'categoryNotifications' : categoryNotifications,
76+ 'sourceNotifications' : sourceNotifications,
77+ 'followedEventCountryIds' : followedEventCountryIds,
78+ };
79+ }
4380
4481 @override
4582 List <Object ?> get props => [
0 commit comments