From 4eccdf3d286d5029a31e21a2ccdc3ea3234fdf61 Mon Sep 17 00:00:00 2001 From: Ian Su Date: Sat, 6 Aug 2022 22:31:55 +0800 Subject: [PATCH] feat: save icon into db --- .../lib/user/application/user_service.dart | 5 ++++ .../application/user/settings_user_bloc.dart | 9 ++++-- .../settings/widgets/settings_user_view.dart | 2 +- .../rust-lib/flowy-database/src/schema.rs | 1 + .../flowy-user/src/entities/parser/mod.rs | 2 ++ .../src/entities/parser/user_icon.rs | 16 +++++++++++ .../flowy-user/src/entities/user_profile.rs | 28 ++++++++++++++++++- .../flowy-user/src/services/database.rs | 5 ++++ 8 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 frontend/rust-lib/flowy-user/src/entities/parser/user_icon.rs diff --git a/frontend/app_flowy/lib/user/application/user_service.dart b/frontend/app_flowy/lib/user/application/user_service.dart index 48bea6aa41..28b904fb5c 100644 --- a/frontend/app_flowy/lib/user/application/user_service.dart +++ b/frontend/app_flowy/lib/user/application/user_service.dart @@ -19,6 +19,7 @@ class UserService { String? name, String? password, String? email, + String? icon, }) { var payload = UpdateUserProfilePayloadPB.create()..id = userId; @@ -34,6 +35,10 @@ class UserService { payload.email = email; } + if (icon != null) { + payload.icon = icon; + } + return UserEventUpdateUserProfile(payload).send(); } diff --git a/frontend/app_flowy/lib/workspace/application/user/settings_user_bloc.dart b/frontend/app_flowy/lib/workspace/application/user/settings_user_bloc.dart index 527eb7ba35..18c3a76fb4 100644 --- a/frontend/app_flowy/lib/workspace/application/user/settings_user_bloc.dart +++ b/frontend/app_flowy/lib/workspace/application/user/settings_user_bloc.dart @@ -36,7 +36,12 @@ class SettingsUserViewBloc extends Bloc { }); }, updateUserIcon: (String icon) { - emit(state.copyWith(icon: icon)); + _userService.updateUserProfile(icon: icon).then((result) { + result.fold( + (l) => null, + (err) => Log.error(err), + ); + }); }, ); }); @@ -74,12 +79,10 @@ class SettingsUserState with _$SettingsUserState { const factory SettingsUserState({ required UserProfilePB userProfile, required Either successOrFailure, - required String icon, }) = _SettingsUserState; factory SettingsUserState.initial(UserProfilePB userProfile) => SettingsUserState( userProfile: userProfile, successOrFailure: left(unit), - icon: 'page', ); } diff --git a/frontend/app_flowy/lib/workspace/presentation/settings/widgets/settings_user_view.dart b/frontend/app_flowy/lib/workspace/presentation/settings/widgets/settings_user_view.dart index a6b67f143a..9e7b2904cb 100644 --- a/frontend/app_flowy/lib/workspace/presentation/settings/widgets/settings_user_view.dart +++ b/frontend/app_flowy/lib/workspace/presentation/settings/widgets/settings_user_view.dart @@ -33,7 +33,7 @@ class SettingsUserView extends StatelessWidget { } Widget _renderCurrentIcon(BuildContext context) { - String icon = context.read().state.icon; + String icon = context.read().state.userProfile.icon; return _CurrentIcon(icon); } } diff --git a/frontend/rust-lib/flowy-database/src/schema.rs b/frontend/rust-lib/flowy-database/src/schema.rs index e41fd6d865..4a68c709dd 100644 --- a/frontend/rust-lib/flowy-database/src/schema.rs +++ b/frontend/rust-lib/flowy-database/src/schema.rs @@ -87,6 +87,7 @@ table! { name -> Text, token -> Text, email -> Text, + icon -> Text, workspace -> Text, } } diff --git a/frontend/rust-lib/flowy-user/src/entities/parser/mod.rs b/frontend/rust-lib/flowy-user/src/entities/parser/mod.rs index 71259509f2..792af0c146 100644 --- a/frontend/rust-lib/flowy-user/src/entities/parser/mod.rs +++ b/frontend/rust-lib/flowy-user/src/entities/parser/mod.rs @@ -1,11 +1,13 @@ // https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/ mod user_email; +mod user_icon; mod user_id; mod user_name; mod user_password; mod user_workspace; pub use user_email::*; +pub use user_icon::*; pub use user_id::*; pub use user_name::*; pub use user_password::*; diff --git a/frontend/rust-lib/flowy-user/src/entities/parser/user_icon.rs b/frontend/rust-lib/flowy-user/src/entities/parser/user_icon.rs new file mode 100644 index 0000000000..69258ca848 --- /dev/null +++ b/frontend/rust-lib/flowy-user/src/entities/parser/user_icon.rs @@ -0,0 +1,16 @@ +use crate::errors::ErrorCode; + +#[derive(Debug)] +pub struct UserIcon(pub String); + +impl UserIcon { + pub fn parse(s: String) -> Result { + Ok(Self(s)) + } +} + +impl AsRef for UserIcon { + fn as_ref(&self) -> &str { + &self.0 + } +} diff --git a/frontend/rust-lib/flowy-user/src/entities/user_profile.rs b/frontend/rust-lib/flowy-user/src/entities/user_profile.rs index 276894ffc8..d19f3293c8 100644 --- a/frontend/rust-lib/flowy-user/src/entities/user_profile.rs +++ b/frontend/rust-lib/flowy-user/src/entities/user_profile.rs @@ -2,7 +2,7 @@ use flowy_derive::ProtoBuf; use std::convert::TryInto; use crate::{ - entities::parser::{UserEmail, UserId, UserName, UserPassword}, + entities::parser::{UserEmail, UserIcon, UserId, UserName, UserPassword}, errors::ErrorCode, }; @@ -25,6 +25,9 @@ pub struct UserProfilePB { #[pb(index = 4)] pub token: String, + + #[pb(index = 5)] + pub icon: String, } #[derive(ProtoBuf, Default)] @@ -40,6 +43,9 @@ pub struct UpdateUserProfilePayloadPB { #[pb(index = 4, one_of)] pub password: Option, + + #[pb(index = 5, one_of)] + pub icon: Option, } impl UpdateUserProfilePayloadPB { @@ -64,6 +70,11 @@ impl UpdateUserProfilePayloadPB { self.password = Some(password.to_owned()); self } + + pub fn icon(mut self, icon: &str) -> Self { + self.icon = Some(icon.to_owned()); + self + } } #[derive(ProtoBuf, Default, Clone, Debug)] @@ -79,6 +90,9 @@ pub struct UpdateUserProfileParams { #[pb(index = 4, one_of)] pub password: Option, + + #[pb(index = 5, one_of)] + pub icon: Option, } impl UpdateUserProfileParams { @@ -88,6 +102,7 @@ impl UpdateUserProfileParams { name: None, email: None, password: None, + icon: None, } } @@ -105,6 +120,11 @@ impl UpdateUserProfileParams { self.password = Some(password.to_owned()); self } + + pub fn icon(mut self, icon: &str) -> Self { + self.icon = Some(icon.to_owned()); + self + } } impl TryInto for UpdateUserProfilePayloadPB { @@ -128,11 +148,17 @@ impl TryInto for UpdateUserProfilePayloadPB { Some(password) => Some(UserPassword::parse(password)?.0), }; + let icon = match self.icon { + None => None, + Some(icon) => Some(UserIcon::parse(icon)?.0), + }; + Ok(UpdateUserProfileParams { id, name, email, password, + icon, }) } } diff --git a/frontend/rust-lib/flowy-user/src/services/database.rs b/frontend/rust-lib/flowy-user/src/services/database.rs index 64ebd705a7..7e214adb68 100644 --- a/frontend/rust-lib/flowy-user/src/services/database.rs +++ b/frontend/rust-lib/flowy-user/src/services/database.rs @@ -81,6 +81,7 @@ pub struct UserTable { pub(crate) name: String, pub(crate) token: String, pub(crate) email: String, + pub(crate) icon: String, pub(crate) workspace: String, // deprecated } @@ -91,6 +92,7 @@ impl UserTable { name, email, token, + icon: "".to_owned(), workspace: "".to_owned(), } } @@ -120,6 +122,7 @@ impl std::convert::From for UserProfilePB { email: table.email, name: table.name, token: table.token, + icon: table.icon, } } } @@ -131,6 +134,7 @@ pub struct UserTableChangeset { pub workspace: Option, // deprecated pub name: Option, pub email: Option, + pub icon: Option, } impl UserTableChangeset { @@ -140,6 +144,7 @@ impl UserTableChangeset { workspace: None, name: params.name, email: params.email, + icon: params.icon, } } }