Skip to content

Commit

Permalink
feature complete
Browse files Browse the repository at this point in the history
  • Loading branch information
William Jordan-Cooley authored and William Jordan-Cooley committed Jan 13, 2024
1 parent 46df9e5 commit e94c76a
Show file tree
Hide file tree
Showing 11 changed files with 578 additions and 125 deletions.
4 changes: 4 additions & 0 deletions lib/pangea/choreographer/controllers/it_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,8 @@ class CurrentITStep {
}
}
}

// get continuance with highest level
Continuance get best =>
continuances.reduce((a, b) => a.level > b.level ? a : b);
}
46 changes: 31 additions & 15 deletions lib/pangea/choreographer/widgets/it_bar.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import 'dart:developer';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'package:fluffychat/pangea/choreographer/controllers/choreographer.dart';
import 'package:fluffychat/pangea/choreographer/controllers/it_controller.dart';
import 'package:fluffychat/pangea/choreographer/widgets/it_bar_buttons.dart';
import 'package:fluffychat/pangea/choreographer/widgets/it_feedback_card.dart';
import 'package:fluffychat/pangea/choreographer/widgets/translation_finished_flow.dart';
import 'package:fluffychat/pangea/constants/choreo_constants.dart';
import 'package:fluffychat/pangea/utils/error_handler.dart';
import 'package:fluffychat/pangea/widgets/igc/word_data_card.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import '../../../config/app_config.dart';
import '../../controllers/it_feedback_controller.dart';
import '../../models/it_response_model.dart';
import '../../utils/overlay.dart';
import '../../widgets/igc/word_data_card.dart';
import 'choice_array.dart';

class ITBar extends StatelessWidget {
Expand Down Expand Up @@ -225,17 +227,31 @@ class ITChoices extends StatelessWidget {
]) =>
OverlayUtil.showPositionedCard(
context: context,
cardToShow: WordDataCard(
word: controller.currentITStep!.continuances[index].text,
wordLang: controller.targetLangCode,
fullText: sourceText ?? controller.choreographer.currentText,
fullTextLang: sourceText != null
? controller.sourceLangCode
: controller.targetLangCode,
hasInfo: controller.currentITStep!.continuances[index].hasInfo,
choiceFeedback: choiceFeedback,
room: controller.choreographer.chatController.room,
),
cardToShow: choiceFeedback == null
? WordDataCard(
word: controller.currentITStep!.continuances[index].text,
wordLang: controller.targetLangCode,
fullText: sourceText ?? controller.choreographer.currentText,
fullTextLang: sourceText != null
? controller.sourceLangCode
: controller.targetLangCode,
hasInfo: controller.currentITStep!.continuances[index].hasInfo,
choiceFeedback: choiceFeedback,
room: controller.choreographer.chatController.room,
)
: ITFeedbackCard(
req: ITFeedbackRequestModel(
sourceText: sourceText!,
currentText: controller.choreographer.currentText,
chosenContinuance:
controller.currentITStep!.continuances[index].text,
bestContinuance: controller.currentITStep!.best.text,
feedbackLang: controller.targetLangCode,
sourceTextLang: controller.sourceLangCode,
targetLang: controller.targetLangCode,
),
choiceFeedback: choiceFeedback,
),
cardSize: const Size(300, 300),
borderColor: borderColor,
transformTargetId: controller.choreographer.itBarTransformTargetKey,
Expand Down
218 changes: 218 additions & 0 deletions lib/pangea/choreographer/widgets/it_feedback_card.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
import 'package:fluffychat/pangea/repo/full_text_translation_repo.dart';
import 'package:fluffychat/pangea/widgets/igc/why_button.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart';

import '../../../config/app_config.dart';
import '../../../widgets/matrix.dart';
import '../../controllers/it_feedback_controller.dart';
import '../../controllers/pangea_controller.dart';
import '../../utils/bot_style.dart';
import '../../widgets/common/bot_face_svg.dart';
import '../../widgets/igc/card_error_widget.dart';
import '../../widgets/igc/card_header.dart';

class ITFeedbackCard extends StatefulWidget {
final ITFeedbackRequestModel req;
final String choiceFeedback;

const ITFeedbackCard({
super.key,
required this.req,
required this.choiceFeedback,
});

@override
State<ITFeedbackCard> createState() => ITFeedbackCardController();
}

class ITFeedbackCardController extends State<ITFeedbackCard> {
final PangeaController controller = MatrixState.pangeaController;

Object? error;
bool isLoadingFeedback = false;
bool isTranslating = false;
ITFeedbackResponseModel? res;
String? translatedFeedback;

Response get noLanguages => Response("", 405);

@override
void initState() {
if (!mounted) return;
//any setup?
super.initState();
}

Future<void> getFeedback() async {
setState(() {
isLoadingFeedback = true;
});
controller.itFeedback
.get(widget.req)
.then((value) {
res = value;
})
.catchError((e) => error = e)
.whenComplete(
() => setState(() {
isLoadingFeedback = false;
}),
);
}

Future<void> translateFeedback() async {
setState(() {
isTranslating = true;
});
FullTextTranslationRepo.translate(
accessToken: await controller.userController.accessToken,
request: FullTextTranslationRequestModel(
text: res!.text,
tgtLang: controller.languageController.userL1?.langCode ??
widget.req.sourceTextLang,
userL1: controller.languageController.userL1?.langCode ??
widget.req.sourceTextLang,
userL2: controller.languageController.userL2?.langCode ??
widget.req.targetLang,
),
)
.then((value) {
translatedFeedback = value.bestTranslation;
})
.catchError((e) => error = e)
.whenComplete(
() => setState(() {
isTranslating = false;
}),
);
}

void handleGetExplanationButtonPress() {
if (isLoadingFeedback) return;
getFeedback();
}

@override
Widget build(BuildContext context) => error == null
? ITFeedbackCardView(controller: this)
: CardErrorWidget(error: error);
}

class ITFeedbackCardView extends StatelessWidget {
const ITFeedbackCardView({
super.key,
required this.controller,
});

final ITFeedbackCardController controller;

@override
Widget build(BuildContext context) {
final ScrollController scrollController = ScrollController();

return Scrollbar(
thumbVisibility: true,
controller: scrollController,
child: SingleChildScrollView(
controller: scrollController,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.center,
children: [
CardHeader(
text: controller.widget.req.chosenContinuance,
botExpression: BotExpression.down,
),
Text(
controller.widget.choiceFeedback,
style: BotStyle.text(context),
),
const SizedBox(height: 20),
if (controller.res == null)
WhyButton(
onPress: controller.handleGetExplanationButtonPress,
loading: controller.isLoadingFeedback,
),
if (controller.res != null)
Text(
controller.res!.text,
style: BotStyle.text(context),
),
// if res is not null, show a button to translate the text
if (controller.res != null && controller.translatedFeedback == null)
Column(
children: [
const SizedBox(height: 10),
TranslateButton(
onPress: controller.translateFeedback,
loading: controller.isTranslating,
),
],
),
if (controller.translatedFeedback != null)
//add little line to separate the text from the translation
Column(
children: [
const Divider(
color: AppConfig.primaryColor,
thickness: 2,
height: 20, // Set the space around the divider
indent: 20, // Set the starting space (left padding)
endIndent: 20, // Set the ending space (right padding)
),
Text(
controller.translatedFeedback!,
style: BotStyle.text(context),
),
],
),
],
),
),
);
}
}

// button to translate the text
class TranslateButton extends StatelessWidget {
const TranslateButton({
super.key,
required this.onPress,
required this.loading,
});

final VoidCallback onPress;
final bool loading;

@override
Widget build(BuildContext context) {
return TextButton(
onPressed: loading ? null : onPress,
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all<Color>(
AppConfig.primaryColor.withOpacity(0.1),
),
),
child: SizedBox(
width: 150, // set the width of the button contents here
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
if (!loading) const Icon(Icons.translate),
if (loading)
const Center(
child: SizedBox(
width: 24.0,
height: 24.0,
child: CircularProgressIndicator(),
),
),
// const SizedBox(width: 8),
// Text(L10n.of(context)!.translate),
],
),
),
);
}
}
2 changes: 1 addition & 1 deletion lib/pangea/config/environment.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Environment {
}

static String get choreoApi {
// return "http://localhost:8000/choreo";
return "http://localhost:8000/choreo";
return dotenv.env['CHOREO_API'] ?? 'Not found';
}

Expand Down
11 changes: 11 additions & 0 deletions lib/pangea/constants/model_keys.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,15 @@ class ModelKey {
static const String targetWord = "target_word";
static const String baseExampleSentence = "base_example_sentence";
static const String targetExampleSentence = "target_example_sentence";

//add goldTranslation, goldContinuance, chosenContinuance
static const String goldTranslation = "gold_translation";
static const String goldContinuance = "gold_continuance";
static const String chosenContinuance = "chosen_continuance";

// sourceText, currentText, bestContinuance, feedback_lang
static const String sourceText = "src";
static const String currentText = "current";
static const String bestContinuance = "best_continuance";
static const String feedbackLang = "feedback_lang";
}
Loading

0 comments on commit e94c76a

Please sign in to comment.