From a7c103f40845b93e7115118d9b8451a8f8843f98 Mon Sep 17 00:00:00 2001 From: sowjanyakch Date: Wed, 13 Nov 2024 12:14:29 +0100 Subject: [PATCH] refactor code Signed-off-by: sowjanyakch --- .../talk/settings/SettingsActivity.kt | 87 ++++++++++--------- 1 file changed, 45 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/settings/SettingsActivity.kt b/app/src/main/java/com/nextcloud/talk/settings/SettingsActivity.kt index 0c2a62c658..c7ea56607f 100644 --- a/app/src/main/java/com/nextcloud/talk/settings/SettingsActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/settings/SettingsActivity.kt @@ -1280,31 +1280,33 @@ class SettingsActivity : BaseActivity(), SetPhoneNumberDialogFragment.SetPhoneNu } private fun observeReadPrivacy() { - lifecycleScope.launch(Dispatchers.IO) { + lifecycleScope.launch { var state = appPreferences.readPrivacy readPrivacyFlow.collect { newBoolean -> if (state != newBoolean) { state = newBoolean val booleanValue = if (newBoolean) "0" else "1" val json = "{\"key\": \"read_status_privacy\", \"value\" : $booleanValue}" - try { - credentials?.let { credentials -> - ncApiCoroutines.setReadStatusPrivacy( - credentials, - ApiUtils.getUrlForUserSettings(currentUser!!.baseUrl!!), - json.toRequestBody("application/json".toMediaTypeOrNull()) - ) - Log.i(TAG, "reading status set") - } - } catch (e: Exception) { - withContext(Dispatchers.Main) { - appPreferences.setReadPrivacy(!newBoolean) - binding.settingsReadPrivacySwitch.isChecked = !newBoolean - } - if (e is HttpException && e.code() == HTTP_ERROR_CODE_BAD_REQUEST) { - Log.e(TAG, "read_status_privacy : Key or value is invalid") - } else { - Log.e(TAG, "Error setting read status", e) + withContext(Dispatchers.IO) { + try { + credentials?.let { credentials -> + ncApiCoroutines.setReadStatusPrivacy( + credentials, + ApiUtils.getUrlForUserSettings(currentUser!!.baseUrl!!), + json.toRequestBody("application/json".toMediaTypeOrNull()) + ) + Log.i(TAG, "reading status set") + } + } catch (e: Exception) { + withContext(Dispatchers.Main) { + appPreferences.setReadPrivacy(!newBoolean) + binding.settingsReadPrivacySwitch.isChecked = !newBoolean + } + if (e is HttpException && e.code() == HTTP_ERROR_CODE_BAD_REQUEST) { + Log.e(TAG, "read_status_privacy : Key or value is invalid") + } else { + Log.e(TAG, "Error setting read status", e) + } } } } @@ -1313,35 +1315,36 @@ class SettingsActivity : BaseActivity(), SetPhoneNumberDialogFragment.SetPhoneNu } private fun observeTypingStatus() { - lifecycleScope.launch(Dispatchers.IO) { + lifecycleScope.launch { var state = appPreferences.typingStatus typingStatusFlow.collect { newBoolean -> if (state != newBoolean) { state = newBoolean val booleanValue = if (newBoolean) "0" else "1" val json = "{\"key\": \"typing_privacy\", \"value\" : $booleanValue}" - - try { - credentials?.let { credentials -> - ncApiCoroutines.setTypingStatusPrivacy( - credentials, - ApiUtils.getUrlForUserSettings(currentUser!!.baseUrl!!), - json.toRequestBody("application/json".toMediaTypeOrNull()) - ) - } - withContext(Dispatchers.Main) { - loadCapabilitiesAndUpdateSettings() - Log.i(TAG, "typing status set") - } - } catch (e: Exception) { - withContext(Dispatchers.Main) { - appPreferences.typingStatus = !newBoolean - binding.settingsTypingStatusSwitch.isChecked = !newBoolean - } - if (e is HttpException && e.code() == HTTP_ERROR_CODE_BAD_REQUEST) { - Log.e(TAG, "typing_privacy : Key or value is invalid") - } else { - Log.e(TAG, "Error setting typing status", e) + withContext(Dispatchers.IO) { + try { + credentials?.let { credentials -> + ncApiCoroutines.setTypingStatusPrivacy( + credentials, + ApiUtils.getUrlForUserSettings(currentUser!!.baseUrl!!), + json.toRequestBody("application/json".toMediaTypeOrNull()) + ) + } + withContext(Dispatchers.Main) { + loadCapabilitiesAndUpdateSettings() + Log.i(TAG, "typing status set") + } + } catch (e: Exception) { + withContext(Dispatchers.Main) { + appPreferences.typingStatus = !newBoolean + binding.settingsTypingStatusSwitch.isChecked = !newBoolean + } + if (e is HttpException && e.code() == HTTP_ERROR_CODE_BAD_REQUEST) { + Log.e(TAG, "typing_privacy : Key or value is invalid") + } else { + Log.e(TAG, "Error setting typing status", e) + } } } }