diff --git a/lib/features/diary/models/diary,dart b/lib/features/diary/models/diary,dart new file mode 100644 index 00000000..f7c59357 --- /dev/null +++ b/lib/features/diary/models/diary,dart @@ -0,0 +1,19 @@ +import 'package:freezed_annotation/freezed_annotation.dart'; + +part 'diary.freezed.dart'; +part 'diary.g.dart'; + +@freezed +class Diary with _$Diary { + const factory Diary({ + required int id, + required DateTime date, + required int closed, + String? comment, + DateTime? wakeup, + DateTime? retire, + }) = _Diary; + + factory Diary.fromJson(Map json) => + _$DiaryFromJson(json); +} diff --git a/lib/features/diary/models/diary_model.dart b/lib/features/diary/models/diary_model.dart index fe7d6a24..079d483f 100644 --- a/lib/features/diary/models/diary_model.dart +++ b/lib/features/diary/models/diary_model.dart @@ -1,127 +1,21 @@ import 'package:freezed_annotation/freezed_annotation.dart'; +import './meal.dart'; part 'diary_model.freezed.dart'; part 'diary_model.g.dart'; @freezed class DiaryModel with _$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; + const factory DiaryModel({ + required int id, + required DateTime date, + required int closed, + required List? meals, + String? comment, + DateTime? wakeup, + DateTime? retire, + }) = _DiaryModel; factory DiaryModel.fromJson(Map json) => _$DiaryModelFromJson(json); } - -class Meal { - late int id; - late String time; - late int type; - late int satiety; - late int mood; - List? portions; - List? photos; - - Meal( - {required this.id, - required this.time, - required this.type, - required this.satiety, - required this.mood, - this.portions, - this.photos}); - - Meal.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 Portion.fromJson(v)); - }); - } - if (json['photos'] != null) { - json['photos'].forEach((v) { - photos.add(new Photo.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 Portion { - late int id; - late String productName; - late int quantity; - late String portion; - - Portion( - {required this.id, - required this.productName, - required this.quantity, - required this.portion}); - - Portion.fromJson(v); - - 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 Photo { - late int id; - late String originalName; - late String path; - late int size; - - Photo( - {required this.id, - required this.originalName, - required this.path, - required this.size}); - - Photo.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; - } -} diff --git a/lib/features/diary/models/diary_model.freezed.dart b/lib/features/diary/models/diary_model.freezed.dart index 6705ecfe..2c612ec8 100644 --- a/lib/features/diary/models/diary_model.freezed.dart +++ b/lib/features/diary/models/diary_model.freezed.dart @@ -22,11 +22,11 @@ DiaryModel _$DiaryModelFromJson(Map json) { 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; + List? get meals => throw _privateConstructorUsedError; + String? get comment => throw _privateConstructorUsedError; + DateTime? get wakeup => throw _privateConstructorUsedError; + DateTime? get retire => throw _privateConstructorUsedError; Map toJson() => throw _privateConstructorUsedError; @JsonKey(ignore: true) @@ -43,11 +43,11 @@ abstract class $DiaryModelCopyWith<$Res> { $Res call( {int id, DateTime date, - DateTime wakeup, - DateTime retire, int closed, - String comment, - List meals}); + List? meals, + String? comment, + DateTime? wakeup, + DateTime? retire}); } /// @nodoc @@ -65,11 +65,11 @@ class _$DiaryModelCopyWithImpl<$Res, $Val extends DiaryModel> $Res call({ Object? id = null, Object? date = null, - Object? wakeup = null, - Object? retire = null, Object? closed = null, - Object? comment = null, - Object? meals = null, + Object? meals = freezed, + Object? comment = freezed, + Object? wakeup = freezed, + Object? retire = freezed, }) { return _then(_value.copyWith( id: null == id @@ -80,26 +80,26 @@ class _$DiaryModelCopyWithImpl<$Res, $Val extends DiaryModel> ? _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 + meals: freezed == meals ? _value.meals : meals // ignore: cast_nullable_to_non_nullable - as List, + as List?, + comment: freezed == comment + ? _value.comment + : comment // ignore: cast_nullable_to_non_nullable + as String?, + wakeup: freezed == wakeup + ? _value.wakeup + : wakeup // ignore: cast_nullable_to_non_nullable + as DateTime?, + retire: freezed == retire + ? _value.retire + : retire // ignore: cast_nullable_to_non_nullable + as DateTime?, ) as $Val); } } @@ -115,11 +115,11 @@ abstract class _$$DiaryModelImplCopyWith<$Res> $Res call( {int id, DateTime date, - DateTime wakeup, - DateTime retire, int closed, - String comment, - List meals}); + List? meals, + String? comment, + DateTime? wakeup, + DateTime? retire}); } /// @nodoc @@ -135,11 +135,11 @@ class __$$DiaryModelImplCopyWithImpl<$Res> $Res call({ Object? id = null, Object? date = null, - Object? wakeup = null, - Object? retire = null, Object? closed = null, - Object? comment = null, - Object? meals = null, + Object? meals = freezed, + Object? comment = freezed, + Object? wakeup = freezed, + Object? retire = freezed, }) { return _then(_$DiaryModelImpl( id: null == id @@ -150,26 +150,26 @@ class __$$DiaryModelImplCopyWithImpl<$Res> ? _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 + meals: freezed == meals ? _value._meals : meals // ignore: cast_nullable_to_non_nullable - as List, + as List?, + comment: freezed == comment + ? _value.comment + : comment // ignore: cast_nullable_to_non_nullable + as String?, + wakeup: freezed == wakeup + ? _value.wakeup + : wakeup // ignore: cast_nullable_to_non_nullable + as DateTime?, + retire: freezed == retire + ? _value.retire + : retire // ignore: cast_nullable_to_non_nullable + as DateTime?, )); } } @@ -180,11 +180,11 @@ 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}) + required final List? meals, + this.comment, + this.wakeup, + this.retire}) : _meals = meals; factory _$DiaryModelImpl.fromJson(Map json) => @@ -195,24 +195,27 @@ class _$DiaryModelImpl implements _DiaryModel { @override final DateTime date; @override - final DateTime wakeup; - @override - final DateTime retire; - @override final int closed; + final List? _meals; @override - final String comment; - final List _meals; - @override - List get meals { + List? get meals { + final value = _meals; + if (value == null) return null; if (_meals is EqualUnmodifiableListView) return _meals; // ignore: implicit_dynamic_type - return EqualUnmodifiableListView(_meals); + return EqualUnmodifiableListView(value); } + @override + final String? comment; + @override + final DateTime? wakeup; + @override + final DateTime? retire; + @override String toString() { - return 'DiaryModel(id: $id, date: $date, wakeup: $wakeup, retire: $retire, closed: $closed, comment: $comment, meals: $meals)'; + return 'DiaryModel(id: $id, date: $date, closed: $closed, meals: $meals, comment: $comment, wakeup: $wakeup, retire: $retire)'; } @override @@ -222,17 +225,17 @@ class _$DiaryModelImpl implements _DiaryModel { 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) && + const DeepCollectionEquality().equals(other._meals, _meals) && (identical(other.comment, comment) || other.comment == comment) && - const DeepCollectionEquality().equals(other._meals, _meals)); + (identical(other.wakeup, wakeup) || other.wakeup == wakeup) && + (identical(other.retire, retire) || other.retire == retire)); } @JsonKey(ignore: true) @override - int get hashCode => Object.hash(runtimeType, id, date, wakeup, retire, closed, - comment, const DeepCollectionEquality().hash(_meals)); + int get hashCode => Object.hash(runtimeType, id, date, closed, + const DeepCollectionEquality().hash(_meals), comment, wakeup, retire); @JsonKey(ignore: true) @override @@ -252,11 +255,11 @@ 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; + required final List? meals, + final String? comment, + final DateTime? wakeup, + final DateTime? retire}) = _$DiaryModelImpl; factory _DiaryModel.fromJson(Map json) = _$DiaryModelImpl.fromJson; @@ -266,15 +269,15 @@ abstract class _DiaryModel implements DiaryModel { @override DateTime get date; @override - DateTime get wakeup; - @override - DateTime get retire; - @override int get closed; @override - String get comment; + List? get meals; @override - List get meals; + String? get comment; + @override + DateTime? get wakeup; + @override + DateTime? get retire; @override @JsonKey(ignore: true) _$$DiaryModelImplCopyWith<_$DiaryModelImpl> get copyWith => diff --git a/lib/features/diary/models/diary_model.g.dart b/lib/features/diary/models/diary_model.g.dart index 9fabca81..fc9f7d0e 100644 --- a/lib/features/diary/models/diary_model.g.dart +++ b/lib/features/diary/models/diary_model.g.dart @@ -10,22 +10,26 @@ _$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) => Meal.fromJson(e as Map)) + meals: (json['meals'] as List?) + ?.map((e) => Meal.fromJson(e as Map)) .toList(), + comment: json['comment'] as String?, + wakeup: json['wakeup'] == null + ? null + : DateTime.parse(json['wakeup'] as String), + retire: json['retire'] == null + ? null + : DateTime.parse(json['retire'] as String), ); 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, + 'comment': instance.comment, + 'wakeup': instance.wakeup?.toIso8601String(), + 'retire': instance.retire?.toIso8601String(), }; diff --git a/lib/features/diary/models/meal.dart b/lib/features/diary/models/meal.dart new file mode 100644 index 00000000..a268a470 --- /dev/null +++ b/lib/features/diary/models/meal.dart @@ -0,0 +1,21 @@ +import 'package:freezed_annotation/freezed_annotation.dart'; +import './portion.dart'; +import './photo.dart'; + +part 'meal.freezed.dart'; +part 'meal.g.dart'; + +@freezed +class Meal with _$Meal { + const factory Meal({ + required int id, + required String time, + required int type, + required int satiety, + required int mood, + required List portions, + required List photos, + }) = _Meal; + + factory Meal.fromJson(Map json) => _$MealFromJson(json); +} diff --git a/lib/features/diary/models/meal.freezed.dart b/lib/features/diary/models/meal.freezed.dart new file mode 100644 index 00000000..2c249b06 --- /dev/null +++ b/lib/features/diary/models/meal.freezed.dart @@ -0,0 +1,291 @@ +// 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 'meal.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'); + +Meal _$MealFromJson(Map json) { + return _Meal.fromJson(json); +} + +/// @nodoc +mixin _$Meal { + int get id => throw _privateConstructorUsedError; + String get time => throw _privateConstructorUsedError; + int get type => throw _privateConstructorUsedError; + int get satiety => throw _privateConstructorUsedError; + int get mood => throw _privateConstructorUsedError; + List get portions => throw _privateConstructorUsedError; + List get photos => throw _privateConstructorUsedError; + + Map toJson() => throw _privateConstructorUsedError; + @JsonKey(ignore: true) + $MealCopyWith get copyWith => throw _privateConstructorUsedError; +} + +/// @nodoc +abstract class $MealCopyWith<$Res> { + factory $MealCopyWith(Meal value, $Res Function(Meal) then) = + _$MealCopyWithImpl<$Res, Meal>; + @useResult + $Res call( + {int id, + String time, + int type, + int satiety, + int mood, + List portions, + List photos}); +} + +/// @nodoc +class _$MealCopyWithImpl<$Res, $Val extends Meal> + implements $MealCopyWith<$Res> { + _$MealCopyWithImpl(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? time = null, + Object? type = null, + Object? satiety = null, + Object? mood = null, + Object? portions = null, + Object? photos = null, + }) { + return _then(_value.copyWith( + id: null == id + ? _value.id + : id // ignore: cast_nullable_to_non_nullable + as int, + time: null == time + ? _value.time + : time // ignore: cast_nullable_to_non_nullable + as String, + type: null == type + ? _value.type + : type // ignore: cast_nullable_to_non_nullable + as int, + satiety: null == satiety + ? _value.satiety + : satiety // ignore: cast_nullable_to_non_nullable + as int, + mood: null == mood + ? _value.mood + : mood // ignore: cast_nullable_to_non_nullable + as int, + portions: null == portions + ? _value.portions + : portions // ignore: cast_nullable_to_non_nullable + as List, + photos: null == photos + ? _value.photos + : photos // ignore: cast_nullable_to_non_nullable + as List, + ) as $Val); + } +} + +/// @nodoc +abstract class _$$MealImplCopyWith<$Res> implements $MealCopyWith<$Res> { + factory _$$MealImplCopyWith( + _$MealImpl value, $Res Function(_$MealImpl) then) = + __$$MealImplCopyWithImpl<$Res>; + @override + @useResult + $Res call( + {int id, + String time, + int type, + int satiety, + int mood, + List portions, + List photos}); +} + +/// @nodoc +class __$$MealImplCopyWithImpl<$Res> + extends _$MealCopyWithImpl<$Res, _$MealImpl> + implements _$$MealImplCopyWith<$Res> { + __$$MealImplCopyWithImpl(_$MealImpl _value, $Res Function(_$MealImpl) _then) + : super(_value, _then); + + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? id = null, + Object? time = null, + Object? type = null, + Object? satiety = null, + Object? mood = null, + Object? portions = null, + Object? photos = null, + }) { + return _then(_$MealImpl( + id: null == id + ? _value.id + : id // ignore: cast_nullable_to_non_nullable + as int, + time: null == time + ? _value.time + : time // ignore: cast_nullable_to_non_nullable + as String, + type: null == type + ? _value.type + : type // ignore: cast_nullable_to_non_nullable + as int, + satiety: null == satiety + ? _value.satiety + : satiety // ignore: cast_nullable_to_non_nullable + as int, + mood: null == mood + ? _value.mood + : mood // ignore: cast_nullable_to_non_nullable + as int, + portions: null == portions + ? _value._portions + : portions // ignore: cast_nullable_to_non_nullable + as List, + photos: null == photos + ? _value._photos + : photos // ignore: cast_nullable_to_non_nullable + as List, + )); + } +} + +/// @nodoc +@JsonSerializable() +class _$MealImpl implements _Meal { + const _$MealImpl( + {required this.id, + required this.time, + required this.type, + required this.satiety, + required this.mood, + required final List portions, + required final List photos}) + : _portions = portions, + _photos = photos; + + factory _$MealImpl.fromJson(Map json) => + _$$MealImplFromJson(json); + + @override + final int id; + @override + final String time; + @override + final int type; + @override + final int satiety; + @override + final int mood; + final List _portions; + @override + List get portions { + if (_portions is EqualUnmodifiableListView) return _portions; + // ignore: implicit_dynamic_type + return EqualUnmodifiableListView(_portions); + } + + final List _photos; + @override + List get photos { + if (_photos is EqualUnmodifiableListView) return _photos; + // ignore: implicit_dynamic_type + return EqualUnmodifiableListView(_photos); + } + + @override + String toString() { + return 'Meal(id: $id, time: $time, type: $type, satiety: $satiety, mood: $mood, portions: $portions, photos: $photos)'; + } + + @override + bool operator ==(Object other) { + return identical(this, other) || + (other.runtimeType == runtimeType && + other is _$MealImpl && + (identical(other.id, id) || other.id == id) && + (identical(other.time, time) || other.time == time) && + (identical(other.type, type) || other.type == type) && + (identical(other.satiety, satiety) || other.satiety == satiety) && + (identical(other.mood, mood) || other.mood == mood) && + const DeepCollectionEquality().equals(other._portions, _portions) && + const DeepCollectionEquality().equals(other._photos, _photos)); + } + + @JsonKey(ignore: true) + @override + int get hashCode => Object.hash( + runtimeType, + id, + time, + type, + satiety, + mood, + const DeepCollectionEquality().hash(_portions), + const DeepCollectionEquality().hash(_photos)); + + @JsonKey(ignore: true) + @override + @pragma('vm:prefer-inline') + _$$MealImplCopyWith<_$MealImpl> get copyWith => + __$$MealImplCopyWithImpl<_$MealImpl>(this, _$identity); + + @override + Map toJson() { + return _$$MealImplToJson( + this, + ); + } +} + +abstract class _Meal implements Meal { + const factory _Meal( + {required final int id, + required final String time, + required final int type, + required final int satiety, + required final int mood, + required final List portions, + required final List photos}) = _$MealImpl; + + factory _Meal.fromJson(Map json) = _$MealImpl.fromJson; + + @override + int get id; + @override + String get time; + @override + int get type; + @override + int get satiety; + @override + int get mood; + @override + List get portions; + @override + List get photos; + @override + @JsonKey(ignore: true) + _$$MealImplCopyWith<_$MealImpl> get copyWith => + throw _privateConstructorUsedError; +} diff --git a/lib/features/diary/models/meal.g.dart b/lib/features/diary/models/meal.g.dart new file mode 100644 index 00000000..5536ed92 --- /dev/null +++ b/lib/features/diary/models/meal.g.dart @@ -0,0 +1,32 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'meal.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +_$MealImpl _$$MealImplFromJson(Map json) => _$MealImpl( + id: (json['id'] as num).toInt(), + time: json['time'] as String, + type: (json['type'] as num).toInt(), + satiety: (json['satiety'] as num).toInt(), + mood: (json['mood'] as num).toInt(), + portions: (json['portions'] as List) + .map((e) => Portion.fromJson(e as Map)) + .toList(), + photos: (json['photos'] as List) + .map((e) => Photo.fromJson(e as Map)) + .toList(), + ); + +Map _$$MealImplToJson(_$MealImpl instance) => + { + 'id': instance.id, + 'time': instance.time, + 'type': instance.type, + 'satiety': instance.satiety, + 'mood': instance.mood, + 'portions': instance.portions, + 'photos': instance.photos, + }; diff --git a/lib/features/diary/models/photo.dart b/lib/features/diary/models/photo.dart new file mode 100644 index 00000000..f057ec50 --- /dev/null +++ b/lib/features/diary/models/photo.dart @@ -0,0 +1,16 @@ +import 'package:freezed_annotation/freezed_annotation.dart'; + +part 'photo.freezed.dart'; +part 'photo.g.dart'; + +@freezed +class Photo with _$Photo { + const factory Photo({ + required int id, + required String originalName, + required String path, + required int size, + }) = _Photo; + + factory Photo.fromJson(Map json) => _$PhotoFromJson(json); +} diff --git a/lib/features/diary/models/photo.freezed.dart b/lib/features/diary/models/photo.freezed.dart new file mode 100644 index 00000000..6b4520b8 --- /dev/null +++ b/lib/features/diary/models/photo.freezed.dart @@ -0,0 +1,204 @@ +// 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 'photo.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'); + +Photo _$PhotoFromJson(Map json) { + return _Photo.fromJson(json); +} + +/// @nodoc +mixin _$Photo { + int get id => throw _privateConstructorUsedError; + String get originalName => throw _privateConstructorUsedError; + String get path => throw _privateConstructorUsedError; + int get size => throw _privateConstructorUsedError; + + Map toJson() => throw _privateConstructorUsedError; + @JsonKey(ignore: true) + $PhotoCopyWith get copyWith => throw _privateConstructorUsedError; +} + +/// @nodoc +abstract class $PhotoCopyWith<$Res> { + factory $PhotoCopyWith(Photo value, $Res Function(Photo) then) = + _$PhotoCopyWithImpl<$Res, Photo>; + @useResult + $Res call({int id, String originalName, String path, int size}); +} + +/// @nodoc +class _$PhotoCopyWithImpl<$Res, $Val extends Photo> + implements $PhotoCopyWith<$Res> { + _$PhotoCopyWithImpl(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? originalName = null, + Object? path = null, + Object? size = null, + }) { + return _then(_value.copyWith( + id: null == id + ? _value.id + : id // ignore: cast_nullable_to_non_nullable + as int, + originalName: null == originalName + ? _value.originalName + : originalName // ignore: cast_nullable_to_non_nullable + as String, + path: null == path + ? _value.path + : path // ignore: cast_nullable_to_non_nullable + as String, + size: null == size + ? _value.size + : size // ignore: cast_nullable_to_non_nullable + as int, + ) as $Val); + } +} + +/// @nodoc +abstract class _$$PhotoImplCopyWith<$Res> implements $PhotoCopyWith<$Res> { + factory _$$PhotoImplCopyWith( + _$PhotoImpl value, $Res Function(_$PhotoImpl) then) = + __$$PhotoImplCopyWithImpl<$Res>; + @override + @useResult + $Res call({int id, String originalName, String path, int size}); +} + +/// @nodoc +class __$$PhotoImplCopyWithImpl<$Res> + extends _$PhotoCopyWithImpl<$Res, _$PhotoImpl> + implements _$$PhotoImplCopyWith<$Res> { + __$$PhotoImplCopyWithImpl( + _$PhotoImpl _value, $Res Function(_$PhotoImpl) _then) + : super(_value, _then); + + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? id = null, + Object? originalName = null, + Object? path = null, + Object? size = null, + }) { + return _then(_$PhotoImpl( + id: null == id + ? _value.id + : id // ignore: cast_nullable_to_non_nullable + as int, + originalName: null == originalName + ? _value.originalName + : originalName // ignore: cast_nullable_to_non_nullable + as String, + path: null == path + ? _value.path + : path // ignore: cast_nullable_to_non_nullable + as String, + size: null == size + ? _value.size + : size // ignore: cast_nullable_to_non_nullable + as int, + )); + } +} + +/// @nodoc +@JsonSerializable() +class _$PhotoImpl implements _Photo { + const _$PhotoImpl( + {required this.id, + required this.originalName, + required this.path, + required this.size}); + + factory _$PhotoImpl.fromJson(Map json) => + _$$PhotoImplFromJson(json); + + @override + final int id; + @override + final String originalName; + @override + final String path; + @override + final int size; + + @override + String toString() { + return 'Photo(id: $id, originalName: $originalName, path: $path, size: $size)'; + } + + @override + bool operator ==(Object other) { + return identical(this, other) || + (other.runtimeType == runtimeType && + other is _$PhotoImpl && + (identical(other.id, id) || other.id == id) && + (identical(other.originalName, originalName) || + other.originalName == originalName) && + (identical(other.path, path) || other.path == path) && + (identical(other.size, size) || other.size == size)); + } + + @JsonKey(ignore: true) + @override + int get hashCode => Object.hash(runtimeType, id, originalName, path, size); + + @JsonKey(ignore: true) + @override + @pragma('vm:prefer-inline') + _$$PhotoImplCopyWith<_$PhotoImpl> get copyWith => + __$$PhotoImplCopyWithImpl<_$PhotoImpl>(this, _$identity); + + @override + Map toJson() { + return _$$PhotoImplToJson( + this, + ); + } +} + +abstract class _Photo implements Photo { + const factory _Photo( + {required final int id, + required final String originalName, + required final String path, + required final int size}) = _$PhotoImpl; + + factory _Photo.fromJson(Map json) = _$PhotoImpl.fromJson; + + @override + int get id; + @override + String get originalName; + @override + String get path; + @override + int get size; + @override + @JsonKey(ignore: true) + _$$PhotoImplCopyWith<_$PhotoImpl> get copyWith => + throw _privateConstructorUsedError; +} diff --git a/lib/features/diary/models/photo.g.dart b/lib/features/diary/models/photo.g.dart new file mode 100644 index 00000000..9b212a83 --- /dev/null +++ b/lib/features/diary/models/photo.g.dart @@ -0,0 +1,22 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'photo.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +_$PhotoImpl _$$PhotoImplFromJson(Map json) => _$PhotoImpl( + id: (json['id'] as num).toInt(), + originalName: json['originalName'] as String, + path: json['path'] as String, + size: (json['size'] as num).toInt(), + ); + +Map _$$PhotoImplToJson(_$PhotoImpl instance) => + { + 'id': instance.id, + 'originalName': instance.originalName, + 'path': instance.path, + 'size': instance.size, + }; diff --git a/lib/features/diary/models/portion.dart b/lib/features/diary/models/portion.dart new file mode 100644 index 00000000..9712d9b3 --- /dev/null +++ b/lib/features/diary/models/portion.dart @@ -0,0 +1,17 @@ +import 'package:freezed_annotation/freezed_annotation.dart'; + +part 'portion.freezed.dart'; +part 'portion.g.dart'; + +@freezed +class Portion with _$Portion { + const factory Portion({ + required int id, + required String productName, + required int quantity, + required String portion, + }) = _Portion; + + factory Portion.fromJson(Map json) => + _$PortionFromJson(json); +} diff --git a/lib/features/diary/models/portion.freezed.dart b/lib/features/diary/models/portion.freezed.dart new file mode 100644 index 00000000..ab792133 --- /dev/null +++ b/lib/features/diary/models/portion.freezed.dart @@ -0,0 +1,206 @@ +// 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 'portion.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'); + +Portion _$PortionFromJson(Map json) { + return _Portion.fromJson(json); +} + +/// @nodoc +mixin _$Portion { + int get id => throw _privateConstructorUsedError; + String get productName => throw _privateConstructorUsedError; + int get quantity => throw _privateConstructorUsedError; + String get portion => throw _privateConstructorUsedError; + + Map toJson() => throw _privateConstructorUsedError; + @JsonKey(ignore: true) + $PortionCopyWith get copyWith => throw _privateConstructorUsedError; +} + +/// @nodoc +abstract class $PortionCopyWith<$Res> { + factory $PortionCopyWith(Portion value, $Res Function(Portion) then) = + _$PortionCopyWithImpl<$Res, Portion>; + @useResult + $Res call({int id, String productName, int quantity, String portion}); +} + +/// @nodoc +class _$PortionCopyWithImpl<$Res, $Val extends Portion> + implements $PortionCopyWith<$Res> { + _$PortionCopyWithImpl(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? productName = null, + Object? quantity = null, + Object? portion = null, + }) { + return _then(_value.copyWith( + id: null == id + ? _value.id + : id // ignore: cast_nullable_to_non_nullable + as int, + productName: null == productName + ? _value.productName + : productName // ignore: cast_nullable_to_non_nullable + as String, + quantity: null == quantity + ? _value.quantity + : quantity // ignore: cast_nullable_to_non_nullable + as int, + portion: null == portion + ? _value.portion + : portion // ignore: cast_nullable_to_non_nullable + as String, + ) as $Val); + } +} + +/// @nodoc +abstract class _$$PortionImplCopyWith<$Res> implements $PortionCopyWith<$Res> { + factory _$$PortionImplCopyWith( + _$PortionImpl value, $Res Function(_$PortionImpl) then) = + __$$PortionImplCopyWithImpl<$Res>; + @override + @useResult + $Res call({int id, String productName, int quantity, String portion}); +} + +/// @nodoc +class __$$PortionImplCopyWithImpl<$Res> + extends _$PortionCopyWithImpl<$Res, _$PortionImpl> + implements _$$PortionImplCopyWith<$Res> { + __$$PortionImplCopyWithImpl( + _$PortionImpl _value, $Res Function(_$PortionImpl) _then) + : super(_value, _then); + + @pragma('vm:prefer-inline') + @override + $Res call({ + Object? id = null, + Object? productName = null, + Object? quantity = null, + Object? portion = null, + }) { + return _then(_$PortionImpl( + id: null == id + ? _value.id + : id // ignore: cast_nullable_to_non_nullable + as int, + productName: null == productName + ? _value.productName + : productName // ignore: cast_nullable_to_non_nullable + as String, + quantity: null == quantity + ? _value.quantity + : quantity // ignore: cast_nullable_to_non_nullable + as int, + portion: null == portion + ? _value.portion + : portion // ignore: cast_nullable_to_non_nullable + as String, + )); + } +} + +/// @nodoc +@JsonSerializable() +class _$PortionImpl implements _Portion { + const _$PortionImpl( + {required this.id, + required this.productName, + required this.quantity, + required this.portion}); + + factory _$PortionImpl.fromJson(Map json) => + _$$PortionImplFromJson(json); + + @override + final int id; + @override + final String productName; + @override + final int quantity; + @override + final String portion; + + @override + String toString() { + return 'Portion(id: $id, productName: $productName, quantity: $quantity, portion: $portion)'; + } + + @override + bool operator ==(Object other) { + return identical(this, other) || + (other.runtimeType == runtimeType && + other is _$PortionImpl && + (identical(other.id, id) || other.id == id) && + (identical(other.productName, productName) || + other.productName == productName) && + (identical(other.quantity, quantity) || + other.quantity == quantity) && + (identical(other.portion, portion) || other.portion == portion)); + } + + @JsonKey(ignore: true) + @override + int get hashCode => + Object.hash(runtimeType, id, productName, quantity, portion); + + @JsonKey(ignore: true) + @override + @pragma('vm:prefer-inline') + _$$PortionImplCopyWith<_$PortionImpl> get copyWith => + __$$PortionImplCopyWithImpl<_$PortionImpl>(this, _$identity); + + @override + Map toJson() { + return _$$PortionImplToJson( + this, + ); + } +} + +abstract class _Portion implements Portion { + const factory _Portion( + {required final int id, + required final String productName, + required final int quantity, + required final String portion}) = _$PortionImpl; + + factory _Portion.fromJson(Map json) = _$PortionImpl.fromJson; + + @override + int get id; + @override + String get productName; + @override + int get quantity; + @override + String get portion; + @override + @JsonKey(ignore: true) + _$$PortionImplCopyWith<_$PortionImpl> get copyWith => + throw _privateConstructorUsedError; +} diff --git a/lib/features/diary/models/portion.g.dart b/lib/features/diary/models/portion.g.dart new file mode 100644 index 00000000..aae21620 --- /dev/null +++ b/lib/features/diary/models/portion.g.dart @@ -0,0 +1,23 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'portion.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +_$PortionImpl _$$PortionImplFromJson(Map json) => + _$PortionImpl( + id: (json['id'] as num).toInt(), + productName: json['productName'] as String, + quantity: (json['quantity'] as num).toInt(), + portion: json['portion'] as String, + ); + +Map _$$PortionImplToJson(_$PortionImpl instance) => + { + 'id': instance.id, + 'productName': instance.productName, + 'quantity': instance.quantity, + 'portion': instance.portion, + }; diff --git a/lib/features/diary/presentation/bloc/diary_bloc.dart b/lib/features/diary/presentation/bloc/diary_bloc.dart index efc7b268..f2d0b7f6 100644 --- a/lib/features/diary/presentation/bloc/diary_bloc.dart +++ b/lib/features/diary/presentation/bloc/diary_bloc.dart @@ -1,5 +1,5 @@ import 'dart:async'; -import 'package:eatvio_app/features/diary/models/diary_model.dart'; +import 'package:eatvio_app/features/diary/models/meal.dart'; import 'package:eatvio_app/features/diary/repositories/diary_repository.dart'; import 'package:intl/intl.dart'; import 'package:equatable/equatable.dart'; @@ -53,10 +53,10 @@ class DiaryBloc extends Bloc { ) async { emit(const DiaryState.loading()); try { - final diary = - await diaryRepository.getDiary(await diaryRepository.getDiaryID()); + Map data = await diaryRepository.getDiaryID(); + final int? diaryID = data['id']; emit(DiaryState.loaded( - diary: diary, + diary: await diaryRepository.getDiary(diaryID ?? 45), commentController: TextEditingController(), productNameController: TextEditingController(), productQuantityController: TextEditingController(), @@ -359,8 +359,8 @@ class DiaryBloc extends Bloc { ) async { if (state is DiaryStateLoaded) { var currentState = state as DiaryStateLoaded; - Meal? meal = - currentState.diary.meals.firstWhere((meal) => meal.id == event.value); + Meal? meal = currentState.diary.meals! + .firstWhere((meal) => meal.id == event.value); DiaryStateLoaded updatedState = currentState.copyWith( currentMeal: event.value, diff --git a/lib/features/diary/presentation/bloc/diary_state.dart b/lib/features/diary/presentation/bloc/diary_state.dart index d1c2c602..b9f26953 100644 --- a/lib/features/diary/presentation/bloc/diary_state.dart +++ b/lib/features/diary/presentation/bloc/diary_state.dart @@ -1,4 +1,7 @@ import 'package:eatvio_app/features/diary/models/diary_model.dart'; +import 'package:eatvio_app/features/diary/models/meal.dart'; +import 'package:eatvio_app/features/diary/models/photo.dart'; +import 'package:eatvio_app/features/diary/models/portion.dart'; import 'package:flutter/material.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; diff --git a/lib/features/diary/presentation/bloc/diary_state.freezed.dart b/lib/features/diary/presentation/bloc/diary_state.freezed.dart index d5d614a5..8a5aba44 100644 --- a/lib/features/diary/presentation/bloc/diary_state.freezed.dart +++ b/lib/features/diary/presentation/bloc/diary_state.freezed.dart @@ -592,6 +592,7 @@ abstract class _$$DiaryStateLoadedImplCopyWith<$Res> { bool isAdded}); $DiaryModelCopyWith<$Res> get diary; + $MealCopyWith<$Res>? get currentMeal; } /// @nodoc @@ -747,6 +748,18 @@ class __$$DiaryStateLoadedImplCopyWithImpl<$Res> return _then(_value.copyWith(diary: value)); }); } + + @override + @pragma('vm:prefer-inline') + $MealCopyWith<$Res>? get currentMeal { + if (_value.currentMeal == null) { + return null; + } + + return $MealCopyWith<$Res>(_value.currentMeal!, (value) { + return _then(_value.copyWith(currentMeal: value)); + }); + } } /// @nodoc diff --git a/lib/features/diary/presentation/pages/diary_screen.dart b/lib/features/diary/presentation/pages/diary_screen.dart index da609814..7f46d092 100644 --- a/lib/features/diary/presentation/pages/diary_screen.dart +++ b/lib/features/diary/presentation/pages/diary_screen.dart @@ -1,6 +1,9 @@ // ignore_for_file: unused_local_variable import 'package:eatvio_app/features/diary/models/diary_model.dart'; +import 'package:eatvio_app/features/diary/models/meal.dart'; +import 'package:eatvio_app/features/diary/models/photo.dart'; +import 'package:eatvio_app/features/diary/models/portion.dart'; import 'package:eatvio_app/features/diary/presentation/bloc/diary_bloc.dart'; import 'package:eatvio_app/features/diary/presentation/bloc/diary_state.dart'; import 'package:eatvio_app/locator_service.dart'; @@ -674,12 +677,12 @@ class DiaryScreen extends StatelessWidget { } _buildCarousel(BuildContext context, DiaryStateLoaded state) { - if (state.currentMeal!.photos!.isEmpty) { + if (state.currentMeal!.photos.isEmpty) { return Text("Фото нет"); } else { return CarouselSlider( options: CarouselOptions(height: 340.0), - items: state.currentMeal!.photos!.map((photo) { + items: state.currentMeal!.photos.map((photo) { return _buildPhoto( context, photo); // Assuming photos are stored as strings }).toList(), @@ -1311,8 +1314,8 @@ bool checkConsumedValues( return false; // Возвращаем true } -List mealsToDropdown(List meals) { - return meals +List mealsToDropdown(List? meals) { + return meals! .map((meal) => SelectionPopupModel( id: meal.id, title: "Прием пищи ${meal.id}", diff --git a/lib/features/diary/repositories/diary_repository.dart b/lib/features/diary/repositories/diary_repository.dart index 440f4195..2cadfd8d 100644 --- a/lib/features/diary/repositories/diary_repository.dart +++ b/lib/features/diary/repositories/diary_repository.dart @@ -6,7 +6,7 @@ class DiaryRepository { DiaryRepository({required this.diaryService}); - Future getDiaryID() async { + Future> getDiaryID() async { try { return await diaryService.getDiaryID(); } catch (e) { diff --git a/lib/features/diary/services/diary_service.dart b/lib/features/diary/services/diary_service.dart index dd27f0ba..967c2af3 100644 --- a/lib/features/diary/services/diary_service.dart +++ b/lib/features/diary/services/diary_service.dart @@ -10,7 +10,7 @@ abstract class DiaryService { factory DiaryService(Dio dio, {String baseUrl}) = _DiaryService; @GET('/diary/index') - Future getDiaryID(); + Future> getDiaryID(); @GET('/diary/{id}/show/') Future getDiary(@Path() int id); diff --git a/lib/features/diary/services/diary_service.g.dart b/lib/features/diary/services/diary_service.g.dart index 294e4e3d..62ca8f67 100644 --- a/lib/features/diary/services/diary_service.g.dart +++ b/lib/features/diary/services/diary_service.g.dart @@ -19,28 +19,29 @@ class _DiaryService implements DiaryService { String? baseUrl; @override - Future getDiaryID() async { + Future> getDiaryID() async { final _extra = {}; final queryParameters = {}; final _headers = {}; const Map? _data = null; - final _result = await _dio.fetch(_setStreamType(Options( + final _result = await _dio + .fetch>(_setStreamType>(Options( method: 'GET', headers: _headers, extra: _extra, ) - .compose( - _dio.options, - '/diary/index', - queryParameters: queryParameters, - data: _data, - ) - .copyWith( - baseUrl: _combineBaseUrls( - _dio.options.baseUrl, - baseUrl, - )))); - final _value = _result.data!; + .compose( + _dio.options, + '/diary/index', + queryParameters: queryParameters, + data: _data, + ) + .copyWith( + baseUrl: _combineBaseUrls( + _dio.options.baseUrl, + baseUrl, + )))); + final _value = _result.data!.cast(); return _value; } diff --git a/test/diary_test.dart b/test/diary_test.dart deleted file mode 100644 index d5063476..00000000 --- a/test/diary_test.dart +++ /dev/null @@ -1,72 +0,0 @@ -/*import 'package:flutter/material.dart'; -import 'package:flutter_bloc/flutter_bloc.dart'; -import 'package:flutter_test/flutter_test.dart'; -import 'package:eatvio_app/presentation/diary/diary_edit_screen/diary_edit_screen.dart'; -import 'package:eatvio_app/features/diary/presentation/bloc/diary_bloc.dart'; -import 'package:eatvio_app/presentation/diary/diary_edit_screen/models/diary_edit_model.dart'; - -void main() { - group('DiaryEditScreen', () { - /*testWidgets('renders CircularProgressIndicator when in initial state', - (WidgetTester tester) async { - await tester.pumpWidget( - MaterialApp( - home: DiaryEditScreen(), - ), - ); - });*/ - - testWidgets('renders _buildDiaryEditScreen when in loaded state', (WidgetTester tester) async { - final diaryEditBloc = DiaryEditBloc(DiaryEditState(diaryEditModelObj: DiaryEditModel())); - final loadedState = DiaryEditLoadedState( - diaryId: 1, - selectedSatiety: 4, - autocompleteList: [], - photos: [], - portions: [], - wakeupExpanded: false, - moderationExpanded: false, - recommendedChartValues: [10, 10, 10, 10], - consumedChartValues: [1, 2, 3, 4], - moodExpanded: false,); - - await tester.pumpWidget( - MaterialApp( - home: BlocProvider.value( - value: diaryEditBloc, - child: DiaryEditScreen(), - ), - ), - ); - - diaryEditBloc.emit(loadedState); - await tester.pump(); - - //expect(find.byType(_buildDiaryEditScreen), findsOneWidget); - }); - - /*testWidgets('renders error message when in error state', - (WidgetTester tester) async { - final diaryEditBloc = - DiaryEditBloc(DiaryEditState(diaryEditModelObj: DiaryEditModel())); - final errorState = DiaryEditErrorState("error"); - - await tester.pumpWidget( - MaterialApp( - home: BlocProvider.value( - value: diaryEditBloc, - child: DiaryEditScreen(), - ), - ), - ); - - diaryEditBloc.emit(errorState); - await tester.pump(); - - expect(find.text('Ошибка'), findsOneWidget); - });*/ - - // Add more tests for other scenarios if needed - }); -} -*/ \ No newline at end of file