mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-05 04:57:37 +01:00
382 lines
14 KiB
Dart
382 lines
14 KiB
Dart
/*
|
|
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
|
* Copyright (c) 2026 wger Team
|
|
*
|
|
* wger Workout Manager is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:wger/core/consts.dart';
|
|
import 'package:wger/core/error_dialogs.dart';
|
|
import 'package:wger/core/exceptions/http_exception.dart';
|
|
import 'package:wger/core/form_screen.dart';
|
|
import 'package:wger/core/network/network_provider.dart';
|
|
import 'package:wger/core/wide_screen_wrapper.dart';
|
|
import 'package:wger/core/widgets/app_bar.dart';
|
|
import 'package:wger/core/widgets/progress_indicator.dart';
|
|
import 'package:wger/features/account/providers/account_notifier.dart';
|
|
import 'package:wger/features/account/widgets/forms.dart';
|
|
import 'package:wger/features/exercises/providers/add_exercise_notifier.dart';
|
|
import 'package:wger/features/exercises/providers/exercises_notifier.dart';
|
|
import 'package:wger/features/exercises/screens/exercise_screen.dart';
|
|
import 'package:wger/features/exercises/widgets/add_exercise/steps/step_1_basics.dart';
|
|
import 'package:wger/features/exercises/widgets/add_exercise/steps/step_2_variations.dart';
|
|
import 'package:wger/features/exercises/widgets/add_exercise/steps/step_3_description.dart';
|
|
import 'package:wger/features/exercises/widgets/add_exercise/steps/step_4_translations.dart';
|
|
import 'package:wger/features/exercises/widgets/add_exercise/steps/step_5_images.dart';
|
|
import 'package:wger/features/exercises/widgets/add_exercise/steps/step_6_overview.dart';
|
|
import 'package:wger/l10n/generated/app_localizations.dart';
|
|
|
|
class AddExerciseScreen extends ConsumerWidget {
|
|
const AddExerciseScreen({super.key});
|
|
|
|
static const routeName = '/exercises/add';
|
|
|
|
@override
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
final isOnline = ref.watch(networkStatusProvider);
|
|
if (!isOnline) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text(AppLocalizations.of(context).contributeExercise),
|
|
),
|
|
body: Center(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(24),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
const Icon(Icons.cloud_off, size: 48),
|
|
const SizedBox(height: 16),
|
|
Text(
|
|
AppLocalizations.of(context).youAreOffline,
|
|
textAlign: TextAlign.center,
|
|
style: Theme.of(context).textTheme.titleMedium,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
final account = ref.watch(accountProvider).value;
|
|
if (account == null) {
|
|
return const Scaffold(body: Center(child: CircularProgressIndicator()));
|
|
}
|
|
return account.isTrustworthy ? const AddExerciseStepper() : const EmailNotVerified();
|
|
}
|
|
}
|
|
|
|
class AddExerciseStepper extends ConsumerStatefulWidget {
|
|
const AddExerciseStepper({super.key});
|
|
|
|
static const STEPS_IN_FORM = 6;
|
|
static const DESCRIPTION_STEP_INDEX = 2;
|
|
static const TRANSLATION_STEP_INDEX = 3;
|
|
|
|
@override
|
|
_AddExerciseStepperState createState() => _AddExerciseStepperState();
|
|
}
|
|
|
|
class _AddExerciseStepperState extends ConsumerState<AddExerciseStepper> {
|
|
int _currentStep = 0;
|
|
int lastStepIndex = AddExerciseStepper.STEPS_IN_FORM - 1;
|
|
bool _isLoading = false;
|
|
bool _isValidatingLanguage = false;
|
|
String? _languageError;
|
|
Widget errorWidget = const SizedBox.shrink();
|
|
|
|
final List<GlobalKey<FormState>> _keys = [
|
|
GlobalKey<FormState>(),
|
|
GlobalKey<FormState>(),
|
|
GlobalKey<FormState>(),
|
|
GlobalKey<FormState>(),
|
|
GlobalKey<FormState>(),
|
|
GlobalKey<FormState>(),
|
|
];
|
|
|
|
Widget _controlsBuilder(BuildContext context, ControlsDetails details) {
|
|
return Column(
|
|
children: [
|
|
const SizedBox(height: 10),
|
|
if (_currentStep == lastStepIndex) errorWidget,
|
|
if (_languageError != null && details.stepIndex == _currentStep)
|
|
Padding(
|
|
padding: const EdgeInsets.only(bottom: 8),
|
|
child: Text(
|
|
_languageError!,
|
|
style: TextStyle(color: Theme.of(context).colorScheme.error),
|
|
),
|
|
),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
|
children: [
|
|
OutlinedButton(
|
|
onPressed: details.onStepCancel,
|
|
child: Text(AppLocalizations.of(context).previous),
|
|
),
|
|
|
|
// Submit button on last step
|
|
if (_currentStep == lastStepIndex)
|
|
ElevatedButton(
|
|
onPressed: _isLoading
|
|
? null
|
|
: () async {
|
|
setState(() {
|
|
_isLoading = true;
|
|
errorWidget = const SizedBox.shrink();
|
|
});
|
|
final addExerciseNotifier = ref.read(addExerciseProvider.notifier);
|
|
// postExerciseToServer() clears the form, so capture the
|
|
// name now for the success dialog's fallback.
|
|
final submittedName = ref.read(addExerciseProvider).exerciseNameEn ?? '';
|
|
|
|
int? exerciseId;
|
|
try {
|
|
exerciseId = await addExerciseNotifier.postExerciseToServer();
|
|
} on WgerHttpException catch (error) {
|
|
if (context.mounted) {
|
|
setState(() {
|
|
errorWidget = FormHttpErrorsWidget(error);
|
|
});
|
|
}
|
|
} finally {
|
|
if (mounted) {
|
|
setState(() {
|
|
_isLoading = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
if (exerciseId == null || !context.mounted) {
|
|
return;
|
|
}
|
|
|
|
// The new exercise only reaches the local DB once it
|
|
// syncs, so it may not be found yet. The dialog still
|
|
// reports success, but we only offer to open it if it
|
|
// is actually already available.
|
|
final exercise = ref
|
|
.read(exercisesProvider)
|
|
.value
|
|
?.getByIdOrNull(exerciseId);
|
|
final name =
|
|
exercise
|
|
?.getTranslation(Localizations.localeOf(context).languageCode)
|
|
.name ??
|
|
submittedName;
|
|
|
|
return showDialog(
|
|
context: context,
|
|
builder: (BuildContext context) {
|
|
return AlertDialog(
|
|
title: Text(AppLocalizations.of(context).success),
|
|
content: Text(AppLocalizations.of(context).cacheWarning),
|
|
actions: [
|
|
TextButton(
|
|
child: Text(name),
|
|
onPressed: () {
|
|
Navigator.of(context).pop();
|
|
if (exercise != null) {
|
|
Navigator.pushReplacementNamed(
|
|
context,
|
|
ExerciseDetailScreen.routeName,
|
|
arguments: exercise,
|
|
);
|
|
}
|
|
},
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
},
|
|
child: _isLoading
|
|
? const FormProgressIndicator()
|
|
: Text(AppLocalizations.of(context).save),
|
|
)
|
|
else
|
|
ElevatedButton(
|
|
onPressed: _isValidatingLanguage ? null : details.onStepContinue,
|
|
child: _isValidatingLanguage
|
|
? const FormProgressIndicator()
|
|
: Text(AppLocalizations.of(context).next),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: EmptyAppBar(AppLocalizations.of(context).contributeExercise),
|
|
body: WidescreenWrapper(
|
|
child: Stepper(
|
|
controlsBuilder: _controlsBuilder,
|
|
steps: [
|
|
Step(
|
|
title: Text(AppLocalizations.of(context).baseData),
|
|
content: Step1Basics(formkey: _keys[0]),
|
|
),
|
|
Step(
|
|
title: Text(AppLocalizations.of(context).variations),
|
|
content: Step2Variations(formkey: _keys[1]),
|
|
),
|
|
Step(
|
|
title: Text(AppLocalizations.of(context).description),
|
|
content: Step3Description(formkey: _keys[2]),
|
|
),
|
|
Step(
|
|
title: Text(AppLocalizations.of(context).translation),
|
|
content: Step4Translation(formkey: _keys[3]),
|
|
),
|
|
Step(
|
|
title: Text(AppLocalizations.of(context).images),
|
|
content: Step5Images(formkey: _keys[4]),
|
|
),
|
|
Step(title: Text(AppLocalizations.of(context).overview), content: Step6Overview()),
|
|
],
|
|
currentStep: _currentStep,
|
|
onStepContinue: () async {
|
|
if (!(_keys[_currentStep].currentState?.validate() ?? false)) {
|
|
return;
|
|
}
|
|
_keys[_currentStep].currentState?.save();
|
|
|
|
final addExerciseState = ref.read(addExerciseProvider);
|
|
final addExerciseNotifier = ref.read(addExerciseProvider.notifier);
|
|
|
|
// The Description and Translation steps require a language check
|
|
if (_currentStep == AddExerciseStepper.DESCRIPTION_STEP_INDEX ||
|
|
_currentStep == AddExerciseStepper.TRANSLATION_STEP_INDEX) {
|
|
final isTranslationStep = _currentStep == AddExerciseStepper.TRANSLATION_STEP_INDEX;
|
|
final language = isTranslationStep
|
|
? addExerciseState.languageTranslation
|
|
: addExerciseState.languageEn;
|
|
final description = isTranslationStep
|
|
? addExerciseState.descriptionTrans
|
|
: addExerciseState.descriptionEn;
|
|
|
|
// Only validate if there is text and a language to check against
|
|
if (language != null && description != null && description.isNotEmpty) {
|
|
setState(() {
|
|
_isValidatingLanguage = true;
|
|
_languageError = null;
|
|
});
|
|
|
|
final error = await addExerciseNotifier.validateLanguage(
|
|
description,
|
|
language.shortName,
|
|
);
|
|
|
|
if (!mounted) {
|
|
return;
|
|
}
|
|
|
|
if (error != null) {
|
|
setState(() {
|
|
_isValidatingLanguage = false;
|
|
_languageError = error;
|
|
});
|
|
return;
|
|
}
|
|
|
|
setState(() {
|
|
_isValidatingLanguage = false;
|
|
_languageError = null;
|
|
});
|
|
}
|
|
} else {
|
|
setState(() => _languageError = null);
|
|
}
|
|
|
|
if (_currentStep != lastStepIndex) {
|
|
setState(() {
|
|
_currentStep += 1;
|
|
});
|
|
}
|
|
},
|
|
onStepCancel: () => setState(() {
|
|
_languageError = null;
|
|
if (_currentStep != 0) {
|
|
_currentStep -= 1;
|
|
}
|
|
}),
|
|
/*
|
|
onStepTapped: (int index) {
|
|
setState(() {
|
|
_currentStep = index;
|
|
});
|
|
},
|
|
*/
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class EmailNotVerified extends ConsumerWidget {
|
|
const EmailNotVerified({super.key});
|
|
|
|
@override
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
return Scaffold(
|
|
appBar: EmptyAppBar(AppLocalizations.of(context).unVerifiedEmail),
|
|
body: Container(
|
|
padding: const EdgeInsets.all(25),
|
|
child: Center(
|
|
child: Card(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
ListTile(
|
|
leading: const Icon(Icons.warning),
|
|
title: Text(AppLocalizations.of(context).unVerifiedEmail),
|
|
subtitle: Text(
|
|
AppLocalizations.of(
|
|
context,
|
|
).contributeExerciseWarning(MIN_ACCOUNT_AGE.toString()),
|
|
),
|
|
),
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.end,
|
|
children: [
|
|
TextButton(
|
|
onPressed: () {
|
|
Navigator.pushReplacementNamed(
|
|
context,
|
|
FormScreen.routeName,
|
|
arguments: FormScreenArguments(
|
|
AppLocalizations.of(context).userProfile,
|
|
const UserProfileForm(),
|
|
),
|
|
);
|
|
},
|
|
child: Text(AppLocalizations.of(context).userProfile),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|