feat(app): Initial layouts for subscribers, feed display fix, feed photo upload front end

This commit is contained in:
2024-06-27 13:17:53 +03:00
parent 6e7f1acf46
commit b1508c3553
12 changed files with 709 additions and 184 deletions

View File

@@ -0,0 +1,74 @@
//import 'package:eatvio_app/presentation/auth/auth_screen/auth_screen.dart';
import 'dart:async';
import 'package:eatvio_app/app_router.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import '/core/app_export.dart';
import 'package:eatvio_app/presentation/users/subscribers_screen/models/subscribers_model.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:hive/hive.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:go_router/go_router.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 {
final box = await Hive.openBox('token');
final token = box.get('token');
if (token == null) {
AppRouter.navigatorKey.currentContext?.goNamed('authScreen');
}
final response = await http.get(
Uri.parse(dotenv.env['SERVER_ROOT']! + '/profile/subscribers'),
headers: {
'Authorization': 'Bearer $token',
},
);
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];
}