import 'package:flutter/material.dart'; import 'package:collection/collection.dart'; import 'package:fluffychat/pangea/constructs/construct_form.dart'; import 'package:fluffychat/pangea/practice_activities/practice_choice.dart'; class PracticeMatchActivity { /// The constructIdenfifiers involved in the activity /// and the forms that are acceptable answers final Map> matchInfo; List choices = List.empty(growable: true); PracticeMatchActivity({ required this.matchInfo, }) { for (final ith in matchInfo.entries) { debugPrint( 'Construct: ${ith.key.form}, Forms: ${ith.value}', ); } final List usedForms = []; for (final matchEntry in matchInfo.entries) { if (matchEntry.value.isEmpty) { throw Exception( "No forms available for construct ${matchEntry.key}", ); } final String choiceContent = matchEntry.value.firstWhere( (element) => !usedForms.contains(element), orElse: () => throw Exception( "No unique form available for construct ${matchEntry.key.form}", ), ); choices.add( PracticeChoice( choiceContent: choiceContent, form: matchEntry.key, ), ); usedForms.add(choiceContent); debugPrint( 'Added PracticeChoice Construct: ${matchEntry.key}, Forms: ${matchEntry.value}', ); } choices.sort( (a, b) => a.choiceContent.length.compareTo(b.choiceContent.length), ); } factory PracticeMatchActivity.fromJson(Map json) { final Map> matchInfo = {}; for (final constructJson in json['match_info']) { final ConstructForm cId = ConstructForm.fromJson(constructJson['cId']); final List surfaceForms = List.from(constructJson['forms']); matchInfo[cId] = surfaceForms; } return PracticeMatchActivity( matchInfo: matchInfo, ); } Map toJson() { final List> matchInfo = []; for (final cId in this.matchInfo.keys) { matchInfo.add({ 'cId': cId.toJson(), 'forms': this.matchInfo[cId], }); } return { 'match_info': matchInfo, }; } @override bool operator ==(Object other) { if (identical(this, other)) return true; return other is PracticeMatchActivity && const MapEquality().equals(other.matchInfo, matchInfo); } @override int get hashCode => const MapEquality().hash(matchInfo); }