use lifecycleScope

Signed-off-by: sowjanyakch <sowjanya.kch@gmail.com>
This commit is contained in:
sowjanyakch 2024-11-12 21:56:00 +01:00 committed by Marcel Hibbe
parent 240bdd6771
commit 4439da9bd1
No known key found for this signature in database
GPG key ID: C793F8B59F43CE7B

View file

@ -36,6 +36,7 @@ import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat import androidx.core.content.res.ResourcesCompat
import androidx.core.view.ViewCompat import androidx.core.view.ViewCompat
import androidx.lifecycle.lifecycleScope
import androidx.work.OneTimeWorkRequest import androidx.work.OneTimeWorkRequest
import androidx.work.WorkInfo import androidx.work.WorkInfo
import androidx.work.WorkManager import androidx.work.WorkManager
@ -89,6 +90,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody import okhttp3.RequestBody.Companion.toRequestBody
import retrofit2.HttpException import retrofit2.HttpException
@ -1278,29 +1280,33 @@ class SettingsActivity : BaseActivity(), SetPhoneNumberDialogFragment.SetPhoneNu
} }
private fun observeReadPrivacy() { private fun observeReadPrivacy() {
CoroutineScope(Dispatchers.Main).launch { lifecycleScope.launch {
var state = appPreferences.readPrivacy var state = appPreferences.readPrivacy
readPrivacyFlow.collect { newBoolean -> readPrivacyFlow.collect { newBoolean ->
if (state != newBoolean) { if (state != newBoolean) {
state = newBoolean state = newBoolean
val booleanValue = if (newBoolean) "0" else "1" val booleanValue = if (newBoolean) "0" else "1"
val json = "{\"key\": \"read_status_privacy\", \"value\" : $booleanValue}" val json = "{\"key\": \"read_status_privacy\", \"value\" : $booleanValue}"
try { withContext(Dispatchers.IO) {
credentials?.let { credentials -> try {
ncApiCoroutines.setReadStatusPrivacy( credentials?.let { credentials ->
credentials, ncApiCoroutines.setReadStatusPrivacy(
ApiUtils.getUrlForUserSettings(currentUser!!.baseUrl!!), credentials,
json.toRequestBody("application/json".toMediaTypeOrNull()) ApiUtils.getUrlForUserSettings(currentUser!!.baseUrl!!),
) json.toRequestBody("application/json".toMediaTypeOrNull())
Log.i(TAG, "reading status set") )
} Log.i(TAG, "reading status set")
} catch (e: Exception) { }
appPreferences.setReadPrivacy(!newBoolean) } catch (e: Exception) {
binding.settingsReadPrivacySwitch.isChecked = !newBoolean withContext(Dispatchers.Main) {
if (e is HttpException && e.code() == HTTP_ERROR_CODE_BAD_REQUEST) { appPreferences.setReadPrivacy(!newBoolean)
Log.e(TAG, "read_status_privacy : Key or value is invalid") binding.settingsReadPrivacySwitch.isChecked = !newBoolean
} else { }
Log.e(TAG, "Error updating read status", e) 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)
}
} }
} }
} }
@ -1309,31 +1315,36 @@ class SettingsActivity : BaseActivity(), SetPhoneNumberDialogFragment.SetPhoneNu
} }
private fun observeTypingStatus() { private fun observeTypingStatus() {
CoroutineScope(Dispatchers.Main).launch { lifecycleScope.launch {
var state = appPreferences.typingStatus var state = appPreferences.typingStatus
typingStatusFlow.collect { newBoolean -> typingStatusFlow.collect { newBoolean ->
if (state != newBoolean) { if (state != newBoolean) {
state = newBoolean state = newBoolean
val booleanValue = if (newBoolean) "0" else "1" val booleanValue = if (newBoolean) "0" else "1"
val json = "{\"key\": \"typing_privacy\", \"value\" : $booleanValue}" val json = "{\"key\": \"typing_privacy\", \"value\" : $booleanValue}"
withContext(Dispatchers.IO) {
try { try {
credentials?.let { credentials -> credentials?.let { credentials ->
ncApiCoroutines.setTypingStatusPrivacy( ncApiCoroutines.setTypingStatusPrivacy(
credentials, credentials,
ApiUtils.getUrlForUserSettings(currentUser!!.baseUrl!!), ApiUtils.getUrlForUserSettings(currentUser!!.baseUrl!!),
json.toRequestBody("application/json".toMediaTypeOrNull()) json.toRequestBody("application/json".toMediaTypeOrNull())
) )
loadCapabilitiesAndUpdateSettings() }
Log.i(TAG, "typing status set") withContext(Dispatchers.Main) {
} loadCapabilitiesAndUpdateSettings()
} catch (e: Exception) { Log.i(TAG, "typing status set")
appPreferences.typingStatus = !newBoolean }
binding.settingsTypingStatusSwitch.isChecked = !newBoolean } catch (e: Exception) {
if (e is HttpException && e.code() == HTTP_ERROR_CODE_BAD_REQUEST) { withContext(Dispatchers.Main) {
Log.e(TAG, "typing_privacy : Key or value is invalid") appPreferences.typingStatus = !newBoolean
} else { binding.settingsTypingStatusSwitch.isChecked = !newBoolean
Log.e(TAG, "Error updating typing status", e) }
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)
}
} }
} }
} }