From 74e6315a57731f075e94abc1bb5e3899361c6195 Mon Sep 17 00:00:00 2001 From: Oliver Walters Date: Tue, 9 Feb 2021 08:57:56 +1100 Subject: [PATCH] Edit existing profile --- lib/l10n | 2 +- lib/settings/login.dart | 63 ++++++++++++++++++++++++++++++----------- 2 files changed, 47 insertions(+), 18 deletions(-) diff --git a/lib/l10n b/lib/l10n index 86fbf66a..ef139565 160000 --- a/lib/l10n +++ b/lib/l10n @@ -1 +1 @@ -Subproject commit 86fbf66aea18c43cc8920eddce22df5966a7a875 +Subproject commit ef139565bba4fcd5889aa242a0062827eab5eceb diff --git a/lib/settings/login.dart b/lib/settings/login.dart index 06b99ed9..868760dd 100644 --- a/lib/settings/login.dart +++ b/lib/settings/login.dart @@ -27,9 +27,7 @@ class _InvenTreeLoginSettingsState extends State { List profiles; - _InvenTreeLoginSettingsState(this.profiles) { - - } + _InvenTreeLoginSettingsState(this.profiles); void _reload() async { @@ -39,13 +37,19 @@ class _InvenTreeLoginSettingsState extends State { }); } - void _createProfile(BuildContext context) { + void _editProfile(BuildContext context, {UserProfile userProfile, bool createNew = false}) { var _name; var _server; var _username; var _password; + UserProfile profile; + + if (userProfile != null) { + profile = userProfile; + } + showFormDialog( context, I18N.of(context).profileAdd, @@ -63,15 +67,26 @@ class _InvenTreeLoginSettingsState extends State { if (_addProfileKey.currentState.validate()) { _addProfileKey.currentState.save(); - // TODO - create the new profile... - UserProfile profile = UserProfile( - name: _name, - server: _server, - username: _username, - password: _password - ); + if (createNew) { + // TODO - create the new profile... + UserProfile profile = UserProfile( + name: _name, + server: _server, + username: _username, + password: _password + ); - _addProfile(profile); + _addProfile(profile); + } else { + + profile.name = _name; + profile.server = _server; + profile.username = _username; + profile.password = _password; + + _updateProfile(profile); + + } } } ) @@ -80,24 +95,28 @@ class _InvenTreeLoginSettingsState extends State { StringField( label: I18N.of(context).name, hint: "Enter profile name", + initial: createNew ? '' : profile.name, onSaved: (value) => _name = value, validator: _validateProfileName, ), StringField( label: I18N.of(context).server, hint: "http[s]://:", + initial: createNew ? '' : profile.server, validator: _validateServer, onSaved: (value) => _server = value, ), StringField( label: I18N.of(context).username, hint: "Enter username", + initial: createNew ? '' : profile.username, onSaved: (value) => _username = value, validator: _validateUsername, ), StringField( label: I18N.of(context).password, hint: "Enter password", + initial: createNew ? '' : profile.password, onSaved: (value) => _password = value, validator: _validatePassword, ) @@ -173,6 +192,16 @@ class _InvenTreeLoginSettingsState extends State { _reload(); } + void _updateProfile(UserProfile profile) async { + + await UserProfileDBManager().updateProfile(profile); + + // Dismiss the dialog + Navigator.of(context).pop(); + + _reload(); + } + void _addProfile(UserProfile profile) async { await UserProfileDBManager().addProfile(profile); @@ -216,8 +245,8 @@ class _InvenTreeLoginSettingsState extends State { ), SimpleDialogOption( onPressed: () { - //Navigator.of(context).pop(); - // TODO - Edit profile! + Navigator.of(context).pop(); + _editProfile(context, userProfile: profile); }, child: Text(I18N.of(context).profileEdit), ), @@ -226,7 +255,7 @@ class _InvenTreeLoginSettingsState extends State { // Navigator.of(context, rootNavigator: true).pop(); confirmationDialog( context, - "Delete", + I18N.of(context).delete, "Delete this profile?", onAccept: () { _deleteProfile(profile); @@ -248,7 +277,7 @@ class _InvenTreeLoginSettingsState extends State { return Scaffold( appBar: AppBar( - title: Text(I18N.of(context).profile), + title: Text(I18N.of(context).profileSelect), ), body: Container( child: ListView( @@ -258,7 +287,7 @@ class _InvenTreeLoginSettingsState extends State { floatingActionButton: FloatingActionButton( child: Icon(FontAwesomeIcons.plus), onPressed: () { - _createProfile(context); + _editProfile(context, createNew: true); }, ) );