LCOV - code coverage report
Current view: top level - lib/msc_extensions/msc_3381_polls/models - poll_event_content.dart (source / functions) Hit Total Coverage
Test: merged.info Lines: 34 34 100.0 %
Date: 2025-01-06 12:44:40 Functions: 0 0 -

          Line data    Source code
       1             : import 'package:collection/collection.dart';
       2             : 
       3             : class PollEventContent {
       4             :   final String mText;
       5             :   final PollStartContent pollStartContent;
       6             : 
       7           2 :   const PollEventContent({
       8             :     required this.mText,
       9             :     required this.pollStartContent,
      10             :   });
      11             :   static const String mTextJsonKey = 'org.matrix.msc1767.text';
      12             :   static const String startType = 'org.matrix.msc3381.poll.start';
      13             :   static const String responseType = 'org.matrix.msc3381.poll.response';
      14             :   static const String endType = 'org.matrix.msc3381.poll.end';
      15             : 
      16           2 :   factory PollEventContent.fromJson(Map<String, dynamic> json) =>
      17           2 :       PollEventContent(
      18           2 :         mText: json[mTextJsonKey],
      19           4 :         pollStartContent: PollStartContent.fromJson(json[startType]),
      20             :       );
      21             : 
      22           4 :   Map<String, dynamic> toJson() => {
      23           2 :         mTextJsonKey: mText,
      24           4 :         startType: pollStartContent.toJson(),
      25             :       };
      26             : }
      27             : 
      28             : class PollStartContent {
      29             :   final PollKind? kind;
      30             :   final int maxSelections;
      31             :   final PollQuestion question;
      32             :   final List<PollAnswer> answers;
      33             : 
      34           2 :   const PollStartContent({
      35             :     this.kind,
      36             :     required this.maxSelections,
      37             :     required this.question,
      38             :     required this.answers,
      39             :   });
      40             : 
      41           2 :   factory PollStartContent.fromJson(Map<String, dynamic> json) =>
      42           2 :       PollStartContent(
      43             :         kind: PollKind.values
      44          10 :             .singleWhereOrNull((kind) => kind.name == json['kind']),
      45           2 :         maxSelections: json['max_selections'],
      46           4 :         question: PollQuestion.fromJson(json['question']),
      47           2 :         answers: (json['answers'] as List)
      48           6 :             .map((i) => PollAnswer.fromJson(i))
      49           2 :             .toList(),
      50             :       );
      51             : 
      52           4 :   Map<String, dynamic> toJson() => {
      53           8 :         if (kind != null) 'kind': kind?.name,
      54           4 :         'max_selections': maxSelections,
      55           6 :         'question': question.toJson(),
      56          12 :         'answers': answers.map((i) => i.toJson()).toList(),
      57             :       };
      58             : }
      59             : 
      60             : class PollQuestion {
      61             :   final String mText;
      62             : 
      63           2 :   const PollQuestion({
      64             :     required this.mText,
      65             :   });
      66             : 
      67           4 :   factory PollQuestion.fromJson(Map<String, dynamic> json) => PollQuestion(
      68           2 :         mText: json[PollEventContent.mTextJsonKey],
      69             :       );
      70             : 
      71           4 :   Map<String, dynamic> toJson() => {
      72           2 :         PollEventContent.mTextJsonKey: mText,
      73             :       };
      74             : }
      75             : 
      76             : class PollAnswer {
      77             :   final String id;
      78             :   final String mText;
      79             : 
      80           2 :   const PollAnswer({required this.id, required this.mText});
      81             : 
      82           4 :   factory PollAnswer.fromJson(Map<String, Object?> json) => PollAnswer(
      83           2 :         id: json['id'] as String,
      84           2 :         mText: json[PollEventContent.mTextJsonKey] as String,
      85             :       );
      86             : 
      87           4 :   Map<String, Object?> toJson() => {
      88           2 :         'id': id,
      89           2 :         PollEventContent.mTextJsonKey: mText,
      90             :       };
      91             : }
      92             : 
      93             : enum PollKind {
      94             :   disclosed('org.matrix.msc3381.poll.disclosed'),
      95             :   undisclosed('org.matrix.msc3381.poll.undisclosed');
      96             : 
      97             :   const PollKind(this.name);
      98             : 
      99             :   final String name;
     100             : }

Generated by: LCOV version 1.14