From be55a908d5bde434c223e15cc35e5aa4788bba89 Mon Sep 17 00:00:00 2001 From: sowjanyakch Date: Tue, 15 Oct 2024 22:53:24 +0200 Subject: [PATCH 01/11] Rename .java to .kt Signed-off-by: sowjanyakch --- .../{LeaveConversationWorker.java => LeaveConversationWorker.kt} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename app/src/main/java/com/nextcloud/talk/jobs/{LeaveConversationWorker.java => LeaveConversationWorker.kt} (100%) diff --git a/app/src/main/java/com/nextcloud/talk/jobs/LeaveConversationWorker.java b/app/src/main/java/com/nextcloud/talk/jobs/LeaveConversationWorker.kt similarity index 100% rename from app/src/main/java/com/nextcloud/talk/jobs/LeaveConversationWorker.java rename to app/src/main/java/com/nextcloud/talk/jobs/LeaveConversationWorker.kt From f6824f94f1b9d69873835338b6bf86e319acd399 Mon Sep 17 00:00:00 2001 From: sowjanyakch Date: Tue, 15 Oct 2024 22:53:24 +0200 Subject: [PATCH 02/11] Convert LeaveConversationWorker from java to Kotlin Signed-off-by: sowjanyakch --- .../talk/jobs/LeaveConversationWorker.kt | 161 ++++++++---------- 1 file changed, 70 insertions(+), 91 deletions(-) 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 aee52845e4..fcf4567e48 100644 --- a/app/src/main/java/com/nextcloud/talk/jobs/LeaveConversationWorker.kt +++ b/app/src/main/java/com/nextcloud/talk/jobs/LeaveConversationWorker.kt @@ -5,111 +5,90 @@ * SPDX-FileCopyrightText: 2017-2018 Mario Danic * SPDX-License-Identifier: GPL-3.0-or-later */ -package com.nextcloud.talk.jobs; +package com.nextcloud.talk.jobs + +import android.content.Context +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 + +@AutoInjector(NextcloudTalkApplication::class) +class LeaveConversationWorker(val context: Context, workerParams: WorkerParameters) : + Worker(context, workerParams) { -import android.content.Context; -import android.util.Log; + @Inject + lateinit var ncApi: NcApi -import com.nextcloud.talk.api.NcApi; -import com.nextcloud.talk.application.NextcloudTalkApplication; -import com.nextcloud.talk.data.user.model.User; -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; + @Inject + lateinit var userManager: UserManager -import org.greenrobot.eventbus.EventBus; -import java.net.CookieManager; + override fun doWork(): Result { + sharedApplication!!.componentApplication.inject(this) + val data = inputData + val conversationToken = data.getString(BundleKeys.KEY_ROOM_TOKEN) + val currentUser = userManager.currentUser.blockingGet() -import javax.inject.Inject; + if (currentUser != null) { + val credentials = getCredentials(currentUser.username, currentUser.token) -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; + val apiVersion = getConversationApiVersion(currentUser, intArrayOf(ApiUtils.API_V4, 1)) -@AutoInjector(NextcloudTalkApplication.class) -public class LeaveConversationWorker extends Worker { + ncApi.removeSelfFromRoom( + credentials, getUrlForParticipantsSelf( + apiVersion, + currentUser.baseUrl, + conversationToken + ) + ) + .subscribeOn(Schedulers.io()) + .subscribe(object : Observer { + var disposable: Disposable? = null - private static final String TAG = "LeaveConversationWorker"; + override fun onSubscribe(d: Disposable) { + disposable = d + } - @Inject - Retrofit retrofit; - - @Inject - OkHttpClient okHttpClient; + override fun onNext(p0: GenericOverall) { - @Inject - UserManager userManager; + } - @Inject - EventBus eventBus; + override fun onError(e: Throwable) { + Log.e(TAG, "failed to remove self from room", e) + } - NcApi ncApi; + override fun onComplete() { + disposable!!.dispose() + } + }) + } - public LeaveConversationWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { - super(context, workerParams); - NextcloudTalkApplication.Companion.getSharedApplication().getComponentApplication().inject(this); + return Result.success() } - @NonNull - @Override - public Result doWork() { - Data data = getInputData(); - long operationUserId = data.getLong(BundleKeys.KEY_INTERNAL_USER_ID, -1); - 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()) - .blockingSubscribe(new Observer() { - Disposable disposable; - - @Override - public void onSubscribe(Disposable d) { - disposable = d; - } - - @Override - public void onNext(GenericOverall genericOverall) { - eventBus.postSticky(eventStatus); - } - - @Override - public void onError(Throwable e) { - Log.e(TAG, "failed to remove self from room", e); - } - - @Override - public void onComplete() { - disposable.dispose(); - } - }); - } - - return Result.success(); + companion object { + private const val TAG = "LeaveConversationWorker" } } From e04f2362e334979fd0c45f84cb1cce20b03137aa Mon Sep 17 00:00:00 2001 From: sowjanyakch Date: Tue, 15 Oct 2024 22:53:56 +0200 Subject: [PATCH 03/11] remove unused imports Signed-off-by: sowjanyakch --- .../com/nextcloud/talk/jobs/LeaveConversationWorker.kt | 7 ------- 1 file changed, 7 deletions(-) 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 fcf4567e48..1aa232adee 100644 --- a/app/src/main/java/com/nextcloud/talk/jobs/LeaveConversationWorker.kt +++ b/app/src/main/java/com/nextcloud/talk/jobs/LeaveConversationWorker.kt @@ -15,23 +15,16 @@ 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 @AutoInjector(NextcloudTalkApplication::class) From 088e6fd79f5dbd13d0419fdc2fd1ff0d8ea05a9e Mon Sep 17 00:00:00 2001 From: sowjanyakch Date: Tue, 22 Oct 2024 08:38:48 +0200 Subject: [PATCH 04/11] Start unique work Signed-off-by: sowjanyakch --- .../ConversationInfoActivity.kt | 46 +++++++++++++++---- .../talk/jobs/LeaveConversationWorker.kt | 14 +++--- app/src/main/res/values/strings.xml | 3 ++ 3 files changed, 47 insertions(+), 16 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 6381194a0f..1f3d646718 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationinfo/ConversationInfoActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationinfo/ConversationInfoActivity.kt @@ -21,13 +21,16 @@ import android.view.MenuItem import android.view.View import android.view.View.GONE import android.view.View.VISIBLE +import android.widget.Toast import androidx.appcompat.app.AlertDialog import androidx.core.content.res.ResourcesCompat import androidx.fragment.app.FragmentTransaction import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.lifecycleScope import androidx.work.Data +import androidx.work.ExistingWorkPolicy import androidx.work.OneTimeWorkRequest +import androidx.work.WorkInfo import androidx.work.WorkManager import autodagger.AutoInjector import com.afollestad.materialdialogs.LayoutMode.WRAP_CONTENT @@ -635,17 +638,40 @@ class ConversationInfoActivity : } private fun leaveConversation() { - workerData?.let { - WorkManager.getInstance(context).enqueue( - OneTimeWorkRequest.Builder( - LeaveConversationWorker::class - .java - ).setInputData(it).build() - ) + workerData?.let { data -> + val workRequest = OneTimeWorkRequest.Builder(LeaveConversationWorker::class.java) + .setInputData(data) + .build() - val intent = Intent(context, MainActivity::class.java) - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) - startActivity(intent) + + WorkManager.getInstance(context) + .enqueueUniqueWork( + "leave_conversation_work", + ExistingWorkPolicy.REPLACE, + 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) + } + WorkInfo.State.FAILED -> { + + Toast.makeText(context, R.string.nc_last_moderator_leaving_room_warning, Toast.LENGTH_LONG).show() + } + else -> { + + } + } + } + }) } } 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 1aa232adee..1f6f222264 100644 --- a/app/src/main/java/com/nextcloud/talk/jobs/LeaveConversationWorker.kt +++ b/app/src/main/java/com/nextcloud/talk/jobs/LeaveConversationWorker.kt @@ -14,7 +14,6 @@ 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.models.json.generic.GenericOverall import com.nextcloud.talk.users.UserManager import com.nextcloud.talk.utils.ApiUtils @@ -39,10 +38,10 @@ class LeaveConversationWorker(val context: Context, workerParams: WorkerParamete override fun doWork(): Result { - sharedApplication!!.componentApplication.inject(this) val data = inputData val conversationToken = data.getString(BundleKeys.KEY_ROOM_TOKEN) val currentUser = userManager.currentUser.blockingGet() + lateinit var workResult:Result if (currentUser != null) { val credentials = getCredentials(currentUser.username, currentUser.token) @@ -58,10 +57,9 @@ class LeaveConversationWorker(val context: Context, workerParams: WorkerParamete ) .subscribeOn(Schedulers.io()) .subscribe(object : Observer { - var disposable: Disposable? = null override fun onSubscribe(d: Disposable) { - disposable = d + } override fun onNext(p0: GenericOverall) { @@ -70,15 +68,19 @@ class LeaveConversationWorker(val context: Context, workerParams: WorkerParamete 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() + } } override fun onComplete() { - disposable!!.dispose() + workResult = Result.success() + } }) } - return Result.success() + return workResult } companion object { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 92ed5be924..fb81016d33 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -505,6 +505,9 @@ How to translate with transifex: Delete Message deleted successfully, but it might have been leaked to other services You are not allowed to start a call + + You need to promote a new moderator before you can leave the conversation + Share Send to From aa5fd42c59540bb20033e8578c471b58f5a2ebbb Mon Sep 17 00:00:00 2001 From: sowjanyakch Date: Fri, 25 Oct 2024 09:17:30 +0200 Subject: [PATCH 05/11] handle workResult Signed-off-by: sowjanyakch --- .../java/com/nextcloud/talk/jobs/LeaveConversationWorker.kt | 4 ++++ 1 file changed, 4 insertions(+) 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 1f6f222264..801efbea23 100644 --- a/app/src/main/java/com/nextcloud/talk/jobs/LeaveConversationWorker.kt +++ b/app/src/main/java/com/nextcloud/talk/jobs/LeaveConversationWorker.kt @@ -38,6 +38,7 @@ class LeaveConversationWorker(val context: Context, workerParams: WorkerParamete override fun doWork(): Result { + NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this) val data = inputData val conversationToken = data.getString(BundleKeys.KEY_ROOM_TOKEN) val currentUser = userManager.currentUser.blockingGet() @@ -78,6 +79,9 @@ class LeaveConversationWorker(val context: Context, workerParams: WorkerParamete } }) + }else{ + + workResult = Result.failure() } return workResult From a07a5446befc4b9ba4b0a9973450c82dc1fd1991 Mon Sep 17 00:00:00 2001 From: sowjanyakch Date: Sun, 27 Oct 2024 16:59:13 +0100 Subject: [PATCH 06/11] 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 1f3d646718..ca39bb0726 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 801efbea23..7e9f63b5e7 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 { From 9df91b6e74a09e438fd9e0f77687c2b8be5e30d0 Mon Sep 17 00:00:00 2001 From: sowjanyakch Date: Sun, 27 Oct 2024 17:01:17 +0100 Subject: [PATCH 07/11] refresh main activity after leaving a conversation Signed-off-by: sowjanyakch --- .../nextcloud/talk/ui/dialog/ConversationsListBottomDialog.kt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/src/main/java/com/nextcloud/talk/ui/dialog/ConversationsListBottomDialog.kt b/app/src/main/java/com/nextcloud/talk/ui/dialog/ConversationsListBottomDialog.kt index 66cff6d7b3..9dbd718413 100644 --- a/app/src/main/java/com/nextcloud/talk/ui/dialog/ConversationsListBottomDialog.kt +++ b/app/src/main/java/com/nextcloud/talk/ui/dialog/ConversationsListBottomDialog.kt @@ -6,6 +6,7 @@ */ package com.nextcloud.talk.ui.dialog +import android.content.Intent import android.os.Bundle import android.text.TextUtils import android.view.View @@ -18,6 +19,7 @@ import autodagger.AutoInjector import com.google.android.material.bottomsheet.BottomSheetBehavior import com.google.android.material.bottomsheet.BottomSheetDialog import com.nextcloud.talk.R +import com.nextcloud.talk.activities.MainActivity import com.nextcloud.talk.api.NcApi import com.nextcloud.talk.application.NextcloudTalkApplication import com.nextcloud.talk.conversation.RenameConversationDialogFragment @@ -393,6 +395,8 @@ class ConversationsListBottomDialog( conversation.displayName ) ) + val intent = Intent(context, MainActivity::class.java) + context.startActivity(intent) } WorkInfo.State.FAILED -> { From f6489eb10d11f6bc71ec205172201c87763397c6 Mon Sep 17 00:00:00 2001 From: sowjanyakch Date: Sun, 27 Oct 2024 17:04:14 +0100 Subject: [PATCH 08/11] ktlintFormat Signed-off-by: sowjanyakch --- .../talk/conversationinfo/ConversationInfoActivity.kt | 8 +++++--- .../com/nextcloud/talk/jobs/LeaveConversationWorker.kt | 4 ++-- 2 files changed, 7 insertions(+), 5 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 ca39bb0726..b5c66e3dd6 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationinfo/ConversationInfoActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationinfo/ConversationInfoActivity.kt @@ -660,11 +660,13 @@ class ConversationInfoActivity : startActivity(intent) } WorkInfo.State.FAILED -> { - - Toast.makeText(context, R.string.nc_last_moderator_leaving_room_warning, Toast.LENGTH_LONG).show() + Toast.makeText( + context, + R.string.nc_last_moderator_leaving_room_warning, + Toast.LENGTH_LONG + ).show() } else -> { - } } } 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 7e9f63b5e7..3c946b254e 100644 --- a/app/src/main/java/com/nextcloud/talk/jobs/LeaveConversationWorker.kt +++ b/app/src/main/java/com/nextcloud/talk/jobs/LeaveConversationWorker.kt @@ -53,13 +53,13 @@ class LeaveConversationWorker(context: Context, workerParams: WorkerParameters) 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) { From 6c4685dce5893fdbcd2ae00bc093e55d816da7f6 Mon Sep 17 00:00:00 2001 From: sowjanyakch Date: Tue, 29 Oct 2024 16:57:01 +0100 Subject: [PATCH 09/11] handle errors properly Signed-off-by: sowjanyakch --- .../conversationinfo/ConversationInfoActivity.kt | 12 ++++++------ .../talk/jobs/LeaveConversationWorker.kt | 16 ++++++++++++++-- 2 files changed, 20 insertions(+), 8 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 b5c66e3dd6..2cfcb07ea1 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationinfo/ConversationInfoActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationinfo/ConversationInfoActivity.kt @@ -21,7 +21,6 @@ import android.view.MenuItem import android.view.View import android.view.View.GONE import android.view.View.VISIBLE -import android.widget.Toast import androidx.appcompat.app.AlertDialog import androidx.core.content.res.ResourcesCompat import androidx.fragment.app.FragmentTransaction @@ -660,11 +659,12 @@ class ConversationInfoActivity : startActivity(intent) } WorkInfo.State.FAILED -> { - Toast.makeText( - context, - R.string.nc_last_moderator_leaving_room_warning, - Toast.LENGTH_LONG - ).show() + val errorType = workInfo.outputData.getString("error_type") + if (errorType == LeaveConversationWorker.ERROR_NO_OTHER_MODERATORS_OR_OWNERS_LEFT) { + Snackbar.make( binding.root, R.string.nc_last_moderator_leaving_room_warning, Snackbar.LENGTH_LONG ).show() + } else { + Snackbar.make( binding.root, R.string.nc_common_error_sorry, Snackbar.LENGTH_LONG ).show() + } } else -> { } 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 3c946b254e..55eeb897e6 100644 --- a/app/src/main/java/com/nextcloud/talk/jobs/LeaveConversationWorker.kt +++ b/app/src/main/java/com/nextcloud/talk/jobs/LeaveConversationWorker.kt @@ -10,6 +10,7 @@ package com.nextcloud.talk.jobs import android.annotation.SuppressLint import android.content.Context import android.util.Log +import androidx.work.Data import androidx.work.ListenableWorker import androidx.work.WorkerParameters import androidx.work.impl.utils.futures.SettableFuture @@ -28,6 +29,7 @@ import io.reactivex.Observer import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.Disposable import io.reactivex.schedulers.Schedulers +import retrofit2.HttpException import javax.inject.Inject @SuppressLint("RestrictedApi") @@ -67,9 +69,17 @@ class LeaveConversationWorker(context: Context, workerParams: WorkerParameters) override fun onError(e: Throwable) { Log.e(TAG, "Failed to remove self from room", e) - if (e.message?.contains("HTTP 400") == true) { - result.set(Result.failure()) + val httpException = e as? HttpException + val errorData = if (httpException?.code() == 400) { + Data.Builder() + .putString("error_type", ERROR_NO_OTHER_MODERATORS_OR_OWNERS_LEFT) + .build() } + else { + Data.Builder() + .putString("error_type", ERROR_OTHER) + .build() } + result.set(Result.failure(errorData)) } override fun onComplete() { @@ -85,5 +95,7 @@ class LeaveConversationWorker(context: Context, workerParams: WorkerParameters) companion object { private const val TAG = "LeaveConversationWorker" + const val ERROR_NO_OTHER_MODERATORS_OR_OWNERS_LEFT = "NO_OTHER_MODERATORS_OR_OWNERS_LEFT" + const val ERROR_OTHER = "ERROR_OTHER" } } From a39ca256dea7aba191c5fa9889350baf2c571913 Mon Sep 17 00:00:00 2001 From: sowjanyakch Date: Tue, 29 Oct 2024 17:24:31 +0100 Subject: [PATCH 10/11] add const val for error code Signed-off-by: sowjanyakch --- .../conversationinfo/ConversationInfoActivity.kt | 12 ++++++++++-- .../nextcloud/talk/jobs/LeaveConversationWorker.kt | 9 +++++---- 2 files changed, 15 insertions(+), 6 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 2cfcb07ea1..56630615a8 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationinfo/ConversationInfoActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationinfo/ConversationInfoActivity.kt @@ -661,9 +661,17 @@ class ConversationInfoActivity : WorkInfo.State.FAILED -> { val errorType = workInfo.outputData.getString("error_type") if (errorType == LeaveConversationWorker.ERROR_NO_OTHER_MODERATORS_OR_OWNERS_LEFT) { - Snackbar.make( binding.root, R.string.nc_last_moderator_leaving_room_warning, Snackbar.LENGTH_LONG ).show() + Snackbar.make( + binding.root, + R.string.nc_last_moderator_leaving_room_warning, + Snackbar.LENGTH_LONG + ).show() } else { - Snackbar.make( binding.root, R.string.nc_common_error_sorry, Snackbar.LENGTH_LONG ).show() + Snackbar.make( + binding.root, + R.string.nc_common_error_sorry, + Snackbar.LENGTH_LONG + ).show() } } else -> { 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 55eeb897e6..bf746199e2 100644 --- a/app/src/main/java/com/nextcloud/talk/jobs/LeaveConversationWorker.kt +++ b/app/src/main/java/com/nextcloud/talk/jobs/LeaveConversationWorker.kt @@ -70,15 +70,15 @@ class LeaveConversationWorker(context: Context, workerParams: WorkerParameters) override fun onError(e: Throwable) { Log.e(TAG, "Failed to remove self from room", e) val httpException = e as? HttpException - val errorData = if (httpException?.code() == 400) { + val errorData = if (httpException?.code() == HTTP_ERROR_CODE_400) { Data.Builder() .putString("error_type", ERROR_NO_OTHER_MODERATORS_OR_OWNERS_LEFT) .build() - } - else { + } else { Data.Builder() .putString("error_type", ERROR_OTHER) - .build() } + .build() + } result.set(Result.failure(errorData)) } @@ -97,5 +97,6 @@ class LeaveConversationWorker(context: Context, workerParams: WorkerParameters) private const val TAG = "LeaveConversationWorker" const val ERROR_NO_OTHER_MODERATORS_OR_OWNERS_LEFT = "NO_OTHER_MODERATORS_OR_OWNERS_LEFT" const val ERROR_OTHER = "ERROR_OTHER" + const val HTTP_ERROR_CODE_400 = 400 } } From 2ad8c2eabcd114825460e9b214b8cd68e2e6f8d3 Mon Sep 17 00:00:00 2001 From: Marcel Hibbe Date: Tue, 5 Nov 2024 15:00:51 +0100 Subject: [PATCH 11/11] set nc_last_moderator_leaving_room_warning to one line in strings.xml Signed-off-by: Marcel Hibbe --- app/src/main/res/values/strings.xml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index fb81016d33..8fc1c3406a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -505,9 +505,7 @@ How to translate with transifex: Delete Message deleted successfully, but it might have been leaked to other services You are not allowed to start a call - - You need to promote a new moderator before you can leave the conversation - + You need to promote a new moderator before you can leave the conversation Share Send to