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.
This commit is contained in:
tommyxchow
2021-12-12 16:02:46 -05:00
parent 0457d3e85e
commit 934732770a
3 changed files with 93 additions and 47 deletions

View File

@ -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<String, dynamic> json) => _$EmoteBTTVGlobalFromJson(json);
factory EmoteBTTV.fromJson(Map<String, dynamic> json) => _$EmoteBTTVFromJson(json);
}
@JsonSerializable(createToJson: false)
@ -83,29 +85,12 @@ class UserBTTV {
factory UserBTTV.fromJson(Map<String, dynamic> 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<String, dynamic> json) => _$EmoteBTTVSharedFromJson(json);
}
@JsonSerializable(createToJson: false)
class EmoteBTTVChannel {
final String id;
final List<String> bots;
final List<EmoteBTTVGlobal> channelEmotes;
final List<EmoteBTTVShared> sharedEmotes;
final List<EmoteBTTV> channelEmotes;
final List<EmoteBTTV> sharedEmotes;
const EmoteBTTVChannel(
this.id,
@ -245,3 +230,70 @@ class Emote7TV {
factory Emote7TV.fromJson(Map<String, dynamic> 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,
}

View File

@ -24,12 +24,14 @@ EmoteTwitch _$EmoteTwitchFromJson(Map<String, dynamic> json) => EmoteTwitch(
(json['theme_mode'] as List<dynamic>).map((e) => e as String).toList(),
);
EmoteBTTVGlobal _$EmoteBTTVGlobalFromJson(Map<String, dynamic> json) =>
EmoteBTTVGlobal(
EmoteBTTV _$EmoteBTTVFromJson(Map<String, dynamic> 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<String, dynamic>),
);
UserBTTV _$UserBTTVFromJson(Map<String, dynamic> json) => UserBTTV(
@ -39,23 +41,15 @@ UserBTTV _$UserBTTVFromJson(Map<String, dynamic> json) => UserBTTV(
json['providerId'] as String,
);
EmoteBTTVShared _$EmoteBTTVSharedFromJson(Map<String, dynamic> json) =>
EmoteBTTVShared(
json['id'] as String,
json['code'] as String,
json['imageType'] as String,
UserBTTV.fromJson(json['user'] as Map<String, dynamic>),
);
EmoteBTTVChannel _$EmoteBTTVChannelFromJson(Map<String, dynamic> json) =>
EmoteBTTVChannel(
json['id'] as String,
(json['bots'] as List<dynamic>).map((e) => e as String).toList(),
(json['channelEmotes'] as List<dynamic>)
.map((e) => EmoteBTTVGlobal.fromJson(e as Map<String, dynamic>))
.map((e) => EmoteBTTV.fromJson(e as Map<String, dynamic>))
.toList(),
(json['sharedEmotes'] as List<dynamic>)
.map((e) => EmoteBTTVShared.fromJson(e as Map<String, dynamic>))
.map((e) => EmoteBTTV.fromJson(e as Map<String, dynamic>))
.toList(),
);

View File

@ -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<EmoteBTTVGlobal> emotes = decoded.map((emote) => EmoteBTTVGlobal.fromJson(emote)).toList();
final List<EmoteBTTV> 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");
});
});