From f0fb55c70f94f33cecfc076063ca077dea182524 Mon Sep 17 00:00:00 2001 From: Jagandeep Brar Date: Fri, 31 Jan 2020 11:36:09 -0600 Subject: [PATCH] [Profiles] Ability to rename a profile (#103) Closes #89 --- lib/configuration/profiles.dart | 32 ++++ .../subpages/general/tabs/profile.dart | 32 +++- lib/system/ui/dialogs/system.dart | 146 ++++++++++++++++++ 3 files changed, 209 insertions(+), 1 deletion(-) diff --git a/lib/configuration/profiles.dart b/lib/configuration/profiles.dart index 2ccf64cab..f33ea3bec 100644 --- a/lib/configuration/profiles.dart +++ b/lib/configuration/profiles.dart @@ -15,6 +15,13 @@ class Profiles { profileList = profiles; } + static Future profileExists(String name) async { + final prefs = await SharedPreferences.getInstance(); + profileList = prefs.getStringList('profiles'); + if(profileList.contains(name)) return true; + return false; + } + static Future createProfile(String name) async { final prefs = await SharedPreferences.getInstance(); profileList = prefs.getStringList('profiles'); @@ -37,6 +44,31 @@ class Profiles { prefs.remove('${name}_nzbget'); } + static Future renameProfile(String oldName, String newName) async { + final prefs = await SharedPreferences.getInstance(); + //Set profile list, enabled_profile + profileList.add(newName); + prefs.setStringList('profiles', profileList); + if(enabledProfile == oldName) { + enabledProfile = newName; + prefs.setString('enabled_profile', enabledProfile); + } + //Set Automation + List _sonarr = prefs.getStringList('${oldName}_sonarr'); + List _radarr = prefs.getStringList('${oldName}_radarr'); + List _lidarr = prefs.getStringList('${oldName}_lidarr'); + prefs.setStringList('${newName}_sonarr', _sonarr); + prefs.setStringList('${newName}_radarr', _radarr); + prefs.setStringList('${newName}_lidarr', _lidarr); + //Set Clients + List _nzbget = prefs.getStringList('${oldName}_nzbget'); + List _sabnzbd = prefs.getStringList('${oldName}_sabnzbd'); + prefs.setStringList('${newName}_nzbget', _nzbget); + prefs.setStringList('${newName}_sabnzbd', _sabnzbd); + //Finally delete the old profile + await deleteProfile(oldName); + } + static Future setProfile(String name) async { final prefs = await SharedPreferences.getInstance(); prefs.setString('enabled_profile', name); diff --git a/lib/pages/settings/subpages/general/tabs/profile.dart b/lib/pages/settings/subpages/general/tabs/profile.dart index 9ecfef753..192f3449f 100644 --- a/lib/pages/settings/subpages/general/tabs/profile.dart +++ b/lib/pages/settings/subpages/general/tabs/profile.dart @@ -76,7 +76,7 @@ class _State extends State { onTap: () async { List _values = await SystemDialogs.showAddProfilePrompt(context); if(_values[0]) { - if(_profiles.contains(_values[1])) { + if(await Profiles.profileExists(_values[1])) { Notifications.showSnackBar(_scaffoldKey, 'Unable to add profile: Name already exists'); } else { _enabled = _values[1]; @@ -91,6 +91,36 @@ class _State extends State { margin: Elements.getCardMargin(), elevation: 4.0, ), + Card( + child: ListTile( + title: Elements.getTitle('Rename'), + subtitle: Elements.getSubtitle('Rename a profile'), + trailing: IconButton( + icon: Elements.getIcon(Icons.text_format), + onPressed: null, + ), + onTap: () async { + List _values = await SystemDialogs.showRenameProfilePrompt(context, _profiles); + if(_values[0]) { + String oldName = _values[1]; + _values = await SystemDialogs.showRenameProfileFieldPrompt(context); + if(_values[0]) { + String newName = _values[1]; + if(await Profiles.profileExists(newName)) { + Notifications.showSnackBar(_scaffoldKey, 'Unable to rename profile: Name already exists'); + } else { + await Profiles.renameProfile(oldName, newName); + await Configuration.pullAndSanitizeValues(); + _refreshData(); + Notifications.showSnackBar(_scaffoldKey, '"$oldName" has been renamed to "$newName"'); + } + } + } + } + ), + margin: Elements.getCardMargin(), + elevation: 4.0, + ), Card( child: ListTile( title: Elements.getTitle('Delete'), diff --git a/lib/system/ui/dialogs/system.dart b/lib/system/ui/dialogs/system.dart index a49ccf38f..23347fe70 100644 --- a/lib/system/ui/dialogs/system.dart +++ b/lib/system/ui/dialogs/system.dart @@ -673,6 +673,152 @@ class SystemDialogs { return [flag, profile]; } + static Future> showRenameProfilePrompt(BuildContext context, List profiles) async { + bool flag = false; + String profile = ''; + await showDialog( + context: context, + builder: (BuildContext context) { + return AlertDialog( + title: Text( + 'Rename Profile', + textAlign: TextAlign.center, + style: TextStyle( + fontWeight: FontWeight.bold, + ), + ), + actions: [ + FlatButton( + child: Text( + 'Cancel', + style: TextStyle( + color: Color(Constants.ACCENT_COLOR), + ), + ), + onPressed: () { + Navigator.of(context).pop(); + } + ), + ], + content: Container( + child: ListView.builder( + shrinkWrap: true, + itemCount: profiles.length, + itemBuilder: (BuildContext context, int index) { + return ListTile( + leading: Icon( + Icons.settings, + color: Constants.LIST_COLOUR_ICONS[index%Constants.LIST_COLOUR_ICONS.length], + ), + title: Text( + profiles[index], + style: TextStyle( + color: Colors.white, + ), + ), + onTap: () async { + profile = profiles[index]; + flag = true; + Navigator.of(context).pop(); + }, + contentPadding: EdgeInsets.fromLTRB(32.0, 0.0, 0.0, 0.0), + ); + }, + ), + width: 400, + ), + contentPadding: EdgeInsets.fromLTRB(0.0, 16.0, 0.0, 0.0), + ); + }, + ); + return [flag, profile]; + } + + static Future> showRenameProfileFieldPrompt(BuildContext context) async { + final profileController = TextEditingController(); + bool flag = false; + String profile = ''; + await showDialog( + context: context, + builder: (BuildContext context) { + return AlertDialog( + title: Text( + 'Rename Profile', + textAlign: TextAlign.center, + style: TextStyle( + fontWeight: FontWeight.bold, + ), + ), + actions: [ + FlatButton( + child: Text( + 'Cancel', + style: TextStyle( + color: Colors.white, + ), + ), + onPressed: () { + Navigator.of(context).pop(); + }, + ), + FlatButton( + child: Text( + 'Rename', + style: TextStyle( + color: Color(Constants.ACCENT_COLOR), + ), + ), + onPressed: () async { + flag = true; + profile = profileController.text; + Navigator.of(context).pop(); + }, + ), + ], + content: SingleChildScrollView( + child: ListBody( + children: [ + TextField( + autofocus: true, + autocorrect: false, + controller: profileController, + decoration: InputDecoration( + labelText: 'New Profile Name', + labelStyle: TextStyle( + color: Colors.white54, + decoration: TextDecoration.none, + ), + focusedBorder: UnderlineInputBorder( + borderSide: BorderSide( + color: Color(Constants.ACCENT_COLOR), + ), + ), + enabledBorder: UnderlineInputBorder( + borderSide: BorderSide( + color: Color(Constants.ACCENT_COLOR), + ), + ) + ), + style: TextStyle( + color: Colors.white, + ), + cursorColor: Color(Constants.ACCENT_COLOR), + textInputAction: TextInputAction.done, + onSubmitted: (_) { + flag = true; + profile = profileController.text; + Navigator.of(context).pop(); + }, + ), + ], + ), + ), + ); + } + ); + return [flag, profile]; + } + static Future> showAddProfilePrompt(BuildContext context) async { final profileController = TextEditingController(); bool flag = false;