mirror of
https://github.com/wger-project/flutter.git
synced 2026-11-29 09:20:56 +01:00
progress reading data, using ivm tables
This commit is contained in:
@@ -107,24 +107,14 @@ class Log {
|
||||
return ingredient.nutritionalValues / (100 / weight);
|
||||
}
|
||||
|
||||
static Future<List<Log>> readByMealId(int mealId) async {
|
||||
final results = await db.getAll('SELECT * FROM $tableLogItems WHERE meal_id = ?', [mealId]);
|
||||
return results.map((r) => Log.fromRow(r)).toList();
|
||||
}
|
||||
|
||||
static Future<List<Log>> readByPlanId(int planId) async {
|
||||
final results = await db.getAll('SELECT * FROM $tableLogItems WHERE plan_id = ?', [planId]);
|
||||
return results.map((r) {
|
||||
final log = Log.fromRow(r);
|
||||
// TODO:
|
||||
// need to find a way to set ingredients. since we don't use powersync for it, we need to fetch
|
||||
// but this needs a context, therofere this needs a context, and all callers do, so we should probably
|
||||
// move all that stuff into the nutritionprovider, so we keep context out of the models
|
||||
// however, still unsolved:
|
||||
// mealItem stuff then?
|
||||
// nutrition image
|
||||
// nutrition_ingredientcategory
|
||||
// nutrition_ingredientweightunit
|
||||
// nutrition_weightunit;
|
||||
// nutrition_mealitem
|
||||
log.ingredient = Provider.of<NutritionPlansProvider>(context, listen: false).fetchIngredient(id),
|
||||
return log;
|
||||
}).toList();
|
||||
return results.map((r) => Log.fromRow(r)).toList();
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -93,7 +93,7 @@ class Meal {
|
||||
factory Meal.fromRow(sqlite.Row row) {
|
||||
return Meal(
|
||||
id: int.parse(row['id']),
|
||||
plan: row['plan'],
|
||||
plan: row['plan_id'],
|
||||
time: stringToTime(row['time']),
|
||||
name: row['name'],
|
||||
);
|
||||
@@ -103,7 +103,7 @@ class Meal {
|
||||
|
||||
Meal copyWith({
|
||||
int? id,
|
||||
int? planId,
|
||||
int? plan,
|
||||
TimeOfDay? time,
|
||||
String? name,
|
||||
List<MealItem>? mealItems,
|
||||
@@ -111,7 +111,7 @@ class Meal {
|
||||
}) {
|
||||
return Meal(
|
||||
id: id ?? this.id,
|
||||
plan: planId ?? this.planId,
|
||||
plan: plan ?? planId,
|
||||
time: time ?? this.time,
|
||||
name: name ?? this.name,
|
||||
mealItems: mealItems ?? this.mealItems,
|
||||
@@ -119,6 +119,14 @@ class Meal {
|
||||
);
|
||||
}
|
||||
|
||||
Future<Meal> loadChildren() async {
|
||||
print('loadChildren called. plan is $planId');
|
||||
return copyWith(
|
||||
mealItems: await MealItem.readByMealId(id!),
|
||||
diaryEntries: await Log.readByMealId(id!),
|
||||
);
|
||||
}
|
||||
|
||||
static Future<Meal> read(int id) async {
|
||||
final results = await db.get('SELECT * FROM $tableMeals WHERE id = ?', [id]);
|
||||
return Meal.fromRow(results);
|
||||
@@ -128,6 +136,6 @@ class Meal {
|
||||
print('Meal.readByPlanId: SELECT * FROM $tableMeals WHERE plan_id = $planId');
|
||||
final results = await db.getAll('SELECT * FROM $tableMeals WHERE plan_id = ?', [planId]);
|
||||
print(results.rows.length);
|
||||
return results.map((r) => Meal.fromRow(r)).toList();
|
||||
return Future.wait(results.map((r) => Meal.fromRow(r).loadChildren()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,10 +17,14 @@
|
||||
*/
|
||||
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:powersync/sqlite3.dart' as sqlite;
|
||||
|
||||
import 'package:wger/helpers/json.dart';
|
||||
import 'package:wger/models/nutrition/ingredient.dart';
|
||||
import 'package:wger/models/nutrition/ingredient_weight_unit.dart';
|
||||
import 'package:wger/models/nutrition/nutritional_values.dart';
|
||||
import 'package:wger/models/schema.dart';
|
||||
import 'package:wger/powersync.dart';
|
||||
|
||||
part 'meal_item.g.dart';
|
||||
|
||||
@@ -71,6 +75,15 @@ class MealItem {
|
||||
|
||||
Map<String, dynamic> toJson() => _$MealItemToJson(this);
|
||||
|
||||
factory MealItem.fromRow(sqlite.Row row) {
|
||||
return MealItem(
|
||||
amount: row['amount'],
|
||||
weightUnitId: row['weight_unit_id'],
|
||||
mealId: row['meal_id'],
|
||||
ingredientId: row['ingredient_id'],
|
||||
);
|
||||
}
|
||||
|
||||
/// Calculations
|
||||
/// TODO why does this not consider weightUnitObj ? should we do the same as Log.nutritionalValues here?
|
||||
NutritionalValues get nutritionalValues {
|
||||
@@ -112,4 +125,9 @@ class MealItem {
|
||||
m.weightUnitObj = weightUnitObj ?? this.weightUnitObj;
|
||||
return m;
|
||||
}
|
||||
|
||||
static Future<List<MealItem>> readByMealId(int mealId) async {
|
||||
final results = await db.getAll('SELECT * FROM $tableMealItems WHERE meal_id = ?', [mealId]);
|
||||
return results.map((r) => MealItem.fromRow(r)).toList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -327,10 +327,10 @@ class NutritionalPlan {
|
||||
});
|
||||
}
|
||||
|
||||
static Stream<NutritionalPlan> watchNutritionPlan(int id) {
|
||||
static Stream<NutritionalPlan?> watchNutritionPlan(int id) {
|
||||
return db.onChange([tableNutritionPlans, tableLogItems, tableMeals]).asyncMap((event) async {
|
||||
final row = await db.get('SELECT * FROM $tableNutritionPlans WHERE id = ?', [id]);
|
||||
return NutritionalPlan.fromRow(row).loadChildren();
|
||||
final row = await db.getOptional('SELECT * FROM $tableNutritionPlans WHERE id = ?', [id]);
|
||||
return row == null ? null : NutritionalPlan.fromRow(row).loadChildren();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -16,10 +16,10 @@ assumptions: nutrition_ingredientcategory, nutrition_weightunit, nutrition_ingre
|
||||
|
||||
// User,NutritionPlan,Meal,LogItem,MealItem,Ingredient
|
||||
const tableMuscles = 'exercises_muscle';
|
||||
const tableLogItems = 'nutrition_logitem';
|
||||
const tableLogItems = 'ivm_nutrition_logitem';
|
||||
const tableNutritionPlans = 'nutrition_nutritionplan';
|
||||
const tableMeals = 'nutrition_meal';
|
||||
const tableMealItems = 'nutrition_mealitem';
|
||||
const tableMeals = 'ivm_nutrition_meal';
|
||||
const tableMealItems = 'ivm_nutrition_mealitem';
|
||||
|
||||
Schema schema = const Schema([
|
||||
Table(
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:developer';
|
||||
|
||||
@@ -91,6 +92,66 @@ class NutritionPlansProvider with ChangeNotifier {
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<NutritionalPlan> _enrichPlan(NutritionalPlan plan) async {
|
||||
// TODO: set up ingredient images
|
||||
|
||||
final List<Log> diaryEntries = [];
|
||||
for (final diaryEntry in plan.diaryEntries) {
|
||||
diaryEntry.ingredient = await fetchIngredient(diaryEntry.ingredientId);
|
||||
diaryEntries.add(diaryEntry);
|
||||
}
|
||||
|
||||
final List<Meal> meals = [];
|
||||
for (final meal in plan.meals) {
|
||||
final List<MealItem> mealItems = [];
|
||||
for (final mealItem in meal.mealItems) {
|
||||
mealItem.ingredient = await fetchIngredient(mealItem.ingredientId);
|
||||
mealItems.add(mealItem);
|
||||
}
|
||||
meal.mealItems = mealItems;
|
||||
meal.diaryEntries = diaryEntries.where((d) => d.mealId == meal.id).toList();
|
||||
meals.add(meal);
|
||||
}
|
||||
|
||||
plan.meals = meals;
|
||||
plan.diaryEntries = diaryEntries;
|
||||
|
||||
return plan;
|
||||
}
|
||||
|
||||
Stream<NutritionalPlan?> watchNutritionPlan(int id) {
|
||||
return NutritionalPlan.watchNutritionPlan(id).transform(
|
||||
StreamTransformer.fromHandlers(
|
||||
handleData: (plan, sink) async {
|
||||
if (plan == null) {
|
||||
sink.add(plan);
|
||||
return;
|
||||
}
|
||||
sink.add(await _enrichPlan(plan));
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Stream<NutritionalPlan> watchNutritionPlanLast() {
|
||||
return NutritionalPlan.watchNutritionPlanLast().transform(
|
||||
StreamTransformer.fromHandlers(
|
||||
handleData: (plan, sink) async {
|
||||
sink.add(await _enrichPlan(plan));
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Stream<List<NutritionalPlan>> watchNutritionPlans() {
|
||||
return NutritionalPlan.watchNutritionPlans().transform(
|
||||
StreamTransformer.fromHandlers(
|
||||
handleData: (plans, sink) async {
|
||||
sink.add(await Future.wait(plans.map((plan) => _enrichPlan(plan))));
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
/*
|
||||
TODO implement:
|
||||
ingredient.image = image;
|
||||
|
||||
@@ -42,7 +42,6 @@ class _DashboardScreenState extends State<DashboardScreen> {
|
||||
padding: EdgeInsets.all(10),
|
||||
child: Column(
|
||||
children: [
|
||||
DashboardMuscleWidget(),
|
||||
DashboardWorkoutWidget(),
|
||||
DashboardNutritionWidget(),
|
||||
DashboardWeightWidget(),
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
@@ -26,7 +28,7 @@ import 'package:wger/widgets/nutrition/nutritional_diary_detail.dart';
|
||||
/// Arguments passed to the form screen
|
||||
class NutritionalDiaryArguments {
|
||||
/// Nutritional plan
|
||||
final NutritionalPlan plan;
|
||||
final int plan;
|
||||
|
||||
/// Date to show data for
|
||||
final DateTime date;
|
||||
@@ -34,23 +36,50 @@ class NutritionalDiaryArguments {
|
||||
const NutritionalDiaryArguments(this.plan, this.date);
|
||||
}
|
||||
|
||||
class NutritionalDiaryScreen extends StatelessWidget {
|
||||
class NutritionalDiaryScreen extends StatefulWidget {
|
||||
const NutritionalDiaryScreen();
|
||||
static const routeName = '/nutritional-diary';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final args = ModalRoute.of(context)!.settings.arguments as NutritionalDiaryArguments;
|
||||
State<NutritionalDiaryScreen> createState() => _NutritionalDiaryScreenState();
|
||||
}
|
||||
|
||||
class _NutritionalDiaryScreenState extends State<NutritionalDiaryScreen> {
|
||||
NutritionalPlan? _plan;
|
||||
late DateTime date;
|
||||
StreamSubscription? _subscription;
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
final args = ModalRoute.of(context)!.settings.arguments as NutritionalDiaryArguments;
|
||||
date = args.date;
|
||||
|
||||
final stream =
|
||||
Provider.of<NutritionPlansProvider>(context, listen: false).watchNutritionPlan(args.plan);
|
||||
_subscription = stream.listen((plan) {
|
||||
if (!context.mounted) {
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_plan = plan;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(DateFormat.yMd(Localizations.localeOf(context).languageCode).format(args.date)),
|
||||
title: Text(DateFormat.yMd(Localizations.localeOf(context).languageCode).format(date)),
|
||||
),
|
||||
body: Consumer<NutritionPlansProvider>(
|
||||
builder: (context, nutritionProvider, child) => SingleChildScrollView(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: NutritionalDiaryDetailWidget(args.plan, args.date),
|
||||
child: _plan == null
|
||||
? const Text('plan not found')
|
||||
: NutritionalDiaryDetailWidget(_plan!, date),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:flutter_svg_icons/flutter_svg_icons.dart';
|
||||
@@ -32,153 +34,189 @@ enum NutritionalPlanOptions {
|
||||
delete,
|
||||
}
|
||||
|
||||
class NutritionalPlanScreen extends StatelessWidget {
|
||||
class NutritionalPlanScreen extends StatefulWidget {
|
||||
const NutritionalPlanScreen();
|
||||
static const routeName = '/nutritional-plan-detail';
|
||||
|
||||
@override
|
||||
_NutritionalPlanScreenState createState() => _NutritionalPlanScreenState();
|
||||
}
|
||||
|
||||
class _NutritionalPlanScreenState extends State<NutritionalPlanScreen> {
|
||||
NutritionalPlan? _plan;
|
||||
StreamSubscription? _subscription;
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
final id = ModalRoute.of(context)!.settings.arguments as int;
|
||||
//final id = 111;
|
||||
|
||||
final stream =
|
||||
Provider.of<NutritionPlansProvider>(context, listen: false).watchNutritionPlan(id);
|
||||
_subscription = stream.listen((plan) {
|
||||
if (!context.mounted) {
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_plan = plan;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_subscription?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
const appBarForeground = Colors.white;
|
||||
final nutritionalPlan = ModalRoute.of(context)!.settings.arguments as NutritionalPlan;
|
||||
|
||||
return Scaffold(
|
||||
//appBar: getAppBar(nutritionalPlan),
|
||||
floatingActionButton: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
FloatingActionButton(
|
||||
heroTag: null,
|
||||
tooltip: AppLocalizations.of(context).logIngredient,
|
||||
onPressed: () {
|
||||
Navigator.pushNamed(
|
||||
context,
|
||||
FormScreen.routeName,
|
||||
arguments: FormScreenArguments(
|
||||
AppLocalizations.of(context).logIngredient,
|
||||
IngredientLogForm(nutritionalPlan),
|
||||
hasListView: true,
|
||||
),
|
||||
);
|
||||
},
|
||||
child: const SvgIcon(
|
||||
icon: SvgIconData('assets/icons/ingredient-diary.svg'),
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
FloatingActionButton(
|
||||
heroTag: null,
|
||||
tooltip: AppLocalizations.of(context).logMeal,
|
||||
onPressed: () {
|
||||
Navigator.of(context).pushNamed(
|
||||
LogMealsScreen.routeName,
|
||||
arguments: nutritionalPlan,
|
||||
);
|
||||
},
|
||||
child: const SvgIcon(
|
||||
icon: SvgIconData('assets/icons/meal-diary.svg'),
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: CustomScrollView(
|
||||
slivers: [
|
||||
SliverAppBar(
|
||||
foregroundColor: appBarForeground,
|
||||
pinned: true,
|
||||
iconTheme: const IconThemeData(color: appBarForeground),
|
||||
actions: [
|
||||
if (!nutritionalPlan.onlyLogging)
|
||||
IconButton(
|
||||
icon: const SvgIcon(
|
||||
icon: SvgIconData('assets/icons/meal-add.svg'),
|
||||
),
|
||||
floatingActionButton: _plan == null
|
||||
? const Offstage()
|
||||
: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.end,
|
||||
children: [
|
||||
FloatingActionButton(
|
||||
heroTag: null,
|
||||
tooltip: AppLocalizations.of(context).logIngredient,
|
||||
onPressed: () {
|
||||
Navigator.pushNamed(
|
||||
context,
|
||||
FormScreen.routeName,
|
||||
arguments: FormScreenArguments(
|
||||
AppLocalizations.of(context).addMeal,
|
||||
MealForm(nutritionalPlan.id!),
|
||||
AppLocalizations.of(context).logIngredient,
|
||||
IngredientLogForm(_plan!),
|
||||
hasListView: true,
|
||||
),
|
||||
);
|
||||
},
|
||||
child: const SvgIcon(
|
||||
icon: SvgIconData('assets/icons/ingredient-diary.svg'),
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
PopupMenuButton<NutritionalPlanOptions>(
|
||||
icon: const Icon(Icons.more_vert, color: appBarForeground),
|
||||
onSelected: (value) {
|
||||
switch (value) {
|
||||
case NutritionalPlanOptions.edit:
|
||||
Navigator.pushNamed(
|
||||
context,
|
||||
FormScreen.routeName,
|
||||
arguments: FormScreenArguments(
|
||||
AppLocalizations.of(context).edit,
|
||||
PlanForm(nutritionalPlan),
|
||||
hasListView: true,
|
||||
),
|
||||
);
|
||||
break;
|
||||
case NutritionalPlanOptions.delete:
|
||||
Provider.of<NutritionPlansProvider>(context, listen: false)
|
||||
.deletePlan(nutritionalPlan.id!);
|
||||
Navigator.of(context).pop();
|
||||
break;
|
||||
}
|
||||
},
|
||||
itemBuilder: (BuildContext context) {
|
||||
return [
|
||||
PopupMenuItem<NutritionalPlanOptions>(
|
||||
value: NutritionalPlanOptions.edit,
|
||||
child: ListTile(
|
||||
leading: const Icon(Icons.edit),
|
||||
title: Text(AppLocalizations.of(context).edit),
|
||||
),
|
||||
),
|
||||
const PopupMenuDivider(),
|
||||
PopupMenuItem<NutritionalPlanOptions>(
|
||||
value: NutritionalPlanOptions.delete,
|
||||
child: ListTile(
|
||||
leading: const Icon(Icons.delete),
|
||||
title: Text(AppLocalizations.of(context).delete),
|
||||
),
|
||||
),
|
||||
];
|
||||
},
|
||||
),
|
||||
],
|
||||
flexibleSpace: FlexibleSpaceBar(
|
||||
titlePadding: const EdgeInsets.fromLTRB(56, 0, 56, 16),
|
||||
title: Text(
|
||||
nutritionalPlan.getLabel(context),
|
||||
style: Theme.of(context).textTheme.titleLarge?.copyWith(color: appBarForeground),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
FloatingActionButton(
|
||||
heroTag: null,
|
||||
tooltip: AppLocalizations.of(context).logMeal,
|
||||
onPressed: () {
|
||||
Navigator.of(context).pushNamed(
|
||||
LogMealsScreen.routeName,
|
||||
arguments: _plan,
|
||||
);
|
||||
},
|
||||
child: const SvgIcon(
|
||||
icon: SvgIconData('assets/icons/meal-diary.svg'),
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
FutureBuilder(
|
||||
future: NutritionalPlan.read(nutritionalPlan.id!),
|
||||
builder: (context, AsyncSnapshot<NutritionalPlan> snapshot) =>
|
||||
snapshot.connectionState == ConnectionState.waiting
|
||||
? SliverList(
|
||||
delegate: SliverChildListDelegate(
|
||||
[
|
||||
const SizedBox(
|
||||
height: 200,
|
||||
child: Center(
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
),
|
||||
],
|
||||
body: _plan == null
|
||||
? const Text('plan not found')
|
||||
: CustomScrollView(
|
||||
slivers: [
|
||||
SliverAppBar(
|
||||
foregroundColor: appBarForeground,
|
||||
pinned: true,
|
||||
iconTheme: const IconThemeData(color: appBarForeground),
|
||||
actions: [
|
||||
if (!_plan!.onlyLogging)
|
||||
IconButton(
|
||||
icon: const SvgIcon(
|
||||
icon: SvgIconData('assets/icons/meal-add.svg'),
|
||||
),
|
||||
)
|
||||
: Consumer<NutritionPlansProvider>(
|
||||
builder: (context, value, child) =>
|
||||
NutritionalPlanDetailWidget(nutritionalPlan),
|
||||
onPressed: () {
|
||||
Navigator.pushNamed(
|
||||
context,
|
||||
FormScreen.routeName,
|
||||
arguments: FormScreenArguments(
|
||||
AppLocalizations.of(context).addMeal,
|
||||
MealForm(_plan!.id!),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
PopupMenuButton<NutritionalPlanOptions>(
|
||||
icon: const Icon(Icons.more_vert, color: appBarForeground),
|
||||
onSelected: (value) {
|
||||
switch (value) {
|
||||
case NutritionalPlanOptions.edit:
|
||||
Navigator.pushNamed(
|
||||
context,
|
||||
FormScreen.routeName,
|
||||
arguments: FormScreenArguments(
|
||||
AppLocalizations.of(context).edit,
|
||||
PlanForm(_plan),
|
||||
hasListView: true,
|
||||
),
|
||||
);
|
||||
break;
|
||||
case NutritionalPlanOptions.delete:
|
||||
Provider.of<NutritionPlansProvider>(context, listen: false)
|
||||
.deletePlan(_plan!.id!);
|
||||
Navigator.of(context).pop();
|
||||
break;
|
||||
}
|
||||
},
|
||||
itemBuilder: (BuildContext context) {
|
||||
return [
|
||||
PopupMenuItem<NutritionalPlanOptions>(
|
||||
value: NutritionalPlanOptions.edit,
|
||||
child: ListTile(
|
||||
leading: const Icon(Icons.edit),
|
||||
title: Text(AppLocalizations.of(context).edit),
|
||||
),
|
||||
),
|
||||
const PopupMenuDivider(),
|
||||
PopupMenuItem<NutritionalPlanOptions>(
|
||||
value: NutritionalPlanOptions.delete,
|
||||
child: ListTile(
|
||||
leading: const Icon(Icons.delete),
|
||||
title: Text(AppLocalizations.of(context).delete),
|
||||
),
|
||||
),
|
||||
];
|
||||
},
|
||||
),
|
||||
],
|
||||
flexibleSpace: FlexibleSpaceBar(
|
||||
titlePadding: const EdgeInsets.fromLTRB(56, 0, 56, 16),
|
||||
title: Text(
|
||||
_plan!.getLabel(context),
|
||||
style:
|
||||
Theme.of(context).textTheme.titleLarge?.copyWith(color: appBarForeground),
|
||||
),
|
||||
),
|
||||
),
|
||||
FutureBuilder(
|
||||
future: NutritionalPlan.read(_plan!.id!),
|
||||
builder: (context, AsyncSnapshot<NutritionalPlan> snapshot) =>
|
||||
snapshot.connectionState == ConnectionState.waiting
|
||||
? SliverList(
|
||||
delegate: SliverChildListDelegate(
|
||||
[
|
||||
const SizedBox(
|
||||
height: 200,
|
||||
child: Center(
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
: Consumer<NutritionPlansProvider>(
|
||||
builder: (context, value, child) =>
|
||||
NutritionalPlanDetailWidget(_plan!),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ class NutritionalPlansScreen extends StatelessWidget {
|
||||
},
|
||||
),
|
||||
body: Consumer<NutritionPlansProvider>(
|
||||
builder: (context, nutritionProvider, child) => NutritionalPlansList(nutritionProvider),
|
||||
builder: (context, nutritionProvider, child) => NutritionalPlansList(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -65,7 +65,8 @@ class _DashboardNutritionWidgetState extends State<DashboardNutritionWidget> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
final stream = NutritionalPlan.watchNutritionPlanLast();
|
||||
final stream =
|
||||
Provider.of<NutritionPlansProvider>(context, listen: false).watchNutritionPlanLast();
|
||||
_subscription = stream.listen((plan) {
|
||||
if (!context.mounted) {
|
||||
return;
|
||||
@@ -127,7 +128,7 @@ class _DashboardNutritionWidgetState extends State<DashboardNutritionWidget> {
|
||||
onPressed: () {
|
||||
Navigator.of(context).pushNamed(
|
||||
NutritionalPlanScreen.routeName,
|
||||
arguments: _plan,
|
||||
arguments: _plan!.id,
|
||||
);
|
||||
},
|
||||
),
|
||||
|
||||
@@ -672,7 +672,7 @@ class _PlanFormState extends State<PlanForm> {
|
||||
if (context.mounted) {
|
||||
Navigator.of(context).pushReplacementNamed(
|
||||
NutritionalPlanScreen.routeName,
|
||||
arguments: widget._plan,
|
||||
arguments: widget._plan.id,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,7 +117,7 @@ class NutritionalDiaryTable extends StatelessWidget {
|
||||
return GestureDetector(
|
||||
onTap: () => Navigator.of(context).pushNamed(
|
||||
NutritionalDiaryScreen.routeName,
|
||||
arguments: NutritionalDiaryArguments(plan, date),
|
||||
arguments: NutritionalDiaryArguments(plan.id!, date),
|
||||
),
|
||||
child: element,
|
||||
);
|
||||
|
||||
@@ -16,33 +16,64 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:wger/models/nutrition/nutritional_plan.dart';
|
||||
import 'package:wger/providers/nutrition.dart';
|
||||
import 'package:wger/screens/nutritional_plan_screen.dart';
|
||||
import 'package:wger/widgets/core/text_prompt.dart';
|
||||
|
||||
class NutritionalPlansList extends StatelessWidget {
|
||||
final NutritionPlansProvider _nutritionProvider;
|
||||
class NutritionalPlansList extends StatefulWidget {
|
||||
@override
|
||||
_NutritionalPlansListState createState() => _NutritionalPlansListState();
|
||||
}
|
||||
|
||||
const NutritionalPlansList(this._nutritionProvider);
|
||||
class _NutritionalPlansListState extends State<NutritionalPlansList> {
|
||||
List<NutritionalPlan> _plans = [];
|
||||
StreamSubscription? _subscription;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
final stream =
|
||||
Provider.of<NutritionPlansProvider>(context, listen: false).watchNutritionPlans();
|
||||
_subscription = stream.listen((plans) {
|
||||
if (!context.mounted) {
|
||||
return;
|
||||
}
|
||||
setState(() {
|
||||
_plans = plans;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_subscription?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return _nutritionProvider.items.isEmpty
|
||||
final provider = Provider.of<NutritionPlansProvider>(context, listen: false);
|
||||
|
||||
return _plans.isEmpty
|
||||
? const TextPrompt()
|
||||
: ListView.builder(
|
||||
padding: const EdgeInsets.all(10.0),
|
||||
itemCount: _nutritionProvider.items.length,
|
||||
itemCount: _plans.length,
|
||||
itemBuilder: (context, index) {
|
||||
final currentPlan = _nutritionProvider.items[index];
|
||||
final currentPlan = _plans[index];
|
||||
return Card(
|
||||
child: ListTile(
|
||||
onTap: () {
|
||||
Navigator.of(context).pushNamed(
|
||||
NutritionalPlanScreen.routeName,
|
||||
arguments: currentPlan,
|
||||
arguments: currentPlan.id,
|
||||
);
|
||||
},
|
||||
title: Text(currentPlan.getLabel(context)),
|
||||
@@ -57,7 +88,6 @@ class NutritionalPlansList extends StatelessWidget {
|
||||
icon: const Icon(Icons.delete),
|
||||
tooltip: AppLocalizations.of(context).delete,
|
||||
onPressed: () async {
|
||||
// Delete the plan from DB
|
||||
await showDialog(
|
||||
context: context,
|
||||
builder: (BuildContext contextDialog) {
|
||||
@@ -80,13 +110,8 @@ class NutritionalPlansList extends StatelessWidget {
|
||||
),
|
||||
),
|
||||
onPressed: () {
|
||||
// Confirmed, delete the plan
|
||||
_nutritionProvider.deletePlan(currentPlan.id!);
|
||||
|
||||
// Close the popup
|
||||
provider.deletePlan(currentPlan.id!);
|
||||
Navigator.of(contextDialog).pop();
|
||||
|
||||
// and inform the user
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
|
||||
@@ -103,7 +103,7 @@ void main() {
|
||||
group('fetchAndSetPlanFull', () {
|
||||
test('should correctly load a full nutritional plan', () async {
|
||||
// arrange
|
||||
await nutritionProvider.fetchAndSetPlanFull(1);
|
||||
// await nutritionProvider.fetchAndSetPlanFull(1);
|
||||
|
||||
// assert
|
||||
expect(nutritionProvider.items.isEmpty, false);
|
||||
|
||||
Reference in New Issue
Block a user