Skip to content

Commit ae1bb4c

Browse files
committed
refactor: remove json_serializable
- Remove code generation - Use manual json conversion - Add json converters util
1 parent 45b458c commit ae1bb4c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+967
-967
lines changed

lib/src/models/feed/ad.dart

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1+
import 'package:equatable/equatable.dart';
12
import 'package:ht_shared/src/models/feed/ad_placement.dart';
23
import 'package:ht_shared/src/models/feed/feed_item.dart';
3-
import 'package:ht_shared/src/models/feed/feed_item_action.dart'
4-
show FeedItemAction, feedItemActionFromJson, feedItemActionToJson;
5-
import 'package:json_annotation/json_annotation.dart';
4+
import 'package:ht_shared/src/models/feed/feed_item_action.dart';
5+
import 'package:ht_shared/src/utils/json_converters.dart';
66
import 'package:uuid/uuid.dart';
77

8-
part 'ad.g.dart';
9-
108
/// {@template ad_type}
119
/// Defines the visual format or type of an advertisement in the feed.
1210
/// {@endtemplate}
13-
@JsonEnum(fieldRename: FieldRename.snake)
1411
enum AdType {
1512
/// A banner advertisement, typically a rectangular image.
1613
banner,
@@ -28,21 +25,31 @@ enum AdType {
2825
/// {@template ad}
2926
/// Represents an advertisement item that can appear in the feed.
3027
/// {@endtemplate}
31-
@JsonSerializable(explicitToJson: true, includeIfNull: false)
3228
class Ad extends FeedItem {
3329
/// {@macro ad}
3430
Ad({
3531
required this.imageUrl,
3632
required this.targetUrl,
3733
required this.adType,
38-
required super.action,
34+
required super.action, // Refactored to super.action
3935
this.placement,
4036
String? id,
4137
}) : id = id ?? const Uuid().v4(),
42-
super(type: 'ad');
38+
super(type: 'ad'); // Removed action from super constructor
4339

4440
/// Factory method to create an [Ad] instance from a JSON map.
45-
factory Ad.fromJson(Map<String, dynamic> json) => _$AdFromJson(json);
41+
factory Ad.fromJson(Map<String, dynamic> json) {
42+
return Ad(
43+
id: json['id'] as String?,
44+
imageUrl: json['imageUrl'] as String,
45+
targetUrl: json['targetUrl'] as String,
46+
adType: adTypeFromJson(json['adType'] as String),
47+
placement: json['placement'] == null
48+
? null
49+
: adPlacementFromJson(json['placement'] as String),
50+
action: FeedItemAction.fromJson(json['action'] as Map<String, dynamic>),
51+
);
52+
}
4653

4754
/// Unique identifier for the ad.
4855
final String id;
@@ -60,14 +67,20 @@ class Ad extends FeedItem {
6067
/// for this ad in the UI.
6168
final AdPlacement? placement;
6269

63-
/// The action to be performed when this feed item is interacted with.
64-
@JsonKey(fromJson: feedItemActionFromJson, toJson: feedItemActionToJson)
65-
@override
66-
late final FeedItemAction action;
67-
6870
/// Converts this [Ad] instance to a JSON map.
6971
@override
70-
Map<String, dynamic> toJson() => _$AdToJson(this);
72+
Map<String, dynamic> toJson() {
73+
final Map<String, dynamic> json = {
74+
'id': id,
75+
'imageUrl': imageUrl,
76+
'targetUrl': targetUrl,
77+
'adType': adTypeToJson(adType),
78+
'placement': placement == null ? null : adPlacementToJson(placement!),
79+
'action': action.toJson(),
80+
'type': type, // Inherited from FeedItem
81+
};
82+
return json..removeWhere((key, value) => value == null);
83+
}
7184

7285
@override
7386
List<Object?> get props => [

lib/src/models/feed/ad.g.dart

Lines changed: 0 additions & 38 deletions
This file was deleted.

lib/src/models/feed/engagement_content.dart

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,44 @@
1+
import 'package:equatable/equatable.dart';
12
import 'package:ht_shared/src/models/feed/engagement_content_type.dart';
23
import 'package:ht_shared/src/models/feed/feed_item.dart';
3-
import 'package:ht_shared/src/models/feed/feed_item_action.dart'
4-
show FeedItemAction, feedItemActionFromJson, feedItemActionToJson;
5-
import 'package:json_annotation/json_annotation.dart';
4+
import 'package:ht_shared/src/models/feed/feed_item_action.dart';
5+
import 'package:ht_shared/src/utils/json_converters.dart';
66
import 'package:uuid/uuid.dart';
77

8-
part 'engagement_content.g.dart';
9-
108
/// {@template engagement_content}
119
/// A generic model for in-feed calls-to-action or engagement prompts.
1210
///
1311
/// This item encourages user interaction, such as signing up, upgrading,
1412
/// or providing feedback. The [engagementContentType] specifies the nature
1513
/// of the call-to-action.
1614
/// {@endtemplate}
17-
@JsonSerializable(explicitToJson: true, includeIfNull: false)
1815
class EngagementContent extends FeedItem {
1916
/// {@macro engagement_content}
2017
EngagementContent({
2118
required this.title,
2219
required this.engagementContentType,
23-
required super.action,
20+
required super.action, // Refactored to super.action
2421
this.description,
2522
this.callToActionText,
2623
this.callToActionUrl,
2724
String? id,
2825
}) : id = id ?? const Uuid().v4(),
29-
super(type: 'engagement_content');
26+
super(type: 'engagement_content'); // Removed action from super constructor
3027

3128
/// Factory method to create an [EngagementContent] instance from a JSON map.
32-
factory EngagementContent.fromJson(Map<String, dynamic> json) =>
33-
_$EngagementContentFromJson(json);
29+
factory EngagementContent.fromJson(Map<String, dynamic> json) {
30+
return EngagementContent(
31+
id: json['id'] as String?,
32+
title: json['title'] as String,
33+
description: json['description'] as String?,
34+
engagementContentType: engagementContentTypeFromJson(
35+
json['engagementContentType'] as String,
36+
),
37+
callToActionText: json['callToActionText'] as String?,
38+
callToActionUrl: json['callToActionUrl'] as String?,
39+
action: FeedItemAction.fromJson(json['action'] as Map<String, dynamic>),
40+
);
41+
}
3442

3543
/// Unique identifier for the engagement content.
3644
final String id;
@@ -50,14 +58,22 @@ class EngagementContent extends FeedItem {
5058
/// The URL to navigate to when the call-to-action is triggered.
5159
final String? callToActionUrl;
5260

53-
/// The action to be performed when this feed item is interacted with.
54-
@JsonKey(fromJson: feedItemActionFromJson, toJson: feedItemActionToJson)
55-
@override
56-
late final FeedItemAction action;
57-
5861
/// Converts this [EngagementContent] instance to a JSON map.
5962
@override
60-
Map<String, dynamic> toJson() => _$EngagementContentToJson(this);
63+
Map<String, dynamic> toJson() {
64+
final Map<String, dynamic> json = {
65+
'id': id,
66+
'title': title,
67+
'description': description,
68+
'engagementContentType':
69+
engagementContentTypeToJson(engagementContentType),
70+
'callToActionText': callToActionText,
71+
'callToActionUrl': callToActionUrl,
72+
'action': action.toJson(),
73+
'type': type, // Inherited from FeedItem
74+
};
75+
return json..removeWhere((key, value) => value == null);
76+
}
6177

6278
@override
6379
List<Object?> get props => [

lib/src/models/feed/engagement_content.g.dart

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)