enum for event type
This commit is contained in:
parent
3325ebcad7
commit
3ce1f53bc4
4 changed files with 17 additions and 20 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
import "package:nexus/models/content/encrypted.dart";
|
import "package:collection/collection.dart";
|
||||||
|
import "package:freezed_annotation/freezed_annotation.dart";
|
||||||
import "package:nexus/models/content/membership.dart";
|
import "package:nexus/models/content/membership.dart";
|
||||||
import "package:nexus/models/content/message.dart";
|
import "package:nexus/models/content/message.dart";
|
||||||
|
|
||||||
|
|
@ -8,10 +9,19 @@ class Content {
|
||||||
|
|
||||||
Map<String, dynamic> toJson() => {};
|
Map<String, dynamic> toJson() => {};
|
||||||
static Content fromEventJson(Map<String, dynamic> eventJson) =>
|
static Content fromEventJson(Map<String, dynamic> eventJson) =>
|
||||||
switch (eventJson["type"]) {
|
(EventType.values
|
||||||
EncryptedContent.type => EncryptedContent.fromJson,
|
.firstWhereOrNull((eventType) => eventType == eventJson["type"])
|
||||||
MembershipContent.type => MembershipContent.fromJson,
|
?.contentFromJson ??
|
||||||
MessageContent.type => MessageContent.fromJson,
|
Content.fromJson)(eventJson);
|
||||||
_ => Content.fromJson,
|
}
|
||||||
}(eventJson);
|
|
||||||
|
@JsonEnum(valueField: "type")
|
||||||
|
enum EventType {
|
||||||
|
encrypted("m.room.encrypted", Content.fromJson),
|
||||||
|
membership("m.room.member", MembershipContent.fromJson),
|
||||||
|
message("m.room.message", MessageContent.fromJson);
|
||||||
|
|
||||||
|
final String type;
|
||||||
|
final Content Function(Map<String, dynamic> json) contentFromJson;
|
||||||
|
const EventType(this.type, this.contentFromJson);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +0,0 @@
|
||||||
import "package:nexus/models/content/content.dart";
|
|
||||||
|
|
||||||
class EncryptedContent extends Content {
|
|
||||||
EncryptedContent();
|
|
||||||
factory EncryptedContent.fromJson(Map<String, dynamic> json) =>
|
|
||||||
EncryptedContent();
|
|
||||||
|
|
||||||
static const type = "m.room.encrypted";
|
|
||||||
}
|
|
||||||
|
|
@ -6,8 +6,6 @@ part "membership.g.dart";
|
||||||
|
|
||||||
@freezed
|
@freezed
|
||||||
abstract class MembershipContent extends Content with _$MembershipContent {
|
abstract class MembershipContent extends Content with _$MembershipContent {
|
||||||
static const type = "m.room.membership";
|
|
||||||
|
|
||||||
MembershipContent._();
|
MembershipContent._();
|
||||||
const factory MembershipContent({
|
const factory MembershipContent({
|
||||||
@JsonKey(name: "displayname") required String displayName,
|
@JsonKey(name: "displayname") required String displayName,
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,6 @@ part "message.g.dart";
|
||||||
|
|
||||||
@Freezed(unionKey: "msgtype", fallbackUnion: "default")
|
@Freezed(unionKey: "msgtype", fallbackUnion: "default")
|
||||||
abstract class MessageContent extends Content with _$MessageContent {
|
abstract class MessageContent extends Content with _$MessageContent {
|
||||||
static const type = "m.room.message";
|
|
||||||
|
|
||||||
MessageContent._();
|
MessageContent._();
|
||||||
const factory MessageContent({
|
const factory MessageContent({
|
||||||
required String msgtype,
|
required String msgtype,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue