forked from eatvio/eatvio_app
moved diets logic to a diets folder
This commit is contained in:
@@ -1,14 +0,0 @@
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
|
||||
part 'diet_likes_model.freezed.dart';
|
||||
part 'diet_likes_model.g.dart';
|
||||
|
||||
@freezed
|
||||
class DietLikesModel with _$DietLikesModel {
|
||||
const factory DietLikesModel({
|
||||
@JsonKey(name: 'total_likers') required int totalLikes,
|
||||
@JsonKey(name: 'is_liked') required bool isLiked,
|
||||
}) = _DietLikesModel;
|
||||
|
||||
factory DietLikesModel.fromJson(Map<String, Object?> json) => _$DietLikesModelFromJson(json);
|
||||
}
|
||||
@@ -1,197 +0,0 @@
|
||||
// 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 'diet_likes_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');
|
||||
|
||||
DietLikesModel _$DietLikesModelFromJson(Map<String, dynamic> json) {
|
||||
return _DietLikesModel.fromJson(json);
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
mixin _$DietLikesModel {
|
||||
@JsonKey(name: 'total_likers')
|
||||
int get totalLikes => throw _privateConstructorUsedError;
|
||||
@JsonKey(name: 'is_liked')
|
||||
bool get isLiked => throw _privateConstructorUsedError;
|
||||
|
||||
/// Serializes this DietLikesModel to a JSON map.
|
||||
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
|
||||
|
||||
/// Create a copy of DietLikesModel
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
$DietLikesModelCopyWith<DietLikesModel> get copyWith =>
|
||||
throw _privateConstructorUsedError;
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
abstract class $DietLikesModelCopyWith<$Res> {
|
||||
factory $DietLikesModelCopyWith(
|
||||
DietLikesModel value, $Res Function(DietLikesModel) then) =
|
||||
_$DietLikesModelCopyWithImpl<$Res, DietLikesModel>;
|
||||
@useResult
|
||||
$Res call(
|
||||
{@JsonKey(name: 'total_likers') int totalLikes,
|
||||
@JsonKey(name: 'is_liked') bool isLiked});
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
class _$DietLikesModelCopyWithImpl<$Res, $Val extends DietLikesModel>
|
||||
implements $DietLikesModelCopyWith<$Res> {
|
||||
_$DietLikesModelCopyWithImpl(this._value, this._then);
|
||||
|
||||
// ignore: unused_field
|
||||
final $Val _value;
|
||||
// ignore: unused_field
|
||||
final $Res Function($Val) _then;
|
||||
|
||||
/// Create a copy of DietLikesModel
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@pragma('vm:prefer-inline')
|
||||
@override
|
||||
$Res call({
|
||||
Object? totalLikes = null,
|
||||
Object? isLiked = null,
|
||||
}) {
|
||||
return _then(_value.copyWith(
|
||||
totalLikes: null == totalLikes
|
||||
? _value.totalLikes
|
||||
: totalLikes // ignore: cast_nullable_to_non_nullable
|
||||
as int,
|
||||
isLiked: null == isLiked
|
||||
? _value.isLiked
|
||||
: isLiked // ignore: cast_nullable_to_non_nullable
|
||||
as bool,
|
||||
) as $Val);
|
||||
}
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
abstract class _$$DietLikesModelImplCopyWith<$Res>
|
||||
implements $DietLikesModelCopyWith<$Res> {
|
||||
factory _$$DietLikesModelImplCopyWith(_$DietLikesModelImpl value,
|
||||
$Res Function(_$DietLikesModelImpl) then) =
|
||||
__$$DietLikesModelImplCopyWithImpl<$Res>;
|
||||
@override
|
||||
@useResult
|
||||
$Res call(
|
||||
{@JsonKey(name: 'total_likers') int totalLikes,
|
||||
@JsonKey(name: 'is_liked') bool isLiked});
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
class __$$DietLikesModelImplCopyWithImpl<$Res>
|
||||
extends _$DietLikesModelCopyWithImpl<$Res, _$DietLikesModelImpl>
|
||||
implements _$$DietLikesModelImplCopyWith<$Res> {
|
||||
__$$DietLikesModelImplCopyWithImpl(
|
||||
_$DietLikesModelImpl _value, $Res Function(_$DietLikesModelImpl) _then)
|
||||
: super(_value, _then);
|
||||
|
||||
/// Create a copy of DietLikesModel
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@pragma('vm:prefer-inline')
|
||||
@override
|
||||
$Res call({
|
||||
Object? totalLikes = null,
|
||||
Object? isLiked = null,
|
||||
}) {
|
||||
return _then(_$DietLikesModelImpl(
|
||||
totalLikes: null == totalLikes
|
||||
? _value.totalLikes
|
||||
: totalLikes // ignore: cast_nullable_to_non_nullable
|
||||
as int,
|
||||
isLiked: null == isLiked
|
||||
? _value.isLiked
|
||||
: isLiked // ignore: cast_nullable_to_non_nullable
|
||||
as bool,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
@JsonSerializable()
|
||||
class _$DietLikesModelImpl implements _DietLikesModel {
|
||||
const _$DietLikesModelImpl(
|
||||
{@JsonKey(name: 'total_likers') required this.totalLikes,
|
||||
@JsonKey(name: 'is_liked') required this.isLiked});
|
||||
|
||||
factory _$DietLikesModelImpl.fromJson(Map<String, dynamic> json) =>
|
||||
_$$DietLikesModelImplFromJson(json);
|
||||
|
||||
@override
|
||||
@JsonKey(name: 'total_likers')
|
||||
final int totalLikes;
|
||||
@override
|
||||
@JsonKey(name: 'is_liked')
|
||||
final bool isLiked;
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'DietLikesModel(totalLikes: $totalLikes, isLiked: $isLiked)';
|
||||
}
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return identical(this, other) ||
|
||||
(other.runtimeType == runtimeType &&
|
||||
other is _$DietLikesModelImpl &&
|
||||
(identical(other.totalLikes, totalLikes) ||
|
||||
other.totalLikes == totalLikes) &&
|
||||
(identical(other.isLiked, isLiked) || other.isLiked == isLiked));
|
||||
}
|
||||
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
@override
|
||||
int get hashCode => Object.hash(runtimeType, totalLikes, isLiked);
|
||||
|
||||
/// Create a copy of DietLikesModel
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
@override
|
||||
@pragma('vm:prefer-inline')
|
||||
_$$DietLikesModelImplCopyWith<_$DietLikesModelImpl> get copyWith =>
|
||||
__$$DietLikesModelImplCopyWithImpl<_$DietLikesModelImpl>(
|
||||
this, _$identity);
|
||||
|
||||
@override
|
||||
Map<String, dynamic> toJson() {
|
||||
return _$$DietLikesModelImplToJson(
|
||||
this,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
abstract class _DietLikesModel implements DietLikesModel {
|
||||
const factory _DietLikesModel(
|
||||
{@JsonKey(name: 'total_likers') required final int totalLikes,
|
||||
@JsonKey(name: 'is_liked') required final bool isLiked}) =
|
||||
_$DietLikesModelImpl;
|
||||
|
||||
factory _DietLikesModel.fromJson(Map<String, dynamic> json) =
|
||||
_$DietLikesModelImpl.fromJson;
|
||||
|
||||
@override
|
||||
@JsonKey(name: 'total_likers')
|
||||
int get totalLikes;
|
||||
@override
|
||||
@JsonKey(name: 'is_liked')
|
||||
bool get isLiked;
|
||||
|
||||
/// Create a copy of DietLikesModel
|
||||
/// with the given fields replaced by the non-null parameter values.
|
||||
@override
|
||||
@JsonKey(includeFromJson: false, includeToJson: false)
|
||||
_$$DietLikesModelImplCopyWith<_$DietLikesModelImpl> get copyWith =>
|
||||
throw _privateConstructorUsedError;
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'diet_likes_model.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
_$DietLikesModelImpl _$$DietLikesModelImplFromJson(Map<String, dynamic> json) =>
|
||||
_$DietLikesModelImpl(
|
||||
totalLikes: (json['total_likers'] as num).toInt(),
|
||||
isLiked: json['is_liked'] as bool,
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$$DietLikesModelImplToJson(
|
||||
_$DietLikesModelImpl instance) =>
|
||||
<String, dynamic>{
|
||||
'total_likers': instance.totalLikes,
|
||||
'is_liked': instance.isLiked,
|
||||
};
|
||||
@@ -1,106 +0,0 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:eatvio_app/core/data/diet/models/diet_likes_model.dart';
|
||||
import 'package:eatvio_app/core/data/diet/services/diet_service.dart';
|
||||
import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
class DietRepository {
|
||||
DietRepository({required this.dietsService});
|
||||
final DietService dietsService;
|
||||
|
||||
final _dietLikeStateStreamController = StreamController<DietLikesState>.broadcast();
|
||||
final _dietFavoriteStateStreamController = StreamController<DietFavoriteState>.broadcast();
|
||||
final _dietCommentsStateStreamController = StreamController<DietCommentsState>.broadcast();
|
||||
Stream get dietLikeState => _dietLikeStateStreamController.stream;
|
||||
Stream get dietFavoriteState => _dietFavoriteStateStreamController.stream;
|
||||
Stream get dietCommentsState => _dietCommentsStateStreamController.stream;
|
||||
|
||||
Future<DietLikesModel> likeDiet(int id) async {
|
||||
try {
|
||||
return await dietsService.likeDiet(id: id);
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<Diet> getDiet(int id) async {
|
||||
try {
|
||||
final diet = await dietsService.getDiet(id: id);
|
||||
return diet;
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<Diet> addToFavorite(int id) async {
|
||||
try {
|
||||
return await dietsService.addToFavorite(id: id);
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<Diet> removeFromFavorite(int id) async {
|
||||
try {
|
||||
return await dietsService.removeFromFavorite(id: id);
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
void emitLikesStateToFeed(DietLikesState state) => _dietLikeStateStreamController.add(state);
|
||||
void emitFavoriteStateToFeed(DietFavoriteState state) => _dietFavoriteStateStreamController.add(state);
|
||||
void emitCommentsStateToFeed(DietCommentsState state) => _dietCommentsStateStreamController.add(state);
|
||||
}
|
||||
|
||||
class DietLikesState extends Equatable {
|
||||
final bool wasLikedByMe;
|
||||
final int totalLikes;
|
||||
final int index;
|
||||
|
||||
DietLikesState({
|
||||
required this.wasLikedByMe,
|
||||
required this.totalLikes,
|
||||
required this.index,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
wasLikedByMe,
|
||||
totalLikes,
|
||||
index,
|
||||
];
|
||||
}
|
||||
|
||||
class DietFavoriteState extends Equatable {
|
||||
final bool isFavorite;
|
||||
final int index;
|
||||
|
||||
DietFavoriteState({
|
||||
required this.isFavorite,
|
||||
required this.index,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
isFavorite,
|
||||
index,
|
||||
];
|
||||
}
|
||||
|
||||
class DietCommentsState extends Equatable {
|
||||
final int index;
|
||||
final int totalComments;
|
||||
|
||||
DietCommentsState({
|
||||
required this.index,
|
||||
required this.totalComments,
|
||||
});
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
index,
|
||||
totalComments,
|
||||
];
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:eatvio_app/core/data/diet/models/diet_likes_model.dart';
|
||||
import 'package:eatvio_app/features/feed/models/diets_pagination_model.dart';
|
||||
import 'package:retrofit/retrofit.dart';
|
||||
|
||||
part 'diet_service.g.dart';
|
||||
|
||||
@RestApi()
|
||||
abstract class DietService {
|
||||
factory DietService(Dio dio, {String baseUrl}) = _DietService;
|
||||
|
||||
@POST('/api/diets/{id}/like')
|
||||
Future<DietLikesModel> likeDiet({
|
||||
@Path() required int id,
|
||||
});
|
||||
|
||||
@POST('/api/diets/{id}/add_to_favorite')
|
||||
Future<Diet> addToFavorite({
|
||||
@Path() required int id,
|
||||
});
|
||||
|
||||
@POST('/api/diets/{id}/remove_from_favorite')
|
||||
Future<Diet> removeFromFavorite({
|
||||
@Path() required int id,
|
||||
});
|
||||
|
||||
@GET('/api/diets/show/{id}')
|
||||
Future<Diet> getDiet({
|
||||
@Path() required int id,
|
||||
});
|
||||
}
|
||||
@@ -1,185 +0,0 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'diet_service.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// RetrofitGenerator
|
||||
// **************************************************************************
|
||||
|
||||
// ignore_for_file: unnecessary_brace_in_string_interps,no_leading_underscores_for_local_identifiers,unused_element,unnecessary_string_interpolations
|
||||
|
||||
class _DietService implements DietService {
|
||||
_DietService(
|
||||
this._dio, {
|
||||
this.baseUrl,
|
||||
this.errorLogger,
|
||||
});
|
||||
|
||||
final Dio _dio;
|
||||
|
||||
String? baseUrl;
|
||||
|
||||
final ParseErrorLogger? errorLogger;
|
||||
|
||||
@override
|
||||
Future<DietLikesModel> likeDiet({required int id}) async {
|
||||
final _extra = <String, dynamic>{};
|
||||
final queryParameters = <String, dynamic>{};
|
||||
final _headers = <String, dynamic>{};
|
||||
const Map<String, dynamic>? _data = null;
|
||||
final _options = _setStreamType<DietLikesModel>(Options(
|
||||
method: 'POST',
|
||||
headers: _headers,
|
||||
extra: _extra,
|
||||
)
|
||||
.compose(
|
||||
_dio.options,
|
||||
'/api/diets/${id}/like',
|
||||
queryParameters: queryParameters,
|
||||
data: _data,
|
||||
)
|
||||
.copyWith(
|
||||
baseUrl: _combineBaseUrls(
|
||||
_dio.options.baseUrl,
|
||||
baseUrl,
|
||||
)));
|
||||
final _result = await _dio.fetch<Map<String, dynamic>>(_options);
|
||||
late DietLikesModel _value;
|
||||
try {
|
||||
_value = DietLikesModel.fromJson(_result.data!);
|
||||
} on Object catch (e, s) {
|
||||
errorLogger?.logError(e, s, _options);
|
||||
rethrow;
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Diet> addToFavorite({required int id}) async {
|
||||
final _extra = <String, dynamic>{};
|
||||
final queryParameters = <String, dynamic>{};
|
||||
final _headers = <String, dynamic>{};
|
||||
const Map<String, dynamic>? _data = null;
|
||||
final _options = _setStreamType<Diet>(Options(
|
||||
method: 'POST',
|
||||
headers: _headers,
|
||||
extra: _extra,
|
||||
)
|
||||
.compose(
|
||||
_dio.options,
|
||||
'/api/diets/${id}/add_to_favorite',
|
||||
queryParameters: queryParameters,
|
||||
data: _data,
|
||||
)
|
||||
.copyWith(
|
||||
baseUrl: _combineBaseUrls(
|
||||
_dio.options.baseUrl,
|
||||
baseUrl,
|
||||
)));
|
||||
final _result = await _dio.fetch<Map<String, dynamic>>(_options);
|
||||
late Diet _value;
|
||||
try {
|
||||
_value = Diet.fromJson(_result.data!);
|
||||
} on Object catch (e, s) {
|
||||
errorLogger?.logError(e, s, _options);
|
||||
rethrow;
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Diet> removeFromFavorite({required int id}) async {
|
||||
final _extra = <String, dynamic>{};
|
||||
final queryParameters = <String, dynamic>{};
|
||||
final _headers = <String, dynamic>{};
|
||||
const Map<String, dynamic>? _data = null;
|
||||
final _options = _setStreamType<Diet>(Options(
|
||||
method: 'POST',
|
||||
headers: _headers,
|
||||
extra: _extra,
|
||||
)
|
||||
.compose(
|
||||
_dio.options,
|
||||
'/api/diets/${id}/remove_from_favorite',
|
||||
queryParameters: queryParameters,
|
||||
data: _data,
|
||||
)
|
||||
.copyWith(
|
||||
baseUrl: _combineBaseUrls(
|
||||
_dio.options.baseUrl,
|
||||
baseUrl,
|
||||
)));
|
||||
final _result = await _dio.fetch<Map<String, dynamic>>(_options);
|
||||
late Diet _value;
|
||||
try {
|
||||
_value = Diet.fromJson(_result.data!);
|
||||
} on Object catch (e, s) {
|
||||
errorLogger?.logError(e, s, _options);
|
||||
rethrow;
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Diet> getDiet({required int id}) async {
|
||||
final _extra = <String, dynamic>{};
|
||||
final queryParameters = <String, dynamic>{};
|
||||
final _headers = <String, dynamic>{};
|
||||
const Map<String, dynamic>? _data = null;
|
||||
final _options = _setStreamType<Diet>(Options(
|
||||
method: 'GET',
|
||||
headers: _headers,
|
||||
extra: _extra,
|
||||
)
|
||||
.compose(
|
||||
_dio.options,
|
||||
'/api/diets/show/${id}',
|
||||
queryParameters: queryParameters,
|
||||
data: _data,
|
||||
)
|
||||
.copyWith(
|
||||
baseUrl: _combineBaseUrls(
|
||||
_dio.options.baseUrl,
|
||||
baseUrl,
|
||||
)));
|
||||
final _result = await _dio.fetch<Map<String, dynamic>>(_options);
|
||||
late Diet _value;
|
||||
try {
|
||||
_value = Diet.fromJson(_result.data!);
|
||||
} on Object catch (e, s) {
|
||||
errorLogger?.logError(e, s, _options);
|
||||
rethrow;
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
|
||||
RequestOptions _setStreamType<T>(RequestOptions requestOptions) {
|
||||
if (T != dynamic &&
|
||||
!(requestOptions.responseType == ResponseType.bytes ||
|
||||
requestOptions.responseType == ResponseType.stream)) {
|
||||
if (T == String) {
|
||||
requestOptions.responseType = ResponseType.plain;
|
||||
} else {
|
||||
requestOptions.responseType = ResponseType.json;
|
||||
}
|
||||
}
|
||||
return requestOptions;
|
||||
}
|
||||
|
||||
String _combineBaseUrls(
|
||||
String dioBaseUrl,
|
||||
String? baseUrl,
|
||||
) {
|
||||
if (baseUrl == null || baseUrl.trim().isEmpty) {
|
||||
return dioBaseUrl;
|
||||
}
|
||||
|
||||
final url = Uri.parse(baseUrl);
|
||||
|
||||
if (url.isAbsolute) {
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
return Uri.parse(dioBaseUrl).resolveUri(url).toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user