diff --git a/lib/base/networking/api/handler/settings_handler.dart b/lib/base/networking/api/handler/settings_handler.dart index 10b592de..cd58d04d 100644 --- a/lib/base/networking/api/handler/settings_handler.dart +++ b/lib/base/networking/api/handler/settings_handler.dart @@ -67,6 +67,46 @@ class SettingsHandler { } } + Future updateName(String newName) async { + try { + _logger.i('Updating user settings...'); + final request = PatchUserSettingsRequest() + ..userSettings.add(UserSetting(type: UserSettingType.PREFERRED_NAME, value: newName)); + + await _grpcHandler.callGrpcMethod( + (client) async { + await client.patchUserSettings(request); + _logger.i('User settings updated successfully'); + }, + ); + return true; + } catch (e) { + _logger.e('Error updating user settings: $e'); + rethrow; + } + } + + Future updateGreeting(String newGreeting) async { + try { + _logger.i('Updating user settings...'); + final request = PatchUserSettingsRequest() + ..userSettings.add(UserSetting(type: UserSettingType.GREETING, value: newGreeting)); + + await _grpcHandler.callGrpcMethod( + (client) async { + await client.patchUserSettings(request); + _logger.i('User settings updated successfully'); + }, + ); + return true; + } catch (e) { + _logger.e('Error updating user settings: $e'); + rethrow; + } + } + + + /// Parses playback speeds from the user settings. List parsePlaybackSpeeds(List? userSettings) { final playbackSpeedSetting = userSettings?.firstWhere( @@ -89,30 +129,6 @@ class SettingsHandler { return []; } - /// Updates the preferred greeting in user settings. - Future updatePreferredGreeting( - String newGreeting, - List currentSettings, - ) async { - try { - var greetingSetting = currentSettings.firstWhere( - (setting) => setting.type == UserSettingType.GREETING, - orElse: () => - UserSetting(type: UserSettingType.GREETING, value: newGreeting), - ); - greetingSetting.value = newGreeting; - - if (!currentSettings.contains(greetingSetting)) { - currentSettings.add(greetingSetting); - } - await updateUserSettings(currentSettings); - return true; - } catch (e) { - _logger.e('Error updating greeting: $e'); - return false; - } - } - /// Updates the preferred name in user settings. Future updatePreferredName( String newName, @@ -152,9 +168,8 @@ class SettingsHandler { playbackSpeedSetting.value = jsonEncode( speeds.map((s) => {"speed": s, "enabled": true}).toList(), ); - if (!currentSettings.contains(playbackSpeedSetting)) { - currentSettings.add(playbackSpeedSetting); - } - await updateUserSettings(currentSettings); + await updateUserSettings([playbackSpeedSetting]); } + + } diff --git a/lib/view_models/setting_view_model.dart b/lib/view_models/setting_view_model.dart index 82574ea2..cb47b7bf 100644 --- a/lib/view_models/setting_view_model.dart +++ b/lib/view_models/setting_view_model.dart @@ -89,7 +89,7 @@ class SettingViewModel extends StateNotifier { Future updatePreferredGreeting(String newGreeting) async { try { await SettingsHandler(_grpcHandler) - .updatePreferredGreeting(newGreeting, state.userSettings ?? []); + .updateGreeting(newGreeting); await fetchUserSettings(); } catch (e) { _logger.e('Error updating greeting: $e'); @@ -99,7 +99,7 @@ class SettingViewModel extends StateNotifier { Future updatePreferredName(String newName) async { try { await SettingsHandler(_grpcHandler) - .updatePreferredName(newName, state.userSettings ?? []); + .updateName(newName); await fetchUserSettings(); return true; } catch (e) {