diff --git a/lib/user_profile.dart b/lib/user_profile.dart index 2d4a474c..9eff1edf 100644 --- a/lib/user_profile.dart +++ b/lib/user_profile.dart @@ -106,13 +106,6 @@ class UserProfileDBManager { return true; } - /* - * Mark the particular profile as selected - */ - Future selectProfile(int key) async { - await store.record("selected").put(await _db, key); - } - /* * Update the selected profile in the database. * The unique integer is used to determine if the profile already exists. @@ -196,4 +189,32 @@ class UserProfileDBManager { return profileList; } + + /* + * Mark the particular profile as selected + */ + Future selectProfile(int key) async { + await store.record("selected").put(await _db, key); + } + + /* + * Look-up and select a profile by name. + * Return true if the profile was selected + */ + Future selectProfileByName(String name) async { + var profiles = await getAllProfiles(); + + for (var prf in profiles) { + if (prf.name == name) { + int key = prf.key ?? -1; + + if (key >= 0) { + await selectProfile(key); + return true; + } + } + } + + return false; + } } diff --git a/test/api_test.dart b/test/api_test.dart index 4553d338..2ed41619 100644 --- a/test/api_test.dart +++ b/test/api_test.dart @@ -41,6 +41,10 @@ void main() { await UserProfileDBManager().updateProfile(prf); } + // Ensure the profile is selected + assert(! await UserProfileDBManager().selectProfileByName("Missing Profile")); + assert(await UserProfileDBManager().selectProfileByName("Test Profile")); + }); group("Login Tests:", () {