SimpleCloudNotifier/flutter/lib/models/channel.dart

131 lines
4.0 KiB
Dart
Raw Permalink Normal View History

import 'package:hive_flutter/hive_flutter.dart';
2024-02-18 17:36:58 +01:00
import 'package:simplecloudnotifier/models/subscription.dart';
import 'package:simplecloudnotifier/state/interfaces.dart';
2024-02-18 17:36:58 +01:00
part 'channel.g.dart';
@HiveType(typeId: 104)
class Channel extends HiveObject implements FieldDebuggable {
@HiveField(0)
2024-02-18 17:36:58 +01:00
final String channelID;
@HiveField(10)
2024-02-18 17:36:58 +01:00
final String ownerUserID;
@HiveField(11)
final String internalName; // = InternalName, used for sending, normalized, cannot be changed
@HiveField(12)
final String displayName; // = DisplayName, used for display purposes, can be changed, initially equals InternalName
@HiveField(13)
final String? descriptionName; // = DescriptionName, (optional), longer description text, initally nil
@HiveField(14)
2024-02-18 17:36:58 +01:00
final String? subscribeKey;
@HiveField(15)
2024-02-18 17:36:58 +01:00
final String timestampCreated;
@HiveField(16)
2024-02-18 17:36:58 +01:00
final String? timestampLastSent;
@HiveField(17)
2024-02-18 17:36:58 +01:00
final int messagesSent;
Channel({
2024-02-18 17:36:58 +01:00
required this.channelID,
required this.ownerUserID,
required this.internalName,
required this.displayName,
required this.descriptionName,
required this.subscribeKey,
required this.timestampCreated,
required this.timestampLastSent,
required this.messagesSent,
});
factory Channel.fromJson(Map<String, dynamic> json) {
2024-05-25 18:09:39 +02:00
return Channel(
2024-05-25 21:36:05 +02:00
channelID: json['channel_id'] as String,
ownerUserID: json['owner_user_id'] as String,
internalName: json['internal_name'] as String,
displayName: json['display_name'] as String,
descriptionName: json['description_name'] as String?,
subscribeKey: json['subscribe_key'] as String?,
timestampCreated: json['timestamp_created'] as String,
timestampLastSent: json['timestamp_lastsent'] as String?,
messagesSent: json['messages_sent'] as int,
2024-05-25 18:09:39 +02:00
);
2024-02-18 17:36:58 +01:00
}
@override
String toString() {
return 'Channel[${this.channelID}]';
}
List<(String, String)> debugFieldList() {
return [
('channelID', this.channelID),
('ownerUserID', this.ownerUserID),
('internalName', this.internalName),
('displayName', this.displayName),
('descriptionName', this.descriptionName ?? ''),
('subscribeKey', this.subscribeKey ?? ''),
('timestampCreated', this.timestampCreated),
('timestampLastSent', this.timestampLastSent ?? ''),
('messagesSent', '${this.messagesSent}'),
];
}
2024-07-13 00:11:13 +02:00
ChannelPreview toPreview() {
return ChannelPreview(
channelID: this.channelID,
ownerUserID: this.ownerUserID,
internalName: this.internalName,
displayName: this.displayName,
descriptionName: this.descriptionName,
);
}
2024-02-18 17:36:58 +01:00
}
class ChannelWithSubscription {
final Channel channel;
2024-06-25 12:00:34 +02:00
final Subscription? subscription;
2024-02-18 17:36:58 +01:00
ChannelWithSubscription({
required this.channel,
2024-02-18 17:36:58 +01:00
required this.subscription,
});
factory ChannelWithSubscription.fromJson(Map<String, dynamic> json) {
2024-05-25 18:09:39 +02:00
return ChannelWithSubscription(
channel: Channel.fromJson(json),
2024-06-25 12:00:34 +02:00
subscription: json['subscription'] == null ? null : Subscription.fromJson(json['subscription'] as Map<String, dynamic>),
2024-05-25 18:09:39 +02:00
);
}
static List<ChannelWithSubscription> fromJsonArray(List<dynamic> jsonArr) {
2024-05-25 21:36:05 +02:00
return jsonArr.map<ChannelWithSubscription>((e) => ChannelWithSubscription.fromJson(e as Map<String, dynamic>)).toList();
2024-02-18 17:36:58 +01:00
}
}
2024-06-12 01:17:00 +02:00
class ChannelPreview {
final String channelID;
final String ownerUserID;
final String internalName;
final String displayName;
final String? descriptionName;
const ChannelPreview({
required this.channelID,
required this.ownerUserID,
required this.internalName,
required this.displayName,
required this.descriptionName,
});
factory ChannelPreview.fromJson(Map<String, dynamic> json) {
return ChannelPreview(
channelID: json['channel_id'] as String,
ownerUserID: json['owner_user_id'] as String,
internalName: json['internal_name'] as String,
displayName: json['display_name'] as String,
descriptionName: json['description_name'] as String?,
);
}
}