refactor(ui): CropBloc -> CropCubit

This commit is contained in:
PapaTutuWawa 2024-04-01 15:34:22 +02:00
parent a8398eb07b
commit 32444fa9f5
6 changed files with 30 additions and 59 deletions

View File

@ -15,7 +15,7 @@ import 'package:moxxyv2/ui/bloc/account.dart';
import 'package:moxxyv2/ui/bloc/blocklist.dart';
import 'package:moxxyv2/ui/bloc/conversation_bloc.dart';
import 'package:moxxyv2/ui/bloc/conversations.dart';
import 'package:moxxyv2/ui/bloc/crop_bloc.dart';
import 'package:moxxyv2/ui/bloc/crop.dart';
import 'package:moxxyv2/ui/bloc/cropbackground_bloc.dart';
import 'package:moxxyv2/ui/bloc/devices_bloc.dart';
import 'package:moxxyv2/ui/bloc/groupchat/joingroupchat_bloc.dart';
@ -110,7 +110,7 @@ void setupBlocs(GlobalKey<NavigatorState> navKey) {
GetIt.I.registerSingleton<ProfileCubit>(ProfileCubit());
GetIt.I.registerSingleton<PreferencesBloc>(PreferencesBloc());
GetIt.I.registerSingleton<StartChatBloc>(StartChatBloc());
GetIt.I.registerSingleton<CropBloc>(CropBloc());
GetIt.I.registerSingleton<CropCubit>(CropCubit());
GetIt.I.registerSingleton<SendFilesBloc>(SendFilesBloc());
GetIt.I.registerSingleton<CropBackgroundBloc>(CropBackgroundBloc());
GetIt.I.registerSingleton<ShareSelectionBloc>(ShareSelectionBloc());
@ -168,8 +168,8 @@ void main() async {
BlocProvider<StartChatBloc>(
create: (_) => GetIt.I.get<StartChatBloc>(),
),
BlocProvider<CropBloc>(
create: (_) => GetIt.I.get<CropBloc>(),
BlocProvider<CropCubit>(
create: (_) => GetIt.I.get<CropCubit>(),
),
BlocProvider<SendFilesBloc>(
create: (_) => GetIt.I.get<SendFilesBloc>(),

View File

@ -9,23 +9,22 @@ import 'package:get_it/get_it.dart';
import 'package:moxxyv2/ui/bloc/navigation_bloc.dart';
import 'package:moxxyv2/ui/constants.dart';
part 'crop_bloc.freezed.dart';
part 'crop_event.dart';
part 'crop_state.dart';
part 'crop.freezed.dart';
class CropBloc extends Bloc<CropEvent, CropState> {
CropBloc() : super(CropState()) {
on<ImageCroppedEvent>(_onImageCropped);
on<ResetImageEvent>(_onImageReset);
on<SetImageEvent>(_onImageSet);
}
@freezed
class CropState with _$CropState {
factory CropState({
@Default(null) Uint8List? image,
@Default(false) bool isWorking,
}) = _CropState;
}
class CropCubit extends Cubit<CropState> {
CropCubit() : super(CropState());
late Completer<Uint8List?> _completer;
final GlobalKey cropKey = GlobalKey();
Future<void> _onImageCropped(
ImageCroppedEvent event,
Emitter<CropState> emit,
) async {
Future<void> croppedImage() async {
emit(
state.copyWith(
isWorking: true,
@ -39,13 +38,10 @@ class CropBloc extends Bloc<CropEvent, CropState> {
GetIt.I.get<NavigationBloc>().add(PoppedRouteEvent());
await _onImageReset(ResetImageEvent(), emit);
await resetImage();
}
Future<void> _onImageReset(
ResetImageEvent event,
Emitter<CropState> emit,
) async {
Future<void> resetImage() async {
emit(
state.copyWith(
image: null,
@ -54,10 +50,10 @@ class CropBloc extends Bloc<CropEvent, CropState> {
);
}
Future<void> _onImageSet(SetImageEvent event, Emitter<CropState> emit) async {
Future<void> setImage(Uint8List image) async {
emit(
state.copyWith(
image: event.image,
image: image,
),
);
}
@ -68,7 +64,7 @@ class CropBloc extends Bloc<CropEvent, CropState> {
final bytes = await file.readAsBytes();
add(SetImageEvent(bytes));
return setImage(bytes);
}
/// User-callable function. Loads the image, navigates to the page
@ -78,7 +74,7 @@ class CropBloc extends Bloc<CropEvent, CropState> {
Future<Uint8List?> cropImage(String path) {
_completer = Completer();
add(ResetImageEvent());
resetImage();
GetIt.I.get<NavigationBloc>().add(
PushedNamedEvent(
const NavigationDestination(cropRoute),
@ -101,7 +97,7 @@ class CropBloc extends Bloc<CropEvent, CropState> {
),
);
add(SetImageEvent(data));
setImage(data);
return _completer.future;
}

View File

@ -1,12 +0,0 @@
part of 'crop_bloc.dart';
abstract class CropEvent {}
class ImageCroppedEvent extends CropEvent {}
class ResetImageEvent extends CropEvent {}
class SetImageEvent extends CropEvent {
SetImageEvent(this.image);
final Uint8List image;
}

View File

@ -1,9 +0,0 @@
part of 'crop_bloc.dart';
@freezed
class CropState with _$CropState {
factory CropState({
@Default(null) Uint8List? image,
@Default(false) bool isWorking,
}) = _CropState;
}

View File

@ -15,7 +15,7 @@ import 'package:moxxyv2/i18n/strings.g.dart';
import 'package:moxxyv2/shared/helpers.dart';
import 'package:moxxyv2/shared/models/message.dart';
import 'package:moxxyv2/shared/models/omemo_device.dart';
import 'package:moxxyv2/ui/bloc/crop_bloc.dart';
import 'package:moxxyv2/ui/bloc/crop.dart';
import 'package:moxxyv2/ui/bloc/sticker_pack_bloc.dart';
import 'package:moxxyv2/ui/constants.dart';
import 'package:moxxyv2/ui/pages/util/qrcode.dart';
@ -180,7 +180,7 @@ Future<Uint8List?> pickAndCropImage(BuildContext context) async {
);
if (result != null) {
return GetIt.I.get<CropBloc>().cropImageWithData(result.data!);
return GetIt.I.get<CropCubit>().cropImageWithData(result.data!);
}
return null;

View File

@ -2,7 +2,7 @@ import 'package:cropperx/cropperx.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:moxxyv2/i18n/strings.g.dart';
import 'package:moxxyv2/ui/bloc/crop_bloc.dart';
import 'package:moxxyv2/ui/bloc/crop.dart';
import 'package:moxxyv2/ui/constants.dart';
import 'package:moxxyv2/ui/widgets/backdrop_spinner.dart';
import 'package:moxxyv2/ui/widgets/cancel_button.dart';
@ -28,7 +28,7 @@ class CropPage extends StatelessWidget {
child: Cropper(
backgroundColor: Colors.black,
image: Image.memory(state.image!),
cropperKey: context.read<CropBloc>().cropKey,
cropperKey: context.read<CropCubit>().cropKey,
overlayType: OverlayType.circle,
),
),
@ -52,11 +52,7 @@ class CropPage extends StatelessWidget {
FilledButton(
onPressed: state.isWorking
? null
: () async {
context.read<CropBloc>().add(
ImageCroppedEvent(),
);
},
: context.read<CropCubit>().croppedImage,
child: Text(t.pages.crop.setProfilePicture),
),
],
@ -77,11 +73,11 @@ class CropPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocBuilder<CropBloc, CropState>(
return BlocBuilder<CropCubit, CropState>(
builder: (context, state) {
return PopScope(
onPopInvoked: (_) {
context.read<CropBloc>().add(ResetImageEvent());
context.read<CropCubit>().resetImage();
},
child: SafeArea(
child: state.image != null