From 1b2b7646b6744fe2687ecc30ce32d8f365f14816 Mon Sep 17 00:00:00 2001 From: maxxkim Date: Wed, 3 Jul 2024 16:39:06 +0300 Subject: [PATCH] refactor(diary): Diary, Meals, Portions and Models models --- lib/features/diary/models/diary_model.dart | 124 +++++++- .../diary/models/diary_model.freezed.dart | 282 ++++++++++++++++++ lib/features/diary/models/diary_model.g.dart | 31 ++ .../bloc/diary_edit_bloc.dart | 1 + 4 files changed, 422 insertions(+), 16 deletions(-) create mode 100644 lib/features/diary/models/diary_model.freezed.dart create mode 100644 lib/features/diary/models/diary_model.g.dart diff --git a/lib/features/diary/models/diary_model.dart b/lib/features/diary/models/diary_model.dart index 63aaefc4..84832f18 100644 --- a/lib/features/diary/models/diary_model.dart +++ b/lib/features/diary/models/diary_model.dart @@ -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}) = _DiaryModel; - factory DiaryModel.fromJson(Map json) => - _$DiaryModelFromJson(json); + factory DiaryModel.fromJson(Map json) => _$DiaryModelFromJson(json); +} + +class Meals { + int? id; + String? time; + int? type; + int? satiety; + int? mood; + List? portions; + List? photos; + + Meals({this.id, this.time, this.type, this.satiety, this.mood, this.portions, this.photos}); + + Meals.fromJson(Map json) { + List portions = []; + List 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 toJson() { + final Map data = new Map(); + 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 json) { + id = json['id']; + productName = json['product_name']; + quantity = json['quantity']; + portion = json['portion']; + } + + Map toJson() { + final Map data = new Map(); + 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 json) { + id = json['id']; + originalName = json['original_name']; + path = json['path']; + size = json['size']; + } + + Map toJson() { + final Map data = new Map(); + data['id'] = this.id; + data['original_name'] = this.originalName; + data['path'] = this.path; + data['size'] = this.size; + return data; + } } -*/ \ No newline at end of file diff --git a/lib/features/diary/models/diary_model.freezed.dart b/lib/features/diary/models/diary_model.freezed.dart new file mode 100644 index 00000000..5ede0101 --- /dev/null +++ b/lib/features/diary/models/diary_model.freezed.dart @@ -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 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 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 get meals => throw _privateConstructorUsedError; + + Map toJson() => throw _privateConstructorUsedError; + @JsonKey(ignore: true) + $DiaryModelCopyWith 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}); +} + +/// @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, + ) 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}); +} + +/// @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, + )); + } +} + +/// @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; + + factory _$DiaryModelImpl.fromJson(Map 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; + @override + List 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 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}) = _$DiaryModelImpl; + + factory _DiaryModel.fromJson(Map 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 get meals; + @override + @JsonKey(ignore: true) + _$$DiaryModelImplCopyWith<_$DiaryModelImpl> get copyWith => + throw _privateConstructorUsedError; +} diff --git a/lib/features/diary/models/diary_model.g.dart b/lib/features/diary/models/diary_model.g.dart new file mode 100644 index 00000000..cab25b61 --- /dev/null +++ b/lib/features/diary/models/diary_model.g.dart @@ -0,0 +1,31 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'diary_model.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +_$DiaryModelImpl _$$DiaryModelImplFromJson(Map 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) + .map((e) => Meals.fromJson(e as Map)) + .toList(), + ); + +Map _$$DiaryModelImplToJson(_$DiaryModelImpl instance) => + { + 'id': instance.id, + 'date': instance.date.toIso8601String(), + 'wakeup': instance.wakeup.toIso8601String(), + 'retire': instance.retire.toIso8601String(), + 'closed': instance.closed, + 'comment': instance.comment, + 'meals': instance.meals, + }; diff --git a/lib/presentation/diary/diary_edit_screen/bloc/diary_edit_bloc.dart b/lib/presentation/diary/diary_edit_screen/bloc/diary_edit_bloc.dart index 1e8af7fe..e93f2957 100644 --- a/lib/presentation/diary/diary_edit_screen/bloc/diary_edit_bloc.dart +++ b/lib/presentation/diary/diary_edit_screen/bloc/diary_edit_bloc.dart @@ -60,6 +60,7 @@ class DiaryEditBloc extends Bloc { final diary = await fetchDiaryData(token, diaryId); print("Screen load success"); if (diary is Map) { + print(diary); final String diaryDate = diary['date'] ?? DateTime.now().toString(); final chartValues = await fetchChartValues(token, diaryId, emit); if (state is DiaryEditLoadedState) {