forked from eatvio/eatvio_app
Token verification and API data load
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
import 'dart:convert';
|
||||
import '/core/app_export.dart';
|
||||
import 'package:eatvio_app/presentation/chat/chat_select_screen/models/chat_select_model.dart';
|
||||
import 'package:eatvio_app/presentation/chat/chat_select_screen/models/chat_entry.dart'; // ignore: must_be_immutable
|
||||
@@ -7,11 +11,12 @@ part 'chat_select_event.dart';
|
||||
part 'chat_select_state.dart';
|
||||
|
||||
/// A bloc that manages the state of a ChatSelect according to the event that is dispatched to it.
|
||||
///
|
||||
|
||||
class ChatSelectBloc extends Bloc<ChatSelectEvent, ChatSelectState> {
|
||||
ChatSelectBloc(ChatSelectState initialState) : super(initialState) {
|
||||
on<ChatSelectInitialEvent>(_onInitialize);
|
||||
}
|
||||
|
||||
List<ChatItemModel> fillChatEntryList() {
|
||||
return [
|
||||
ChatItemModel(
|
||||
@@ -37,13 +42,45 @@ class ChatSelectBloc extends Bloc<ChatSelectEvent, ChatSelectState> {
|
||||
];
|
||||
}
|
||||
|
||||
_onInitialize(
|
||||
Future<void> _onInitialize(
|
||||
ChatSelectInitialEvent event,
|
||||
Emitter<ChatSelectState> emit,
|
||||
) async {
|
||||
emit(state.copyWith(
|
||||
chatSelectModelObj: state.chatSelectModelObj?.copyWith(
|
||||
userprofileItemList: fillChatEntryList(),
|
||||
)));
|
||||
try {
|
||||
final box = await Hive.openBox('token');
|
||||
final token = box.get('token');
|
||||
if (token == null) {
|
||||
NavigatorService.popAndPushNamed(
|
||||
AppRoutes.authScreen,
|
||||
);
|
||||
}
|
||||
final response = await http.get(
|
||||
Uri.parse(dotenv.env['SERVER_ROOT']! + '/chat/index'),
|
||||
headers: {
|
||||
'Authorization': 'Bearer $token',
|
||||
},
|
||||
);
|
||||
if (response.statusCode == 200) {
|
||||
final data = jsonDecode(response.body);
|
||||
if (data is Map<String, dynamic>) {
|
||||
if (state is ChatSelectLoadedState) {
|
||||
var currentState = state as ChatSelectLoadedState;
|
||||
emit(currentState.copyWith());
|
||||
} else {
|
||||
emit(ChatSelectLoadedState());
|
||||
}
|
||||
} else {
|
||||
// Один или несколько ключевых полей отсутствуют, обрабатываем ошибку
|
||||
emit(ChatSelectErrorState(
|
||||
'Некоторые необходимые данные отсутствуют.'));
|
||||
}
|
||||
} else {
|
||||
// Статус ответа не равен 200, обрабатываем ошибку
|
||||
emit(ChatSelectErrorState('Ошибка сервера: ${response.statusCode}'));
|
||||
}
|
||||
} catch (e) {
|
||||
// Ошибка при декодировании JSON или другая ошибка выполнения
|
||||
emit(ChatSelectErrorState('Ошибка при получении данных профиля: $e'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,27 +5,48 @@ part of 'chat_select_bloc.dart';
|
||||
/// Represents the state of ChatSelect in the application.
|
||||
class ChatSelectState extends Equatable {
|
||||
ChatSelectState({
|
||||
this.selectedDropDownValue,
|
||||
this.chatSelectModelObj,
|
||||
});
|
||||
|
||||
SelectionPopupModel? selectedDropDownValue;
|
||||
|
||||
ChatSelectModel? chatSelectModelObj;
|
||||
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
selectedDropDownValue,
|
||||
chatSelectModelObj,
|
||||
];
|
||||
ChatSelectState copyWith({
|
||||
SelectionPopupModel? selectedDropDownValue,
|
||||
ChatSelectModel? chatSelectModelObj,
|
||||
}) {
|
||||
return ChatSelectState(
|
||||
selectedDropDownValue:
|
||||
selectedDropDownValue ?? this.selectedDropDownValue,
|
||||
chatSelectModelObj: chatSelectModelObj ?? this.chatSelectModelObj,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ChatSelectLoadedState extends ChatSelectState {
|
||||
ChatSelectLoadedState({
|
||||
this.chatSelectModelObj,
|
||||
}) : super(chatSelectModelObj: null);
|
||||
|
||||
ChatSelectModel? chatSelectModelObj;
|
||||
@override
|
||||
List<Object?> get props => [
|
||||
chatSelectModelObj,
|
||||
];
|
||||
ChatSelectLoadedState copyWith({
|
||||
ChatSelectModel? chatSelectModelObj,
|
||||
}) {
|
||||
return ChatSelectLoadedState(
|
||||
chatSelectModelObj: chatSelectModelObj ?? this.chatSelectModelObj,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ChatSelectErrorState extends ChatSelectState {
|
||||
final String errorMessage;
|
||||
|
||||
ChatSelectErrorState(this.errorMessage);
|
||||
|
||||
@override
|
||||
List<Object> get props => [errorMessage];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user