Skip to content

Commit

Permalink
Rework the Settings updates
Browse files Browse the repository at this point in the history
  • Loading branch information
GravityDarkLab committed Jan 22, 2024
1 parent 007ac17 commit da916a2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 30 deletions.
71 changes: 43 additions & 28 deletions lib/base/networking/api/handler/settings_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,46 @@ class SettingsHandler {
}
}

Future<bool> 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<bool> 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<double> parsePlaybackSpeeds(List<UserSetting>? userSettings) {
final playbackSpeedSetting = userSettings?.firstWhere(
Expand All @@ -89,30 +129,6 @@ class SettingsHandler {
return [];
}

/// Updates the preferred greeting in user settings.
Future<bool> updatePreferredGreeting(
String newGreeting,
List<UserSetting> 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<bool> updatePreferredName(
String newName,
Expand Down Expand Up @@ -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]);
}


}
4 changes: 2 additions & 2 deletions lib/view_models/setting_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class SettingViewModel extends StateNotifier<SettingState> {
Future<void> updatePreferredGreeting(String newGreeting) async {
try {
await SettingsHandler(_grpcHandler)
.updatePreferredGreeting(newGreeting, state.userSettings ?? []);
.updateGreeting(newGreeting);
await fetchUserSettings();
} catch (e) {
_logger.e('Error updating greeting: $e');
Expand All @@ -99,7 +99,7 @@ class SettingViewModel extends StateNotifier<SettingState> {
Future<bool> updatePreferredName(String newName) async {
try {
await SettingsHandler(_grpcHandler)
.updatePreferredName(newName, state.userSettings ?? []);
.updateName(newName);
await fetchUserSettings();
return true;
} catch (e) {
Expand Down

0 comments on commit da916a2

Please sign in to comment.