Remove the json serializable decorators, not needed anymore

This commit is contained in:
Roland Geider
2025-10-26 00:23:38 +02:00
parent d138e0f013
commit 2aa120602b
5 changed files with 36 additions and 150 deletions

View File

@@ -1,6 +1,6 @@
/* /*
* This file is part of wger Workout Manager <https://github.com/wger-project>. * This file is part of wger Workout Manager <https://github.com/wger-project>.
* Copyright (C) 2020, 2021 wger Team * Copyright (c) 2020, wger Team
* *
* wger Workout Manager is free software: you can redistribute it and/or modify * 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 * it under the terms of the GNU Affero General Public License as published by
@@ -16,22 +16,14 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
import 'package:drift/drift.dart' as drift; import 'package:drift/drift.dart';
import 'package:json_annotation/json_annotation.dart'; import 'package:wger/database/powersync/database.dart';
import 'package:powersync/sqlite3.dart' as sqlite;
import 'package:wger/helpers/json.dart';
part 'weight_entry.g.dart';
@JsonSerializable()
class WeightEntry { class WeightEntry {
@JsonKey(required: true)
String? id; String? id;
@JsonKey(required: true, fromJson: stringToNum, toJson: numToString)
late num weight = 0; late num weight = 0;
@JsonKey(required: true)
late DateTime date; late DateTime date;
WeightEntry({this.id, num? weight, DateTime? date}) { WeightEntry({this.id, num? weight, DateTime? date}) {
@@ -42,50 +34,17 @@ class WeightEntry {
} }
} }
factory WeightEntry.fromRow(sqlite.Row row) {
return WeightEntry(
id: row['uuid'],
date: DateTime.parse(row['date']),
weight: row['weight'],
);
}
WeightEntry copyWith({String? id, int? weight, DateTime? date}) => WeightEntry( WeightEntry copyWith({String? id, int? weight, DateTime? date}) => WeightEntry(
id: id, id: id,
weight: weight ?? this.weight, weight: weight ?? this.weight,
date: date ?? this.date, date: date ?? this.date,
); );
factory WeightEntry.fromData( WeightEntryTableCompanion toCompanion({bool includeId = false}) {
Map<String, dynamic> data, return WeightEntryTableCompanion(
drift.GeneratedDatabase db, { id: includeId && id != null ? Value(id!) : const Value.absent(),
String? prefix, date: Value(date),
}) { weight: Value(weight as double),
final effectivePrefix = prefix ?? '';
final rawId = data['${effectivePrefix}id'];
final rawWeight = data['${effectivePrefix}weight'];
final rawDate = data['${effectivePrefix}date'];
final parsedId = rawId as String?;
final parsedWeight = rawWeight is num
? rawWeight
: (rawWeight == null ? 0 : num.tryParse(rawWeight.toString()) ?? 0);
final parsedDate = rawDate is DateTime
? rawDate
: (rawDate == null
? DateTime.now()
: DateTime.tryParse(rawDate.toString()) ?? DateTime.now());
return WeightEntry(
id: parsedId,
weight: parsedWeight,
date: parsedDate,
); );
} }
// Boilerplate
factory WeightEntry.fromJson(Map<String, dynamic> json) => _$WeightEntryFromJson(json);
Map<String, dynamic> toJson() => _$WeightEntryToJson(this);
} }

View File

@@ -1,22 +0,0 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'weight_entry.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
WeightEntry _$WeightEntryFromJson(Map<String, dynamic> json) {
$checkKeys(json, requiredKeys: const ['id', 'weight', 'date']);
return WeightEntry(
id: json['id'] as String?,
weight: stringToNum(json['weight'] as String?),
date: json['date'] == null ? null : DateTime.parse(json['date'] as String),
);
}
Map<String, dynamic> _$WeightEntryToJson(WeightEntry instance) => <String, dynamic>{
'id': instance.id,
'weight': numToString(instance.weight),
'date': instance.date.toIso8601String(),
};

View File

@@ -1,3 +1,21 @@
/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* Copyright (c) 2020, 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/>.
*/
/* /*
* Repository for body weight network operations. * Repository for body weight network operations.
*/ */
@@ -35,23 +53,11 @@ class BodyWeightRepository {
Future<void> updateLocalDrift(WeightEntry entry) async { Future<void> updateLocalDrift(WeightEntry entry) async {
_logger.finer('Updating local weight entry ${entry.id}'); _logger.finer('Updating local weight entry ${entry.id}');
final stmt = _db.update(_db.weightEntryTable)..where((t) => t.id.equals(entry.id!)); final stmt = _db.update(_db.weightEntryTable)..where((t) => t.id.equals(entry.id!));
await stmt.write( await stmt.write(entry.toCompanion());
WeightEntryTableCompanion(
date: Value(entry.date),
weight: Value(entry.weight as double),
),
);
} }
Future<void> addLocalDrift(WeightEntry entry) async { Future<void> addLocalDrift(WeightEntry entry) async {
_logger.finer('Adding local weight entry ${entry.date}'); _logger.finer('Adding local weight entry ${entry.date}');
await _db await _db.into(_db.weightEntryTable).insert(entry.toCompanion());
.into(_db.weightEntryTable)
.insert(
WeightEntryTableCompanion.insert(
date: Value(entry.date),
weight: entry.weight as double,
),
);
} }
} }

View File

@@ -1,63 +0,0 @@
/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* Copyright (C) 2020, 2021 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.
*
* wger Workout Manager 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:flutter_test/flutter_test.dart';
import 'package:wger/models/body_weight/weight_entry.dart';
void main() {
group('fetchPost', () {
test('Test that the weight entries are correctly converted to json', () {
expect(
WeightEntry(id: '1', weight: 80, date: DateTime(2020, 12, 31, 12, 34)).toJson(),
{'id': 1, 'weight': '80', 'date': '2020-12-31T12:34:00.000'},
);
expect(
WeightEntry(id: '2', weight: 70.2, date: DateTime(2020, 12, 01)).toJson(),
{'id': 2, 'weight': '70.2', 'date': '2020-12-01T00:00:00.000'},
);
});
test('Test that the weight entries are correctly converted from json', () {
final WeightEntry weightEntryObj = WeightEntry(
id: '1',
weight: 80,
date: DateTime(2020, 12, 31),
);
final WeightEntry weightEntry = WeightEntry.fromJson({
'id': 1,
'weight': '80',
'date': '2020-12-31',
});
expect(weightEntry.id, weightEntryObj.id);
expect(weightEntry.weight, weightEntryObj.weight);
expect(weightEntry.date, weightEntryObj.date);
});
});
group('model', () {
test('Test the individual values from the model', () {
WeightEntry weightModel;
weightModel = WeightEntry(id: '1', weight: 80, date: DateTime(2020, 10, 01));
expect(weightModel.id, 1);
expect(weightModel.weight, 80);
expect(weightModel.date, DateTime(2020, 10, 01));
});
});
}

View File

@@ -1,13 +1,13 @@
/* /*
* This file is part of wger Workout Manager <https://github.com/wger-project>. * This file is part of wger Workout Manager <https://github.com/wger-project>.
* Copyright (C) 2020, 2021 wger Team * Copyright (c) 2020, wger Team
* *
* wger Workout Manager is free software: you can redistribute it and/or modify * 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 * 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 * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* wger Workout Manager is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details. * GNU Affero General Public License for more details.
@@ -55,6 +55,7 @@ final testBenchPress = Exercise(
uuid: '364f196c-881b-4839-8bfc-9e8f651521b6', uuid: '364f196c-881b-4839-8bfc-9e8f651521b6',
created: DateTime(2021, 09, 01), created: DateTime(2021, 09, 01),
lastUpdate: DateTime(2021, 09, 10), lastUpdate: DateTime(2021, 09, 10),
categoryId: 1,
category: tCategory1, category: tCategory1,
equipment: const [tEquipment1, tEquipment2], equipment: const [tEquipment1, tEquipment2],
muscles: const [tMuscle1, tMuscle2], muscles: const [tMuscle1, tMuscle2],
@@ -67,6 +68,7 @@ final testCrunches = Exercise(
uuid: '82415754-fc4c-49ea-8ca7-1516dd36d5a0', uuid: '82415754-fc4c-49ea-8ca7-1516dd36d5a0',
created: DateTime(2021, 08, 01), created: DateTime(2021, 08, 01),
lastUpdate: DateTime(2021, 08, 10), lastUpdate: DateTime(2021, 08, 10),
categoryId: 2,
category: tCategory2, category: tCategory2,
equipment: const [tEquipment2], equipment: const [tEquipment2],
muscles: const [tMuscle1], muscles: const [tMuscle1],
@@ -79,6 +81,7 @@ final testDeadLift = Exercise(
uuid: 'ca84e2c5-5608-4d6d-ba57-6d4b6b5e7acd', uuid: 'ca84e2c5-5608-4d6d-ba57-6d4b6b5e7acd',
created: DateTime(2021, 08, 01), created: DateTime(2021, 08, 01),
lastUpdate: DateTime(2021, 08, 01), lastUpdate: DateTime(2021, 08, 01),
categoryId: 3,
category: tCategory3, category: tCategory3,
equipment: const [tEquipment2], equipment: const [tEquipment2],
muscles: const [tMuscle1], muscles: const [tMuscle1],
@@ -91,6 +94,7 @@ final testCurls = Exercise(
uuid: '361f024c-fdf8-4146-b7d7-0c1b67c58141', uuid: '361f024c-fdf8-4146-b7d7-0c1b67c58141',
created: DateTime(2021, 08, 01), created: DateTime(2021, 08, 01),
lastUpdate: DateTime(2021, 08, 01), lastUpdate: DateTime(2021, 08, 01),
categoryId: 3,
category: tCategory3, category: tCategory3,
equipment: const [tEquipment2], equipment: const [tEquipment2],
muscles: const [tMuscle1], muscles: const [tMuscle1],
@@ -103,6 +107,7 @@ final testSquats = Exercise(
uuid: '361f024c-fdf8-4146-b7d7-0c1b67c58141', uuid: '361f024c-fdf8-4146-b7d7-0c1b67c58141',
created: DateTime(2021, 08, 01), created: DateTime(2021, 08, 01),
lastUpdate: DateTime(2021, 08, 01), lastUpdate: DateTime(2021, 08, 01),
categoryId: 3,
category: tCategory3, category: tCategory3,
equipment: const [tEquipment2], equipment: const [tEquipment2],
muscles: const [tMuscle1], muscles: const [tMuscle1],
@@ -115,6 +120,7 @@ final testSideRaises = Exercise(
uuid: '721ff972-c568-41e3-8cf5-cf1e5c5c801c', uuid: '721ff972-c568-41e3-8cf5-cf1e5c5c801c',
created: DateTime(2022, 11, 01), created: DateTime(2022, 11, 01),
lastUpdate: DateTime(2022, 11, 01), lastUpdate: DateTime(2022, 11, 01),
categoryId: 4,
category: tCategory4, category: tCategory4,
equipment: const [tEquipment2], equipment: const [tEquipment2],
muscles: const [tMuscle1], muscles: const [tMuscle1],