Skip to content

Commit a0986b1

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

22 files changed

+168
-292
lines changed

lib/helpers/consts.dart

+1-1
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

+24-10
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';
@@ -32,13 +33,13 @@ part 'log.g.dart';
3233
@JsonSerializable()
3334
class Log {
3435
@JsonKey(required: true)
35-
int? id;
36+
String? id;
3637

3738
@JsonKey(required: false, name: 'meal')
38-
int? mealId;
39+
String? mealId;
3940

4041
@JsonKey(required: true, name: 'plan')
41-
int planId;
42+
String planId;
4243

4344
@JsonKey(required: true)
4445
late DateTime datetime;
@@ -81,7 +82,7 @@ class Log {
8182

8283
factory Log.fromRow(sqlite.Row row) {
8384
return Log(
84-
id: int.parse(row['id']),
85+
id: row['id'],
8586
mealId: row['meal_id'],
8687
ingredientId: row['ingredient_id'],
8788
weightUnitId: row['weight_unit_id'],
@@ -107,12 +108,12 @@ class Log {
107108
return ingredient.nutritionalValues / (100 / weight);
108109
}
109110

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

115-
static Future<List<Log>> readByPlanId(int planId) async {
116+
static Future<List<Log>> readByPlanId(String planId) async {
116117
final results = await db.getAll('SELECT * FROM $tableLogItems WHERE plan_id = ?', [planId]);
117118
return results.map((r) => Log.fromRow(r)).toList();
118119
}
@@ -121,10 +122,23 @@ class Log {
121122
Future<void> delete() async {
122123
await db.execute('DELETE FROM $logItemsTable WHERE id = ?', [id]);
123124
}
125+
*/
124126

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

lib/models/nutrition/log.g.dart

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/models/nutrition/meal.dart

+8-8
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

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/models/nutrition/meal_item.dart

+4-4
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

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/models/nutrition/nutritional_plan.dart

+6-6
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,7 +327,7 @@ 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();

lib/models/nutrition/nutritional_plan.g.dart

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/models/schema.dart

+4
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,6 +48,7 @@ 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'),
5153
Column.integer('plan_id'),
5254
Column.integer('weight_unit_id'),
@@ -60,6 +62,7 @@ Schema schema = const Schema([
6062
tableMeals,
6163
[
6264
Column.integer('order'),
65+
Column.integer('remote_id'),
6366
Column.text('time'),
6467
Column.integer('plan_id'),
6568
Column.text('name'),
@@ -72,6 +75,7 @@ Schema schema = const Schema([
7275
Column.integer('amount'),
7376
Column.integer('ingredient_id'),
7477
Column.integer('meal_id'),
78+
Column.integer('remote_id'),
7579
Column.integer('weight_unit_id'),
7680
],
7781
),

lib/powersync.dart

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ class DjangoConnector extends PowerSyncBackendConnector {
6060
'data': {'id': op.id, ...?op.opData},
6161
};
6262

63+
log.fine('DIETER Uploading record', record);
64+
6365
switch (op.op) {
6466
case UpdateType.put:
6567
await apiClient.upsert(record);

0 commit comments

Comments
 (0)