From a07a5446befc4b9ba4b0a9973450c82dc1fd1991 Mon Sep 17 00:00:00 2001 From: sowjanyakch Date: Sun, 27 Oct 2024 16:59:13 +0100 Subject: [PATCH] make worker listenable Signed-off-by: sowjanyakch --- .../ConversationInfoActivity.kt | 3 -- .../talk/jobs/LeaveConversationWorker.kt | 46 +++++++++---------- 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/conversationinfo/ConversationInfoActivity.kt b/app/src/main/java/com/nextcloud/talk/conversationinfo/ConversationInfoActivity.kt index 1f3d64671..ca39bb072 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationinfo/ConversationInfoActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationinfo/ConversationInfoActivity.kt @@ -643,7 +643,6 @@ class ConversationInfoActivity : .setInputData(data) .build() - WorkManager.getInstance(context) .enqueueUniqueWork( "leave_conversation_work", @@ -651,13 +650,11 @@ class ConversationInfoActivity : workRequest ) - WorkManager.getInstance(context).getWorkInfoByIdLiveData(workRequest.id) .observe(this, { workInfo: WorkInfo? -> if (workInfo != null) { when (workInfo.state) { WorkInfo.State.SUCCEEDED -> { - val intent = Intent(context, MainActivity::class.java) intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) startActivity(intent) diff --git a/app/src/main/java/com/nextcloud/talk/jobs/LeaveConversationWorker.kt b/app/src/main/java/com/nextcloud/talk/jobs/LeaveConversationWorker.kt index 801efbea2..7e9f63b5e 100644 --- a/app/src/main/java/com/nextcloud/talk/jobs/LeaveConversationWorker.kt +++ b/app/src/main/java/com/nextcloud/talk/jobs/LeaveConversationWorker.kt @@ -7,11 +7,14 @@ */ package com.nextcloud.talk.jobs +import android.annotation.SuppressLint import android.content.Context import android.util.Log -import androidx.work.Worker +import androidx.work.ListenableWorker import androidx.work.WorkerParameters +import androidx.work.impl.utils.futures.SettableFuture import autodagger.AutoInjector +import com.google.common.util.concurrent.ListenableFuture import com.nextcloud.talk.api.NcApi import com.nextcloud.talk.application.NextcloudTalkApplication 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.bundle.BundleKeys import io.reactivex.Observer +import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.Disposable import io.reactivex.schedulers.Schedulers import javax.inject.Inject +@SuppressLint("RestrictedApi") @AutoInjector(NextcloudTalkApplication::class) -class LeaveConversationWorker(val context: Context, workerParams: WorkerParameters) : - Worker(context, workerParams) { +class LeaveConversationWorker(context: Context, workerParams: WorkerParameters) : + ListenableWorker(context, workerParams) { @Inject lateinit var ncApi: NcApi @@ -36,55 +41,46 @@ class LeaveConversationWorker(val context: Context, workerParams: WorkerParamete @Inject lateinit var userManager: UserManager + private val result = SettableFuture.create() - override fun doWork(): Result { + override fun startWork(): ListenableFuture { NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this) - val data = inputData - val conversationToken = data.getString(BundleKeys.KEY_ROOM_TOKEN) + val conversationToken = inputData.getString(BundleKeys.KEY_ROOM_TOKEN) 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 apiVersion = getConversationApiVersion(currentUser, intArrayOf(ApiUtils.API_V4, 1)) ncApi.removeSelfFromRoom( - credentials, getUrlForParticipantsSelf( - apiVersion, - currentUser.baseUrl, - conversationToken - ) + credentials, getUrlForParticipantsSelf(apiVersion, currentUser.baseUrl, conversationToken) ) .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) .subscribe(object : Observer { - override fun onSubscribe(d: Disposable) { } override fun onNext(p0: GenericOverall) { - } override fun onError(e: Throwable) { - Log.e(TAG, "failed to remove self from room", e) - if(e.message?.contains("HTTP 400") == true){ - workResult = Result.failure() + Log.e(TAG, "Failed to remove self from room", e) + if (e.message?.contains("HTTP 400") == true) { + result.set(Result.failure()) } } override fun onComplete() { - workResult = Result.success() - + result.set(Result.success()) } }) - }else{ - - workResult = Result.failure() + } else { + result.set(Result.failure()) } - return workResult + return result } companion object {