From 3d4f48a88d1ea5932b4098ad7cef3f64a8a33bcd Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Tue, 29 Mar 2022 23:58:00 +0200 Subject: [PATCH] Hide the translation fields, not all users will need this --- lib/l10n/app_en.arb | 1 + lib/providers/add_exercise_provider.dart | 37 ++++--- lib/screens/add_exercise_screen.dart | 131 +++++++++++++---------- 3 files changed, 98 insertions(+), 71 deletions(-) diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index f2f16502..ef2677df 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -584,6 +584,7 @@ "language": "Language", "addExercise": "Add exercise", "translation": "Translation", + "translateExercise": "Translate this exercise now", "baseData": "Basics in English", "@baseData": { "description": "The base data for an exercise such as category, trained muscles, etc." diff --git a/lib/providers/add_exercise_provider.dart b/lib/providers/add_exercise_provider.dart index 310a9696..e86f2516 100644 --- a/lib/providers/add_exercise_provider.dart +++ b/lib/providers/add_exercise_provider.dart @@ -24,10 +24,10 @@ class AddExerciseProvider with ChangeNotifier { String? _nameTranslation; String? _descriptionEn; String? _descriptionTranslation; - Language? _language; + late Language _language; List _alternativeNamesEn = []; List _alternativeNamesTranslation = []; - ExerciseCategory? _category; + late ExerciseCategory _category; List _variations = []; List _equipment = []; List _primaryMuscles = []; @@ -44,10 +44,8 @@ class AddExerciseProvider with ChangeNotifier { _nameTranslation = null; _descriptionEn = null; _descriptionTranslation = null; - _language = null; _alternativeNamesEn = []; _alternativeNamesTranslation = []; - _category = null; _variations = []; _equipment = []; _primaryMuscles = []; @@ -64,9 +62,9 @@ class AddExerciseProvider with ChangeNotifier { set equipment(List equipment) => _equipment = equipment; List get equipment => [..._equipment]; set category(ExerciseCategory category) => _category = category; - ExerciseCategory get category => _category!; + ExerciseCategory get category => _category; set language(Language language) => _language = language; - Language get language => _language!; + Language get language => _language; ExerciseBase get base { return ExerciseBase( @@ -130,7 +128,7 @@ class AddExerciseProvider with ChangeNotifier { log(''); log('Language specific...'); - log('Language: ${_language?.shortName}'); + log('Language: ${_language.shortName}'); log('Name: en/$_nameEn translation/$_nameTranslation'); log('Description: en/$_descriptionEn translation/$_descriptionTranslation'); log('Alternate names: en/$_alternativeNamesEn translation/$_alternativeNamesTranslation'); @@ -142,24 +140,31 @@ class AddExerciseProvider with ChangeNotifier { // Create the base final base = await addExerciseBase(); - // Set the variations + // Create the variations // ... - // Create the translations + // Create the base description in English Exercise exerciseTranslationEn = exerciseEn; exerciseTranslationEn.base = base; exerciseTranslationEn = await addExerciseTranslation(exerciseTranslationEn); for (final alias in _alternativeNamesEn) { - exerciseTranslationEn.alias.add(await addExerciseAlias(alias, exerciseTranslationEn.id!)); + if (alias.isNotEmpty) { + exerciseTranslationEn.alias.add(await addExerciseAlias(alias, exerciseTranslationEn.id!)); + } } - Exercise exerciseTranslationLang = exerciseTranslation; - exerciseTranslationLang.base = base; - exerciseTranslationLang = await addExerciseTranslation(exerciseTranslationLang); - for (final alias in _alternativeNamesTranslation) { - exerciseTranslationLang.alias.add(await addExerciseAlias(alias, exerciseTranslationLang.id!)); + // Create the translations + if (_nameTranslation != null && _descriptionTranslation != null) { + Exercise exerciseTranslationLang = exerciseTranslation; + exerciseTranslationLang.base = base; + exerciseTranslationLang = await addExerciseTranslation(exerciseTranslationLang); + for (final alias in _alternativeNamesTranslation) { + exerciseTranslationLang.alias.add( + await addExerciseAlias(alias, exerciseTranslationLang.id!), + ); + } + await addExerciseTranslation(exerciseTranslationLang); } - await addExerciseTranslation(exerciseTranslationLang); // Create the images await addImages(base); diff --git a/lib/screens/add_exercise_screen.dart b/lib/screens/add_exercise_screen.dart index dc1546ad..15e8a711 100644 --- a/lib/screens/add_exercise_screen.dart +++ b/lib/screens/add_exercise_screen.dart @@ -32,7 +32,7 @@ abstract class ValidateStep { } /// The amount of characters an exercise description needs to have -const MIN_CHARS_DESCRIPTION = 4; +const MIN_CHARS_DESCRIPTION = 40; /// The amount of characters an exercise name needs to have const MIN_CHARS_NAME = 5; @@ -75,6 +75,7 @@ class _AddExerciseScreenState extends State { Widget _controlsBuilder(BuildContext context, ControlsDetails details) { return Row( + mainAxisAlignment: MainAxisAlignment.spaceAround, children: [ if (_currentStep == lastStepIndex) ElevatedButton( @@ -82,11 +83,11 @@ class _AddExerciseScreenState extends State { child: Text(AppLocalizations.of(context).save), ) else - TextButton( + OutlinedButton( onPressed: details.onStepContinue, child: Text(AppLocalizations.of(context).next), ), - TextButton( + OutlinedButton( onPressed: details.onStepCancel, child: Text(AppLocalizations.of(context).previous), ), @@ -134,7 +135,6 @@ class _AddExerciseScreenState extends State { if (_keys[_currentStep].currentState?.validate() ?? false) { _keys[_currentStep].currentState?.save(); - context.read().addExercise(); if (_currentStep != lastStepIndex) { setState(() { @@ -145,6 +145,7 @@ class _AddExerciseScreenState extends State { if (_currentStep == lastStepIndex) { _addExercise(); + context.read().addExercise(); } }, onStepCancel: () => setState(() { @@ -183,13 +184,10 @@ class _BasicStepContent extends StatelessWidget { key: formkey, child: Column( children: [ - Text( - AppLocalizations.of(context).baseNameEnglish, - style: Theme.of(context).textTheme.caption, - ), AddExerciseTextArea( onChange: (value) => {}, title: '${AppLocalizations.of(context).name}*', + helperText: AppLocalizations.of(context).baseNameEnglish, isRequired: true, validator: (name) => validateName(name, context), onSaved: (String? name) => addExerciseProvider.exerciseNameEn = name!, @@ -354,67 +352,90 @@ class _DescriptionStepContent extends StatelessWidget { } } -class _DescriptionTranslationStepContent extends StatelessWidget { +class _DescriptionTranslationStepContent extends StatefulWidget { final GlobalKey formkey; const _DescriptionTranslationStepContent({required this.formkey}); + @override + State<_DescriptionTranslationStepContent> createState() => + _DescriptionTranslationStepContentState(); +} + +class _DescriptionTranslationStepContentState extends State<_DescriptionTranslationStepContent> { + bool translate = false; + @override Widget build(BuildContext context) { final addExerciseProvider = context.read(); final exerciseProvider = context.read(); - final languages = exerciseProvider.languages; return Form( - key: formkey, + key: widget.formkey, child: Column( children: [ - ExerciseCategoryInputWidget( - categories: languages, - title: AppLocalizations.of(context).language, - displayName: (Language l) => l.fullName, - callback: (Language newValue) { - addExerciseProvider.language = newValue; + SwitchListTile( + title: Text(AppLocalizations.of(context).translation), + subtitle: Text(AppLocalizations.of(context).translateExercise), + value: translate, + onChanged: (_) { + setState(() { + translate = !translate; + }); }, ), - AddExerciseTextArea( - onChange: (value) => {}, - title: '${AppLocalizations.of(context).name}*', - isRequired: true, - validator: (name) => validateName(name, context), - onSaved: (String? name) => addExerciseProvider.exerciseNameTrans = name!, - ), - AddExerciseTextArea( - onChange: (value) => {}, - title: AppLocalizations.of(context).alternativeNames, - isMultiline: true, - helperText: AppLocalizations.of(context).oneNamePerLine, - validator: (alternateNames) { - // check that each line (name) is at least MIN_CHARACTERS_NAME long - if (alternateNames?.isNotEmpty == true) { - final names = alternateNames!.split('\n'); - for (final name in names) { - if (name.length < MIN_CHARS_NAME || name.length > MAX_CHARS_NAME) { - return AppLocalizations.of(context).enterCharacters( - MIN_CHARS_NAME, - MAX_CHARS_NAME, - ); - } - } - } - return null; - }, - onSaved: (String? alternateName) => - addExerciseProvider.alternateNamesTrans = alternateName!.split('\n'), - ), - AddExerciseTextArea( - onChange: (value) => {}, - title: '${AppLocalizations.of(context).description}*', - isRequired: true, - isMultiline: true, - validator: (name) => validateDescription(name, context), - onSaved: (String? description) => addExerciseProvider.descriptionTrans = description!, - ), + if (translate) + Column( + children: [ + ExerciseCategoryInputWidget( + categories: languages, + title: AppLocalizations.of(context).language, + displayName: (Language l) => l.fullName, + callback: (Language newValue) { + addExerciseProvider.language = newValue; + }, + ), + AddExerciseTextArea( + onChange: (value) => {}, + title: '${AppLocalizations.of(context).name}*', + isRequired: true, + validator: (name) => validateName(name, context), + onSaved: (String? name) => addExerciseProvider.exerciseNameTrans = name!, + ), + AddExerciseTextArea( + onChange: (value) => {}, + title: AppLocalizations.of(context).alternativeNames, + isMultiline: true, + helperText: AppLocalizations.of(context).oneNamePerLine, + validator: (alternateNames) { + // check that each line (name) is at least MIN_CHARACTERS_NAME long + if (alternateNames?.isNotEmpty == true) { + final names = alternateNames!.split('\n'); + for (final name in names) { + if (name.length < MIN_CHARS_NAME || name.length > MAX_CHARS_NAME) { + return AppLocalizations.of(context).enterCharacters( + MIN_CHARS_NAME, + MAX_CHARS_NAME, + ); + } + } + } + return null; + }, + onSaved: (String? alternateName) => + addExerciseProvider.alternateNamesTrans = alternateName!.split('\n'), + ), + AddExerciseTextArea( + onChange: (value) => {}, + title: '${AppLocalizations.of(context).description}*', + isRequired: true, + isMultiline: true, + validator: (name) => validateDescription(name, context), + onSaved: (String? description) => + addExerciseProvider.descriptionTrans = description!, + ), + ], + ), ], ), );