mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: add SettingDialogBloc
This commit is contained in:
@ -10,6 +10,7 @@ import 'package:app_flowy/workspace/application/workspace/prelude.dart';
|
|||||||
import 'package:app_flowy/workspace/application/edit_pannel/edit_pannel_bloc.dart';
|
import 'package:app_flowy/workspace/application/edit_pannel/edit_pannel_bloc.dart';
|
||||||
import 'package:app_flowy/workspace/application/view/prelude.dart';
|
import 'package:app_flowy/workspace/application/view/prelude.dart';
|
||||||
import 'package:app_flowy/workspace/application/menu/prelude.dart';
|
import 'package:app_flowy/workspace/application/menu/prelude.dart';
|
||||||
|
import 'package:app_flowy/workspace/application/settings/prelude.dart';
|
||||||
import 'package:app_flowy/user/application/prelude.dart';
|
import 'package:app_flowy/user/application/prelude.dart';
|
||||||
import 'package:app_flowy/user/presentation/router.dart';
|
import 'package:app_flowy/user/presentation/router.dart';
|
||||||
import 'package:app_flowy/workspace/presentation/home/home_stack.dart';
|
import 'package:app_flowy/workspace/presentation/home/home_stack.dart';
|
||||||
@ -102,6 +103,11 @@ void _resolveFolderDeps(GetIt getIt) {
|
|||||||
(user, _) => MenuUserBloc(user),
|
(user, _) => MenuUserBloc(user),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
//Settings
|
||||||
|
getIt.registerFactoryParam<SettingsDialogBloc, UserProfile, void>(
|
||||||
|
(user, _) => SettingsDialogBloc(user),
|
||||||
|
);
|
||||||
|
|
||||||
//User
|
//User
|
||||||
getIt.registerFactoryParam<SettingsUserViewBloc, UserProfile, void>(
|
getIt.registerFactoryParam<SettingsUserViewBloc, UserProfile, void>(
|
||||||
(user, _) => SettingsUserViewBloc(user),
|
(user, _) => SettingsUserViewBloc(user),
|
||||||
|
@ -0,0 +1 @@
|
|||||||
|
export 'settings_dialog_bloc.dart';
|
@ -0,0 +1,67 @@
|
|||||||
|
import 'package:app_flowy/user/application/user_listener.dart';
|
||||||
|
import 'package:flowy_sdk/log.dart';
|
||||||
|
import 'package:flowy_sdk/protobuf/flowy-error/errors.pb.dart';
|
||||||
|
import 'package:flowy_sdk/protobuf/flowy-user/user_profile.pb.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||||
|
import 'package:dartz/dartz.dart';
|
||||||
|
|
||||||
|
part 'settings_dialog_bloc.freezed.dart';
|
||||||
|
|
||||||
|
class SettingsDialogBloc extends Bloc<SettingsDialogEvent, SettingsDialogState> {
|
||||||
|
final UserListener _userListener;
|
||||||
|
final UserProfile userProfile;
|
||||||
|
|
||||||
|
SettingsDialogBloc(this.userProfile)
|
||||||
|
: _userListener = UserListener(userProfile: userProfile),
|
||||||
|
super(SettingsDialogState.initial(userProfile)) {
|
||||||
|
on<SettingsDialogEvent>((event, emit) async {
|
||||||
|
await event.when(
|
||||||
|
initial: () async {
|
||||||
|
_userListener.start(onProfileUpdated: _profileUpdated);
|
||||||
|
},
|
||||||
|
didReceiveUserProfile: (UserProfile newUserProfile) {
|
||||||
|
emit(state.copyWith(userProfile: newUserProfile));
|
||||||
|
},
|
||||||
|
setViewIndex: (int viewIndex) {
|
||||||
|
emit(state.copyWith(viewIndex: viewIndex));
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> close() async {
|
||||||
|
await _userListener.stop();
|
||||||
|
super.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _profileUpdated(Either<UserProfile, FlowyError> userProfileOrFailed) {
|
||||||
|
userProfileOrFailed.fold(
|
||||||
|
(newUserProfile) => add(SettingsDialogEvent.didReceiveUserProfile(newUserProfile)),
|
||||||
|
(err) => Log.error(err),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@freezed
|
||||||
|
class SettingsDialogEvent with _$SettingsDialogEvent {
|
||||||
|
const factory SettingsDialogEvent.initial() = _Initial;
|
||||||
|
const factory SettingsDialogEvent.didReceiveUserProfile(UserProfile newUserProfile) = _DidReceiveUserProfile;
|
||||||
|
const factory SettingsDialogEvent.setViewIndex(int index) = _SetViewIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
@freezed
|
||||||
|
class SettingsDialogState with _$SettingsDialogState {
|
||||||
|
const factory SettingsDialogState({
|
||||||
|
required UserProfile userProfile,
|
||||||
|
required Either<Unit, String> successOrFailure,
|
||||||
|
required int viewIndex,
|
||||||
|
}) = _SettingsDialogState;
|
||||||
|
|
||||||
|
factory SettingsDialogState.initial(UserProfile userProfile) => SettingsDialogState(
|
||||||
|
userProfile: userProfile,
|
||||||
|
successOrFailure: left(unit),
|
||||||
|
viewIndex: 0,
|
||||||
|
);
|
||||||
|
}
|
@ -1,25 +1,21 @@
|
|||||||
|
import 'package:app_flowy/startup/startup.dart';
|
||||||
import 'package:app_flowy/generated/locale_keys.g.dart';
|
import 'package:app_flowy/generated/locale_keys.g.dart';
|
||||||
import 'package:app_flowy/workspace/application/appearance.dart';
|
import 'package:app_flowy/workspace/application/appearance.dart';
|
||||||
import 'package:app_flowy/workspace/presentation/settings/widgets/settings_appearance_view.dart';
|
import 'package:app_flowy/workspace/presentation/settings/widgets/settings_appearance_view.dart';
|
||||||
import 'package:app_flowy/workspace/presentation/settings/widgets/settings_language_view.dart';
|
import 'package:app_flowy/workspace/presentation/settings/widgets/settings_language_view.dart';
|
||||||
import 'package:app_flowy/workspace/presentation/settings/widgets/settings_user_view.dart';
|
import 'package:app_flowy/workspace/presentation/settings/widgets/settings_user_view.dart';
|
||||||
import 'package:app_flowy/workspace/presentation/settings/widgets/settings_menu.dart';
|
import 'package:app_flowy/workspace/presentation/settings/widgets/settings_menu.dart';
|
||||||
|
import 'package:app_flowy/workspace/application/settings/settings_dialog_bloc.dart';
|
||||||
import 'package:flowy_sdk/protobuf/flowy-user/protobuf.dart' show UserProfile;
|
import 'package:flowy_sdk/protobuf/flowy-user/protobuf.dart' show UserProfile;
|
||||||
import 'package:easy_localization/easy_localization.dart';
|
import 'package:easy_localization/easy_localization.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
class SettingsDialog extends StatefulWidget {
|
class SettingsDialog extends StatelessWidget {
|
||||||
final UserProfile user;
|
final UserProfile user;
|
||||||
SettingsDialog(this.user, {Key? key}) : super(key: ValueKey(user.id));
|
SettingsDialog(this.user, {Key? key}) : super(key: ValueKey(user.id));
|
||||||
|
|
||||||
@override
|
|
||||||
State<SettingsDialog> createState() => _SettingsDialogState();
|
|
||||||
}
|
|
||||||
|
|
||||||
class _SettingsDialogState extends State<SettingsDialog> {
|
|
||||||
int _selectedViewIndex = 0;
|
|
||||||
|
|
||||||
Widget getSettingsView(int index, UserProfile user) {
|
Widget getSettingsView(int index, UserProfile user) {
|
||||||
final List<Widget> settingsViews = [
|
final List<Widget> settingsViews = [
|
||||||
const SettingsAppearanceView(),
|
const SettingsAppearanceView(),
|
||||||
@ -31,7 +27,10 @@ class _SettingsDialogState extends State<SettingsDialog> {
|
|||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return ChangeNotifierProvider.value(
|
return BlocProvider<SettingsDialogBloc>(
|
||||||
|
create: (context) => getIt<SettingsDialogBloc>(param1: user)..add(const SettingsDialogEvent.initial()),
|
||||||
|
child: BlocBuilder<SettingsDialogBloc, SettingsDialogState>(
|
||||||
|
builder: (context, state) => ChangeNotifierProvider.value(
|
||||||
value: Provider.of<AppearanceSettingModel>(context, listen: true),
|
value: Provider.of<AppearanceSettingModel>(context, listen: true),
|
||||||
child: AlertDialog(
|
child: AlertDialog(
|
||||||
shape: RoundedRectangleBorder(
|
shape: RoundedRectangleBorder(
|
||||||
@ -56,22 +55,21 @@ class _SettingsDialogState extends State<SettingsDialog> {
|
|||||||
width: 200,
|
width: 200,
|
||||||
child: SettingsMenu(
|
child: SettingsMenu(
|
||||||
changeSelectedIndex: (index) {
|
changeSelectedIndex: (index) {
|
||||||
setState(() {
|
context.read<SettingsDialogBloc>().add(SettingsDialogEvent.setViewIndex(index));
|
||||||
_selectedViewIndex = index;
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
currentIndex: _selectedViewIndex,
|
currentIndex: context.read<SettingsDialogBloc>().state.viewIndex,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
const VerticalDivider(),
|
const VerticalDivider(),
|
||||||
const SizedBox(width: 10),
|
const SizedBox(width: 10),
|
||||||
Expanded(
|
Expanded(
|
||||||
child: getSettingsView(_selectedViewIndex, widget.user),
|
child: getSettingsView(context.read<SettingsDialogBloc>().state.viewIndex,
|
||||||
|
context.read<SettingsDialogBloc>().state.userProfile),
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,7 +25,6 @@ class SettingsUserView extends StatelessWidget {
|
|||||||
|
|
||||||
Widget _renderUserNameInput(BuildContext context) {
|
Widget _renderUserNameInput(BuildContext context) {
|
||||||
String name = context.read<SettingsUserViewBloc>().state.userProfile.name;
|
String name = context.read<SettingsUserViewBloc>().state.userProfile.name;
|
||||||
debugPrint(name);
|
|
||||||
return _UserNameInput(name);
|
return _UserNameInput(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user