refactor: create SettingsUserViewBloc

This commit is contained in:
Ian Su 2022-07-09 22:00:17 +08:00
parent 51ab1b6798
commit f24e154b4f
7 changed files with 100 additions and 19 deletions

View File

@ -141,7 +141,7 @@
"menu": {
"appearance": "Appearance",
"language": "Language",
"settings": "Settings",
"user": "User",
"open": "Open Settings"
},
"appearance": {

View File

@ -5,6 +5,7 @@ import 'package:app_flowy/workspace/application/app/prelude.dart';
import 'package:app_flowy/workspace/application/doc/prelude.dart';
import 'package:app_flowy/workspace/application/grid/prelude.dart';
import 'package:app_flowy/workspace/application/trash/prelude.dart';
import 'package:app_flowy/workspace/application/user/prelude.dart';
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/view/prelude.dart';
@ -101,6 +102,11 @@ void _resolveFolderDeps(GetIt getIt) {
(user, _) => MenuUserBloc(user),
);
//User
getIt.registerFactoryParam<SettingsUserViewBloc, UserProfile, void>(
(user, _) => SettingsUserViewBloc(user),
);
// App
getIt.registerFactoryParam<AppBloc, App, void>(
(app, _) => AppBloc(

View File

@ -0,0 +1 @@
export 'settings_user_bloc.dart';

View File

@ -0,0 +1,79 @@
import 'package:app_flowy/user/application/user_listener.dart';
import 'package:app_flowy/user/application/user_service.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_user_bloc.freezed.dart';
class SettingsUserViewBloc extends Bloc<SettingsUserEvent, SettingsUserState> {
final UserService _userService;
final UserListener _userListener;
final UserProfile userProfile;
SettingsUserViewBloc(this.userProfile)
: _userListener = UserListener(userProfile: userProfile),
_userService = UserService(userId: userProfile.id),
super(SettingsUserState.initial(userProfile)) {
on<SettingsUserEvent>((event, emit) async {
await event.when(
initial: () async {
_userListener.start(onProfileUpdated: _profileUpdated);
await _initUser();
},
didReceiveUserProfile: (UserProfile newUserProfile) {
emit(state.copyWith(userProfile: newUserProfile));
},
updateUserName: (String name) {
_userService.updateUserProfile(name: name).then((result) {
result.fold(
(l) => null,
(err) => Log.error(err),
);
});
},
);
});
}
@override
Future<void> close() async {
await _userListener.stop();
super.close();
}
Future<void> _initUser() async {
final result = await _userService.initUser();
result.fold((l) => null, (error) => Log.error(error));
}
void _profileUpdated(Either<UserProfile, FlowyError> userProfileOrFailed) {
userProfileOrFailed.fold(
(newUserProfile) => add(SettingsUserEvent.didReceiveUserProfile(newUserProfile)),
(err) => Log.error(err),
);
}
}
@freezed
class SettingsUserEvent with _$SettingsUserEvent {
const factory SettingsUserEvent.initial() = _Initial;
const factory SettingsUserEvent.updateUserName(String name) = _UpdateUserName;
const factory SettingsUserEvent.didReceiveUserProfile(UserProfile newUserProfile) = _DidReceiveUserProfile;
}
@freezed
class SettingsUserState with _$SettingsUserState {
const factory SettingsUserState({
required UserProfile userProfile,
required Either<Unit, String> successOrFailure,
}) = _SettingsUserState;
factory SettingsUserState.initial(UserProfile userProfile) => SettingsUserState(
userProfile: userProfile,
successOrFailure: left(unit),
);
}

View File

@ -2,7 +2,7 @@ import 'package:app_flowy/generated/locale_keys.g.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_language_view.dart';
import 'package:app_flowy/workspace/presentation/settings/widgets/settings_settings_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:flowy_sdk/protobuf/flowy-user/protobuf.dart' show UserProfile;
import 'package:easy_localization/easy_localization.dart';
@ -24,7 +24,7 @@ class _SettingsDialogState extends State<SettingsDialog> {
final List<Widget> settingsViews = [
const SettingsAppearanceView(),
const SettingsLanguageView(),
SettingsSettingsView(user),
SettingsUserView(user),
];
return settingsViews[index];
}

View File

@ -40,7 +40,7 @@ class SettingsMenu extends StatelessWidget {
SettingsMenuElement(
index: 2,
currentIndex: currentIndex,
label: LocaleKeys.settings_menu_settings.tr(),
label: LocaleKeys.settings_menu_user.tr(),
icon: Icons.account_box_outlined,
changeSelectedIndex: changeSelectedIndex,
),

View File

@ -1,22 +1,22 @@
import 'package:app_flowy/startup/startup.dart';
import 'package:flutter/material.dart';
import 'package:app_flowy/workspace/application/menu/menu_user_bloc.dart';
import 'package:app_flowy/workspace/application/user/settings_user_bloc.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flowy_sdk/protobuf/flowy-user/protobuf.dart' show UserProfile;
class SettingsSettingsView extends StatelessWidget {
class SettingsUserView extends StatelessWidget {
final UserProfile user;
SettingsSettingsView(this.user, {Key? key}) : super(key: ValueKey(user.id));
SettingsUserView(this.user, {Key? key}) : super(key: ValueKey(user.id));
@override
Widget build(BuildContext context) {
return BlocProvider<MenuUserBloc>(
create: (context) => getIt<MenuUserBloc>(param1: user)..add(const MenuUserEvent.initial()),
child: BlocBuilder<MenuUserBloc, MenuUserState>(
return BlocProvider<SettingsUserViewBloc>(
create: (context) => getIt<SettingsUserViewBloc>(param1: user)..add(const SettingsUserEvent.initial()),
child: BlocBuilder<SettingsUserViewBloc, SettingsUserState>(
builder: (context, state) => SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: const [UserNameInput()],
children: const [_UserNameInput()],
),
),
),
@ -24,16 +24,11 @@ class SettingsSettingsView extends StatelessWidget {
}
}
class UserNameInput extends StatefulWidget {
const UserNameInput({
class _UserNameInput extends StatelessWidget {
const _UserNameInput({
Key? key,
}) : super(key: key);
@override
State<UserNameInput> createState() => _UserNameInputState();
}
class _UserNameInputState extends State<UserNameInput> {
@override
Widget build(BuildContext context) {
return TextField(
@ -41,7 +36,7 @@ class _UserNameInputState extends State<UserNameInput> {
labelText: 'Name',
),
onSubmitted: (val) {
context.read<MenuUserBloc>().add(MenuUserEvent.updateUserName(val));
context.read<SettingsUserViewBloc>().add(SettingsUserEvent.updateUserName(val));
debugPrint("Value $val submitted");
});
}