refactor(ui): CropBloc -> CropCubit
This commit is contained in:
parent
a8398eb07b
commit
32444fa9f5
@ -15,7 +15,7 @@ import 'package:moxxyv2/ui/bloc/account.dart';
|
|||||||
import 'package:moxxyv2/ui/bloc/blocklist.dart';
|
import 'package:moxxyv2/ui/bloc/blocklist.dart';
|
||||||
import 'package:moxxyv2/ui/bloc/conversation_bloc.dart';
|
import 'package:moxxyv2/ui/bloc/conversation_bloc.dart';
|
||||||
import 'package:moxxyv2/ui/bloc/conversations.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/cropbackground_bloc.dart';
|
||||||
import 'package:moxxyv2/ui/bloc/devices_bloc.dart';
|
import 'package:moxxyv2/ui/bloc/devices_bloc.dart';
|
||||||
import 'package:moxxyv2/ui/bloc/groupchat/joingroupchat_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<ProfileCubit>(ProfileCubit());
|
||||||
GetIt.I.registerSingleton<PreferencesBloc>(PreferencesBloc());
|
GetIt.I.registerSingleton<PreferencesBloc>(PreferencesBloc());
|
||||||
GetIt.I.registerSingleton<StartChatBloc>(StartChatBloc());
|
GetIt.I.registerSingleton<StartChatBloc>(StartChatBloc());
|
||||||
GetIt.I.registerSingleton<CropBloc>(CropBloc());
|
GetIt.I.registerSingleton<CropCubit>(CropCubit());
|
||||||
GetIt.I.registerSingleton<SendFilesBloc>(SendFilesBloc());
|
GetIt.I.registerSingleton<SendFilesBloc>(SendFilesBloc());
|
||||||
GetIt.I.registerSingleton<CropBackgroundBloc>(CropBackgroundBloc());
|
GetIt.I.registerSingleton<CropBackgroundBloc>(CropBackgroundBloc());
|
||||||
GetIt.I.registerSingleton<ShareSelectionBloc>(ShareSelectionBloc());
|
GetIt.I.registerSingleton<ShareSelectionBloc>(ShareSelectionBloc());
|
||||||
@ -168,8 +168,8 @@ void main() async {
|
|||||||
BlocProvider<StartChatBloc>(
|
BlocProvider<StartChatBloc>(
|
||||||
create: (_) => GetIt.I.get<StartChatBloc>(),
|
create: (_) => GetIt.I.get<StartChatBloc>(),
|
||||||
),
|
),
|
||||||
BlocProvider<CropBloc>(
|
BlocProvider<CropCubit>(
|
||||||
create: (_) => GetIt.I.get<CropBloc>(),
|
create: (_) => GetIt.I.get<CropCubit>(),
|
||||||
),
|
),
|
||||||
BlocProvider<SendFilesBloc>(
|
BlocProvider<SendFilesBloc>(
|
||||||
create: (_) => GetIt.I.get<SendFilesBloc>(),
|
create: (_) => GetIt.I.get<SendFilesBloc>(),
|
||||||
|
@ -9,23 +9,22 @@ import 'package:get_it/get_it.dart';
|
|||||||
import 'package:moxxyv2/ui/bloc/navigation_bloc.dart';
|
import 'package:moxxyv2/ui/bloc/navigation_bloc.dart';
|
||||||
import 'package:moxxyv2/ui/constants.dart';
|
import 'package:moxxyv2/ui/constants.dart';
|
||||||
|
|
||||||
part 'crop_bloc.freezed.dart';
|
part 'crop.freezed.dart';
|
||||||
part 'crop_event.dart';
|
|
||||||
part 'crop_state.dart';
|
|
||||||
|
|
||||||
class CropBloc extends Bloc<CropEvent, CropState> {
|
@freezed
|
||||||
CropBloc() : super(CropState()) {
|
class CropState with _$CropState {
|
||||||
on<ImageCroppedEvent>(_onImageCropped);
|
factory CropState({
|
||||||
on<ResetImageEvent>(_onImageReset);
|
@Default(null) Uint8List? image,
|
||||||
on<SetImageEvent>(_onImageSet);
|
@Default(false) bool isWorking,
|
||||||
}
|
}) = _CropState;
|
||||||
|
}
|
||||||
|
|
||||||
|
class CropCubit extends Cubit<CropState> {
|
||||||
|
CropCubit() : super(CropState());
|
||||||
late Completer<Uint8List?> _completer;
|
late Completer<Uint8List?> _completer;
|
||||||
final GlobalKey cropKey = GlobalKey();
|
final GlobalKey cropKey = GlobalKey();
|
||||||
|
|
||||||
Future<void> _onImageCropped(
|
Future<void> croppedImage() async {
|
||||||
ImageCroppedEvent event,
|
|
||||||
Emitter<CropState> emit,
|
|
||||||
) async {
|
|
||||||
emit(
|
emit(
|
||||||
state.copyWith(
|
state.copyWith(
|
||||||
isWorking: true,
|
isWorking: true,
|
||||||
@ -39,13 +38,10 @@ class CropBloc extends Bloc<CropEvent, CropState> {
|
|||||||
|
|
||||||
GetIt.I.get<NavigationBloc>().add(PoppedRouteEvent());
|
GetIt.I.get<NavigationBloc>().add(PoppedRouteEvent());
|
||||||
|
|
||||||
await _onImageReset(ResetImageEvent(), emit);
|
await resetImage();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _onImageReset(
|
Future<void> resetImage() async {
|
||||||
ResetImageEvent event,
|
|
||||||
Emitter<CropState> emit,
|
|
||||||
) async {
|
|
||||||
emit(
|
emit(
|
||||||
state.copyWith(
|
state.copyWith(
|
||||||
image: null,
|
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(
|
emit(
|
||||||
state.copyWith(
|
state.copyWith(
|
||||||
image: event.image,
|
image: image,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -68,7 +64,7 @@ class CropBloc extends Bloc<CropEvent, CropState> {
|
|||||||
|
|
||||||
final bytes = await file.readAsBytes();
|
final bytes = await file.readAsBytes();
|
||||||
|
|
||||||
add(SetImageEvent(bytes));
|
return setImage(bytes);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// User-callable function. Loads the image, navigates to the page
|
/// 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) {
|
Future<Uint8List?> cropImage(String path) {
|
||||||
_completer = Completer();
|
_completer = Completer();
|
||||||
|
|
||||||
add(ResetImageEvent());
|
resetImage();
|
||||||
GetIt.I.get<NavigationBloc>().add(
|
GetIt.I.get<NavigationBloc>().add(
|
||||||
PushedNamedEvent(
|
PushedNamedEvent(
|
||||||
const NavigationDestination(cropRoute),
|
const NavigationDestination(cropRoute),
|
||||||
@ -101,7 +97,7 @@ class CropBloc extends Bloc<CropEvent, CropState> {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
add(SetImageEvent(data));
|
setImage(data);
|
||||||
|
|
||||||
return _completer.future;
|
return _completer.future;
|
||||||
}
|
}
|
@ -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;
|
|
||||||
}
|
|
@ -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;
|
|
||||||
}
|
|
@ -15,7 +15,7 @@ import 'package:moxxyv2/i18n/strings.g.dart';
|
|||||||
import 'package:moxxyv2/shared/helpers.dart';
|
import 'package:moxxyv2/shared/helpers.dart';
|
||||||
import 'package:moxxyv2/shared/models/message.dart';
|
import 'package:moxxyv2/shared/models/message.dart';
|
||||||
import 'package:moxxyv2/shared/models/omemo_device.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/bloc/sticker_pack_bloc.dart';
|
||||||
import 'package:moxxyv2/ui/constants.dart';
|
import 'package:moxxyv2/ui/constants.dart';
|
||||||
import 'package:moxxyv2/ui/pages/util/qrcode.dart';
|
import 'package:moxxyv2/ui/pages/util/qrcode.dart';
|
||||||
@ -180,7 +180,7 @@ Future<Uint8List?> pickAndCropImage(BuildContext context) async {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (result != null) {
|
if (result != null) {
|
||||||
return GetIt.I.get<CropBloc>().cropImageWithData(result.data!);
|
return GetIt.I.get<CropCubit>().cropImageWithData(result.data!);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
@ -2,7 +2,7 @@ import 'package:cropperx/cropperx.dart';
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:moxxyv2/i18n/strings.g.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/constants.dart';
|
||||||
import 'package:moxxyv2/ui/widgets/backdrop_spinner.dart';
|
import 'package:moxxyv2/ui/widgets/backdrop_spinner.dart';
|
||||||
import 'package:moxxyv2/ui/widgets/cancel_button.dart';
|
import 'package:moxxyv2/ui/widgets/cancel_button.dart';
|
||||||
@ -28,7 +28,7 @@ class CropPage extends StatelessWidget {
|
|||||||
child: Cropper(
|
child: Cropper(
|
||||||
backgroundColor: Colors.black,
|
backgroundColor: Colors.black,
|
||||||
image: Image.memory(state.image!),
|
image: Image.memory(state.image!),
|
||||||
cropperKey: context.read<CropBloc>().cropKey,
|
cropperKey: context.read<CropCubit>().cropKey,
|
||||||
overlayType: OverlayType.circle,
|
overlayType: OverlayType.circle,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@ -52,11 +52,7 @@ class CropPage extends StatelessWidget {
|
|||||||
FilledButton(
|
FilledButton(
|
||||||
onPressed: state.isWorking
|
onPressed: state.isWorking
|
||||||
? null
|
? null
|
||||||
: () async {
|
: context.read<CropCubit>().croppedImage,
|
||||||
context.read<CropBloc>().add(
|
|
||||||
ImageCroppedEvent(),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
child: Text(t.pages.crop.setProfilePicture),
|
child: Text(t.pages.crop.setProfilePicture),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
@ -77,11 +73,11 @@ class CropPage extends StatelessWidget {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return BlocBuilder<CropBloc, CropState>(
|
return BlocBuilder<CropCubit, CropState>(
|
||||||
builder: (context, state) {
|
builder: (context, state) {
|
||||||
return PopScope(
|
return PopScope(
|
||||||
onPopInvoked: (_) {
|
onPopInvoked: (_) {
|
||||||
context.read<CropBloc>().add(ResetImageEvent());
|
context.read<CropCubit>().resetImage();
|
||||||
},
|
},
|
||||||
child: SafeArea(
|
child: SafeArea(
|
||||||
child: state.image != null
|
child: state.image != null
|
||||||
|
Loading…
Reference in New Issue
Block a user