feat(profile): some fixes, add buttons

This commit is contained in:
mso7nueh
2024-09-04 17:27:46 +03:00
parent d76f535663
commit f9c10047b2
154 changed files with 1249 additions and 296 deletions

View File

@@ -0,0 +1,72 @@
//import 'package:eatvio_app/old/auth/auth_screen/auth_screen.dart';
import 'dart:async';
import 'dart:convert';
import 'package:eatvio_app/app_router.dart';
import 'package:eatvio_app/core/token_storage.dart';
import 'package:eatvio_app/old/users/subscribers_screen/models/subscribers_model.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:http/http.dart' as http;
import '/core/app_export.dart';
part 'subscribers_event.dart';
part 'subscribers_state.dart';
/// A bloc that manages the state of a Subscribers according to the event that is dispatched to it.
class SubscribersBloc extends Bloc<SubscribersEvent, SubscribersState> {
SubscribersBloc(SubscribersState initialState) : super(initialState) {
on<SubscribersInitialEvent>(_onInitialize);
}
Future<void> _onInitialize(
SubscribersInitialEvent event,
Emitter<SubscribersState> emit,
) async {
try {
if (!await TokenStorage.haveTokenInStorage()) {
AppRouter.navigatorKey.currentContext?.goNamed('authScreen');
}
final response = await http.get(
Uri.parse(baseUrl + '/api/profile/subscribers'),
headers: {
'Authorization': 'Bearer ${await TokenStorage.getTokenFromStorage()}',
},
);
print('Got response');
print(response.statusCode);
print(response.body);
if (response.statusCode == 200) {
print('Got 200');
final data = jsonDecode(response.body);
if (data is List<dynamic>) {
print('Got List');
if (state is SubscribersLoadedState) {
print('Got loaded');
var currentState = state as SubscribersLoadedState;
emit((currentState).copyWith(
subscribers: data,
myProfileModelObj: currentState.myProfileModelObj,
));
} else {
print('Got unloaded');
emit(SubscribersLoadedState(
subscribers: data,
));
}
} else {
// Один или несколько ключевых полей отсутствуют, обрабатываем ошибку
emit(SubscribersErrorState('Неверный формат ответа API.'));
}
} else {
// Статус ответа не равен 200, обрабатываем ошибку
emit(SubscribersErrorState('Ошибка сервера: ${response.statusCode}'));
}
} catch (e) {
// Ошибка при декодировании JSON или другая ошибка выполнения
emit(SubscribersErrorState('Ошибка при получении данных ленты: $e'));
}
}
}

View File

@@ -0,0 +1,16 @@
// ignore_for_file: must_be_immutable
part of 'subscribers_bloc.dart';
/// Abstract class for all events that can be dispatched from the
///Subscribers widget.
///
/// Events must be immutable and implement the [Equatable] interface.
@immutable
abstract class SubscribersEvent extends Equatable {}
/// Event that is dispatched when the Subscribers widget is first created.
class SubscribersInitialEvent extends SubscribersEvent {
@override
List<Object?> get props => [];
}

View File

@@ -0,0 +1,49 @@
// ignore_for_file: must_be_immutable
part of 'subscribers_bloc.dart';
class SubscribersState extends Equatable {
const SubscribersState({this.myProfileModelObj});
final SubscribersModel? myProfileModelObj;
@override
List<Object?> get props => [myProfileModelObj];
}
class SubscribersInitialState extends SubscribersState {
SubscribersInitialState({required SubscribersModel myProfileModelObj}) : super(myProfileModelObj: myProfileModelObj);
}
/// Represents the state of Subscribers in the application.
class SubscribersLoadedState extends SubscribersState {
SubscribersLoadedState({this.myProfileModelObj, this.subscribers}) : super(myProfileModelObj: null);
SubscribersModel? myProfileModelObj;
dynamic subscribers;
@override
List<Object?> get props => [
myProfileModelObj,
subscribers,
];
SubscribersLoadedState copyWith({
SubscribersModel? myProfileModelObj,
dynamic subscribers,
}) {
return SubscribersLoadedState(
myProfileModelObj: myProfileModelObj ?? this.myProfileModelObj,
subscribers: subscribers ?? this.subscribers,
);
}
}
class SubscribersErrorState extends SubscribersState {
final String errorMessage;
const SubscribersErrorState(this.errorMessage);
@override
List<Object> get props => [errorMessage];
}