Convert LeaveConversationWorker from java to Kotlin

Signed-off-by: sowjanyakch <sowjanya.kch@gmail.com>
This commit is contained in:
sowjanyakch 2024-10-15 22:53:24 +02:00 committed by Marcel Hibbe
parent be55a908d5
commit f6824f94f1
No known key found for this signature in database
GPG key ID: C793F8B59F43CE7B

View file

@ -5,111 +5,90 @@
* SPDX-FileCopyrightText: 2017-2018 Mario Danic <mario@lovelyhq.com> * SPDX-FileCopyrightText: 2017-2018 Mario Danic <mario@lovelyhq.com>
* SPDX-License-Identifier: GPL-3.0-or-later * SPDX-License-Identifier: GPL-3.0-or-later
*/ */
package com.nextcloud.talk.jobs; package com.nextcloud.talk.jobs
import android.content.Context; import android.content.Context
import android.util.Log; import android.util.Log
import androidx.work.Worker
import androidx.work.WorkerParameters
import autodagger.AutoInjector
import com.nextcloud.talk.api.NcApi
import com.nextcloud.talk.application.NextcloudTalkApplication
import com.nextcloud.talk.application.NextcloudTalkApplication.Companion.sharedApplication
import com.nextcloud.talk.events.EventStatus
import com.nextcloud.talk.models.json.generic.GenericOverall
import com.nextcloud.talk.users.UserManager
import com.nextcloud.talk.utils.ApiUtils
import com.nextcloud.talk.utils.ApiUtils.getConversationApiVersion
import com.nextcloud.talk.utils.ApiUtils.getCredentials
import com.nextcloud.talk.utils.ApiUtils.getUrlForParticipantsSelf
import com.nextcloud.talk.utils.UserIdUtils.getIdForUser
import com.nextcloud.talk.utils.bundle.BundleKeys
import io.reactivex.Observer
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import okhttp3.JavaNetCookieJar
import okhttp3.OkHttpClient
import org.greenrobot.eventbus.EventBus
import retrofit2.Retrofit
import java.net.CookieManager
import javax.inject.Inject
import com.nextcloud.talk.api.NcApi; @AutoInjector(NextcloudTalkApplication::class)
import com.nextcloud.talk.application.NextcloudTalkApplication; class LeaveConversationWorker(val context: Context, workerParams: WorkerParameters) :
import com.nextcloud.talk.data.user.model.User; Worker(context, workerParams) {
import com.nextcloud.talk.events.EventStatus;
import com.nextcloud.talk.models.json.generic.GenericOverall;
import com.nextcloud.talk.users.UserManager;
import com.nextcloud.talk.utils.ApiUtils;
import com.nextcloud.talk.utils.UserIdUtils;
import com.nextcloud.talk.utils.bundle.BundleKeys;
import org.greenrobot.eventbus.EventBus;
import java.net.CookieManager;
import javax.inject.Inject;
import androidx.annotation.NonNull;
import androidx.work.Data;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
import autodagger.AutoInjector;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.JavaNetCookieJar;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
@AutoInjector(NextcloudTalkApplication.class)
public class LeaveConversationWorker extends Worker {
private static final String TAG = "LeaveConversationWorker";
@Inject @Inject
Retrofit retrofit; lateinit var ncApi: NcApi
@Inject @Inject
OkHttpClient okHttpClient; lateinit var userManager: UserManager
@Inject
UserManager userManager;
@Inject override fun doWork(): Result {
EventBus eventBus; sharedApplication!!.componentApplication.inject(this)
val data = inputData
val conversationToken = data.getString(BundleKeys.KEY_ROOM_TOKEN)
val currentUser = userManager.currentUser.blockingGet()
NcApi ncApi; if (currentUser != null) {
val credentials = getCredentials(currentUser.username, currentUser.token)
public LeaveConversationWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { val apiVersion = getConversationApiVersion(currentUser, intArrayOf(ApiUtils.API_V4, 1))
super(context, workerParams);
NextcloudTalkApplication.Companion.getSharedApplication().getComponentApplication().inject(this);
}
@NonNull ncApi.removeSelfFromRoom(
@Override credentials, getUrlForParticipantsSelf(
public Result doWork() { apiVersion,
Data data = getInputData(); currentUser.baseUrl,
long operationUserId = data.getLong(BundleKeys.KEY_INTERNAL_USER_ID, -1); conversationToken
String conversationToken = data.getString(BundleKeys.KEY_ROOM_TOKEN); )
User operationUser = userManager.getUserWithId(operationUserId).blockingGet(); )
if (operationUser != null) {
String credentials = ApiUtils.getCredentials(operationUser.getUsername(), operationUser.getToken());
ncApi = retrofit.newBuilder().client(okHttpClient.newBuilder().cookieJar(new
JavaNetCookieJar(new CookieManager())).build()).build().create(NcApi.class);
EventStatus eventStatus = new EventStatus(UserIdUtils.INSTANCE.getIdForUser(operationUser),
EventStatus.EventType.CONVERSATION_UPDATE,
true);
int apiVersion = ApiUtils.getConversationApiVersion(operationUser, new int[] {ApiUtils.API_V4, 1});
ncApi.removeSelfFromRoom(credentials, ApiUtils.getUrlForParticipantsSelf(apiVersion,
operationUser.getBaseUrl(),
conversationToken))
.subscribeOn(Schedulers.io()) .subscribeOn(Schedulers.io())
.blockingSubscribe(new Observer<GenericOverall>() { .subscribe(object : Observer<GenericOverall?> {
Disposable disposable; var disposable: Disposable? = null
@Override override fun onSubscribe(d: Disposable) {
public void onSubscribe(Disposable d) { disposable = d
disposable = d;
} }
@Override override fun onNext(p0: GenericOverall) {
public void onNext(GenericOverall genericOverall) {
eventBus.postSticky(eventStatus);
} }
@Override override fun onError(e: Throwable) {
public void onError(Throwable e) { Log.e(TAG, "failed to remove self from room", e)
Log.e(TAG, "failed to remove self from room", e);
} }
@Override override fun onComplete() {
public void onComplete() { disposable!!.dispose()
disposable.dispose();
} }
}); })
} }
return Result.success(); return Result.success()
}
companion object {
private const val TAG = "LeaveConversationWorker"
} }
} }