Powerlevel: use edit text for custom

This commit is contained in:
ganfra 2020-06-05 20:08:39 +02:00
parent e5da5a34cb
commit 07c7ed0a4e
2 changed files with 16 additions and 45 deletions

View File

@ -19,7 +19,6 @@ package im.vector.riotx.features.roommemberprofile.powerlevel
import android.app.Activity import android.app.Activity
import android.content.DialogInterface import android.content.DialogInterface
import android.view.KeyEvent import android.view.KeyEvent
import android.widget.SeekBar
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.core.view.isVisible import androidx.core.view.isVisible
import im.vector.matrix.android.api.session.room.powerlevels.Role import im.vector.matrix.android.api.session.room.powerlevels.Role
@ -29,30 +28,13 @@ import kotlinx.android.synthetic.main.dialog_edit_power_level.view.*
object EditPowerLevelDialogs { object EditPowerLevelDialogs {
private const val SLIDER_STEP = 1
private const val SLIDER_MAX_VALUE = 100
private const val SLIDER_MIN_VALUE = -100
fun showChoice(activity: Activity, currentRole: Role, listener: (Int) -> Unit) { fun showChoice(activity: Activity, currentRole: Role, listener: (Int) -> Unit) {
val dialogLayout = activity.layoutInflater.inflate(R.layout.dialog_edit_power_level, null) val dialogLayout = activity.layoutInflater.inflate(R.layout.dialog_edit_power_level, null)
dialogLayout.powerLevelRadioGroup.setOnCheckedChangeListener { _, checkedId -> dialogLayout.powerLevelRadioGroup.setOnCheckedChangeListener { _, checkedId ->
dialogLayout.powerLevelCustomLayout.isVisible = checkedId == R.id.powerLevelCustomRadio dialogLayout.powerLevelCustomEditLayout.isVisible = checkedId == R.id.powerLevelCustomRadio
} }
dialogLayout.powerLevelCustomSlider.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener { dialogLayout.powerLevelCustomEdit.setText(currentRole.value.toString())
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
dialogLayout.powerLevelCustomTitle.text = activity.getString(R.string.power_level_custom, seekBar.normalizedProgress())
}
override fun onStartTrackingTouch(seekBar: SeekBar?) {
//NOOP
}
override fun onStopTrackingTouch(seekBar: SeekBar?) {
//NOOP
}
})
dialogLayout.powerLevelCustomSlider.max = (SLIDER_MAX_VALUE - SLIDER_MIN_VALUE) / SLIDER_STEP
dialogLayout.powerLevelCustomSlider.progress = SLIDER_MAX_VALUE + (currentRole.value * SLIDER_STEP)
when (currentRole) { when (currentRole) {
Role.Admin -> dialogLayout.powerLevelAdminRadio.isChecked = true Role.Admin -> dialogLayout.powerLevelAdminRadio.isChecked = true
Role.Moderator -> dialogLayout.powerLevelModeratorRadio.isChecked = true Role.Moderator -> dialogLayout.powerLevelModeratorRadio.isChecked = true
@ -69,7 +51,9 @@ object EditPowerLevelDialogs {
R.id.powerLevelAdminRadio -> Role.Admin.value R.id.powerLevelAdminRadio -> Role.Admin.value
R.id.powerLevelModeratorRadio -> Role.Moderator.value R.id.powerLevelModeratorRadio -> Role.Moderator.value
R.id.powerLevelDefaultRadio -> Role.Default.value R.id.powerLevelDefaultRadio -> Role.Default.value
else -> dialogLayout.powerLevelCustomSlider.normalizedProgress() else -> {
dialogLayout.powerLevelCustomEdit.text?.toString()?.toInt() ?: currentRole.value
}
} }
listener(newValue) listener(newValue)
} }
@ -89,8 +73,6 @@ object EditPowerLevelDialogs {
.show() .show()
} }
private fun SeekBar.normalizedProgress() = SLIDER_MIN_VALUE + (progress * SLIDER_STEP)
fun showValidation(activity: Activity, onValidate: () -> Unit) { fun showValidation(activity: Activity, onValidate: () -> Unit) {
// ask to the user to confirmation thu upgrade. // ask to the user to confirmation thu upgrade.
AlertDialog.Builder(activity) AlertDialog.Builder(activity)

View File

@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical" android:orientation="vertical"
android:paddingStart="?dialogPreferredPadding" android:paddingStart="?dialogPreferredPadding"
android:paddingLeft="?dialogPreferredPadding" android:paddingLeft="?dialogPreferredPadding"
@ -47,30 +47,19 @@
</RadioGroup> </RadioGroup>
<RelativeLayout <com.google.android.material.textfield.TextInputLayout
android:id="@+id/powerLevelCustomLayout" android:id="@+id/powerLevelCustomEditLayout"
style="@style/VectorTextInputLayout"
android:hint="@string/power_level_custom_no_value"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content">
android:layout_marginTop="8dp">
<TextView <com.google.android.material.textfield.TextInputEditText
android:id="@+id/powerLevelCustomTitle" android:id="@+id/powerLevelCustomEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
tools:text="Custom (20)"
android:layout_marginStart="16dp"
android:textColor="?riotx_text_primary"
android:textStyle="bold" />
<SeekBar
android:id="@+id/powerLevelCustomSlider"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp" android:inputType="number|numberSigned" />
android:layout_below="@id/powerLevelCustomTitle"
android:max="100" </com.google.android.material.textfield.TextInputLayout>
android:progress="1" />
</RelativeLayout>
</LinearLayout> </LinearLayout>