Merge pull request #722 from pangeachat/use-activity-model-directly
Use activity model directly
This commit is contained in:
commit
1d64475d25
3 changed files with 24 additions and 62 deletions
|
|
@ -22,8 +22,7 @@ import 'package:matrix/matrix.dart';
|
|||
/// Represents an item in the completion cache.
|
||||
class _RequestCacheItem {
|
||||
MessageActivityRequest req;
|
||||
|
||||
Future<PracticeActivityEvent?> practiceActivityEvent;
|
||||
PracticeActivityModel? practiceActivityEvent;
|
||||
|
||||
_RequestCacheItem({
|
||||
required this.req,
|
||||
|
|
@ -103,7 +102,7 @@ class PracticeGenerationController {
|
|||
|
||||
//TODO - allow return of activity content before sending the event
|
||||
// this requires some downstream changes to the way the event is handled
|
||||
Future<PracticeActivityEvent?> getPracticeActivity(
|
||||
Future<PracticeActivityModel?> getPracticeActivity(
|
||||
MessageActivityRequest req,
|
||||
PangeaMessageEvent event,
|
||||
) async {
|
||||
|
|
@ -131,7 +130,7 @@ class PracticeGenerationController {
|
|||
return PracticeActivityEvent(
|
||||
event: existingEvent,
|
||||
timeline: event.timeline,
|
||||
);
|
||||
).practiceActivity;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -142,11 +141,9 @@ class PracticeGenerationController {
|
|||
|
||||
debugPrint('Activity generated: ${res.activity!.toJson()}');
|
||||
|
||||
final Future<PracticeActivityEvent?> eventFuture =
|
||||
_sendAndPackageEvent(res.activity!, event);
|
||||
|
||||
_sendAndPackageEvent(res.activity!, event);
|
||||
_cache[cacheKey] =
|
||||
_RequestCacheItem(req: req, practiceActivityEvent: eventFuture);
|
||||
_RequestCacheItem(req: req, practiceActivityEvent: res.activity!);
|
||||
|
||||
return _cache[cacheKey]!.practiceActivityEvent;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ import 'dart:developer';
|
|||
import 'package:collection/collection.dart';
|
||||
import 'package:fluffychat/pangea/choreographer/widgets/choice_array.dart';
|
||||
import 'package:fluffychat/pangea/controllers/my_analytics_controller.dart';
|
||||
import 'package:fluffychat/pangea/matrix_event_wrappers/practice_activity_event.dart';
|
||||
import 'package:fluffychat/pangea/models/practice_activities.dart/practice_activity_model.dart';
|
||||
import 'package:fluffychat/pangea/models/practice_activities.dart/practice_activity_record_model.dart';
|
||||
import 'package:fluffychat/pangea/widgets/practice_activity/practice_activity_card.dart';
|
||||
|
|
@ -14,7 +13,7 @@ import 'package:flutter/material.dart';
|
|||
/// The multiple choice activity view
|
||||
class MultipleChoiceActivity extends StatefulWidget {
|
||||
final MessagePracticeActivityCardState practiceCardController;
|
||||
final PracticeActivityEvent? currentActivity;
|
||||
final PracticeActivityModel? currentActivity;
|
||||
|
||||
const MultipleChoiceActivity({
|
||||
super.key,
|
||||
|
|
@ -52,9 +51,8 @@ class MultipleChoiceActivityState extends State<MultipleChoiceActivity> {
|
|||
return;
|
||||
}
|
||||
|
||||
final bool isCorrect = widget
|
||||
.currentActivity!.practiceActivity.multipleChoice!
|
||||
.isCorrect(value, index);
|
||||
final bool isCorrect =
|
||||
widget.currentActivity!.multipleChoice!.isCorrect(value, index);
|
||||
|
||||
currentRecordModel?.addResponse(
|
||||
text: value,
|
||||
|
|
@ -74,15 +72,14 @@ class MultipleChoiceActivityState extends State<MultipleChoiceActivity> {
|
|||
widget.practiceCardController.widget.pangeaMessageEvent.eventId,
|
||||
roomId: widget.practiceCardController.widget.pangeaMessageEvent.room.id,
|
||||
constructs: currentRecordModel!.latestResponse!.toUses(
|
||||
widget.practiceCardController.currentActivity!.practiceActivity,
|
||||
widget.practiceCardController.currentActivity!,
|
||||
widget.practiceCardController.metadata,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// If the selected choice is correct, send the record and get the next activity
|
||||
if (widget.currentActivity!.practiceActivity.multipleChoice!
|
||||
.isCorrect(value, index)) {
|
||||
if (widget.currentActivity!.multipleChoice!.isCorrect(value, index)) {
|
||||
widget.practiceCardController.onActivityFinish();
|
||||
}
|
||||
|
||||
|
|
@ -93,8 +90,7 @@ class MultipleChoiceActivityState extends State<MultipleChoiceActivity> {
|
|||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final PracticeActivityModel? practiceActivity =
|
||||
widget.currentActivity?.practiceActivity;
|
||||
final PracticeActivityModel? practiceActivity = widget.currentActivity;
|
||||
|
||||
if (practiceActivity == null) {
|
||||
return const SizedBox();
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ import 'package:fluffychat/pangea/matrix_event_wrappers/pangea_representation_ev
|
|||
import 'package:fluffychat/pangea/matrix_event_wrappers/practice_activity_event.dart';
|
||||
import 'package:fluffychat/pangea/models/analytics/constructs_model.dart';
|
||||
import 'package:fluffychat/pangea/models/practice_activities.dart/message_activity_request.dart';
|
||||
import 'package:fluffychat/pangea/models/practice_activities.dart/practice_activity_model.dart';
|
||||
import 'package:fluffychat/pangea/models/practice_activities.dart/practice_activity_record_model.dart';
|
||||
import 'package:fluffychat/pangea/utils/bot_style.dart';
|
||||
import 'package:fluffychat/pangea/utils/error_handler.dart';
|
||||
|
|
@ -42,7 +43,7 @@ class PracticeActivityCard extends StatefulWidget {
|
|||
}
|
||||
|
||||
class MessagePracticeActivityCardState extends State<PracticeActivityCard> {
|
||||
PracticeActivityEvent? currentActivity;
|
||||
PracticeActivityModel? currentActivity;
|
||||
PracticeActivityRecordModel? currentCompletionRecord;
|
||||
bool fetchingActivity = false;
|
||||
|
||||
|
|
@ -70,7 +71,7 @@ class MessagePracticeActivityCardState extends State<PracticeActivityCard> {
|
|||
if (mounted) setState(() => fetchingActivity = value);
|
||||
}
|
||||
|
||||
void _setPracticeActivity(PracticeActivityEvent? activity) {
|
||||
void _setPracticeActivity(PracticeActivityModel? activity) {
|
||||
//set elsewhere but just in case
|
||||
fetchingActivity = false;
|
||||
|
||||
|
|
@ -83,36 +84,21 @@ class MessagePracticeActivityCardState extends State<PracticeActivityCard> {
|
|||
|
||||
//make new completion record
|
||||
currentCompletionRecord = PracticeActivityRecordModel(
|
||||
question: activity.practiceActivity.question,
|
||||
question: activity.question,
|
||||
);
|
||||
|
||||
widget.overlayController.setSelectedSpan(activity.practiceActivity);
|
||||
widget.overlayController.setSelectedSpan(activity);
|
||||
}
|
||||
|
||||
/// Get an existing activity if there is one.
|
||||
/// If not, get a new activity from the server.
|
||||
Future<void> initialize() async {
|
||||
_setPracticeActivity(
|
||||
_fetchExistingIncompleteActivity() ?? await _fetchNewActivity(),
|
||||
await _fetchNewActivity(),
|
||||
);
|
||||
}
|
||||
|
||||
// if the user did the activity before but awhile ago and we don't have any
|
||||
// more target tokens, maybe we should give them the same activity again
|
||||
PracticeActivityEvent? _fetchExistingIncompleteActivity() {
|
||||
if (practiceActivities.isEmpty) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final List<PracticeActivityEvent> incompleteActivities =
|
||||
practiceActivities.where((element) => !element.isComplete).toList();
|
||||
|
||||
// TODO - maybe check the user's xp for the tgtConstructs and decide if its relevant for them
|
||||
// however, maybe we'd like to go ahead and give them the activity to get some data on our xp?
|
||||
return incompleteActivities.firstOrNull;
|
||||
}
|
||||
|
||||
Future<PracticeActivityEvent?> _fetchNewActivity([
|
||||
Future<PracticeActivityModel?> _fetchNewActivity([
|
||||
ActivityQualityFeedback? activityFeedback,
|
||||
]) async {
|
||||
try {
|
||||
|
|
@ -134,7 +120,7 @@ class MessagePracticeActivityCardState extends State<PracticeActivityCard> {
|
|||
return null;
|
||||
}
|
||||
|
||||
final PracticeActivityEvent? ourNewActivity = await pangeaController
|
||||
final PracticeActivityModel? ourNewActivity = await pangeaController
|
||||
.practiceGenerationController
|
||||
.getPracticeActivity(
|
||||
MessageActivityRequest(
|
||||
|
|
@ -203,30 +189,13 @@ class MessagePracticeActivityCardState extends State<PracticeActivityCard> {
|
|||
// NOTE - multiple choice activity is handling adding these to analytics
|
||||
await targetTokensController.updateTokensWithConstructs(
|
||||
currentCompletionRecord!.usesForAllResponses(
|
||||
currentActivity!.practiceActivity,
|
||||
currentActivity!,
|
||||
metadata,
|
||||
),
|
||||
context,
|
||||
widget.pangeaMessageEvent,
|
||||
);
|
||||
|
||||
// save the record without awaiting to avoid blocking the UI
|
||||
// send a copy of the activity record to make sure its not overwritten by
|
||||
// the new activity
|
||||
MatrixState.pangeaController.activityRecordController
|
||||
.send(currentCompletionRecord!, currentActivity!)
|
||||
.catchError(
|
||||
(e, s) => ErrorHandler.logError(
|
||||
e: e,
|
||||
s: s,
|
||||
m: 'Failed to save record',
|
||||
data: {
|
||||
'record': currentCompletionRecord?.toJson(),
|
||||
'activity': currentActivity?.practiceActivity.toJson(),
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
widget.overlayController.onActivityFinish();
|
||||
|
||||
//
|
||||
|
|
@ -235,7 +204,7 @@ class MessagePracticeActivityCardState extends State<PracticeActivityCard> {
|
|||
_fetchNewActivity(),
|
||||
]);
|
||||
|
||||
_setPracticeActivity(result.last as PracticeActivityEvent?);
|
||||
_setPracticeActivity(result.last as PracticeActivityModel?);
|
||||
} catch (e, s) {
|
||||
_setPracticeActivity(null);
|
||||
debugger(when: kDebugMode);
|
||||
|
|
@ -262,7 +231,7 @@ class MessagePracticeActivityCardState extends State<PracticeActivityCard> {
|
|||
_fetchNewActivity(
|
||||
ActivityQualityFeedback(
|
||||
feedbackText: feedback,
|
||||
badActivity: currentActivity!.practiceActivity,
|
||||
badActivity: currentActivity!,
|
||||
),
|
||||
).then((activity) {
|
||||
_setPracticeActivity(activity);
|
||||
|
|
@ -300,7 +269,7 @@ class MessagePracticeActivityCardState extends State<PracticeActivityCard> {
|
|||
// return sizedbox with height of 80
|
||||
return const SizedBox(height: 80);
|
||||
}
|
||||
switch (currentActivity!.practiceActivity.activityType) {
|
||||
switch (currentActivity!.activityType) {
|
||||
case ActivityTypeEnum.multipleChoice:
|
||||
return MultipleChoiceActivity(
|
||||
practiceCardController: this,
|
||||
|
|
@ -311,7 +280,7 @@ class MessagePracticeActivityCardState extends State<PracticeActivityCard> {
|
|||
e: Exception('Unknown activity type'),
|
||||
m: 'Unknown activity type',
|
||||
data: {
|
||||
'activityType': currentActivity!.practiceActivity.activityType,
|
||||
'activityType': currentActivity!.activityType,
|
||||
},
|
||||
);
|
||||
return Text(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue