Skip to content

Commit 5c44678

Browse files
committed
WIP: use new string id's for nutrition stuff so we can use powersync
1 parent 3daf2f1 commit 5c44678

22 files changed

+182
-301
lines changed

lib/helpers/consts.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ enum EXERCISE_IMAGE_ART_STYLE {
109109
}
110110

111111
/// Dummy ID for pseudo meals
112-
const PSEUDO_MEAL_ID = -1;
112+
const PSEUDO_MEAL_ID = 'deadbeef';
113113

114114
/// Colors used for muscles
115115
const COLOR_MAIN_MUSCLES = Colors.red;

lib/models/nutrition/log.dart

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818

1919
import 'package:json_annotation/json_annotation.dart';
20+
import 'package:powersync/powersync.dart';
2021
import 'package:powersync/sqlite3.dart' as sqlite;
2122
import 'package:wger/helpers/json.dart';
2223
import 'package:wger/models/nutrition/ingredient.dart';
@@ -25,20 +26,19 @@ import 'package:wger/models/nutrition/meal_item.dart';
2526
import 'package:wger/models/nutrition/nutritional_values.dart';
2627
import 'package:wger/models/schema.dart';
2728
import 'package:wger/powersync.dart';
28-
import 'package:wger/providers/nutrition.dart';
2929

3030
part 'log.g.dart';
3131

3232
@JsonSerializable()
3333
class Log {
3434
@JsonKey(required: true)
35-
int? id;
35+
String? id;
3636

3737
@JsonKey(required: false, name: 'meal')
38-
int? mealId;
38+
String? mealId;
3939

4040
@JsonKey(required: true, name: 'plan')
41-
int planId;
41+
String planId;
4242

4343
@JsonKey(required: true)
4444
late DateTime datetime;
@@ -81,7 +81,7 @@ class Log {
8181

8282
factory Log.fromRow(sqlite.Row row) {
8383
return Log(
84-
id: int.parse(row['id']),
84+
id: row['id'],
8585
mealId: row['meal_id'],
8686
ingredientId: row['ingredient_id'],
8787
weightUnitId: row['weight_unit_id'],
@@ -107,12 +107,12 @@ class Log {
107107
return ingredient.nutritionalValues / (100 / weight);
108108
}
109109

110-
static Future<List<Log>> readByMealId(int mealId) async {
110+
static Future<List<Log>> readByMealId(String mealId) async {
111111
final results = await db.getAll('SELECT * FROM $tableLogItems WHERE meal_id = ?', [mealId]);
112112
return results.map((r) => Log.fromRow(r)).toList();
113113
}
114114

115-
static Future<List<Log>> readByPlanId(int planId) async {
115+
static Future<List<Log>> readByPlanId(String planId) async {
116116
final results = await db.getAll('SELECT * FROM $tableLogItems WHERE plan_id = ?', [planId]);
117117
return results.map((r) => Log.fromRow(r)).toList();
118118
}
@@ -121,10 +121,23 @@ class Log {
121121
Future<void> delete() async {
122122
await db.execute('DELETE FROM $logItemsTable WHERE id = ?', [id]);
123123
}
124+
*/
124125

125-
static Future<void> addPhoto(String photoId, String id) async {
126-
await db.execute('UPDATE $logItemsTable SET photo_id = ? WHERE id = ?', [photoId, id]);
126+
Future<void> log() async {
127+
print('DIETER Log.log called id=$id, planId=$planId');
128+
await db.execute(
129+
'INSERT INTO $tableLogItems (id, meal_id, ingredient_id, weight_unit_id, amount, plan_id, datetime, comment) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
130+
[
131+
// generate an id using uuid
132+
uuid.v4(),
133+
mealId,
134+
ingredientId,
135+
weightUnitId,
136+
amount,
137+
planId,
138+
datetime.toIso8601String(),
139+
comment,
140+
],
141+
);
127142
}
128-
}
129-
*/
130143
}

lib/models/nutrition/log.g.dart

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/models/nutrition/meal.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ part 'meal.g.dart';
3333
@JsonSerializable()
3434
class Meal {
3535
@JsonKey(required: false)
36-
late int? id;
36+
late String? id;
3737

3838
@JsonKey(name: 'plan')
39-
late int planId;
39+
late String planId;
4040

4141
@JsonKey(toJson: timeToString, fromJson: stringToTime)
4242
TimeOfDay? time;
@@ -55,7 +55,7 @@ class Meal {
5555

5656
Meal({
5757
this.id,
58-
int? plan,
58+
String? plan,
5959
this.time,
6060
String? name,
6161
List<MealItem>? mealItems,
@@ -92,7 +92,7 @@ class Meal {
9292

9393
factory Meal.fromRow(sqlite.Row row) {
9494
return Meal(
95-
id: int.parse(row['id']),
95+
id: row['id'],
9696
plan: row['plan_id'],
9797
time: stringToTime(row['time']),
9898
name: row['name'],
@@ -102,8 +102,8 @@ class Meal {
102102
Map<String, dynamic> toJson() => _$MealToJson(this);
103103

104104
Meal copyWith({
105-
int? id,
106-
int? plan,
105+
String? id,
106+
String? plan,
107107
TimeOfDay? time,
108108
String? name,
109109
List<MealItem>? mealItems,
@@ -127,12 +127,12 @@ class Meal {
127127
);
128128
}
129129

130-
static Future<Meal> read(int id) async {
130+
static Future<Meal> read(String id) async {
131131
final results = await db.get('SELECT * FROM $tableMeals WHERE id = ?', [id]);
132132
return Meal.fromRow(results);
133133
}
134134

135-
static Future<List<Meal>> readByPlanId(int planId) async {
135+
static Future<List<Meal>> readByPlanId(String planId) async {
136136
print('Meal.readByPlanId: SELECT * FROM $tableMeals WHERE plan_id = $planId');
137137
final results = await db.getAll('SELECT * FROM $tableMeals WHERE plan_id = ?', [planId]);
138138
print(results.rows.length);

lib/models/nutrition/meal.g.dart

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/models/nutrition/meal_item.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class MealItem {
3434
int? id;
3535

3636
@JsonKey(required: false, name: 'meal')
37-
late int mealId;
37+
late String mealId;
3838

3939
@JsonKey(required: false, name: 'ingredient')
4040
late int ingredientId;
@@ -53,7 +53,7 @@ class MealItem {
5353

5454
MealItem({
5555
this.id,
56-
int? mealId,
56+
String? mealId,
5757
required this.ingredientId,
5858
this.weightUnitId,
5959
required this.amount,
@@ -107,7 +107,7 @@ class MealItem {
107107

108108
MealItem copyWith({
109109
int? id,
110-
int? mealId,
110+
String? mealId,
111111
int? ingredientId,
112112
int? weightUnitId,
113113
num? amount,
@@ -126,7 +126,7 @@ class MealItem {
126126
return m;
127127
}
128128

129-
static Future<List<MealItem>> readByMealId(int mealId) async {
129+
static Future<List<MealItem>> readByMealId(String mealId) async {
130130
final results = await db.getAll('SELECT * FROM $tableMealItems WHERE meal_id = ?', [mealId]);
131131
return results.map((r) => MealItem.fromRow(r)).toList();
132132
}

lib/models/nutrition/meal_item.g.dart

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/models/nutrition/nutritional_plan.dart

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ part 'nutritional_plan.g.dart';
3535

3636
@JsonSerializable(explicitToJson: true)
3737
class NutritionalPlan {
38-
@JsonKey(required: true)
39-
int? id;
38+
@JsonKey(required: false)
39+
String? id;
4040

4141
@JsonKey(required: true)
4242
late String description;
@@ -87,7 +87,7 @@ class NutritionalPlan {
8787

8888
factory NutritionalPlan.fromRow(sqlite.Row row) {
8989
return NutritionalPlan(
90-
id: int.parse(row['id']),
90+
id: row['id'],
9191
description: row['description'],
9292
creationDate: DateTime.parse(row['creation_date']),
9393
onlyLogging: row['only_logging'] == 1,
@@ -100,7 +100,7 @@ class NutritionalPlan {
100100
}
101101

102102
NutritionalPlan copyWith({
103-
int? id,
103+
String? id,
104104
String? description,
105105
DateTime? creationDate,
106106
bool? onlyLogging,
@@ -299,7 +299,7 @@ class NutritionalPlan {
299299
);
300300
}
301301

302-
static Future<NutritionalPlan> read(int id) async {
302+
static Future<NutritionalPlan> read(String id) async {
303303
final row = await db.get('SELECT * FROM $tableNutritionPlans WHERE id = ?', [id]);
304304
return NutritionalPlan.fromRow(row).loadChildren();
305305
}
@@ -327,18 +327,21 @@ class NutritionalPlan {
327327
});
328328
}
329329

330-
static Stream<NutritionalPlan?> watchNutritionPlan(int id) {
330+
static Stream<NutritionalPlan?> watchNutritionPlan(String id) {
331331
return db.onChange([tableNutritionPlans, tableLogItems, tableMeals]).asyncMap((event) async {
332332
final row = await db.getOptional('SELECT * FROM $tableNutritionPlans WHERE id = ?', [id]);
333333
return row == null ? null : NutritionalPlan.fromRow(row).loadChildren();
334334
});
335335
}
336336

337-
static Stream<NutritionalPlan> watchNutritionPlanLast() {
337+
static Stream<NutritionalPlan?> watchNutritionPlanLast() {
338338
return db.onChange([tableNutritionPlans, tableLogItems, tableMeals]).asyncMap((event) async {
339-
final row =
340-
await db.get('SELECT * FROM $tableNutritionPlans ORDER BY creation_date DESC LIMIT 1');
341-
return NutritionalPlan.fromRow(row).loadChildren();
339+
final res =
340+
await db.getAll('SELECT * FROM $tableNutritionPlans ORDER BY creation_date DESC LIMIT 1');
341+
if (res.isEmpty) {
342+
return null;
343+
}
344+
return NutritionalPlan.fromRow(res.first).loadChildren();
342345
});
343346
}
344347
/*

lib/models/nutrition/nutritional_plan.g.dart

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/models/schema.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Schema schema = const Schema([
3333
Column.text('description'),
3434
Column.integer('has_goal_calories'),
3535
Column.integer('user_id'),
36+
Column.integer('remote_id'),
3637
Column.integer('only_logging'),
3738
Column.integer('goal_carbohydrates'),
3839
Column.integer('goal_energy'),
@@ -47,10 +48,11 @@ Schema schema = const Schema([
4748
Column.text('datetime'),
4849
Column.text('comment'),
4950
Column.integer('amount'),
51+
Column.integer('remote_id'),
5052
Column.integer('ingredient_id'),
51-
Column.integer('plan_id'),
53+
Column.text('plan_id'),
5254
Column.integer('weight_unit_id'),
53-
Column.integer('meal_id'), // optional
55+
Column.text('meal_id'), // optional
5456
],
5557
indexes: [
5658
// Index('plan', [IndexedColumn('plan_id')])
@@ -60,8 +62,9 @@ Schema schema = const Schema([
6062
tableMeals,
6163
[
6264
Column.integer('order'),
65+
Column.integer('remote_id'),
6366
Column.text('time'),
64-
Column.integer('plan_id'),
67+
Column.text('plan_id'),
6568
Column.text('name'),
6669
],
6770
),
@@ -71,7 +74,8 @@ Schema schema = const Schema([
7174
Column.integer('order'),
7275
Column.integer('amount'),
7376
Column.integer('ingredient_id'),
74-
Column.integer('meal_id'),
77+
Column.text('meal_id'),
78+
Column.integer('remote_id'),
7579
Column.integer('weight_unit_id'),
7680
],
7781
),

0 commit comments

Comments
 (0)