refactor(diary): Diary, Meals, Portions and Models models

This commit is contained in:
2024-07-03 16:39:06 +03:00
parent d58e96d07a
commit 1b2b7646b6
4 changed files with 422 additions and 16 deletions

View File

@@ -1,24 +1,116 @@
/*import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'diary_model.freezed.dart';
part 'diary_model.g.dart';
@freezed
class DiaryModel with _$DiaryModel {
const factory DiaryModel({
required int id,
required String state,
String? image,
required String description,
String? author_name,
String? author_avatar_path,
@JsonKey(name: "youtube_url") required String youtubeUrl,
@JsonKey(name: "created_at") required DateTime createdAt,
@JsonKey(name: "updated_at") required DateTime updatedAt,
required String title,
}) = _DiaryModel;
const factory DiaryModel(
{required int id,
required DateTime date,
required DateTime wakeup,
required DateTime retire,
required int closed,
required String comment,
required List<Meals> meals}) = _DiaryModel;
factory DiaryModel.fromJson(Map<String, Object?> json) =>
_$DiaryModelFromJson(json);
factory DiaryModel.fromJson(Map<String, Object?> json) => _$DiaryModelFromJson(json);
}
class Meals {
int? id;
String? time;
int? type;
int? satiety;
int? mood;
List<Portions>? portions;
List<Photos>? photos;
Meals({this.id, this.time, this.type, this.satiety, this.mood, this.portions, this.photos});
Meals.fromJson(Map<String, dynamic> json) {
List<Portions> portions = [];
List<Photos> photos = [];
id = json['id'];
time = json['time'];
type = json['type'];
satiety = json['satiety'];
mood = json['mood'];
if (json['portions'] != null) {
json['portions'].forEach((v) {
portions.add(new Portions.fromJson(v));
});
}
if (json['photos'] != null) {
json['photos'].forEach((v) {
photos.add(new Photos.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['time'] = this.time;
data['type'] = this.type;
data['satiety'] = this.satiety;
data['mood'] = this.mood;
if (this.portions != null) {
data['portions'] = this.portions?.map((v) => v.toJson()).toList();
}
if (this.photos != null) {
data['photos'] = this.photos?.map((v) => v.toJson()).toList();
}
return data;
}
}
class Portions {
int? id;
String? productName;
int? quantity;
String? portion;
Portions({this.id, this.productName, this.quantity, this.portion});
Portions.fromJson(Map<String, dynamic> json) {
id = json['id'];
productName = json['product_name'];
quantity = json['quantity'];
portion = json['portion'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['product_name'] = this.productName;
data['quantity'] = this.quantity;
data['portion'] = this.portion;
return data;
}
}
class Photos {
int? id;
String? originalName;
String? path;
int? size;
Photos({this.id, this.originalName, this.path, this.size});
Photos.fromJson(Map<String, dynamic> json) {
id = json['id'];
originalName = json['original_name'];
path = json['path'];
size = json['size'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['original_name'] = this.originalName;
data['path'] = this.path;
data['size'] = this.size;
return data;
}
}
*/

View File

@@ -0,0 +1,282 @@
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark
part of 'diary_model.dart';
// **************************************************************************
// FreezedGenerator
// **************************************************************************
T _$identity<T>(T value) => value;
final _privateConstructorUsedError = UnsupportedError(
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models');
DiaryModel _$DiaryModelFromJson(Map<String, dynamic> json) {
return _DiaryModel.fromJson(json);
}
/// @nodoc
mixin _$DiaryModel {
int get id => throw _privateConstructorUsedError;
DateTime get date => throw _privateConstructorUsedError;
DateTime get wakeup => throw _privateConstructorUsedError;
DateTime get retire => throw _privateConstructorUsedError;
int get closed => throw _privateConstructorUsedError;
String get comment => throw _privateConstructorUsedError;
List<Meals> get meals => throw _privateConstructorUsedError;
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
@JsonKey(ignore: true)
$DiaryModelCopyWith<DiaryModel> get copyWith =>
throw _privateConstructorUsedError;
}
/// @nodoc
abstract class $DiaryModelCopyWith<$Res> {
factory $DiaryModelCopyWith(
DiaryModel value, $Res Function(DiaryModel) then) =
_$DiaryModelCopyWithImpl<$Res, DiaryModel>;
@useResult
$Res call(
{int id,
DateTime date,
DateTime wakeup,
DateTime retire,
int closed,
String comment,
List<Meals> meals});
}
/// @nodoc
class _$DiaryModelCopyWithImpl<$Res, $Val extends DiaryModel>
implements $DiaryModelCopyWith<$Res> {
_$DiaryModelCopyWithImpl(this._value, this._then);
// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;
@pragma('vm:prefer-inline')
@override
$Res call({
Object? id = null,
Object? date = null,
Object? wakeup = null,
Object? retire = null,
Object? closed = null,
Object? comment = null,
Object? meals = null,
}) {
return _then(_value.copyWith(
id: null == id
? _value.id
: id // ignore: cast_nullable_to_non_nullable
as int,
date: null == date
? _value.date
: date // ignore: cast_nullable_to_non_nullable
as DateTime,
wakeup: null == wakeup
? _value.wakeup
: wakeup // ignore: cast_nullable_to_non_nullable
as DateTime,
retire: null == retire
? _value.retire
: retire // ignore: cast_nullable_to_non_nullable
as DateTime,
closed: null == closed
? _value.closed
: closed // ignore: cast_nullable_to_non_nullable
as int,
comment: null == comment
? _value.comment
: comment // ignore: cast_nullable_to_non_nullable
as String,
meals: null == meals
? _value.meals
: meals // ignore: cast_nullable_to_non_nullable
as List<Meals>,
) as $Val);
}
}
/// @nodoc
abstract class _$$DiaryModelImplCopyWith<$Res>
implements $DiaryModelCopyWith<$Res> {
factory _$$DiaryModelImplCopyWith(
_$DiaryModelImpl value, $Res Function(_$DiaryModelImpl) then) =
__$$DiaryModelImplCopyWithImpl<$Res>;
@override
@useResult
$Res call(
{int id,
DateTime date,
DateTime wakeup,
DateTime retire,
int closed,
String comment,
List<Meals> meals});
}
/// @nodoc
class __$$DiaryModelImplCopyWithImpl<$Res>
extends _$DiaryModelCopyWithImpl<$Res, _$DiaryModelImpl>
implements _$$DiaryModelImplCopyWith<$Res> {
__$$DiaryModelImplCopyWithImpl(
_$DiaryModelImpl _value, $Res Function(_$DiaryModelImpl) _then)
: super(_value, _then);
@pragma('vm:prefer-inline')
@override
$Res call({
Object? id = null,
Object? date = null,
Object? wakeup = null,
Object? retire = null,
Object? closed = null,
Object? comment = null,
Object? meals = null,
}) {
return _then(_$DiaryModelImpl(
id: null == id
? _value.id
: id // ignore: cast_nullable_to_non_nullable
as int,
date: null == date
? _value.date
: date // ignore: cast_nullable_to_non_nullable
as DateTime,
wakeup: null == wakeup
? _value.wakeup
: wakeup // ignore: cast_nullable_to_non_nullable
as DateTime,
retire: null == retire
? _value.retire
: retire // ignore: cast_nullable_to_non_nullable
as DateTime,
closed: null == closed
? _value.closed
: closed // ignore: cast_nullable_to_non_nullable
as int,
comment: null == comment
? _value.comment
: comment // ignore: cast_nullable_to_non_nullable
as String,
meals: null == meals
? _value._meals
: meals // ignore: cast_nullable_to_non_nullable
as List<Meals>,
));
}
}
/// @nodoc
@JsonSerializable()
class _$DiaryModelImpl implements _DiaryModel {
const _$DiaryModelImpl(
{required this.id,
required this.date,
required this.wakeup,
required this.retire,
required this.closed,
required this.comment,
required final List<Meals> meals})
: _meals = meals;
factory _$DiaryModelImpl.fromJson(Map<String, dynamic> json) =>
_$$DiaryModelImplFromJson(json);
@override
final int id;
@override
final DateTime date;
@override
final DateTime wakeup;
@override
final DateTime retire;
@override
final int closed;
@override
final String comment;
final List<Meals> _meals;
@override
List<Meals> get meals {
if (_meals is EqualUnmodifiableListView) return _meals;
// ignore: implicit_dynamic_type
return EqualUnmodifiableListView(_meals);
}
@override
String toString() {
return 'DiaryModel(id: $id, date: $date, wakeup: $wakeup, retire: $retire, closed: $closed, comment: $comment, meals: $meals)';
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$DiaryModelImpl &&
(identical(other.id, id) || other.id == id) &&
(identical(other.date, date) || other.date == date) &&
(identical(other.wakeup, wakeup) || other.wakeup == wakeup) &&
(identical(other.retire, retire) || other.retire == retire) &&
(identical(other.closed, closed) || other.closed == closed) &&
(identical(other.comment, comment) || other.comment == comment) &&
const DeepCollectionEquality().equals(other._meals, _meals));
}
@JsonKey(ignore: true)
@override
int get hashCode => Object.hash(runtimeType, id, date, wakeup, retire, closed,
comment, const DeepCollectionEquality().hash(_meals));
@JsonKey(ignore: true)
@override
@pragma('vm:prefer-inline')
_$$DiaryModelImplCopyWith<_$DiaryModelImpl> get copyWith =>
__$$DiaryModelImplCopyWithImpl<_$DiaryModelImpl>(this, _$identity);
@override
Map<String, dynamic> toJson() {
return _$$DiaryModelImplToJson(
this,
);
}
}
abstract class _DiaryModel implements DiaryModel {
const factory _DiaryModel(
{required final int id,
required final DateTime date,
required final DateTime wakeup,
required final DateTime retire,
required final int closed,
required final String comment,
required final List<Meals> meals}) = _$DiaryModelImpl;
factory _DiaryModel.fromJson(Map<String, dynamic> json) =
_$DiaryModelImpl.fromJson;
@override
int get id;
@override
DateTime get date;
@override
DateTime get wakeup;
@override
DateTime get retire;
@override
int get closed;
@override
String get comment;
@override
List<Meals> get meals;
@override
@JsonKey(ignore: true)
_$$DiaryModelImplCopyWith<_$DiaryModelImpl> get copyWith =>
throw _privateConstructorUsedError;
}

View File

@@ -0,0 +1,31 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'diary_model.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
_$DiaryModelImpl _$$DiaryModelImplFromJson(Map<String, dynamic> json) =>
_$DiaryModelImpl(
id: (json['id'] as num).toInt(),
date: DateTime.parse(json['date'] as String),
wakeup: DateTime.parse(json['wakeup'] as String),
retire: DateTime.parse(json['retire'] as String),
closed: (json['closed'] as num).toInt(),
comment: json['comment'] as String,
meals: (json['meals'] as List<dynamic>)
.map((e) => Meals.fromJson(e as Map<String, dynamic>))
.toList(),
);
Map<String, dynamic> _$$DiaryModelImplToJson(_$DiaryModelImpl instance) =>
<String, dynamic>{
'id': instance.id,
'date': instance.date.toIso8601String(),
'wakeup': instance.wakeup.toIso8601String(),
'retire': instance.retire.toIso8601String(),
'closed': instance.closed,
'comment': instance.comment,
'meals': instance.meals,
};

View File

@@ -60,6 +60,7 @@ class DiaryEditBloc extends Bloc<DiaryEditEvent, DiaryEditState> {
final diary = await fetchDiaryData(token, diaryId);
print("Screen load success");
if (diary is Map<String, dynamic>) {
print(diary);
final String diaryDate = diary['date'] ?? DateTime.now().toString();
final chartValues = await fetchChartValues(token, diaryId, emit);
if (state is DiaryEditLoadedState) {