From 4f8043001823dbf0f75a6cb3d49e67b42b353637 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Kr=C3=BCger?= Date: Thu, 17 Mar 2022 14:50:14 +0100 Subject: [PATCH] Use correct API endpoint for predefined status MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Resolves: #1830 Signed-off-by: Tim Krüger --- .../talk/ui/dialog/SetStatusDialogFragment.kt | 154 ++++++++++++------ .../com/nextcloud/talk/utils/ApiUtils.java | 4 + detekt.yml | 2 +- 3 files changed, 108 insertions(+), 52 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/ui/dialog/SetStatusDialogFragment.kt b/app/src/main/java/com/nextcloud/talk/ui/dialog/SetStatusDialogFragment.kt index 57584222a4..c89dafab13 100644 --- a/app/src/main/java/com/nextcloud/talk/ui/dialog/SetStatusDialogFragment.kt +++ b/app/src/main/java/com/nextcloud/talk/ui/dialog/SetStatusDialogFragment.kt @@ -60,7 +60,6 @@ import io.reactivex.Observer import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.disposables.Disposable import io.reactivex.schedulers.Schedulers -import kotlinx.android.synthetic.main.dialog_set_status.* import okhttp3.ResponseBody import java.util.Calendar import java.util.Locale @@ -88,6 +87,7 @@ private const val LAST_SECOND_OF_MINUTE = 59 class SetStatusDialogFragment : DialogFragment(), PredefinedStatusClickListener { + private var selectedPredefinedStatus: PredefinedStatus? = null private val logTag = SetStatusDialogFragment::class.java.simpleName private lateinit var binding: DialogSetStatusBinding @@ -133,6 +133,13 @@ class SetStatusDialogFragment : ) predefinedStatusOverall.ocs?.data?.let { it1 -> predefinedStatusesList.addAll(it1) } + if (currentStatus?.messageIsPredefined == true && + currentStatus?.messageId?.isNotEmpty() == true + ) { + val messageId = currentStatus!!.messageId + selectedPredefinedStatus = predefinedStatusesList.first { ps -> messageId == ps.id } + } + adapter.notifyDataSetChanged() } @@ -300,24 +307,34 @@ class SetStatusDialogFragment : } } - @Suppress("ReturnCount") private fun clearAtToUnixTime(clearAt: ClearAt?): Long { + + var returnValue = -1L + if (clearAt != null) { if (clearAt.type == "period") { - return System.currentTimeMillis() / ONE_SECOND_IN_MILLIS + clearAt.time.toLong() + returnValue = System.currentTimeMillis() / ONE_SECOND_IN_MILLIS + clearAt.time.toLong() } else if (clearAt.type == "end-of") { - if (clearAt.time == "day") { - val date = Calendar.getInstance().apply { - set(Calendar.HOUR_OF_DAY, LAST_HOUR_OF_DAY) - set(Calendar.MINUTE, LAST_MINUTE_OF_HOUR) - set(Calendar.SECOND, LAST_SECOND_OF_MINUTE) - } - return date.timeInMillis / ONE_SECOND_IN_MILLIS - } + returnValue = clearAtToUnixTimeTypeEndOf(clearAt) } } - return -1 + return returnValue + } + + private fun clearAtToUnixTimeTypeEndOf( + clearAt: ClearAt + ): Long { + var returnValue = -1L + if (clearAt.time == "day") { + val date = Calendar.getInstance().apply { + set(Calendar.HOUR_OF_DAY, LAST_HOUR_OF_DAY) + set(Calendar.MINUTE, LAST_MINUTE_OF_HOUR) + set(Calendar.SECOND, LAST_SECOND_OF_MINUTE) + } + returnValue = date.timeInMillis / ONE_SECOND_IN_MILLIS + } + return returnValue } private fun openEmojiPopup() { @@ -420,34 +437,63 @@ class SetStatusDialogFragment : // The endpoint '/message/custom' expects a valid emoji as string or null val statusIcon = binding.emoji.text.toString().ifEmpty { null } - ncApi.setCustomStatusMessage( - credentials, - ApiUtils.getUrlForSetCustomStatus(currentUser?.baseUrl), - statusIcon, - inputText, - clearAt - ) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()) - ?.subscribe(object : Observer { + if (selectedPredefinedStatus == null || + selectedPredefinedStatus!!.message != inputText || + selectedPredefinedStatus!!.icon != binding.emoji.text.toString() + ) { + + ncApi.setCustomStatusMessage( + credentials, + ApiUtils.getUrlForSetCustomStatus(currentUser?.baseUrl), + statusIcon, + inputText, + clearAt + ) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) + ?.subscribe(object : Observer { - override fun onSubscribe(d: Disposable) { - // unused atm - } + override fun onSubscribe(d: Disposable) { + // unused atm + } - override fun onNext(t: GenericOverall) { - Log.d(logTag, "CustomStatusMessage successfully set") - dismiss() - } + override fun onNext(t: GenericOverall) { + Log.d(logTag, "CustomStatusMessage successfully set") + dismiss() + } - override fun onError(e: Throwable) { - Log.e(logTag, "failed to set CustomStatusMessage", e) - } + override fun onError(e: Throwable) { + Log.e(logTag, "failed to set CustomStatusMessage", e) + } - override fun onComplete() { - // unused atm - } - }) + override fun onComplete() { + // unused atm + } + }) + } else { + + val clearAt = clearAtToUnixTime(selectedPredefinedStatus!!.clearAt) + + ncApi.setPredefinedStatusMessage( + credentials, ApiUtils.getUrlForSetPredefinedStatus(currentUser?.baseUrl), + selectedPredefinedStatus!!.id, if (clearAt == -1L) null else clearAt + ) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread())?.subscribe(object : Observer { + override fun onSubscribe(d: Disposable) = Unit + + override fun onNext(t: GenericOverall) { + Log.d(logTag, "PredefinedStatusMessage successfully set") + dismiss() + } + + override fun onError(e: Throwable) { + Log.e(logTag, "failed to set PredefinedStatusMessage", e) + } + + override fun onComplete() = Unit + }) + } } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { @@ -455,6 +501,9 @@ class SetStatusDialogFragment : } override fun onClick(predefinedStatus: PredefinedStatus) { + + selectedPredefinedStatus = predefinedStatus + clearAt = clearAtToUnixTime(predefinedStatus.clearAt) binding.emoji.setText(predefinedStatus.icon) binding.customStatusInput.text?.clear() @@ -467,25 +516,28 @@ class SetStatusDialogFragment : if (predefinedStatus.clearAt == null) { binding.clearStatusAfterSpinner.setSelection(0) } else { - val clearAt = predefinedStatus.clearAt!! - if (clearAt.type == "period") { - when (clearAt.time) { - "1800" -> binding.clearStatusAfterSpinner.setSelection(POS_HALF_AN_HOUR) - "3600" -> binding.clearStatusAfterSpinner.setSelection(POS_AN_HOUR) - "14400" -> binding.clearStatusAfterSpinner.setSelection(POS_FOUR_HOURS) - else -> binding.clearStatusAfterSpinner.setSelection(POS_DONT_CLEAR) - } - } else if (clearAt.type == "end-of") { - when (clearAt.time) { - "day" -> binding.clearStatusAfterSpinner.setSelection(POS_TODAY) - "week" -> binding.clearStatusAfterSpinner.setSelection(POS_END_OF_WEEK) - else -> binding.clearStatusAfterSpinner.setSelection(POS_DONT_CLEAR) - } - } + setClearAt(predefinedStatus.clearAt!!) } setClearStatusAfterValue(binding.clearStatusAfterSpinner.selectedItemPosition) } + private fun setClearAt(clearAt: ClearAt) { + if (clearAt.type == "period") { + when (clearAt.time) { + "1800" -> binding.clearStatusAfterSpinner.setSelection(POS_HALF_AN_HOUR) + "3600" -> binding.clearStatusAfterSpinner.setSelection(POS_AN_HOUR) + "14400" -> binding.clearStatusAfterSpinner.setSelection(POS_FOUR_HOURS) + else -> binding.clearStatusAfterSpinner.setSelection(POS_DONT_CLEAR) + } + } else if (clearAt.type == "end-of") { + when (clearAt.time) { + "day" -> binding.clearStatusAfterSpinner.setSelection(POS_TODAY) + "week" -> binding.clearStatusAfterSpinner.setSelection(POS_END_OF_WEEK) + else -> binding.clearStatusAfterSpinner.setSelection(POS_DONT_CLEAR) + } + } + } + /** * Fragment creator */ diff --git a/app/src/main/java/com/nextcloud/talk/utils/ApiUtils.java b/app/src/main/java/com/nextcloud/talk/utils/ApiUtils.java index b189c0f4f1..a2cf1dcc8d 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/ApiUtils.java +++ b/app/src/main/java/com/nextcloud/talk/utils/ApiUtils.java @@ -434,6 +434,10 @@ public static String getUrlForSetCustomStatus(String baseUrl) { return baseUrl + ocsApiVersion + "/apps/user_status/api/v1/user_status/message/custom"; } + public static String getUrlForSetPredefinedStatus(String baseUrl) { + return baseUrl + ocsApiVersion + "/apps/user_status/api/v1/user_status/message/predefined"; + } + public static String getUrlForUserStatuses(String baseUrl) { return baseUrl + ocsApiVersion + "/apps/user_status/api/v1/statuses"; } diff --git a/detekt.yml b/detekt.yml index ee704fefa9..939ff6ae1a 100644 --- a/detekt.yml +++ b/detekt.yml @@ -1,5 +1,5 @@ build: - maxIssues: 99 + maxIssues: 98 weights: # complexity: 2 # LongParameterList: 1