refactor(all): refactor

This commit is contained in:
alex
2024-07-09 17:50:32 +03:00
parent f79b9f03ae
commit e180ff16b6
37 changed files with 1706 additions and 1123 deletions

View File

@@ -37,7 +37,7 @@ class RecipeEditBloc extends Bloc<RecipeEditEvent, RecipeEditState> {
}
final String? recipeID = state.recipeModelObj?.recipeID;
final response = await http.get(
Uri.parse(dotenv.env['SERVER_ROOT']! + '/recipe/show/$recipeID'),
Uri.parse(dotenv.env['SERVER_ROOT']! + '/recipes/show/$recipeID'),
headers: {
'Authorization': 'Bearer $token',
},
@@ -47,7 +47,8 @@ class RecipeEditBloc extends Bloc<RecipeEditEvent, RecipeEditState> {
if (data is Map<String, dynamic>) {
if (state is RecipeEditLoadedState) {
var currentState = state as RecipeEditLoadedState;
emit(currentState.copyWith(
emit(
currentState.copyWith(
recipeModelObj: currentState.recipeModelObj,
title: currentState.title,
token: currentState.token,
@@ -56,7 +57,9 @@ class RecipeEditBloc extends Bloc<RecipeEditEvent, RecipeEditState> {
video_url: currentState.video_url,
steps: currentState.steps,
stepDescriptions: currentState.stepDescriptions,
tags: currentState.tags,),);
tags: currentState.tags,
),
);
} else {
final Map<String, dynamic> defaultStep = {
'description': '',
@@ -75,17 +78,19 @@ class RecipeEditBloc extends Bloc<RecipeEditEvent, RecipeEditState> {
}
}
emit(RecipeEditLoadedState(
token: token,
recipe: data,
title: TextEditingController(text: data['title']),
description: TextEditingController(text: data['description']),
video_url: TextEditingController(text: data['video_url']),
tagsController: TextEditingController(),
tags: await _fetchTags(data['id']),
stepDescriptions: stepDescriptions,
steps: steps,
),);
emit(
RecipeEditLoadedState(
token: token,
recipe: data,
title: TextEditingController(text: data['title']),
description: TextEditingController(text: data['description']),
video_url: TextEditingController(text: data['video_url']),
tagsController: TextEditingController(),
tags: await _fetchTags(data['id']),
stepDescriptions: stepDescriptions,
steps: steps,
),
);
}
} else {
// Один или несколько ключевых полей отсутствуют, обрабатываем ошибку
@@ -195,7 +200,7 @@ class RecipeEditBloc extends Bloc<RecipeEditEvent, RecipeEditState> {
}
final response = await http.get(
Uri.parse(dotenv.env['SERVER_ROOT']! + '/recipe/getTags/$id'),
Uri.parse(dotenv.env['SERVER_ROOT']! + '/recipes/getTags/$id'),
headers: {
'Authorization': 'Bearer $token',
},
@@ -213,7 +218,8 @@ class RecipeEditBloc extends Bloc<RecipeEditEvent, RecipeEditState> {
}
if (state is RecipeEditLoadedState) {
var currentState = state as RecipeEditLoadedState;
emit(currentState.copyWith(
emit(
currentState.copyWith(
recipeModelObj: currentState.recipeModelObj,
recipe: currentState.recipe,
title: currentState.title,
@@ -222,7 +228,9 @@ class RecipeEditBloc extends Bloc<RecipeEditEvent, RecipeEditState> {
video_url: currentState.video_url,
steps: currentState.steps,
stepDescriptions: currentState.stepDescriptions,
tags: currentState.tags,),);
tags: currentState.tags,
),
);
}
}
}

View File

@@ -1,6 +1,8 @@
import 'dart:async';
import 'package:dio/dio.dart';
import 'package:eatvio_app/app_router.dart';
import 'package:eatvio_app/locator_service.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import '/core/app_export.dart';
@@ -31,27 +33,29 @@ class RecipeSelectBloc extends Bloc<RecipeSelectEvent, RecipeSelectState> {
if (token == null) {
AppRouter.navigatorKey.currentContext?.goNamed('authScreen');
}
final response = await http.get(
Uri.parse(dotenv.env['SERVER_ROOT']! + '/recipe/index'),
);
final response = await sl<Dio>().get(dotenv.env['SERVER_ROOT']! + '/recipes/index');
print('Got response');
if (response.statusCode == 200) {
print('Got 200');
final data = jsonDecode(response.body);
final data = response.data['data'];
if (data is List<dynamic>) {
print('Got List');
if (state is RecipeSelectLoadedState) {
print('Got loaded');
var currentState = state as RecipeSelectLoadedState;
emit((currentState).copyWith(
posts: data,
myProfileModelObj: currentState.myProfileModelObj,
),);
emit(
(currentState).copyWith(
posts: data,
myProfileModelObj: currentState.myProfileModelObj,
),
);
} else {
print('Got unloaded');
emit(RecipeSelectLoadedState(
posts: data,
),);
emit(
RecipeSelectLoadedState(
posts: data,
),
);
}
} else {
// Один или несколько ключевых полей отсутствуют, обрабатываем ошибку

View File

@@ -12,14 +12,12 @@ class RecipeSelectState extends Equatable {
}
class RecipeSelectInitialState extends RecipeSelectState {
RecipeSelectInitialState({required RecipeSelectModel myProfileModelObj})
: super(myProfileModelObj: myProfileModelObj);
RecipeSelectInitialState({required RecipeSelectModel myProfileModelObj}) : super(myProfileModelObj: myProfileModelObj);
}
/// Represents the state of RecipeSelect in the application.
class RecipeSelectLoadedState extends RecipeSelectState {
RecipeSelectLoadedState({this.myProfileModelObj, this.posts})
: super(myProfileModelObj: null);
RecipeSelectLoadedState({this.myProfileModelObj, this.posts}) : super(myProfileModelObj: null);
RecipeSelectModel? myProfileModelObj;
List<dynamic>? posts;

View File

@@ -112,6 +112,7 @@ class RecipeSelectScreen extends StatelessWidget {
_buildRecipeList(BuildContext context, RecipeSelectLoadedState state) {
return ListView.builder(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true, // Add this line
itemCount: state.posts?.length ?? 0,
itemBuilder: (context, index) {

View File

@@ -37,7 +37,7 @@ class RecipeBloc extends Bloc<RecipeEvent, RecipeState> {
}
final String? recipeID = state.recipeModelObj?.recipeID;
final response = await http.get(
Uri.parse(dotenv.env['SERVER_ROOT']! + '/recipe/show/$recipeID'),
Uri.parse(dotenv.env['SERVER_ROOT']! + '/recipes/show/$recipeID'),
headers: {
'Authorization': 'Bearer $token',
},
@@ -47,23 +47,29 @@ class RecipeBloc extends Bloc<RecipeEvent, RecipeState> {
if (data is Map<String, dynamic>) {
if (state is RecipeLoadedState) {
var currentState = state as RecipeLoadedState;
emit(currentState.copyWith(
emit(
currentState.copyWith(
recipeModelObj: currentState.recipeModelObj,
recipe: currentState.recipe,
token: currentState.token,
commentController: currentState.commentController,
comments: currentState.comments,
steps: currentState.steps,
author: currentState.author,),);
author: currentState.author,
),
);
} else {
emit(RecipeLoadedState(
emit(
RecipeLoadedState(
token: token,
recipe: data,
steps: data['steps'],
author: await _fetchAuthor(data['id']),
comments: fillCommentList(await _fetchComments(data['id'])),
tags: await _fetchTags(data['id']),
commentController: TextEditingController(),),);
commentController: TextEditingController(),
),
);
}
} else {
// Один или несколько ключевых полей отсутствуют, обрабатываем ошибку
@@ -87,7 +93,7 @@ class RecipeBloc extends Bloc<RecipeEvent, RecipeState> {
}
final int id = event.id;
final response = await http.post(
Uri.parse(dotenv.env['SERVER_ROOT']! + '/recipe/delete/$id'),
Uri.parse(dotenv.env['SERVER_ROOT']! + '/recipes/delete/$id'),
headers: {
'Authorization': 'Bearer $token',
},
@@ -125,7 +131,7 @@ class RecipeBloc extends Bloc<RecipeEvent, RecipeState> {
}
final response = await http.get(
Uri.parse(dotenv.env['SERVER_ROOT']! + '/recipe/getAuthor/$id'),
Uri.parse(dotenv.env['SERVER_ROOT']! + '/recipes/getAuthor/$id'),
headers: {
'Authorization': 'Bearer $token',
},
@@ -141,7 +147,7 @@ class RecipeBloc extends Bloc<RecipeEvent, RecipeState> {
AppRouter.navigatorKey.currentContext?.goNamed('authScreen');
}
final response = await http.get(
Uri.parse(dotenv.env['SERVER_ROOT']! + '/recipe/getComments/$id'),
Uri.parse(dotenv.env['SERVER_ROOT']! + '/recipes/getComments/$id'),
headers: {
'Authorization': 'Bearer $token',
},
@@ -159,7 +165,7 @@ class RecipeBloc extends Bloc<RecipeEvent, RecipeState> {
}
final response = await http.get(
Uri.parse(dotenv.env['SERVER_ROOT']! + '/recipe/getTags/$id'),
Uri.parse(dotenv.env['SERVER_ROOT']! + '/recipes/getTags/$id'),
headers: {
'Authorization': 'Bearer $token',
},
@@ -171,30 +177,35 @@ class RecipeBloc extends Bloc<RecipeEvent, RecipeState> {
List<CommentModel> fillCommentList(List<dynamic> comments) {
List<CommentModel> list = [];
for (int i = 0; i < comments.length; i++) {
list.add(CommentModel(
list.add(
CommentModel(
id: comments[i]['id'].toString(),
userImage: comments[i]['profile_photo_path'],
username: comments[i]['name'],
dateText: DateFormat('dd.MM').format(DateTime.parse(comments[i]['updated_at'])),
greetingText: comments[i]['body'],),);
greetingText: comments[i]['body'],
),
);
}
return list;
}
Future<http.Response> _sendCommentData(String? token, String body, int id) async {
String url = dotenv.env['SERVER_ROOT']! + '/recipe/createComment/$id';
String url = dotenv.env['SERVER_ROOT']! + '/recipes/createComment/$id';
final Map<String, dynamic> credentials = {
'id': id,
'body': body,
};
var response = await http.post(Uri.parse(url),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer $token',
},
body: json.encode(credentials),
encoding: utf8,);
var response = await http.post(
Uri.parse(url),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer $token',
},
body: json.encode(credentials),
encoding: utf8,
);
return response;
}