mirror of
https://github.com/wger-project/flutter.git
synced 2026-11-29 09:20:56 +01:00
149 lines
6.2 KiB
Dart
149 lines
6.2 KiB
Dart
/*
|
|
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
|
* Copyright (c) 2026 wger Team
|
|
*
|
|
* wger Workout Manager is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
import 'package:drift/drift.dart';
|
|
import 'package:powersync/powersync.dart' as ps;
|
|
import 'package:wger/database/converters/json_map_converter.dart';
|
|
import 'package:wger/database/converters/measurement_chart_type_converter.dart';
|
|
import 'package:wger/database/converters/measurement_metric_type_converter.dart';
|
|
import 'package:wger/database/converters/utc_datetime_converter.dart';
|
|
import 'package:wger/features/measurements/models/measurement_category.dart';
|
|
import 'package:wger/features/measurements/models/measurement_entry.dart';
|
|
|
|
@UseRowClass(MeasurementCategory, constructor: 'fromDb')
|
|
class MeasurementCategoryTable extends Table {
|
|
@override
|
|
String get tableName => 'measurements_category';
|
|
|
|
TextColumn get id => text().clientDefault(() => ps.uuid.v7())();
|
|
|
|
TextColumn get name => text()();
|
|
TextColumn get unit => text()();
|
|
// The new 2.7 columns (metricType, order, isOfficial, source) are nullable:
|
|
// rows synced before the schema change lack the keys in their local ps_data
|
|
// and read as NULL until the full re-sync replaces them. The `fromDb`
|
|
// constructors map NULL to the defaults so the UI keeps working meanwhile.
|
|
TextColumn get metricType =>
|
|
text().named('metric_type').map(const MeasurementMetricTypeConverter()).nullable()();
|
|
|
|
/// Chart the user picked, NULL for the one derived from the metric type.
|
|
TextColumn get chartType =>
|
|
text().named('chart_type').nullable().map(const MeasurementChartTypeConverter())();
|
|
|
|
/// Taste-level chart settings (server JSONField), e.g. the trend character.
|
|
/// A missing key, and a missing object, mean the client default.
|
|
TextColumn get chartConfig =>
|
|
text().named('chart_config').map(const JsonMapConverter()).nullable()();
|
|
|
|
/// Multi-value groups: parent category id (max. one level of nesting; only
|
|
/// leaf categories carry entries).
|
|
TextColumn get parentId =>
|
|
text().named('parent_id').nullable().references(MeasurementCategoryTable, #id)();
|
|
|
|
/// Position in the category list; for children, the position within the group
|
|
IntColumn get order => integer().withDefault(const Constant(0)).nullable()();
|
|
|
|
/// Server-managed official category (max. one per metric type and user).
|
|
/// Body weight entries live in the official `body_weight` category.
|
|
BoolColumn get isOfficial =>
|
|
boolean().named('is_official').withDefault(const Constant(false)).nullable()();
|
|
|
|
/// What the server calculates the entries of this category from, `NONE` for
|
|
/// one the user fills themselves. Kept as the raw string: a type added
|
|
/// after this release still has to read as calculated.
|
|
TextColumn get dynamicType =>
|
|
text().named('dynamic_type').withDefault(const Constant('NONE')).nullable()();
|
|
|
|
/// Configuration of the calculation (server JSONField), its keys depend on
|
|
/// [dynamicType].
|
|
TextColumn get dynamicParams =>
|
|
text().named('dynamic_params').map(const JsonMapConverter()).nullable()();
|
|
}
|
|
|
|
const PowersyncMeasurementCategoryTable = ps.Table(
|
|
'measurements_category',
|
|
[
|
|
ps.Column.text('name'),
|
|
ps.Column.text('unit'),
|
|
ps.Column.text('metric_type'),
|
|
ps.Column.text('chart_type'),
|
|
// JSON object, synced and stored as text
|
|
ps.Column.text('chart_config'),
|
|
ps.Column.text('parent_id'),
|
|
ps.Column.integer('order'),
|
|
// Postgres boolean; integer so the view yields 0/1, not the string '0'
|
|
ps.Column.integer('is_official'),
|
|
ps.Column.text('dynamic_type'),
|
|
// JSON object, synced and stored as text
|
|
ps.Column.text('dynamic_params'),
|
|
],
|
|
indexes: [
|
|
ps.Index('parent_idx', [ps.IndexedColumn('parent_id')]),
|
|
],
|
|
);
|
|
|
|
@UseRowClass(MeasurementEntry, constructor: 'fromDb')
|
|
class MeasurementEntryTable extends Table {
|
|
@override
|
|
String get tableName => 'measurements_measurement';
|
|
|
|
TextColumn get id => text().clientDefault(() => ps.uuid.v7())();
|
|
TextColumn get categoryId =>
|
|
text().named('category_id').references(MeasurementCategoryTable, #id)();
|
|
|
|
DateTimeColumn get date => dateTime().map(const UtcDateTimeConverter())();
|
|
RealColumn get value => real()();
|
|
TextColumn get notes => text()();
|
|
|
|
/// Where the reading came from; one of the server's `source` values:
|
|
/// `user` for manual entries or a health platform (`apple`, `google`).
|
|
TextColumn get source => text().withDefault(const Constant('user')).nullable()();
|
|
|
|
/// Platform record UUID, used to deduplicate re-imports. `null` for manual
|
|
/// entries.
|
|
TextColumn get externalId => text().named('external_id').nullable()();
|
|
|
|
/// Per-entry metadata (server JSONField). The `unit` key holds the unit the
|
|
/// value was entered in; absent, the category unit applies.
|
|
TextColumn get extraData => text().named('extra_data').map(const JsonMapConverter()).nullable()();
|
|
}
|
|
|
|
const PowersyncMeasurementEntryTable = ps.Table(
|
|
'measurements_measurement',
|
|
[
|
|
ps.Column.text('category_id'),
|
|
ps.Column.text('date'),
|
|
ps.Column.real('value'),
|
|
ps.Column.text('notes'),
|
|
ps.Column.text('source'),
|
|
ps.Column.text('external_id'),
|
|
// JSON object, synced and stored as text
|
|
ps.Column.text('extra_data'),
|
|
],
|
|
indexes: [
|
|
// Entries are read per category and bounded by date (the charts read a
|
|
// range, the lists sort by it). The date is stored as an ISO string, which
|
|
// sorts the same way the timestamps do, so a range is one index scan
|
|
ps.Index('category_idx', [
|
|
ps.IndexedColumn('category_id'),
|
|
ps.IndexedColumn('date'),
|
|
]),
|
|
ps.Index('external_id_idx', [ps.IndexedColumn('external_id')]),
|
|
],
|
|
);
|