updates to improve navigation of practice activities
parent
8fba3b667e
commit
b12efe7e80
@ -0,0 +1,104 @@
|
||||
import 'package:fluffychat/pangea/enum/activity_type_enum.dart';
|
||||
import 'package:fluffychat/pangea/matrix_event_wrappers/pangea_message_event.dart';
|
||||
import 'package:fluffychat/pangea/matrix_event_wrappers/practice_activity_event.dart';
|
||||
import 'package:fluffychat/pangea/models/practice_activities.dart/practice_activity_record_model.dart';
|
||||
import 'package:fluffychat/pangea/widgets/practice_activity/multiple_choice_activity_view.dart';
|
||||
import 'package:fluffychat/pangea/widgets/practice_activity/practice_activity_card.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class PracticeActivity extends StatefulWidget {
|
||||
final PracticeActivityEvent practiceEvent;
|
||||
final PangeaMessageEvent pangeaMessageEvent;
|
||||
final MessagePracticeActivityCardState controller;
|
||||
|
||||
const PracticeActivity({
|
||||
super.key,
|
||||
required this.practiceEvent,
|
||||
required this.pangeaMessageEvent,
|
||||
required this.controller,
|
||||
});
|
||||
|
||||
@override
|
||||
PracticeActivityContentState createState() => PracticeActivityContentState();
|
||||
}
|
||||
|
||||
class PracticeActivityContentState extends State<PracticeActivity> {
|
||||
PracticeActivityEvent get practiceEvent => widget.practiceEvent;
|
||||
int? selectedChoiceIndex;
|
||||
bool isSubmitted = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
setRecord();
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant PracticeActivity oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.practiceEvent.event.eventId !=
|
||||
widget.practiceEvent.event.eventId) {
|
||||
setRecord();
|
||||
}
|
||||
}
|
||||
|
||||
// sets the record model for the activity
|
||||
// either a new record model that will be sent after submitting the
|
||||
// activity or the record model from the user's previous response
|
||||
void setRecord() {
|
||||
if (widget.controller.recordEvent?.record == null) {
|
||||
final String question =
|
||||
practiceEvent.practiceActivity.multipleChoice!.question;
|
||||
widget.controller.recordModel =
|
||||
PracticeActivityRecordModel(question: question);
|
||||
} else {
|
||||
widget.controller.recordModel = widget.controller.recordEvent!.record;
|
||||
|
||||
// Note that only MultipleChoice activities will have this so we
|
||||
// probably should move this logic to the MultipleChoiceActivity widget
|
||||
selectedChoiceIndex =
|
||||
widget.controller.recordModel?.latestResponse != null
|
||||
? widget.practiceEvent.practiceActivity.multipleChoice
|
||||
?.choiceIndex(widget.controller.recordModel!.latestResponse!)
|
||||
: null;
|
||||
isSubmitted = widget.controller.recordModel?.latestResponse != null;
|
||||
}
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
void updateChoice(int index) {
|
||||
setState(() {
|
||||
selectedChoiceIndex = index;
|
||||
widget.controller.recordModel!.addResponse(
|
||||
text: widget
|
||||
.practiceEvent.practiceActivity.multipleChoice!.choices[index],
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Widget get activityWidget {
|
||||
switch (widget.practiceEvent.practiceActivity.activityType) {
|
||||
case ActivityTypeEnum.multipleChoice:
|
||||
return MultipleChoiceActivityView(
|
||||
controller: this,
|
||||
updateChoice: updateChoice,
|
||||
isActive: !isSubmitted,
|
||||
);
|
||||
default:
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
debugPrint(
|
||||
"MessagePracticeActivityContentState.build with selectedChoiceIndex: $selectedChoiceIndex",
|
||||
);
|
||||
return Column(
|
||||
children: [
|
||||
activityWidget,
|
||||
const SizedBox(height: 8),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -1,165 +0,0 @@
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:fluffychat/config/app_config.dart';
|
||||
import 'package:fluffychat/pangea/enum/activity_type_enum.dart';
|
||||
import 'package:fluffychat/pangea/matrix_event_wrappers/pangea_message_event.dart';
|
||||
import 'package:fluffychat/pangea/matrix_event_wrappers/practice_acitivity_record_event.dart';
|
||||
import 'package:fluffychat/pangea/matrix_event_wrappers/practice_activity_event.dart';
|
||||
import 'package:fluffychat/pangea/models/practice_activities.dart/practice_activity_record_model.dart';
|
||||
import 'package:fluffychat/pangea/utils/error_handler.dart';
|
||||
import 'package:fluffychat/pangea/widgets/practice_activity/multiple_choice_activity.dart';
|
||||
import 'package:fluffychat/pangea/widgets/practice_activity/practice_activity_card.dart';
|
||||
import 'package:fluffychat/widgets/matrix.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||
|
||||
class PracticeActivityContent extends StatefulWidget {
|
||||
final PracticeActivityEvent practiceEvent;
|
||||
final PangeaMessageEvent pangeaMessageEvent;
|
||||
final MessagePracticeActivityCardState controller;
|
||||
|
||||
const PracticeActivityContent({
|
||||
super.key,
|
||||
required this.practiceEvent,
|
||||
required this.pangeaMessageEvent,
|
||||
required this.controller,
|
||||
});
|
||||
|
||||
@override
|
||||
MessagePracticeActivityContentState createState() =>
|
||||
MessagePracticeActivityContentState();
|
||||
}
|
||||
|
||||
class MessagePracticeActivityContentState
|
||||
extends State<PracticeActivityContent> {
|
||||
int? selectedChoiceIndex;
|
||||
PracticeActivityRecordModel? recordModel;
|
||||
bool recordSubmittedThisSession = false;
|
||||
bool recordSubmittedPreviousSession = false;
|
||||
|
||||
PracticeActivityEvent get practiceEvent => widget.practiceEvent;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
initalizeActivity();
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant PracticeActivityContent oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.practiceEvent.event.eventId !=
|
||||
widget.practiceEvent.event.eventId) {
|
||||
initalizeActivity();
|
||||
}
|
||||
}
|
||||
|
||||
void initalizeActivity() {
|
||||
final PracticeActivityRecordEvent? recordEvent =
|
||||
widget.practiceEvent.userRecords.firstOrNull;
|
||||
if (recordEvent?.record == null) {
|
||||
recordModel = PracticeActivityRecordModel(
|
||||
question:
|
||||
widget.practiceEvent.practiceActivity.multipleChoice!.question,
|
||||
);
|
||||
} else {
|
||||
recordModel = recordEvent!.record;
|
||||
|
||||
//Note that only MultipleChoice activities will have this so we probably should move this logic to the MultipleChoiceActivity widget
|
||||
selectedChoiceIndex = recordModel?.latestResponse != null
|
||||
? widget.practiceEvent.practiceActivity.multipleChoice
|
||||
?.choiceIndex(recordModel!.latestResponse!)
|
||||
: null;
|
||||
|
||||
recordSubmittedPreviousSession = true;
|
||||
recordSubmittedThisSession = true;
|
||||
}
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
void updateChoice(int index) {
|
||||
setState(() {
|
||||
selectedChoiceIndex = index;
|
||||
recordModel!.addResponse(
|
||||
text: widget
|
||||
.practiceEvent.practiceActivity.multipleChoice!.choices[index],
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Widget get activityWidget {
|
||||
switch (widget.practiceEvent.practiceActivity.activityType) {
|
||||
case ActivityTypeEnum.multipleChoice:
|
||||
return MultipleChoiceActivity(
|
||||
card: this,
|
||||
updateChoice: updateChoice,
|
||||
isActive:
|
||||
!recordSubmittedPreviousSession && !recordSubmittedThisSession,
|
||||
);
|
||||
default:
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
}
|
||||
|
||||
void sendRecord() {
|
||||
MatrixState.pangeaController.activityRecordController
|
||||
.send(
|
||||
recordModel!,
|
||||
widget.practiceEvent,
|
||||
)
|
||||
.catchError((error) {
|
||||
ErrorHandler.logError(
|
||||
e: error,
|
||||
s: StackTrace.current,
|
||||
data: {
|
||||
'recordModel': recordModel?.toJson(),
|
||||
'practiceEvent': widget.practiceEvent.event.toJson(),
|
||||
},
|
||||
);
|
||||
return null;
|
||||
}).then((_) => widget.controller.showNextActivity());
|
||||
|
||||
setState(() {
|
||||
recordSubmittedThisSession = true;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
debugPrint(
|
||||
"MessagePracticeActivityContentState.build with selectedChoiceIndex: $selectedChoiceIndex",
|
||||
);
|
||||
return Column(
|
||||
children: [
|
||||
activityWidget,
|
||||
const SizedBox(height: 8),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Opacity(
|
||||
opacity: selectedChoiceIndex != null &&
|
||||
!recordSubmittedThisSession &&
|
||||
!recordSubmittedPreviousSession
|
||||
? 1.0
|
||||
: 0.5,
|
||||
child: TextButton(
|
||||
onPressed: () {
|
||||
if (recordSubmittedThisSession ||
|
||||
recordSubmittedPreviousSession) {
|
||||
return;
|
||||
}
|
||||
selectedChoiceIndex != null ? sendRecord() : null;
|
||||
},
|
||||
style: ButtonStyle(
|
||||
backgroundColor: WidgetStateProperty.all<Color>(
|
||||
AppConfig.primaryColor,
|
||||
),
|
||||
),
|
||||
child: Text(L10n.of(context)!.submit),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue