mirror of
https://github.com/wger-project/flutter.git
synced 2026-02-18 00:17:48 +01:00
wip logitem
This commit is contained in:
@@ -17,11 +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/meal_item.dart';
|
||||
import 'package:wger/models/nutrition/nutritional_values.dart';
|
||||
import 'package:wger/models/schema.dart';
|
||||
import 'package:wger/powersync.dart';
|
||||
|
||||
part 'log.g.dart';
|
||||
|
||||
@@ -75,6 +78,19 @@ class Log {
|
||||
amount = mealItem.amount;
|
||||
}
|
||||
|
||||
factory Log.fromRow(sqlite.Row row) {
|
||||
return Log(
|
||||
id: row['id'],
|
||||
mealId: row['meal_id'],
|
||||
ingredientId: row['ingredient_id'],
|
||||
weightUnitId: row['weight_unit_id'],
|
||||
amount: row['amount'],
|
||||
planId: row['plan_id'],
|
||||
datetime: row['datetime'],
|
||||
comment: row['comment'],
|
||||
);
|
||||
}
|
||||
|
||||
// Boilerplate
|
||||
factory Log.fromJson(Map<String, dynamic> json) => _$LogFromJson(json);
|
||||
|
||||
@@ -89,4 +105,31 @@ class Log {
|
||||
|
||||
return ingredient.nutritionalValues / (100 / weight);
|
||||
}
|
||||
/*
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
||||
static Stream<List<TodoList>> watchLists() {
|
||||
// This query is automatically re-run when data in "lists" or "todos" is modified.
|
||||
return db.watch('SELECT * FROM lists ORDER BY created_at, id').map((results) {
|
||||
return results.map(TodoList.fromRow).toList(growable: false);
|
||||
});
|
||||
}
|
||||
|
||||
static Future<TodoList> create(String name) async {
|
||||
final results = await db.execute('''
|
||||
INSERT INTO
|
||||
lists(id, created_at, name, owner_id)
|
||||
VALUES(uuid(), datetime(), ?, ?)
|
||||
RETURNING *
|
||||
''', [name, await getUserId()]);
|
||||
return TodoList.fromRow(results.first);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
import 'package:powersync/sqlite3.dart' as sqlite;
|
||||
import 'package:wger/models/schema.dart';
|
||||
|
||||
import '../../powersync.dart';
|
||||
|
||||
/// TodoItem represents a result row of a query on "todos".
|
||||
///
|
||||
/// This class is immutable - methods on this class do not modify the instance
|
||||
/// directly. Instead, watch or re-query the data to get the updated item.
|
||||
/// confirm how the watch works. this seems like a weird pattern
|
||||
class TodoItem {
|
||||
final String id;
|
||||
final String description;
|
||||
final String? photoId;
|
||||
final bool completed;
|
||||
|
||||
TodoItem(
|
||||
{required this.id,
|
||||
required this.description,
|
||||
required this.completed,
|
||||
required this.photoId});
|
||||
|
||||
factory TodoItem.fromRow(sqlite.Row row) {
|
||||
return TodoItem(
|
||||
id: row['id'],
|
||||
description: row['description'],
|
||||
photoId: row['photo_id'],
|
||||
completed: row['completed'] == 1);
|
||||
}
|
||||
|
||||
Future<void> toggle() async {
|
||||
if (completed) {
|
||||
await db.execute(
|
||||
'UPDATE $logItemsTable SET completed = FALSE, completed_by = NULL, completed_at = NULL WHERE id = ?',
|
||||
[id]);
|
||||
} else {
|
||||
await db.execute(
|
||||
'UPDATE $logItemsTable SET completed = TRUE, completed_by = ?, completed_at = datetime() WHERE id = ?',
|
||||
[await getUserId(), id]);
|
||||
}
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
}
|
||||
/*
|
||||
static Stream<List<TodoList>> watchLists() {
|
||||
// This query is automatically re-run when data in "lists" or "todos" is modified.
|
||||
return db.watch('SELECT * FROM lists ORDER BY created_at, id').map((results) {
|
||||
return results.map(TodoList.fromRow).toList(growable: false);
|
||||
});
|
||||
}
|
||||
|
||||
static Future<TodoList> create(String name) async {
|
||||
final results = await db.execute('''
|
||||
INSERT INTO
|
||||
lists(id, created_at, name, owner_id)
|
||||
VALUES(uuid(), datetime(), ?, ?)
|
||||
RETURNING *
|
||||
''', [name, await getUserId()]);
|
||||
return TodoList.fromRow(results.first);
|
||||
}
|
||||
*/
|
||||
Reference in New Issue
Block a user