LCOV - code coverage report
Current view: top level - lib/msc_extensions/msc_3381_polls/models - poll_event_content.dart (source / functions) Coverage Total Hit
Test: merged.info Lines: 100.0 % 35 35
Test Date: 2025-01-14 12:37:39 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] ?? json['body'],
      69              :       );
      70              : 
      71            4 :   Map<String, dynamic> toJson() => {
      72            2 :         PollEventContent.mTextJsonKey: mText,
      73              :         // Compatible with older Element versions
      74              :         'msgtype': 'm.text',
      75            2 :         'body': mText,
      76              :       };
      77              : }
      78              : 
      79              : class PollAnswer {
      80              :   final String id;
      81              :   final String mText;
      82              : 
      83            2 :   const PollAnswer({required this.id, required this.mText});
      84              : 
      85            4 :   factory PollAnswer.fromJson(Map<String, Object?> json) => PollAnswer(
      86            2 :         id: json['id'] as String,
      87            2 :         mText: json[PollEventContent.mTextJsonKey] as String,
      88              :       );
      89              : 
      90            4 :   Map<String, Object?> toJson() => {
      91            2 :         'id': id,
      92            2 :         PollEventContent.mTextJsonKey: mText,
      93              :       };
      94              : }
      95              : 
      96              : enum PollKind {
      97              :   disclosed('org.matrix.msc3381.poll.disclosed'),
      98              :   undisclosed('org.matrix.msc3381.poll.undisclosed');
      99              : 
     100              :   const PollKind(this.name);
     101              : 
     102              :   final String name;
     103              : }
        

Generated by: LCOV version 2.0-1