From 934732770a3b8bf12ffd3a19608a33fa9e5ad791 Mon Sep 17 00:00:00 2001 From: tommyxchow Date: Sun, 12 Dec 2021 16:02:46 -0500 Subject: [PATCH] Add common emote class When deserialized, emotes have different fields with conflicting names and extraneous fields. This new class allows for a common class that prevents type errors. --- lib/models/emotes.dart | 98 ++++++++++++++++++++++++++++++---------- lib/models/emotes.g.dart | 20 +++----- test/emote_test.dart | 22 ++++----- 3 files changed, 93 insertions(+), 47 deletions(-) diff --git a/lib/models/emotes.dart b/lib/models/emotes.dart index e4849c97..ee09176c 100644 --- a/lib/models/emotes.dart +++ b/lib/models/emotes.dart @@ -50,20 +50,22 @@ class EmoteTwitch { // * BTTV Emotes * @JsonSerializable(createToJson: false) -class EmoteBTTVGlobal { +class EmoteBTTV { final String id; final String code; final String imageType; - final String userId; + final String? userId; + final UserBTTV? user; - const EmoteBTTVGlobal( + const EmoteBTTV( this.id, this.code, this.imageType, this.userId, + this.user, ); - factory EmoteBTTVGlobal.fromJson(Map json) => _$EmoteBTTVGlobalFromJson(json); + factory EmoteBTTV.fromJson(Map json) => _$EmoteBTTVFromJson(json); } @JsonSerializable(createToJson: false) @@ -83,29 +85,12 @@ class UserBTTV { factory UserBTTV.fromJson(Map json) => _$UserBTTVFromJson(json); } -@JsonSerializable(createToJson: false) -class EmoteBTTVShared { - final String id; - final String code; - final String imageType; - final UserBTTV user; - - const EmoteBTTVShared( - this.id, - this.code, - this.imageType, - this.user, - ); - - factory EmoteBTTVShared.fromJson(Map json) => _$EmoteBTTVSharedFromJson(json); -} - @JsonSerializable(createToJson: false) class EmoteBTTVChannel { final String id; final List bots; - final List channelEmotes; - final List sharedEmotes; + final List channelEmotes; + final List sharedEmotes; const EmoteBTTVChannel( this.id, @@ -245,3 +230,70 @@ class Emote7TV { factory Emote7TV.fromJson(Map json) => _$Emote7TVFromJson(json); } + +/// The common emote class. +class Emote { + final String id; + final String name; + final int? width; + final int? height; + final String url; + final EmoteType type; + + const Emote( + this.id, + this.name, + this.width, + this.height, + this.url, + this.type, + ); + + factory Emote.fromTwitch(EmoteTwitch emote, EmoteType type) => Emote( + emote.id, + emote.name, + null, + null, + 'https://static-cdn.jtvnw.net/emoticons/v2/${emote.id}/default/dark/3.0', + type, + ); + + factory Emote.fromBTTV(EmoteBTTV emote, EmoteType type) => Emote( + emote.id, + emote.code, + null, + null, + 'https://cdn.betterttv.net/emote/${emote.id}/3x', + type, + ); + + factory Emote.fromFFZ(EmoteFFZ emote, EmoteType type) => Emote( + emote.id.toString(), + emote.code, + null, + null, + emote.images.url4x ?? emote.images.url1x, + type, + ); + + factory Emote.from7TV(Emote7TV emote, EmoteType type) => Emote( + emote.id, + emote.name, + emote.width.first, + emote.height.first, + emote.urls[3][1], + type, + ); +} + +enum EmoteType { + twitchGlobal, + twitchChannel, + ffzGlobal, + ffzChannel, + bttvGlobal, + bttvChannel, + bttvShared, + sevenTvGlobal, + sevenTvChannel, +} diff --git a/lib/models/emotes.g.dart b/lib/models/emotes.g.dart index 2f2185d4..0c016824 100644 --- a/lib/models/emotes.g.dart +++ b/lib/models/emotes.g.dart @@ -24,12 +24,14 @@ EmoteTwitch _$EmoteTwitchFromJson(Map json) => EmoteTwitch( (json['theme_mode'] as List).map((e) => e as String).toList(), ); -EmoteBTTVGlobal _$EmoteBTTVGlobalFromJson(Map json) => - EmoteBTTVGlobal( +EmoteBTTV _$EmoteBTTVFromJson(Map json) => EmoteBTTV( json['id'] as String, json['code'] as String, json['imageType'] as String, - json['userId'] as String, + json['userId'] as String?, + json['user'] == null + ? null + : UserBTTV.fromJson(json['user'] as Map), ); UserBTTV _$UserBTTVFromJson(Map json) => UserBTTV( @@ -39,23 +41,15 @@ UserBTTV _$UserBTTVFromJson(Map json) => UserBTTV( json['providerId'] as String, ); -EmoteBTTVShared _$EmoteBTTVSharedFromJson(Map json) => - EmoteBTTVShared( - json['id'] as String, - json['code'] as String, - json['imageType'] as String, - UserBTTV.fromJson(json['user'] as Map), - ); - EmoteBTTVChannel _$EmoteBTTVChannelFromJson(Map json) => EmoteBTTVChannel( json['id'] as String, (json['bots'] as List).map((e) => e as String).toList(), (json['channelEmotes'] as List) - .map((e) => EmoteBTTVGlobal.fromJson(e as Map)) + .map((e) => EmoteBTTV.fromJson(e as Map)) .toList(), (json['sharedEmotes'] as List) - .map((e) => EmoteBTTVShared.fromJson(e as Map)) + .map((e) => EmoteBTTV.fromJson(e as Map)) .toList(), ); diff --git a/test/emote_test.dart b/test/emote_test.dart index 1e2ed07d..28f6f61b 100644 --- a/test/emote_test.dart +++ b/test/emote_test.dart @@ -61,7 +61,7 @@ void main() { """; final json = jsonDecode(emoteTrollFace); - final emote = EmoteBTTVGlobal.fromJson(json); + final emote = EmoteBTTV.fromJson(json); expect(emote.id, "54fa8f1401e468494b85b537"); expect(emote.code, ":tf:"); @@ -85,15 +85,15 @@ void main() { """; final decoded = jsonDecode(emoteEZ); - final emote = EmoteBTTVShared.fromJson(decoded); + final emote = EmoteBTTV.fromJson(decoded); expect(emote.id, "5590b223b344e2c42a9e28e3"); expect(emote.code, "EZ"); expect(emote.imageType, "png"); - expect(emote.user.id, "558f7862b344e2c42a9e2822"); - expect(emote.user.name, "helloboat"); - expect(emote.user.displayName, "helloboat"); - expect(emote.user.providerId, "39819556"); + expect(emote.user?.id, "558f7862b344e2c42a9e2822"); + expect(emote.user?.name, "helloboat"); + expect(emote.user?.displayName, "helloboat"); + expect(emote.user?.providerId, "39819556"); }); test("global emotes should parse correctly", () { @@ -107,7 +107,7 @@ void main() { """; final decoded = jsonDecode(sampleJson) as List; - final List emotes = decoded.map((emote) => EmoteBTTVGlobal.fromJson(emote)).toList(); + final List emotes = decoded.map((emote) => EmoteBTTV.fromJson(emote)).toList(); expect(emotes.length, 4); final emoteIds = ["54fa903b01e468494b85b53f", "54fa909b01e468494b85b542", "54fa90ba01e468494b85b543", "54fa90f201e468494b85b545"]; @@ -171,10 +171,10 @@ void main() { expect(sharedEmote.id, "5ed3bde8f54be95e2a838279"); expect(sharedEmote.code, "pugPls"); expect(sharedEmote.imageType, "gif"); - expect(sharedEmote.user.id, "5c5510a1c0a5642a696190d9"); - expect(sharedEmote.user.name, "wolfabelle"); - expect(sharedEmote.user.displayName, "Wolfabelle"); - expect(sharedEmote.user.providerId, "190146087"); + expect(sharedEmote.user?.id, "5c5510a1c0a5642a696190d9"); + expect(sharedEmote.user?.name, "wolfabelle"); + expect(sharedEmote.user?.displayName, "Wolfabelle"); + expect(sharedEmote.user?.providerId, "190146087"); }); });