296 lines
10 KiB
Dart
296 lines
10 KiB
Dart
import 'dart:collection';
|
|
import 'package:aitrainer_app/bloc/exercise_log/exercise_log_bloc.dart';
|
|
import 'package:aitrainer_app/widgets/app_bar.dart';
|
|
import 'package:aitrainer_app/widgets/bottom_nav.dart';
|
|
import 'package:aitrainer_app/widgets/splash.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:intl/intl.dart';
|
|
import 'package:aitrainer_app/localization/app_language.dart';
|
|
import 'package:aitrainer_app/model/cache.dart';
|
|
import 'package:aitrainer_app/model/exercise.dart';
|
|
import 'package:aitrainer_app/model/exercise_type.dart';
|
|
import 'package:aitrainer_app/repository/exercise_repository.dart';
|
|
import 'package:aitrainer_app/library/tree_view.dart';
|
|
import 'package:aitrainer_app/util/common.dart';
|
|
import 'package:aitrainer_app/util/trans.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:aitrainer_app/widgets/treeview_parent_widget.dart';
|
|
|
|
class ExerciseLogPage extends StatefulWidget {
|
|
@override
|
|
_ExerciseLogPage createState() => _ExerciseLogPage();
|
|
}
|
|
|
|
class _ExerciseLogPage extends State<ExerciseLogPage> with Trans, Common {
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
LinkedHashMap arguments = ModalRoute.of(context).settings.arguments;
|
|
//final ExerciseRepository exerciseRepository = arguments['exerciseRepository'];
|
|
final int customerId = arguments['customerId'];
|
|
setContext(context);
|
|
|
|
return BlocProvider(
|
|
create: (context) => ExerciseLogBloc(exerciseRepository: ExerciseRepository())..
|
|
add(ExerciseLogLoad()),
|
|
child: BlocConsumer<ExerciseLogBloc, ExerciseLogState>(
|
|
listener: (context, state) {
|
|
if ( state is ExerciseLogLoading ) {
|
|
return LoadingDialog();
|
|
} else if ( state is ExerciseLogError ) {
|
|
Scaffold.of(context).showSnackBar(SnackBar(
|
|
backgroundColor: Colors.orange,
|
|
content:
|
|
Text(state.message, style: TextStyle(color: Colors.white))));
|
|
}
|
|
},
|
|
builder: (context, state) {
|
|
final exerciseBloc = BlocProvider.of<ExerciseLogBloc>(context);
|
|
if ( state is ExerciseLogReady ) {
|
|
return getExerciseLog(customerId, exerciseBloc);
|
|
} else {
|
|
return getExerciseLog(customerId, exerciseBloc);
|
|
}
|
|
}
|
|
|
|
)
|
|
);
|
|
|
|
|
|
}
|
|
|
|
Widget getExerciseLog(int customerId, ExerciseLogBloc exerciseLogBloc) {
|
|
return Scaffold(
|
|
appBar: AppBarNav(depth: 1),
|
|
body: Container(
|
|
padding: EdgeInsets.all(20),
|
|
decoration: BoxDecoration(
|
|
image: DecorationImage(
|
|
image: customerId == Cache().userLoggedIn.customerId ? AssetImage('asset/image/WT_light_background.png'):
|
|
AssetImage('asset/image/WT_menu_dark.png'),
|
|
fit: BoxFit.cover,
|
|
alignment: Alignment.center,
|
|
),
|
|
),
|
|
child: exerciseWidget(exerciseLogBloc, customerId),
|
|
),
|
|
bottomNavigationBar: BottomNavigator(bottomNavIndex: 1),
|
|
);
|
|
}
|
|
|
|
Widget exerciseWidget(ExerciseLogBloc exerciseLogBloc, int customerId) {
|
|
return TreeView(
|
|
startExpanded: false,
|
|
children: _getTreeChildren(exerciseLogBloc, customerId),
|
|
);
|
|
}
|
|
|
|
List<Widget> _getTreeChildren(ExerciseLogBloc exerciseLogBloc, int customerId) {
|
|
final ExerciseRepository exerciseRepository = exerciseLogBloc.exerciseRepository;
|
|
|
|
if ( customerId == Cache().userLoggedIn.customerId ) {
|
|
exerciseRepository.exerciseList = exerciseRepository.getExerciseList();
|
|
} else if ( Cache().getTrainee() != null && customerId == Cache().getTrainee().customerId ) {
|
|
exerciseRepository.exerciseList = exerciseRepository.getExerciseListTrainee();
|
|
}
|
|
exerciseRepository.sortByDate();
|
|
|
|
List<Widget> listWidget = List();
|
|
|
|
Card explanation = Card(
|
|
color: Colors.white60,
|
|
child: Container(
|
|
padding: EdgeInsets.only(left: 10, right: 5, top: 12, bottom: 8),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
|
children: [
|
|
Row(
|
|
children: [
|
|
Icon(
|
|
Icons.info,
|
|
color: Colors.orangeAccent,
|
|
),
|
|
Text(" "),
|
|
Text(
|
|
t("My Exercise Logs"),
|
|
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
|
|
),
|
|
],
|
|
),
|
|
Divider(
|
|
color: Colors.transparent,
|
|
),
|
|
Text(
|
|
t("In this list you will find all your executed exercises grouped by the date."),
|
|
style: TextStyle(fontSize: 12, fontWeight: FontWeight.normal),
|
|
),
|
|
|
|
],
|
|
)
|
|
)
|
|
);
|
|
listWidget.add(explanation);
|
|
|
|
List<Exercise> listExercises = List();
|
|
String origDate = "";
|
|
exerciseRepository.exerciseList.forEach((exercise) {
|
|
String exerciseDate = DateFormat("yyyy-MM-dd", AppLanguage().appLocal.toString()).format(exercise.dateAdd);
|
|
|
|
if ( origDate != exerciseDate) {
|
|
if ( origDate.length == 0 ) {
|
|
listExercises.add(exercise);
|
|
origDate = exerciseDate;
|
|
} else {
|
|
listWidget.add(
|
|
Container(
|
|
margin: const EdgeInsets.only(left: 4.0),
|
|
child: TreeViewChild(
|
|
startExpanded: false,
|
|
parent: TreeviewParentWidget(text: origDate),
|
|
children: _getChildList(listExercises, exerciseRepository, exerciseLogBloc),
|
|
)
|
|
)
|
|
);
|
|
listExercises = List();
|
|
listExercises.add(exercise);
|
|
origDate = exerciseDate;
|
|
}
|
|
|
|
} else {
|
|
listExercises.add(exercise);
|
|
origDate = exerciseDate;
|
|
}
|
|
|
|
|
|
});
|
|
if ( listExercises.length > 0) {
|
|
listWidget.add(
|
|
Container(
|
|
margin: const EdgeInsets.only(left: 4.0),
|
|
child: TreeViewChild(
|
|
startExpanded: true,
|
|
parent: TreeviewParentWidget(text: origDate),
|
|
children: _getChildList(listExercises, exerciseRepository, exerciseLogBloc),
|
|
)
|
|
)
|
|
);
|
|
}
|
|
|
|
return listWidget;
|
|
}
|
|
|
|
List<Widget> _getChildList(List<Exercise> listExercises, ExerciseRepository exerciseRepository, ExerciseLogBloc exerciseLogBloc) {
|
|
List<Widget> list = List();
|
|
bool isEnglish = AppLanguage().appLocal == Locale('en');
|
|
|
|
listExercises.forEach((exercise) {
|
|
ExerciseType exerciseType = exerciseRepository.getExerciseTypeById(exercise.exerciseTypeId);
|
|
String exerciseName = isEnglish ? exerciseType.name : exerciseType.nameTranslation;
|
|
|
|
String unitQuantity = exerciseType.unitQuantity == "1"
|
|
? exercise.unitQuantity.toStringAsFixed(0) +
|
|
" " +
|
|
t(exerciseType.unitQuantityUnit) +
|
|
" "
|
|
: "";
|
|
|
|
String labelExercise =
|
|
unitQuantity +
|
|
exercise.quantity.toStringAsFixed(0) +
|
|
" " +
|
|
t(exercise.unit);
|
|
|
|
list.add(
|
|
Card(
|
|
margin: EdgeInsets.only(left: 10, top: 5),
|
|
color: Colors.white54,
|
|
child: Container(
|
|
padding: const EdgeInsets.only(left: 5, top: 0, right: 5, bottom: 0),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.start,
|
|
children: [
|
|
|
|
Icon(Icons.accessibility, color: Colors.black12),
|
|
SizedBox(width: 10,),
|
|
Flexible(
|
|
fit: FlexFit.tight,
|
|
flex: 8,
|
|
child: Text(
|
|
exerciseName,
|
|
|
|
style: TextStyle(fontSize: 12, color: Colors.black),
|
|
),
|
|
),
|
|
Flexible(fit: FlexFit.tight, child: SizedBox(width: 10,)),
|
|
Text(labelExercise , style: TextStyle(fontSize: 9, color: Colors.blueAccent.shade700),) ,
|
|
Flexible(fit: FlexFit.tight, child: SizedBox(width: 10,)),
|
|
IconButton(
|
|
icon: Icon(Icons.delete, color: Colors.black12),
|
|
onPressed: () {
|
|
confirmationDialog(exerciseLogBloc, exercise);
|
|
},
|
|
),
|
|
|
|
]),
|
|
)
|
|
),
|
|
);
|
|
|
|
});
|
|
|
|
return list;
|
|
}
|
|
|
|
void confirmationDialog( ExerciseLogBloc bloc, Exercise exercise ) {
|
|
|
|
ExerciseType exerciseType = bloc.exerciseRepository.getExerciseTypeById(exercise.exerciseTypeId);
|
|
String exerciseName = AppLanguage().appLocal == Locale("en")
|
|
? exerciseType.name
|
|
: exerciseType.nameTranslation;
|
|
|
|
String strDate = AppLanguage().appLocal == Locale("en")
|
|
? "on the " + DateFormat(DateFormat.YEAR_MONTH_DAY, AppLanguage().appLocal.toString()).format(exercise.dateAdd.toUtc())
|
|
: DateFormat(DateFormat.YEAR_MONTH_DAY, AppLanguage().appLocal.toString()).format(exercise.dateAdd.toUtc()) + "-n";
|
|
|
|
showCupertinoDialog(
|
|
useRootNavigator: true,
|
|
context: context,
|
|
//barrierDismissible: false,
|
|
builder:(_) => CupertinoAlertDialog(
|
|
title: Text(t("Are you sure to delete this exercise?")),
|
|
content: Column(
|
|
|
|
children: [
|
|
Divider(),
|
|
Text(t("Exercise") + ": " + exerciseName,
|
|
style: (TextStyle(color: Colors.blue)),),
|
|
Text(
|
|
exercise.quantity.toStringAsFixed(0) + "x" + exercise.unitQuantity.toStringAsFixed(0) + " " + exerciseType.unitQuantityUnit,
|
|
style: (TextStyle(color: Colors.deepOrange)),
|
|
),
|
|
Text(
|
|
strDate,
|
|
style: (TextStyle(color: Colors.deepOrange)),
|
|
),
|
|
|
|
]),
|
|
actions: [
|
|
FlatButton(
|
|
child: Text(t("No")),
|
|
onPressed: () => Navigator.pop(context),
|
|
),
|
|
FlatButton(
|
|
child: Text(t("Yes")),
|
|
onPressed: () => {
|
|
print("delete exercise: " + exercise.toJson().toString()),
|
|
bloc.add(ExerciseLogDelete(exercise: exercise)),
|
|
Navigator.pop(context)
|
|
},
|
|
)
|
|
],
|
|
)
|
|
);
|
|
}
|
|
|
|
}
|