Skip to content

Commit

Permalink
Use correct API endpoint for predefined status
Browse files Browse the repository at this point in the history
Resolves: #1830

Signed-off-by: Tim Krüger <[email protected]>
  • Loading branch information
timkrueger committed Mar 17, 2022
1 parent d1d6898 commit c97e3c9
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
}

Expand Down Expand Up @@ -212,17 +219,8 @@ class SetStatusDialogFragment :
binding.emoji.disableKeyboardInput(popup)
binding.emoji.forceSingleEmoji()

val adapter = ArrayAdapter<String>(requireContext(), android.R.layout.simple_spinner_item)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
adapter.add(getString(R.string.dontClear))
adapter.add(getString(R.string.thirtyMinutes))
adapter.add(getString(R.string.oneHour))
adapter.add(getString(R.string.fourHours))
adapter.add(getString(R.string.today))
adapter.add(getString(R.string.thisWeek))

binding.clearStatusAfterSpinner.apply {
this.adapter = adapter
this.adapter = predefinedClearArrayAdapter()
onItemSelectedListener = object : OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
setClearStatusAfterValue(position)
Expand All @@ -244,6 +242,18 @@ class SetStatusDialogFragment :
}
}

private fun predefinedClearArrayAdapter(): ArrayAdapter<String> {
val adapter = ArrayAdapter<String>(requireContext(), android.R.layout.simple_spinner_item)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
adapter.add(getString(R.string.dontClear))
adapter.add(getString(R.string.thirtyMinutes))
adapter.add(getString(R.string.oneHour))
adapter.add(getString(R.string.fourHours))
adapter.add(getString(R.string.today))
adapter.add(getString(R.string.thisWeek))
return adapter
}

@Suppress("ComplexMethod")
private fun setClearStatusAfterValue(item: Int) {

Expand Down Expand Up @@ -297,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() {
Expand Down Expand Up @@ -417,41 +437,73 @@ 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<GenericOverall> {
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<GenericOverall> {

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<GenericOverall> {
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 {
return binding.root
}

override fun onClick(predefinedStatus: PredefinedStatus) {

selectedPredefinedStatus = predefinedStatus

clearAt = clearAtToUnixTime(predefinedStatus.clearAt)
binding.emoji.setText(predefinedStatus.icon)
binding.customStatusInput.text?.clear()
Expand All @@ -464,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
*/
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/nextcloud/talk/utils/ApiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down
2 changes: 1 addition & 1 deletion detekt.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
build:
maxIssues: 99
maxIssues: 97
weights:
# complexity: 2
# LongParameterList: 1
Expand Down

0 comments on commit c97e3c9

Please sign in to comment.