import 'dart:developer'; import 'package:fluffychat/pangea/constants/morph_categories_and_labels.dart'; import 'package:fluffychat/pangea/enum/construct_use_type_enum.dart'; import 'package:fluffychat/pangea/models/practice_activities.dart/practice_activity_model.dart'; import 'package:fluffychat/pangea/utils/error_handler.dart'; import 'package:flutter/foundation.dart'; import 'package:matrix/matrix.dart'; import '../../enum/construct_type_enum.dart'; class ConstructAnalyticsModel { List uses; ConstructAnalyticsModel({ this.uses = const [], }); static const _usesKey = "uses"; factory ConstructAnalyticsModel.fromJson(Map json) { final List uses = []; if (json[_usesKey] is List) { // This is the new format for (final useJson in json[_usesKey]) { // grammar construct uses are deprecated so but some are saved // here we're filtering from data if (["grammar", "g"].contains(useJson['constructType'])) { continue; } else { try { uses.add(OneConstructUse.fromJson(useJson)); } catch (err, s) { ErrorHandler.logError(e: err, s: s); continue; } } } } else { debugger(when: kDebugMode); ErrorHandler.logError(m: "Analytics room with non-list uses"); } return ConstructAnalyticsModel( uses: uses, ); } Map toJson() { return { _usesKey: uses.map((use) => use.toJson()).toList(), }; } } class OneConstructUse { String lemma; // practice activities do not currently include form in the target_construct info // for that, this is nullable String? form; /// For vocab constructs, this is the POS. For morph /// constructs, this is the morphological category. String _category; ConstructTypeEnum constructType; ConstructUseTypeEnum useType; /// Used to unqiuely identify the construct use. Useful in the case /// that a users makes the same type of mistake multiple times in a /// message, and those uses need to be disinguished. String? id; ConstructUseMetaData metadata; OneConstructUse({ required this.useType, required this.lemma, required this.constructType, required this.metadata, category, required this.form, this.id, }) : _category = category ?? "other"; String get chatId => metadata.roomId; String get msgId => metadata.eventId!; DateTime get timeStamp => metadata.timeStamp; factory OneConstructUse.fromJson(Map json) { debugger(when: kDebugMode && json['constructType'] == null); final ConstructTypeEnum constructType = json['constructType'] != null ? ConstructTypeUtil.fromString(json['constructType']) : ConstructTypeEnum.vocab; return OneConstructUse( useType: ConstructUseTypeUtil.fromString(json['useType']), lemma: json['lemma'], form: json['form'], category: getCategory(json, constructType), constructType: constructType, id: json['id'], metadata: ConstructUseMetaData( eventId: json['msgId'], roomId: json['chatId'], timeStamp: DateTime.parse(json['timeStamp']), ), ); } Map toJson() => { 'useType': useType.string, 'chatId': metadata.roomId, 'timeStamp': metadata.timeStamp.toIso8601String(), 'form': form, 'msgId': metadata.eventId, 'lemma': lemma, 'constructType': constructType.string, 'categories': category, 'id': id, }; String get category { if (_category.isEmpty) return "other"; return _category.toLowerCase(); } static String getCategory( Map json, ConstructTypeEnum constructType, ) { final categoryEntry = json['cat'] ?? json['categories']; if (constructType == ConstructTypeEnum.vocab) { final String? category = categoryEntry is String ? categoryEntry : categoryEntry is List && categoryEntry.isNotEmpty ? categoryEntry.first : null; return category ?? "Other"; } if (categoryEntry == null) { return _guessGrammarCategory(json["lemma"]); } if ((categoryEntry is List)) { if (categoryEntry.isEmpty) { return _guessGrammarCategory(json["lemma"]); } return categoryEntry.first; } else if (categoryEntry is String) { return categoryEntry; } debugPrint( "Category entry is not a list or string -${json['cat'] ?? json['categories']}-", ); return _guessGrammarCategory(json["lemma"]); } static String _guessGrammarCategory(String morphLemma) { for (final String category in morphCategoriesAndLabels.keys) { if (morphCategoriesAndLabels[category]!.contains(morphLemma)) { // debugPrint( // "found missing construct category for $morphLemma: $category", // ); return category; } } ErrorHandler.logError( m: "Morph construct lemma $morphLemma not found in morph categories and labels", ); return "Other"; } Room? getRoom(Client client) { return client.getRoomById(metadata.roomId); } Future getEvent(Client client) async { final Room? room = getRoom(client); if (room == null || metadata.eventId == null) return null; return room.getEventById(metadata.eventId!); } int get pointValue => useType.pointValue; ConstructIdentifier get identifier => ConstructIdentifier( lemma: lemma, type: constructType, category: category, ); } class ConstructUseMetaData { String? eventId; String roomId; DateTime timeStamp; ConstructUseMetaData({ required this.roomId, required this.timeStamp, this.eventId, }); }