@@ -2,21 +2,39 @@ import 'package:flutter/material.dart';
22import 'package:flutter_svg/svg.dart' ;
33import 'package:gap/gap.dart' ;
44import 'package:qack/layout/layout_handler.dart' ;
5+ import 'package:qack/presentation/home/bloc/home_bloc.dart' ;
56import 'package:qack/presentation/home/models/base_translation_details.dart' ;
7+ import 'package:qack/presentation/home/repositories/repositories.dart' ;
8+ import 'package:qack/presentation/settings/models/models.dart' ;
69import 'package:qack/theme/themes/light_theme.dart' ;
710import 'package:url_launcher/url_launcher.dart' ;
811import 'package:vector_graphics/vector_graphics.dart' ;
912
1013class TranslationCard extends StatelessWidget {
11- const TranslationCard ({required this .translationDetails, super .key});
12- final BaseTranslationDetails translationDetails;
14+ const TranslationCard ({
15+ required this .status,
16+ required this .translationMapEntry,
17+ required this .exception,
18+ super .key,
19+ });
20+ final HomeStatus status;
21+ final MapEntry <Translator , BaseTranslationDetails > translationMapEntry;
22+
23+ /// This error is a general error in [HomeRepository] 's translateText method.
24+ /// It is not a translation error.
25+ /// See [BaseTranslationError] or [BaseTranslationDetails] .empty methods for
26+ /// translation errors.
27+ final Exception ? exception;
1328
1429 @override
1530 Widget build (BuildContext context) {
1631 const theme = LightTheme ();
1732 return LayoutHandler (
1833 mobile: TranslationCardView (
19- translationDetails: translationDetails,
34+ status: status,
35+ translator: translationMapEntry.key,
36+ translationDetails: translationMapEntry.value,
37+ exception: exception,
2038 cardMargin: const EdgeInsets .only (bottom: 16 ),
2139 cardPadding:
2240 const EdgeInsets .only (top: 12 , left: 12 , right: 12 , bottom: 4 ),
@@ -32,7 +50,10 @@ class TranslationCard extends StatelessWidget {
3250 ),
3351 ),
3452 tablet: TranslationCardView (
35- translationDetails: translationDetails,
53+ status: status,
54+ translator: translationMapEntry.key,
55+ translationDetails: translationMapEntry.value,
56+ exception: exception,
3657 cardMargin: const EdgeInsets .only (bottom: 16 ),
3758 cardPadding:
3859 const EdgeInsets .only (top: 12 , left: 12 , right: 12 , bottom: 4 ),
@@ -53,6 +74,8 @@ class TranslationCard extends StatelessWidget {
5374
5475class TranslationCardView extends StatelessWidget {
5576 const TranslationCardView ({
77+ required this .status,
78+ required this .translator,
5679 required this .translationDetails,
5780 required this .cardMargin,
5881 required this .cardPadding,
@@ -61,8 +84,13 @@ class TranslationCardView extends StatelessWidget {
6184 required this .translationPadding,
6285 required this .titleStyle,
6386 required this .translatedTextStyle,
87+ required this .exception,
6488 super .key,
6589 });
90+ final HomeStatus status;
91+ final Exception ? exception;
92+
93+ final Translator translator;
6694 final BaseTranslationDetails translationDetails;
6795
6896 final EdgeInsets cardMargin;
@@ -79,6 +107,40 @@ class TranslationCardView extends StatelessWidget {
79107 @override
80108 Widget build (BuildContext context) {
81109 const theme = LightTheme ();
110+ var translationStyle = translatedTextStyle;
111+ late final String translationText;
112+
113+ switch (status) {
114+ case HomeStatus .initial:
115+ case HomeStatus .empty:
116+ translationText = 'Empty translation.' ;
117+ case HomeStatus .loading:
118+ translationText = 'Loading...' ;
119+ case HomeStatus .success:
120+ // Check if the translation is empty
121+ if (translationDetails is EmptyTranslationDetails ) {
122+ translationText = 'Loading...' ;
123+ break ;
124+ }
125+
126+ if (translationDetails.status == TranslationStatus .loading) {
127+ translationText = 'Loading...' ;
128+ } else if (translationDetails.status == TranslationStatus .success) {
129+ translationText = translationDetails.translatedText! .outputText;
130+ } else if (translationDetails.status == TranslationStatus .error ||
131+ translationDetails.exception != null ) {
132+ translationText =
133+ 'Translation error: ${translationDetails .exception }' ;
134+ } else {
135+ translationText = 'Unknown error' ;
136+ }
137+ case HomeStatus .error:
138+ translationStyle = translatedTextStyle.copyWith (
139+ color: theme.errorColor,
140+ );
141+ translationText = 'Error: $exception ' ;
142+ }
143+
82144 return Padding (
83145 padding: cardMargin,
84146 child: DecoratedBox (
@@ -99,13 +161,13 @@ class TranslationCardView extends StatelessWidget {
99161 width: 28 ,
100162 child: SvgPicture (
101163 AssetBytesLoader (
102- translationDetails .svgPath,
164+ translator .svgPath,
103165 ),
104166 ),
105167 ),
106168 Gap (titleGap),
107169 Text (
108- translationDetails.translatorName ,
170+ translator.name ,
109171 style: titleStyle,
110172 ),
111173 const Spacer (),
@@ -115,22 +177,25 @@ class TranslationCardView extends StatelessWidget {
115177 padding: translationMargin,
116178 child: InkWell (
117179 onTap: () {
118- launchUrl (
119- Uri (
120- scheme: 'plecoapi' ,
121- host: 'x-callback-url' ,
122- path: 's' ,
123- queryParameters: {
124- 'q' : translationDetails.translatedText.outputText,
125- },
126- ),
127- );
180+ if (status == HomeStatus .success &&
181+ translationDetails.translatedText != null ) {
182+ launchUrl (
183+ Uri (
184+ scheme: 'plecoapi' ,
185+ host: 'x-callback-url' ,
186+ path: 's' ,
187+ queryParameters: {
188+ 'q' : translationText,
189+ },
190+ ),
191+ );
192+ }
128193 },
129194 child: Padding (
130195 padding: translationPadding,
131196 child: Text (
132- translationDetails.translatedText.outputText ,
133- style: translatedTextStyle ,
197+ translationText ,
198+ style: translationStyle ,
134199 ),
135200 ),
136201 ),
0 commit comments