Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Profiles] Ability to rename a profile #103

Merged
merged 1 commit into from
Jan 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions lib/configuration/profiles.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ class Profiles {
profileList = profiles;
}

static Future<bool> profileExists(String name) async {
final prefs = await SharedPreferences.getInstance();
profileList = prefs.getStringList('profiles');
if(profileList.contains(name)) return true;
return false;
}

static Future<void> createProfile(String name) async {
final prefs = await SharedPreferences.getInstance();
profileList = prefs.getStringList('profiles');
Expand All @@ -37,6 +44,31 @@ class Profiles {
prefs.remove('${name}_nzbget');
}

static Future<void> 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<String> _sonarr = prefs.getStringList('${oldName}_sonarr');
List<String> _radarr = prefs.getStringList('${oldName}_radarr');
List<String> _lidarr = prefs.getStringList('${oldName}_lidarr');
prefs.setStringList('${newName}_sonarr', _sonarr);
prefs.setStringList('${newName}_radarr', _radarr);
prefs.setStringList('${newName}_lidarr', _lidarr);
//Set Clients
List<String> _nzbget = prefs.getStringList('${oldName}_nzbget');
List<String> _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<void> setProfile(String name) async {
final prefs = await SharedPreferences.getInstance();
prefs.setString('enabled_profile', name);
Expand Down
32 changes: 31 additions & 1 deletion lib/pages/settings/subpages/general/tabs/profile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class _State extends State<Profile> {
onTap: () async {
List<dynamic> _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];
Expand All @@ -91,6 +91,36 @@ class _State extends State<Profile> {
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<dynamic> _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'),
Expand Down
146 changes: 146 additions & 0 deletions lib/system/ui/dialogs/system.dart
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,152 @@ class SystemDialogs {
return [flag, profile];
}

static Future<List<dynamic>> showRenameProfilePrompt(BuildContext context, List<String> 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: <Widget>[
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<List<dynamic>> 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: <Widget>[
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: <Widget>[
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<List<dynamic>> showAddProfilePrompt(BuildContext context) async {
final profileController = TextEditingController();
bool flag = false;
Expand Down