Skip to content

Commit

Permalink
WIP: use new string id's for nutrition stuff so we can use powersync
Browse files Browse the repository at this point in the history
  • Loading branch information
Dieterbe committed Oct 31, 2024
1 parent 3daf2f1 commit a0986b1
Show file tree
Hide file tree
Showing 22 changed files with 168 additions and 292 deletions.
2 changes: 1 addition & 1 deletion lib/helpers/consts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ enum EXERCISE_IMAGE_ART_STYLE {
}

/// Dummy ID for pseudo meals
const PSEUDO_MEAL_ID = -1;
const PSEUDO_MEAL_ID = 'deadbeef';

/// Colors used for muscles
const COLOR_MAIN_MUSCLES = Colors.red;
Expand Down
34 changes: 24 additions & 10 deletions lib/models/nutrition/log.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

import 'package:json_annotation/json_annotation.dart';
import 'package:powersync/powersync.dart';
import 'package:powersync/sqlite3.dart' as sqlite;
import 'package:wger/helpers/json.dart';
import 'package:wger/models/nutrition/ingredient.dart';
Expand All @@ -32,13 +33,13 @@ part 'log.g.dart';
@JsonSerializable()
class Log {
@JsonKey(required: true)
int? id;
String? id;

@JsonKey(required: false, name: 'meal')
int? mealId;
String? mealId;

@JsonKey(required: true, name: 'plan')
int planId;
String planId;

@JsonKey(required: true)
late DateTime datetime;
Expand Down Expand Up @@ -81,7 +82,7 @@ class Log {

factory Log.fromRow(sqlite.Row row) {
return Log(
id: int.parse(row['id']),
id: row['id'],
mealId: row['meal_id'],
ingredientId: row['ingredient_id'],
weightUnitId: row['weight_unit_id'],
Expand All @@ -107,12 +108,12 @@ class Log {
return ingredient.nutritionalValues / (100 / weight);
}

static Future<List<Log>> readByMealId(int mealId) async {
static Future<List<Log>> readByMealId(String 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 {
static Future<List<Log>> readByPlanId(String planId) async {
final results = await db.getAll('SELECT * FROM $tableLogItems WHERE plan_id = ?', [planId]);
return results.map((r) => Log.fromRow(r)).toList();
}
Expand All @@ -121,10 +122,23 @@ class Log {
Future<void> delete() async {
await db.execute('DELETE FROM $logItemsTable WHERE id = ?', [id]);
}
*/

static Future<void> addPhoto(String photoId, String id) async {
await db.execute('UPDATE $logItemsTable SET photo_id = ? WHERE id = ?', [photoId, id]);
Future<void> log() async {
print('DIETER Log.log called id=$id, planId=$planId');
await db.execute(
'INSERT INTO $tableLogItems (id, meal_id, ingredient_id, weight_unit_id, amount, plan_id, datetime, comment) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
[
// generate an id using uuid
uuid.v4(),
mealId,
ingredientId,
weightUnitId,
amount,
planId,
datetime.toIso8601String(),
comment,
],
);
}
}
*/
}
6 changes: 3 additions & 3 deletions lib/models/nutrition/log.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions lib/models/nutrition/meal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ part 'meal.g.dart';
@JsonSerializable()
class Meal {
@JsonKey(required: false)
late int? id;
late String? id;

@JsonKey(name: 'plan')
late int planId;
late String planId;

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

Meal({
this.id,
int? plan,
String? plan,
this.time,
String? name,
List<MealItem>? mealItems,
Expand Down Expand Up @@ -92,7 +92,7 @@ class Meal {

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

Meal copyWith({
int? id,
int? plan,
String? id,
String? plan,
TimeOfDay? time,
String? name,
List<MealItem>? mealItems,
Expand All @@ -127,12 +127,12 @@ class Meal {
);
}

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

static Future<List<Meal>> readByPlanId(int planId) async {
static Future<List<Meal>> readByPlanId(String planId) async {
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);
Expand Down
4 changes: 2 additions & 2 deletions lib/models/nutrition/meal.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions lib/models/nutrition/meal_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class MealItem {
int? id;

@JsonKey(required: false, name: 'meal')
late int mealId;
late String mealId;

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

MealItem({
this.id,
int? mealId,
String? mealId,
required this.ingredientId,
this.weightUnitId,
required this.amount,
Expand Down Expand Up @@ -107,7 +107,7 @@ class MealItem {

MealItem copyWith({
int? id,
int? mealId,
String? mealId,
int? ingredientId,
int? weightUnitId,
num? amount,
Expand All @@ -126,7 +126,7 @@ class MealItem {
return m;
}

static Future<List<MealItem>> readByMealId(int mealId) async {
static Future<List<MealItem>> readByMealId(String mealId) async {
final results = await db.getAll('SELECT * FROM $tableMealItems WHERE meal_id = ?', [mealId]);
return results.map((r) => MealItem.fromRow(r)).toList();
}
Expand Down
2 changes: 1 addition & 1 deletion lib/models/nutrition/meal_item.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions lib/models/nutrition/nutritional_plan.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ part 'nutritional_plan.g.dart';

@JsonSerializable(explicitToJson: true)
class NutritionalPlan {
@JsonKey(required: true)
int? id;
@JsonKey(required: false)
String? id;

@JsonKey(required: true)
late String description;
Expand Down Expand Up @@ -87,7 +87,7 @@ class NutritionalPlan {

factory NutritionalPlan.fromRow(sqlite.Row row) {
return NutritionalPlan(
id: int.parse(row['id']),
id: row['id'],
description: row['description'],
creationDate: DateTime.parse(row['creation_date']),
onlyLogging: row['only_logging'] == 1,
Expand All @@ -100,7 +100,7 @@ class NutritionalPlan {
}

NutritionalPlan copyWith({
int? id,
String? id,
String? description,
DateTime? creationDate,
bool? onlyLogging,
Expand Down Expand Up @@ -299,7 +299,7 @@ class NutritionalPlan {
);
}

static Future<NutritionalPlan> read(int id) async {
static Future<NutritionalPlan> read(String id) async {
final row = await db.get('SELECT * FROM $tableNutritionPlans WHERE id = ?', [id]);
return NutritionalPlan.fromRow(row).loadChildren();
}
Expand Down Expand Up @@ -327,7 +327,7 @@ class NutritionalPlan {
});
}

static Stream<NutritionalPlan?> watchNutritionPlan(int id) {
static Stream<NutritionalPlan?> watchNutritionPlan(String id) {
return db.onChange([tableNutritionPlans, tableLogItems, tableMeals]).asyncMap((event) async {
final row = await db.getOptional('SELECT * FROM $tableNutritionPlans WHERE id = ?', [id]);
return row == null ? null : NutritionalPlan.fromRow(row).loadChildren();
Expand Down
3 changes: 1 addition & 2 deletions lib/models/nutrition/nutritional_plan.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions lib/models/schema.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Schema schema = const Schema([
Column.text('description'),
Column.integer('has_goal_calories'),
Column.integer('user_id'),
Column.integer('remote_id'),
Column.integer('only_logging'),
Column.integer('goal_carbohydrates'),
Column.integer('goal_energy'),
Expand All @@ -47,6 +48,7 @@ Schema schema = const Schema([
Column.text('datetime'),
Column.text('comment'),
Column.integer('amount'),
Column.integer('remote_id'),
Column.integer('ingredient_id'),
Column.integer('plan_id'),
Column.integer('weight_unit_id'),
Expand All @@ -60,6 +62,7 @@ Schema schema = const Schema([
tableMeals,
[
Column.integer('order'),
Column.integer('remote_id'),
Column.text('time'),
Column.integer('plan_id'),
Column.text('name'),
Expand All @@ -72,6 +75,7 @@ Schema schema = const Schema([
Column.integer('amount'),
Column.integer('ingredient_id'),
Column.integer('meal_id'),
Column.integer('remote_id'),
Column.integer('weight_unit_id'),
],
),
Expand Down
2 changes: 2 additions & 0 deletions lib/powersync.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class DjangoConnector extends PowerSyncBackendConnector {
'data': {'id': op.id, ...?op.opData},
};

log.fine('DIETER Uploading record', record);

switch (op.op) {
case UpdateType.put:
await apiClient.upsert(record);
Expand Down
Loading

0 comments on commit a0986b1

Please sign in to comment.