diff options
Diffstat (limited to 'lib/data/data_providers/users_notifier.dart')
-rw-r--r-- | lib/data/data_providers/users_notifier.dart | 127 |
1 files changed, 71 insertions, 56 deletions
diff --git a/lib/data/data_providers/users_notifier.dart b/lib/data/data_providers/users_notifier.dart index c2755f6..a4e056c 100644 --- a/lib/data/data_providers/users_notifier.dart +++ b/lib/data/data_providers/users_notifier.dart @@ -9,6 +9,12 @@ import 'initialize_settings.dart'; class UsersNotifier extends Notifier<Users> { @override + final List<User> _users = [ + const User(id: '1', name: 'Heather'), + const User(id: '2', name: 'George'), + const User(id: '3', name: 'Riley'), + ]; + Users build() { // Initialize default state. state = Users.initial(); @@ -16,52 +22,70 @@ class UsersNotifier extends Notifier<Users> { return state; } - Future <void> loadSettingsUsers() async { + Future<void> loadSettingsUsers() async { final storageClient = ref.read(storageClientProvider); try { // Access users branch. - final searchResponseUsers = await storageClient.search(storage_api.Key(key: UsersPath.InfotainmentUsers)); - // Add default users if no users are inside the storage API. + final searchResponseUsers = await storageClient + .search(storage_api.Key(key: UsersPath.InfotainmentUsers)); if (searchResponseUsers.result.isEmpty) { + // Add default users if no users are inside the storage API. + debugPrint("Adding default demo user profiles"); loadUsers(); - await storageClient.write(storage_api.KeyValue(key: '${UsersPath.InfotainmentUsers}.${_users[0].id}.id', value: _users[0].id)); - await storageClient.write(storage_api.KeyValue(key: '${UsersPath.InfotainmentUsers}.${_users[0].id}.name', value: _users[0].name)); - await storageClient.write(storage_api.KeyValue(key: '${UsersPath.InfotainmentUsers}.${_users[1].id}.id', value: _users[1].id)); - await storageClient.write(storage_api.KeyValue(key: '${UsersPath.InfotainmentUsers}.${_users[1].id}.name', value: _users[1].name)); - await storageClient.write(storage_api.KeyValue(key: '${UsersPath.InfotainmentUsers}.${_users[2].id}.id', value: _users[2].id)); - await storageClient.write(storage_api.KeyValue(key: '${UsersPath.InfotainmentUsers}.${_users[2].id}.name', value: _users[2].name)); + await storageClient.write(storage_api.KeyValue( + key: '${UsersPath.InfotainmentUsers}.${_users[0].id}.id', + value: _users[0].id)); + await storageClient.write(storage_api.KeyValue( + key: '${UsersPath.InfotainmentUsers}.${_users[0].id}.name', + value: _users[0].name)); + await storageClient.write(storage_api.KeyValue( + key: '${UsersPath.InfotainmentUsers}.${_users[1].id}.id', + value: _users[1].id)); + await storageClient.write(storage_api.KeyValue( + key: '${UsersPath.InfotainmentUsers}.${_users[1].id}.name', + value: _users[1].name)); + await storageClient.write(storage_api.KeyValue( + key: '${UsersPath.InfotainmentUsers}.${_users[2].id}.id', + value: _users[2].id)); + await storageClient.write(storage_api.KeyValue( + key: '${UsersPath.InfotainmentUsers}.${_users[2].id}.name', + value: _users[2].name)); await selectUser(_users[0].id); - } - else { + } else { List<User> users = []; List<String> idList = []; // Get list of all ids. for (var key in searchResponseUsers.result) { - var readResponse = await storageClient.read(storage_api.Key(key: key)); + var readResponse = + await storageClient.read(storage_api.Key(key: key)); if (key.contains('.id')) { idList.insert(0, readResponse.result); } } // Extract names corresponding to ids. for (var id in idList) { - var readResponse = await storageClient.read(storage_api.Key(key:'${UsersPath.InfotainmentUsers}.$id.name')); + var readResponse = await storageClient.read( + storage_api.Key(key: '${UsersPath.InfotainmentUsers}.$id.name')); users.insert(0, User(id: id, name: readResponse.result)); } // Extract id of selected user. - final readResponseSelectedUser = await storageClient.read(storage_api.Key(key: UsersPath.InfotainmentCurrentUser)); + final readResponseSelectedUser = await storageClient + .read(storage_api.Key(key: UsersPath.InfotainmentCurrentUser)); User selectedUser; final userCurrentId = readResponseSelectedUser.result; // Extract name of selected user. - final readResponseCurrentUserName = await storageClient.read(storage_api.Key(key: '${UsersPath.InfotainmentUsers}.$userCurrentId.name')); + final readResponseCurrentUserName = await storageClient.read( + storage_api.Key( + key: '${UsersPath.InfotainmentUsers}.$userCurrentId.name')); final userCurrentName = readResponseCurrentUserName.result; selectedUser = User(id: userCurrentId, name: userCurrentName); - state = Users(users: users, selectedUser: selectedUser); + state = Users(users: users, selectedUser: selectedUser); } } catch (e) { - // Fallback to initial defaults if error. - print('Error loading settings for units: $e'); - loadUsers(); - state = state.copyWith(selectedUser: _users[0]); + // Fallback to initial defaults if error. + debugPrint('Error loading users: $e'); + loadUsers(); + state = state.copyWith(selectedUser: _users[0]); } } @@ -69,16 +93,10 @@ class UsersNotifier extends Notifier<Users> { state = state.copyWith(users: _users); } - final List<User> _users = [ - const User(id: '1', name: 'Heather'), - const User(id: '2', name: 'George'), - const User(id: '3', name: 'Riley'), - ]; - - Future <void> selectUser(String userId) async { + Future<void> selectUser(String userId) async { final storageClient = ref.read(storageClientProvider); - var seletedUser = state.users.firstWhere((user) => user.id == userId); - state = state.copyWith(selectedUser: seletedUser); + var selectedUser = state.users.firstWhere((user) => user.id == userId); + state = state.copyWith(selectedUser: selectedUser); // Write to storage API. try { await storageClient.write(storage_api.KeyValue( @@ -86,68 +104,65 @@ class UsersNotifier extends Notifier<Users> { value: userId, )); } catch (e) { - print('Error saving user: $e'); + debugPrint('Error selecting user: $e'); } - + try { await initializeSettingsUser(ref); } catch (e) { - print('Error loading settings of user: $e'); + debugPrint('Error loading settings of user: $e'); } - } - Future <void> removeUser(String userId) async { + Future<void> removeUser(String userId) async { final storageClient = ref.read(storageClientProvider); var currentUserId = state.selectedUser.id; state.users.removeWhere((user) => user.id == userId); if (state.users.isNotEmpty && currentUserId == userId) { state = state.copyWith(selectedUser: state.users.first); - //Write to API to change selected user. - await storageClient.write(storage_api.KeyValue(key: UsersPath.InfotainmentCurrentUser, value: state.users.first.id)); + // Write to API to change selected user. + await storageClient.write(storage_api.KeyValue( + key: UsersPath.InfotainmentCurrentUser, value: state.users.first.id)); } if (state.users.isEmpty) { state = state.copyWith(selectedUser: const User(id: '0', name: '')); - //Write to API to change selected user. - await storageClient.write(storage_api.KeyValue(key: UsersPath.InfotainmentCurrentUser, value: '0')); + // Write to API to change selected user. + await storageClient.write(storage_api.KeyValue( + key: UsersPath.InfotainmentCurrentUser, value: '0')); } // Delete from storage API. try { - final searchResponse = await storageClient.search(storage_api.Key(key: userId)); + final searchResponse = + await storageClient.search(storage_api.Key(key: userId)); final keyList = searchResponse.result; - //Delete id, name entries of the user from the default namespace. + // Delete id, name entries of the user from the default namespace. for (final key in keyList) { - await storageClient.delete(storage_api.Key( - key: key - )); + await storageClient.delete(storage_api.Key(key: key)); } - //Delete all VSS keys from the user namespace. - await storageClient.deleteNodes(storage_api.Key(key: "Vehicle", namespace: userId)); + // Delete all VSS keys from the user namespace. + await storageClient + .deleteNodes(storage_api.Key(key: "Vehicle", namespace: userId)); } catch (e) { - print('Error removing user with id $userId: $e'); + debugPrint('Error removing user with id $userId: $e'); } } - Future <void> addUser(String userName) async { + Future<void> addUser(String userName) async { final storageClient = ref.read(storageClientProvider); final id = const Uuid().v1(); final user = User(id: id, name: userName); state.users.insert(0, user); - // New user is automaticaly selected. - await selectUser(user.id); + // New user is automatically selected. + await selectUser(user.id); // Write to storage API. try { await storageClient.write(storage_api.KeyValue( - key: '${UsersPath.InfotainmentUsers}.$id.name', - value: userName - )); + key: '${UsersPath.InfotainmentUsers}.$id.name', value: userName)); await storageClient.write(storage_api.KeyValue( - key: '${UsersPath.InfotainmentUsers}.$id.id', - value: id - )); + key: '${UsersPath.InfotainmentUsers}.$id.id', value: id)); } catch (e) { - print('Error adding user with id $id: $e'); + debugPrint('Error adding user with id $id: $e'); } } |