chore(ui): Split the request BLoC into three files
This commit is contained in:
parent
6225ac65d9
commit
4323561774
@ -1,7 +1,10 @@
|
|||||||
import 'package:bloc/bloc.dart';
|
import 'package:bloc/bloc.dart';
|
||||||
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||||
import 'package:moxxyv2/i18n/strings.g.dart';
|
import 'package:moxxyv2/i18n/strings.g.dart';
|
||||||
|
|
||||||
// TODO: Split this up into multiple files.
|
part 'request_bloc.freezed.dart';
|
||||||
|
part 'request_event.dart';
|
||||||
|
part 'request_state.dart';
|
||||||
|
|
||||||
enum Request {
|
enum Request {
|
||||||
notifications,
|
notifications,
|
||||||
@ -18,47 +21,8 @@ enum Request {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract class RequestBlocEvent {}
|
class RequestBloc extends Bloc<RequestEvent, RequestState> {
|
||||||
|
RequestBloc() : super(const RequestState()) {
|
||||||
class RequestsSetEvent extends RequestBlocEvent {
|
|
||||||
RequestsSetEvent(this.requests);
|
|
||||||
|
|
||||||
final List<Request> requests;
|
|
||||||
}
|
|
||||||
|
|
||||||
class NextRequestEvent extends RequestBlocEvent {}
|
|
||||||
|
|
||||||
class ResetRequestEvent extends RequestBlocEvent {}
|
|
||||||
|
|
||||||
class RequestBlocState {
|
|
||||||
const RequestBlocState({
|
|
||||||
required this.requests,
|
|
||||||
required this.currentIndex,
|
|
||||||
required this.shouldShow,
|
|
||||||
});
|
|
||||||
|
|
||||||
factory RequestBlocState.initial() =>
|
|
||||||
const RequestBlocState(requests: [], currentIndex: 0, shouldShow: false);
|
|
||||||
|
|
||||||
final bool shouldShow;
|
|
||||||
final List<Request> requests;
|
|
||||||
final int currentIndex;
|
|
||||||
|
|
||||||
RequestBlocState copyWith({
|
|
||||||
List<Request>? requests,
|
|
||||||
int? currentIndex,
|
|
||||||
bool? shouldShow,
|
|
||||||
}) {
|
|
||||||
return RequestBlocState(
|
|
||||||
requests: requests ?? this.requests,
|
|
||||||
currentIndex: currentIndex ?? this.currentIndex,
|
|
||||||
shouldShow: shouldShow ?? this.shouldShow,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class RequestBloc extends Bloc<RequestBlocEvent, RequestBlocState> {
|
|
||||||
RequestBloc() : super(RequestBlocState.initial()) {
|
|
||||||
on<RequestsSetEvent>(_onRequestsSet);
|
on<RequestsSetEvent>(_onRequestsSet);
|
||||||
on<NextRequestEvent>(_onNextRequest);
|
on<NextRequestEvent>(_onNextRequest);
|
||||||
on<ResetRequestEvent>(_onReset);
|
on<ResetRequestEvent>(_onReset);
|
||||||
@ -66,7 +30,7 @@ class RequestBloc extends Bloc<RequestBlocEvent, RequestBlocState> {
|
|||||||
|
|
||||||
Future<void> _onRequestsSet(
|
Future<void> _onRequestsSet(
|
||||||
RequestsSetEvent event,
|
RequestsSetEvent event,
|
||||||
Emitter<RequestBlocState> emit,
|
Emitter<RequestState> emit,
|
||||||
) async {
|
) async {
|
||||||
emit(
|
emit(
|
||||||
state.copyWith(
|
state.copyWith(
|
||||||
@ -79,7 +43,7 @@ class RequestBloc extends Bloc<RequestBlocEvent, RequestBlocState> {
|
|||||||
|
|
||||||
Future<void> _onNextRequest(
|
Future<void> _onNextRequest(
|
||||||
NextRequestEvent event,
|
NextRequestEvent event,
|
||||||
Emitter<RequestBlocState> emit,
|
Emitter<RequestState> emit,
|
||||||
) async {
|
) async {
|
||||||
if (state.currentIndex + 1 >= state.requests.length) {
|
if (state.currentIndex + 1 >= state.requests.length) {
|
||||||
return _onReset(ResetRequestEvent(), emit);
|
return _onReset(ResetRequestEvent(), emit);
|
||||||
@ -94,7 +58,7 @@ class RequestBloc extends Bloc<RequestBlocEvent, RequestBlocState> {
|
|||||||
|
|
||||||
Future<void> _onReset(
|
Future<void> _onReset(
|
||||||
ResetRequestEvent event,
|
ResetRequestEvent event,
|
||||||
Emitter<RequestBlocState> emit,
|
Emitter<RequestState> emit,
|
||||||
) async {
|
) async {
|
||||||
emit(
|
emit(
|
||||||
state.copyWith(
|
state.copyWith(
|
||||||
|
13
lib/ui/bloc/request_event.dart
Normal file
13
lib/ui/bloc/request_event.dart
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
part of 'request_bloc.dart';
|
||||||
|
|
||||||
|
abstract class RequestEvent {}
|
||||||
|
|
||||||
|
class RequestsSetEvent extends RequestEvent {
|
||||||
|
RequestsSetEvent(this.requests);
|
||||||
|
|
||||||
|
final List<Request> requests;
|
||||||
|
}
|
||||||
|
|
||||||
|
class NextRequestEvent extends RequestEvent {}
|
||||||
|
|
||||||
|
class ResetRequestEvent extends RequestEvent {}
|
10
lib/ui/bloc/request_state.dart
Normal file
10
lib/ui/bloc/request_state.dart
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
part of 'request_bloc.dart';
|
||||||
|
|
||||||
|
@freezed
|
||||||
|
class RequestState with _$RequestState {
|
||||||
|
const factory RequestState({
|
||||||
|
@Default([]) List<Request> requests,
|
||||||
|
@Default(0) int currentIndex,
|
||||||
|
@Default(false) bool shouldShow,
|
||||||
|
}) = _RequestState;
|
||||||
|
}
|
@ -14,7 +14,7 @@ class RequestDialog extends StatelessWidget {
|
|||||||
shape: RoundedRectangleBorder(
|
shape: RoundedRectangleBorder(
|
||||||
borderRadius: BorderRadius.circular(textfieldRadiusRegular),
|
borderRadius: BorderRadius.circular(textfieldRadiusRegular),
|
||||||
),
|
),
|
||||||
child: BlocConsumer<RequestBloc, RequestBlocState>(
|
child: BlocConsumer<RequestBloc, RequestState>(
|
||||||
listener: (_, state) {
|
listener: (_, state) {
|
||||||
// Automatically dismiss the dialog when we're done
|
// Automatically dismiss the dialog when we're done
|
||||||
if (!state.shouldShow) {
|
if (!state.shouldShow) {
|
||||||
|
Loading…
Reference in New Issue
Block a user