convert rxjava to coroutine - favourite conversation

Signed-off-by: sowjanyakch <sowjanya.kch@gmail.com>
This commit is contained in:
sowjanyakch 2024-11-15 19:27:00 +01:00
parent eb9eb99ca3
commit 262f5b1cf4
No known key found for this signature in database
GPG key ID: F7AA2A8B65B50220
2 changed files with 30 additions and 31 deletions

View file

@ -116,4 +116,10 @@ interface NcApiCoroutines {
@DELETE @DELETE
suspend fun unarchiveConversation(@Header("Authorization") authorization: String, @Url url: String): GenericOverall suspend fun unarchiveConversation(@Header("Authorization") authorization: String, @Url url: String): GenericOverall
@POST
suspend fun addConversationToFavorites(
@Header("Authorization") authorization: String,
@Url url: String
): GenericOverall
} }

View file

@ -11,6 +11,7 @@ import android.os.Bundle
import android.text.TextUtils import android.text.TextUtils
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.lifecycle.lifecycleScope
import androidx.work.Data import androidx.work.Data
import androidx.work.OneTimeWorkRequest import androidx.work.OneTimeWorkRequest
import androidx.work.WorkInfo import androidx.work.WorkInfo
@ -21,6 +22,7 @@ import com.google.android.material.bottomsheet.BottomSheetDialog
import com.nextcloud.talk.R import com.nextcloud.talk.R
import com.nextcloud.talk.activities.MainActivity import com.nextcloud.talk.activities.MainActivity
import com.nextcloud.talk.api.NcApi import com.nextcloud.talk.api.NcApi
import com.nextcloud.talk.api.NcApiCoroutines
import com.nextcloud.talk.application.NextcloudTalkApplication import com.nextcloud.talk.application.NextcloudTalkApplication
import com.nextcloud.talk.conversation.RenameConversationDialogFragment import com.nextcloud.talk.conversation.RenameConversationDialogFragment
import com.nextcloud.talk.conversationlist.ConversationsListActivity import com.nextcloud.talk.conversationlist.ConversationsListActivity
@ -42,6 +44,9 @@ import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject import javax.inject.Inject
@AutoInjector(NextcloudTalkApplication::class) @AutoInjector(NextcloudTalkApplication::class)
@ -56,6 +61,9 @@ class ConversationsListBottomDialog(
@Inject @Inject
lateinit var ncApi: NcApi lateinit var ncApi: NcApi
@Inject
lateinit var ncApiCoroutines: NcApiCoroutines
@Inject @Inject
lateinit var viewThemeUtils: ViewThemeUtils lateinit var viewThemeUtils: ViewThemeUtils
@ -195,42 +203,27 @@ class ConversationsListBottomDialog(
private fun addConversationToFavorites() { private fun addConversationToFavorites() {
val apiVersion = ApiUtils.getConversationApiVersion(currentUser, intArrayOf(ApiUtils.API_V4, ApiUtils.API_V1)) val apiVersion = ApiUtils.getConversationApiVersion(currentUser, intArrayOf(ApiUtils.API_V4, ApiUtils.API_V1))
ncApi.addConversationToFavorites( val url = ApiUtils.getUrlForRoomFavorite(apiVersion, currentUser.baseUrl!!, conversation.token)
credentials, lifecycleScope.launch {
ApiUtils.getUrlForRoomFavorite( try {
apiVersion, withContext(Dispatchers.IO) {
currentUser.baseUrl!!, ncApiCoroutines.addConversationToFavorites(credentials, url)
conversation.token
)
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.retry(1)
.subscribe(object : Observer<GenericOverall> {
override fun onSubscribe(d: Disposable) {
// unused atm
} }
activity.fetchRooms()
override fun onNext(genericOverall: GenericOverall) { activity.showSnackbar(
activity.fetchRooms() String.format(
activity.showSnackbar( context.resources.getString(R.string.added_to_favorites),
String.format( conversation.displayName
context.resources.getString(R.string.added_to_favorites),
conversation.displayName
)
) )
dismiss() )
} dismiss()
} catch (e: Exception) {
override fun onError(e: Throwable) { withContext(Dispatchers.Main) {
activity.showSnackbar(context.resources.getString(R.string.nc_common_error_sorry)) activity.showSnackbar(context.resources.getString(R.string.nc_common_error_sorry))
dismiss() dismiss()
} }
}
override fun onComplete() { }
// unused atm
}
})
} }
private fun removeConversationFromFavorites() { private fun removeConversationFromFavorites() {