mirror of
https://github.com/wger-project/flutter.git
synced 2026-11-29 09:20:56 +01:00
Handle (transient) case where an exercise has no translations
This commit is contained in:
@@ -19,6 +19,7 @@ import 'package:collection/collection.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:wger/helpers/consts.dart';
|
||||
import 'package:wger/models/core/language.dart';
|
||||
import 'package:wger/models/exercises/category.dart';
|
||||
import 'package:wger/models/exercises/equipment.dart';
|
||||
import 'package:wger/models/exercises/image.dart';
|
||||
@@ -102,18 +103,26 @@ class Exercise extends Equatable {
|
||||
|
||||
bool get showPlateCalculator => equipment.map((e) => e.id).contains(ID_EQUIPMENT_BARBELL);
|
||||
|
||||
/// Returns translation for the given language
|
||||
/// Returns the translation for the given language.
|
||||
///
|
||||
/// If no translation is found, English will be returned
|
||||
///
|
||||
/// Note: we return the first translation as a fallback if we don't find a
|
||||
/// translation in English. This is something that should never happen,
|
||||
/// but we can't make sure that no local installation hasn't deleted
|
||||
/// the entry in English.
|
||||
/// Falls back to English, then to any available translation. If the exercise
|
||||
/// has no translations at all, an empty placeholder in the requested language
|
||||
/// is returned so callers never have to handle a missing translation. This
|
||||
/// happens transiently while translations are still syncing, or for exercises
|
||||
/// that genuinely ship without any translation.
|
||||
Translation getTranslation(String language) {
|
||||
// If the language is in the form en-US, take the language code only
|
||||
final languageCode = language.split('-')[0];
|
||||
|
||||
if (translations.isEmpty) {
|
||||
_logger.info('Exercise-ID $id has no translations, returning an empty placeholder.');
|
||||
return Translation(
|
||||
name: '',
|
||||
description: '',
|
||||
language: Language(id: -1, shortName: languageCode, fullName: ''),
|
||||
);
|
||||
}
|
||||
|
||||
return translations.firstWhere(
|
||||
(e) => e.language.shortName == languageCode,
|
||||
orElse: () => translations.firstWhere(
|
||||
|
||||
@@ -266,6 +266,13 @@ class ExerciseRepository {
|
||||
if (category == null) {
|
||||
continue;
|
||||
}
|
||||
// Skip exercises with no usable translation yet: an exercise without a
|
||||
// name cannot be shown or searched, so keep it out of the catalog until
|
||||
// a translation arrives on a later emission.
|
||||
final translations = translationsByExercise[exerciseRow.id];
|
||||
if (translations == null || translations.isEmpty) {
|
||||
continue;
|
||||
}
|
||||
exercises.add(
|
||||
Exercise(
|
||||
id: exerciseRow.id,
|
||||
@@ -288,7 +295,7 @@ class ExerciseRepository {
|
||||
],
|
||||
images: imagesByExercise[exerciseRow.id] ?? const [],
|
||||
videos: videosByExercise[exerciseRow.id] ?? const [],
|
||||
translations: translationsByExercise[exerciseRow.id] ?? const [],
|
||||
translations: translations,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -252,10 +252,12 @@ void main() {
|
||||
});
|
||||
|
||||
test('separates primary and secondary muscles', () async {
|
||||
await seedLanguage(testEnglish.id, 'en', 'English');
|
||||
await seedCategory(testCategoryArms.id, 'Arms');
|
||||
await seedMuscle(tMuscle1.id, 'Pec');
|
||||
await seedMuscle(tMuscle2.id, 'Tri');
|
||||
await seedExercise(1, categoryId: testCategoryArms.id);
|
||||
await seedTranslation(1, exerciseId: 1, languageId: testEnglish.id, name: 'Bench Press');
|
||||
await linkPrimaryMuscle(1, tMuscle1.id, linkId: 1);
|
||||
await linkSecondaryMuscle(1, tMuscle2.id, linkId: 1);
|
||||
|
||||
@@ -267,10 +269,12 @@ void main() {
|
||||
});
|
||||
|
||||
test('keeps multiple equipment entries per exercise', () async {
|
||||
await seedLanguage(testEnglish.id, 'en', 'English');
|
||||
await seedCategory(testCategoryArms.id, 'Arms');
|
||||
await seedEquipment(testEquipmentBench.id, 'Bench');
|
||||
await seedEquipment(testEquipmentDumbbell.id, 'Dumbbell');
|
||||
await seedExercise(1, categoryId: testCategoryArms.id);
|
||||
await seedTranslation(1, exerciseId: 1, languageId: testEnglish.id, name: 'Bench Press');
|
||||
await linkEquipment(1, testEquipmentBench.id, linkId: 1);
|
||||
await linkEquipment(1, testEquipmentDumbbell.id, linkId: 2);
|
||||
|
||||
@@ -283,8 +287,10 @@ void main() {
|
||||
});
|
||||
|
||||
test('keeps multiple images per exercise', () async {
|
||||
await seedLanguage(testEnglish.id, 'en', 'English');
|
||||
await seedCategory(testCategoryArms.id, 'Arms');
|
||||
await seedExercise(1, categoryId: testCategoryArms.id);
|
||||
await seedTranslation(1, exerciseId: 1, languageId: testEnglish.id, name: 'Bench Press');
|
||||
await seedImage(1, exerciseId: 1, path: 'one.jpg');
|
||||
await seedImage(2, exerciseId: 1, path: 'two.jpg');
|
||||
|
||||
@@ -293,21 +299,25 @@ void main() {
|
||||
expect(state.exercises.single.images, hasLength(2));
|
||||
});
|
||||
|
||||
test('returns the exercise even if it has no translations or images', () async {
|
||||
test('skips an exercise that has no translation yet', () async {
|
||||
// During the initial sync the category can land before any translation.
|
||||
// Such an exercise has no name to show, so it stays out of the catalog
|
||||
// until a translation arrives on a later emission.
|
||||
await seedCategory(testCategoryArms.id, 'Arms');
|
||||
await seedExercise(1, categoryId: testCategoryArms.id);
|
||||
|
||||
final state = await repo.watchAllDrift().first;
|
||||
|
||||
expect(state.exercises, hasLength(1));
|
||||
expect(state.exercises.single.translations, isEmpty);
|
||||
expect(state.exercises.single.images, isEmpty);
|
||||
expect(state.exercises, isEmpty);
|
||||
});
|
||||
|
||||
test('keeps each exercise distinct when more than one is present', () async {
|
||||
await seedLanguage(testEnglish.id, 'en', 'English');
|
||||
await seedCategory(testCategoryArms.id, 'Arms');
|
||||
await seedExercise(1, categoryId: testCategoryArms.id);
|
||||
await seedExercise(2, categoryId: testCategoryArms.id);
|
||||
await seedTranslation(1, exerciseId: 1, languageId: testEnglish.id, name: 'Bench Press');
|
||||
await seedTranslation(2, exerciseId: 2, languageId: testEnglish.id, name: 'Squat');
|
||||
|
||||
final state = await repo.watchAllDrift().first;
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:wger/models/exercises/exercise.dart';
|
||||
|
||||
import '../../test_data/exercises.dart';
|
||||
|
||||
@@ -17,5 +18,18 @@ void main() {
|
||||
expect(base.getTranslation('fr-FR').id, 3);
|
||||
expect(base.getTranslation('pt').id, 5); // English again
|
||||
});
|
||||
|
||||
test('getTranslation returns an empty placeholder when there are no translations', () {
|
||||
// arrange: an exercise can briefly have no translations during the
|
||||
// initial sync, or ship without any. getTranslation must not throw.
|
||||
const exercise = Exercise(id: 1, uuid: 'uuid', category: testCategoryArms);
|
||||
|
||||
// act
|
||||
final translation = exercise.getTranslation('de-AT');
|
||||
|
||||
// assert
|
||||
expect(translation.name, '');
|
||||
expect(translation.language.shortName, 'de');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user