Skip to content

Fix/app performance #444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/providers/body_weight.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class BodyWeightProvider with ChangeNotifier {
_entries = entries;
}

bool wightDataInit = false;

/// Clears all lists
void clear() {
_entries = [];
Expand Down
2 changes: 2 additions & 0 deletions lib/providers/measurement.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class MeasurementProvider with ChangeNotifier {
_categories = [];
}

bool measurementDataInit = false;

/// Finds the category by ID
MeasurementCategory findCategoryById(int id) {
return _categories.firstWhere(
Expand Down
2 changes: 2 additions & 0 deletions lib/providers/nutrition.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class NutritionPlansProvider with ChangeNotifier {
return [..._plans];
}

bool nutritionDataInit = false;

set ingredients(items) {
_ingredients = items;
}
Expand Down
2 changes: 2 additions & 0 deletions lib/providers/workout_plans.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ class WorkoutPlansProvider with ChangeNotifier {
notifyListeners();
}

bool workoutPlansDatainit = false;

/// Fetches a workout plan sparsely, i.e. only with the data on the plan
/// object itself and no child attributes
Future<WorkoutPlan> fetchAndSetPlanSparse(int planId) async {
Expand Down
64 changes: 32 additions & 32 deletions lib/screens/home_tabs_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,53 +74,53 @@ class _HomeTabsScreenState extends State<HomeTabsScreen> with SingleTickerProvid
final authProvider = context.read<AuthProvider>();

if (!authProvider.dataInit) {
final workoutPlansProvider = context.read<WorkoutPlansProvider>();
final nutritionPlansProvider = context.read<NutritionPlansProvider>();
final exercisesProvider = context.read<ExercisesProvider>();
final galleryProvider = context.read<GalleryProvider>();
final weightProvider = context.read<BodyWeightProvider>();
final measurementProvider = context.read<MeasurementProvider>();
final userProvider = context.read<UserProvider>();
// final workoutPlansProvider = context.read<WorkoutPlansProvider>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should ideally be removed if now unused.

// final nutritionPlansProvider = context.read<NutritionPlansProvider>();
// final exercisesProvider = context.read<ExercisesProvider>();
// final galleryProvider = context.read<GalleryProvider>();

// final userProvider = context.read<UserProvider>();

// Base data
log('Loading base data');
try {
await Future.wait([
authProvider.setServerVersion(),
userProvider.fetchAndSetProfile(),
workoutPlansProvider.fetchAndSetUnits(),
nutritionPlansProvider.fetchIngredientsFromCache(),
exercisesProvider.fetchAndSetExercises(),
context.read<UserProvider>().fetchAndSetProfile(),
context.read<WorkoutPlansProvider>().fetchAndSetUnits(),
context.read<NutritionPlansProvider>().fetchIngredientsFromCache(),
context.read<ExercisesProvider>().fetchAndSetExercises(),
context.read<GalleryProvider>().fetchAndSetGallery(),
]);
} catch (e) {
log('fire! fire!');
log(e.toString());
}

// Plans, weight and gallery
log('Loading plans, weight, measurements and gallery');
await Future.wait([
galleryProvider.fetchAndSetGallery(),
nutritionPlansProvider.fetchAndSetAllPlansSparse(),
workoutPlansProvider.fetchAndSetAllPlansSparse(),
weightProvider.fetchAndSetEntries(),
measurementProvider.fetchAndSetAllCategoriesAndEntries(),
]);
// log('Loading plans, weight, measurements and gallery');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove commented out code if not going to be used (see prev comment).

// await Future.wait([
// galleryProvider.fetchAndSetGallery(),
// nutritionPlansProvider.fetchAndSetAllPlansSparse(),
// workoutPlansProvider.fetchAndSetAllPlansSparse(),
// weightProvider.fetchAndSetEntries(),
// measurementProvider.fetchAndSetAllCategoriesAndEntries(),
// ]);

// Current nutritional plan
log('Loading current nutritional plan');
if (nutritionPlansProvider.currentPlan != null) {
final plan = nutritionPlansProvider.currentPlan!;
await nutritionPlansProvider.fetchAndSetPlanFull(plan.id!);
}

// Current workout plan
log('Loading current workout plan');
if (workoutPlansProvider.activePlan != null) {
final planId = workoutPlansProvider.activePlan!.id!;
await workoutPlansProvider.fetchAndSetWorkoutPlanFull(planId);
workoutPlansProvider.setCurrentPlan(planId);
}
// log('Loading current nutritional plan');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove (see prev comment)

// if (nutritionPlansProvider.currentPlan != null) {
// final plan = nutritionPlansProvider.currentPlan!;
// await nutritionPlansProvider.fetchAndSetPlanFull(plan.id!);
// }

// // Current workout plan
// log('Loading current workout plan');
// if (workoutPlansProvider.activePlan != null) {
// final planId = workoutPlansProvider.activePlan!.id!;
// await workoutPlansProvider.fetchAndSetWorkoutPlanFull(planId);
// workoutPlansProvider.setCurrentPlan(planId);
// }
}

authProvider.dataInit = true;
Expand Down
88 changes: 84 additions & 4 deletions lib/widgets/dashboard/widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import 'dart:developer';

import 'package:carousel_slider/carousel_slider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
Expand All @@ -24,6 +26,7 @@ import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
import 'package:wger/models/nutrition/nutritional_plan.dart';
import 'package:wger/models/workouts/workout_plan.dart';
import 'package:wger/providers/auth.dart';
import 'package:wger/providers/body_weight.dart';
import 'package:wger/providers/measurement.dart';
import 'package:wger/providers/nutrition.dart';
Expand Down Expand Up @@ -57,8 +60,24 @@ class _DashboardNutritionWidgetState extends State<DashboardNutritionWidget> {
@override
void initState() {
super.initState();
_plan = Provider.of<NutritionPlansProvider>(context, listen: false).currentPlan;
_hasContent = _plan != null;
_initNutritionData();
}

Future<void> _initNutritionData() async {
final nutritionPlansProvider = context.read<NutritionPlansProvider>();

if (!nutritionPlansProvider.nutritionDataInit) {
log('Loading Nutrition plans');
await Future.wait([
nutritionPlansProvider.fetchAndSetAllPlansSparse(),
]);
log('Loading current nutritional plan');
if (nutritionPlansProvider.currentPlan != null) {
final plan = nutritionPlansProvider.currentPlan!;
await nutritionPlansProvider.fetchAndSetPlanFull(plan.id!);
}
nutritionPlansProvider.nutritionDataInit = true;
}
}

List<Widget> getContent() {
Expand Down Expand Up @@ -156,6 +175,8 @@ class _DashboardNutritionWidgetState extends State<DashboardNutritionWidget> {

@override
Widget build(BuildContext context) {
_plan = Provider.of<NutritionPlansProvider>(context, listen: true).currentPlan;
_hasContent = _plan != null;
return Card(
child: Column(
children: [
Expand Down Expand Up @@ -239,6 +260,24 @@ class DashboardWeightWidget extends StatefulWidget {
class _DashboardWeightWidgetState extends State<DashboardWeightWidget> {
late BodyWeightProvider weightEntriesData;

@override
void initState() {
// TODO: implement initState
super.initState();
_initWeightData();
}

Future<void> _initWeightData() async {
final weightProvider = context.read<BodyWeightProvider>();
if (!weightProvider.wightDataInit) {
log('Loading weight');
await Future.wait([
weightProvider.fetchAndSetEntries(),
]);
weightProvider.wightDataInit = true;
}
}

@override
Widget build(BuildContext context) {
weightEntriesData = Provider.of<BodyWeightProvider>(context, listen: false);
Expand Down Expand Up @@ -318,6 +357,24 @@ class _DashboardMeasurementWidgetState extends State<DashboardMeasurementWidget>
int _current = 0;
final CarouselController _controller = CarouselController();

@override
void initState() {
// TODO: implement initState
super.initState();
_initMeasurementData();
}

Future<void> _initMeasurementData() async {
final measurementProvider = context.read<MeasurementProvider>();
if (!measurementProvider.measurementDataInit) {
log('Loading measurements');
await Future.wait([
measurementProvider.fetchAndSetAllCategoriesAndEntries(),
]);
measurementProvider.measurementDataInit = true;
}
}

@override
Widget build(BuildContext context) {
final provider = Provider.of<MeasurementProvider>(context, listen: false);
Expand Down Expand Up @@ -437,8 +494,29 @@ class _DashboardWorkoutWidgetState extends State<DashboardWorkoutWidget> {
@override
void initState() {
super.initState();
_workoutPlan = context.read<WorkoutPlansProvider>().activePlan;
_hasContent = _workoutPlan != null;
_initWorkoutPlansData();
}

Future<void> _initWorkoutPlansData() async {
final workoutPlansProvider = context.read<WorkoutPlansProvider>();

if (!workoutPlansProvider.workoutPlansDatainit) {
log('Loading Workout plans');
try {
await Future.wait([
workoutPlansProvider.fetchAndSetAllPlansSparse(),
]);
} catch (e, _) {
log(e.toString());
}
log('Loading current workout plan');
if (workoutPlansProvider.activePlan != null) {
final planId = workoutPlansProvider.activePlan!.id!;
await workoutPlansProvider.fetchAndSetWorkoutPlanFull(planId);
workoutPlansProvider.setCurrentPlan(planId);
}
workoutPlansProvider.workoutPlansDatainit = true;
}
}

Widget getTrailing() {
Expand Down Expand Up @@ -522,6 +600,8 @@ class _DashboardWorkoutWidgetState extends State<DashboardWorkoutWidget> {

@override
Widget build(BuildContext context) {
_workoutPlan = Provider.of<WorkoutPlansProvider>(context, listen: true).activePlan;
_hasContent = _workoutPlan != null;
return Card(
child: Column(
children: [
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/measurements/charts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class _MeasurementChartWidgetFlState extends State<MeasurementChartWidgetFl> {
color: Theme.of(context).colorScheme.secondary,
barWidth: 2,
isStrokeCapRound: true,
dotData: const FlDotData(
dotData: FlDotData(
show: true,
),
),
Expand Down
6 changes: 3 additions & 3 deletions lib/widgets/nutrition/charts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -253,17 +253,17 @@ class NutritionalDiaryChartWidgetFlState extends State<NutritionalDiaryChartWidg
getTitlesWidget: leftTitles,
),
),
topTitles: const AxisTitles(
topTitles: AxisTitles(
sideTitles: SideTitles(showTitles: false),
),
rightTitles: const AxisTitles(
rightTitles: AxisTitles(
sideTitles: SideTitles(showTitles: false),
),
),
gridData: FlGridData(
show: true,
checkToShowHorizontalLine: (value) => value % 10 == 0,
getDrawingHorizontalLine: (value) => const FlLine(
getDrawingHorizontalLine: (value) => FlLine(
color: Colors.black,
strokeWidth: 1,
),
Expand Down
Loading