make worker listenable

Signed-off-by: sowjanyakch <sowjanya.kch@gmail.com>
This commit is contained in:
sowjanyakch 2024-10-27 16:59:13 +01:00 committed by Marcel Hibbe
parent aa5fd42c59
commit a07a5446be
No known key found for this signature in database
GPG key ID: C793F8B59F43CE7B
2 changed files with 21 additions and 28 deletions

View file

@ -643,7 +643,6 @@ class ConversationInfoActivity :
.setInputData(data) .setInputData(data)
.build() .build()
WorkManager.getInstance(context) WorkManager.getInstance(context)
.enqueueUniqueWork( .enqueueUniqueWork(
"leave_conversation_work", "leave_conversation_work",
@ -651,13 +650,11 @@ class ConversationInfoActivity :
workRequest workRequest
) )
WorkManager.getInstance(context).getWorkInfoByIdLiveData(workRequest.id) WorkManager.getInstance(context).getWorkInfoByIdLiveData(workRequest.id)
.observe(this, { workInfo: WorkInfo? -> .observe(this, { workInfo: WorkInfo? ->
if (workInfo != null) { if (workInfo != null) {
when (workInfo.state) { when (workInfo.state) {
WorkInfo.State.SUCCEEDED -> { WorkInfo.State.SUCCEEDED -> {
val intent = Intent(context, MainActivity::class.java) val intent = Intent(context, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent) startActivity(intent)

View file

@ -7,11 +7,14 @@
*/ */
package com.nextcloud.talk.jobs package com.nextcloud.talk.jobs
import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.util.Log import android.util.Log
import androidx.work.Worker import androidx.work.ListenableWorker
import androidx.work.WorkerParameters import androidx.work.WorkerParameters
import androidx.work.impl.utils.futures.SettableFuture
import autodagger.AutoInjector import autodagger.AutoInjector
import com.google.common.util.concurrent.ListenableFuture
import com.nextcloud.talk.api.NcApi import com.nextcloud.talk.api.NcApi
import com.nextcloud.talk.application.NextcloudTalkApplication import com.nextcloud.talk.application.NextcloudTalkApplication
import com.nextcloud.talk.models.json.generic.GenericOverall import com.nextcloud.talk.models.json.generic.GenericOverall
@ -22,13 +25,15 @@ import com.nextcloud.talk.utils.ApiUtils.getCredentials
import com.nextcloud.talk.utils.ApiUtils.getUrlForParticipantsSelf import com.nextcloud.talk.utils.ApiUtils.getUrlForParticipantsSelf
import com.nextcloud.talk.utils.bundle.BundleKeys import com.nextcloud.talk.utils.bundle.BundleKeys
import io.reactivex.Observer import io.reactivex.Observer
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 javax.inject.Inject import javax.inject.Inject
@SuppressLint("RestrictedApi")
@AutoInjector(NextcloudTalkApplication::class) @AutoInjector(NextcloudTalkApplication::class)
class LeaveConversationWorker(val context: Context, workerParams: WorkerParameters) : class LeaveConversationWorker(context: Context, workerParams: WorkerParameters) :
Worker(context, workerParams) { ListenableWorker(context, workerParams) {
@Inject @Inject
lateinit var ncApi: NcApi lateinit var ncApi: NcApi
@ -36,55 +41,46 @@ class LeaveConversationWorker(val context: Context, workerParams: WorkerParamete
@Inject @Inject
lateinit var userManager: UserManager lateinit var userManager: UserManager
private val result = SettableFuture.create<Result>()
override fun doWork(): Result { override fun startWork(): ListenableFuture<Result> {
NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this) NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this)
val data = inputData val conversationToken = inputData.getString(BundleKeys.KEY_ROOM_TOKEN)
val conversationToken = data.getString(BundleKeys.KEY_ROOM_TOKEN)
val currentUser = userManager.currentUser.blockingGet() val currentUser = userManager.currentUser.blockingGet()
lateinit var workResult:Result
if (currentUser != null) { if (currentUser != null && conversationToken != null) {
val credentials = getCredentials(currentUser.username, currentUser.token) val credentials = getCredentials(currentUser.username, currentUser.token)
val apiVersion = getConversationApiVersion(currentUser, intArrayOf(ApiUtils.API_V4, 1)) val apiVersion = getConversationApiVersion(currentUser, intArrayOf(ApiUtils.API_V4, 1))
ncApi.removeSelfFromRoom( ncApi.removeSelfFromRoom(
credentials, getUrlForParticipantsSelf( credentials, getUrlForParticipantsSelf(apiVersion, currentUser.baseUrl, conversationToken)
apiVersion,
currentUser.baseUrl,
conversationToken
)
) )
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : Observer<GenericOverall?> { .subscribe(object : Observer<GenericOverall?> {
override fun onSubscribe(d: Disposable) { override fun onSubscribe(d: Disposable) {
} }
override fun onNext(p0: GenericOverall) { override fun onNext(p0: GenericOverall) {
} }
override fun onError(e: Throwable) { override fun onError(e: Throwable) {
Log.e(TAG, "failed to remove self from room", e) Log.e(TAG, "Failed to remove self from room", e)
if (e.message?.contains("HTTP 400") == true) { if (e.message?.contains("HTTP 400") == true) {
workResult = Result.failure() result.set(Result.failure())
} }
} }
override fun onComplete() { override fun onComplete() {
workResult = Result.success() result.set(Result.success())
} }
}) })
} else { } else {
result.set(Result.failure())
workResult = Result.failure()
} }
return workResult return result
} }
companion object { companion object {