feat: save icon into db

This commit is contained in:
Ian Su
2022-08-06 22:31:55 +08:00
parent 7fc9a085c5
commit 4eccdf3d28
8 changed files with 63 additions and 5 deletions

View File

@ -19,6 +19,7 @@ class UserService {
String? name, String? name,
String? password, String? password,
String? email, String? email,
String? icon,
}) { }) {
var payload = UpdateUserProfilePayloadPB.create()..id = userId; var payload = UpdateUserProfilePayloadPB.create()..id = userId;
@ -34,6 +35,10 @@ class UserService {
payload.email = email; payload.email = email;
} }
if (icon != null) {
payload.icon = icon;
}
return UserEventUpdateUserProfile(payload).send(); return UserEventUpdateUserProfile(payload).send();
} }

View File

@ -36,7 +36,12 @@ class SettingsUserViewBloc extends Bloc<SettingsUserEvent, SettingsUserState> {
}); });
}, },
updateUserIcon: (String icon) { 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({ const factory SettingsUserState({
required UserProfilePB userProfile, required UserProfilePB userProfile,
required Either<Unit, String> successOrFailure, required Either<Unit, String> successOrFailure,
required String icon,
}) = _SettingsUserState; }) = _SettingsUserState;
factory SettingsUserState.initial(UserProfilePB userProfile) => SettingsUserState( factory SettingsUserState.initial(UserProfilePB userProfile) => SettingsUserState(
userProfile: userProfile, userProfile: userProfile,
successOrFailure: left(unit), successOrFailure: left(unit),
icon: 'page',
); );
} }

View File

@ -33,7 +33,7 @@ class SettingsUserView extends StatelessWidget {
} }
Widget _renderCurrentIcon(BuildContext context) { Widget _renderCurrentIcon(BuildContext context) {
String icon = context.read<SettingsUserViewBloc>().state.icon; String icon = context.read<SettingsUserViewBloc>().state.userProfile.icon;
return _CurrentIcon(icon); return _CurrentIcon(icon);
} }
} }

View File

@ -87,6 +87,7 @@ table! {
name -> Text, name -> Text,
token -> Text, token -> Text,
email -> Text, email -> Text,
icon -> Text,
workspace -> Text, workspace -> Text,
} }
} }

View File

@ -1,11 +1,13 @@
// https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/ // https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-validate/
mod user_email; mod user_email;
mod user_icon;
mod user_id; mod user_id;
mod user_name; mod user_name;
mod user_password; mod user_password;
mod user_workspace; mod user_workspace;
pub use user_email::*; pub use user_email::*;
pub use user_icon::*;
pub use user_id::*; pub use user_id::*;
pub use user_name::*; pub use user_name::*;
pub use user_password::*; pub use user_password::*;

View File

@ -0,0 +1,16 @@
use crate::errors::ErrorCode;
#[derive(Debug)]
pub struct UserIcon(pub String);
impl UserIcon {
pub fn parse(s: String) -> Result<UserIcon, ErrorCode> {
Ok(Self(s))
}
}
impl AsRef<str> for UserIcon {
fn as_ref(&self) -> &str {
&self.0
}
}

View File

@ -2,7 +2,7 @@ use flowy_derive::ProtoBuf;
use std::convert::TryInto; use std::convert::TryInto;
use crate::{ use crate::{
entities::parser::{UserEmail, UserId, UserName, UserPassword}, entities::parser::{UserEmail, UserIcon, UserId, UserName, UserPassword},
errors::ErrorCode, errors::ErrorCode,
}; };
@ -25,6 +25,9 @@ pub struct UserProfilePB {
#[pb(index = 4)] #[pb(index = 4)]
pub token: String, pub token: String,
#[pb(index = 5)]
pub icon: String,
} }
#[derive(ProtoBuf, Default)] #[derive(ProtoBuf, Default)]
@ -40,6 +43,9 @@ pub struct UpdateUserProfilePayloadPB {
#[pb(index = 4, one_of)] #[pb(index = 4, one_of)]
pub password: Option<String>, pub password: Option<String>,
#[pb(index = 5, one_of)]
pub icon: Option<String>,
} }
impl UpdateUserProfilePayloadPB { impl UpdateUserProfilePayloadPB {
@ -64,6 +70,11 @@ impl UpdateUserProfilePayloadPB {
self.password = Some(password.to_owned()); self.password = Some(password.to_owned());
self self
} }
pub fn icon(mut self, icon: &str) -> Self {
self.icon = Some(icon.to_owned());
self
}
} }
#[derive(ProtoBuf, Default, Clone, Debug)] #[derive(ProtoBuf, Default, Clone, Debug)]
@ -79,6 +90,9 @@ pub struct UpdateUserProfileParams {
#[pb(index = 4, one_of)] #[pb(index = 4, one_of)]
pub password: Option<String>, pub password: Option<String>,
#[pb(index = 5, one_of)]
pub icon: Option<String>,
} }
impl UpdateUserProfileParams { impl UpdateUserProfileParams {
@ -88,6 +102,7 @@ impl UpdateUserProfileParams {
name: None, name: None,
email: None, email: None,
password: None, password: None,
icon: None,
} }
} }
@ -105,6 +120,11 @@ impl UpdateUserProfileParams {
self.password = Some(password.to_owned()); self.password = Some(password.to_owned());
self self
} }
pub fn icon(mut self, icon: &str) -> Self {
self.icon = Some(icon.to_owned());
self
}
} }
impl TryInto<UpdateUserProfileParams> for UpdateUserProfilePayloadPB { impl TryInto<UpdateUserProfileParams> for UpdateUserProfilePayloadPB {
@ -128,11 +148,17 @@ impl TryInto<UpdateUserProfileParams> for UpdateUserProfilePayloadPB {
Some(password) => Some(UserPassword::parse(password)?.0), Some(password) => Some(UserPassword::parse(password)?.0),
}; };
let icon = match self.icon {
None => None,
Some(icon) => Some(UserIcon::parse(icon)?.0),
};
Ok(UpdateUserProfileParams { Ok(UpdateUserProfileParams {
id, id,
name, name,
email, email,
password, password,
icon,
}) })
} }
} }

View File

@ -81,6 +81,7 @@ pub struct UserTable {
pub(crate) name: String, pub(crate) name: String,
pub(crate) token: String, pub(crate) token: String,
pub(crate) email: String, pub(crate) email: String,
pub(crate) icon: String,
pub(crate) workspace: String, // deprecated pub(crate) workspace: String, // deprecated
} }
@ -91,6 +92,7 @@ impl UserTable {
name, name,
email, email,
token, token,
icon: "".to_owned(),
workspace: "".to_owned(), workspace: "".to_owned(),
} }
} }
@ -120,6 +122,7 @@ impl std::convert::From<UserTable> for UserProfilePB {
email: table.email, email: table.email,
name: table.name, name: table.name,
token: table.token, token: table.token,
icon: table.icon,
} }
} }
} }
@ -131,6 +134,7 @@ pub struct UserTableChangeset {
pub workspace: Option<String>, // deprecated pub workspace: Option<String>, // deprecated
pub name: Option<String>, pub name: Option<String>,
pub email: Option<String>, pub email: Option<String>,
pub icon: Option<String>,
} }
impl UserTableChangeset { impl UserTableChangeset {
@ -140,6 +144,7 @@ impl UserTableChangeset {
workspace: None, workspace: None,
name: params.name, name: params.name,
email: params.email, email: params.email,
icon: params.icon,
} }
} }
} }