Improve AddPiHoleFragment with enter button handling
This commit is contained in:
parent
eb260dec4c
commit
738d2cc81d
4 changed files with 100 additions and 94 deletions
|
@ -15,8 +15,6 @@ import androidx.navigation.fragment.findNavController
|
|||
import kotlinx.android.synthetic.main.fragment_add_pi_hole.*
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.launch
|
||||
import org.koin.android.ext.android.inject
|
||||
import kotlin.coroutines.CoroutineContext
|
||||
|
||||
|
@ -40,8 +38,10 @@ class AddPiHoleFragment : Fragment(), CoroutineScope {
|
|||
FragmentNavigatorExtras(piHelperLogo to "piHelperLogo")
|
||||
)
|
||||
}
|
||||
connectButton.setOnClickListener {
|
||||
launch {
|
||||
ipAddress.setOnEditorActionListener { _, _, _ ->
|
||||
connectButton.performClick()
|
||||
}
|
||||
connectButton.setSuspendingOnClickListener(this) {
|
||||
val progressDialog = AlertDialog.Builder(it.context)
|
||||
.setTitle(R.string.connecting_to_pihole)
|
||||
.setNegativeButton(R.string.action_cancel) { _, _ ->
|
||||
|
@ -74,7 +74,6 @@ class AddPiHoleFragment : Fragment(), CoroutineScope {
|
|||
progressDialog.dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
cancel()
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
package com.wbrawner.pihelper
|
||||
|
||||
import android.view.View
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.launch
|
||||
import java.math.BigInteger
|
||||
import java.security.MessageDigest
|
||||
|
||||
|
@ -7,3 +11,14 @@ fun String.hash(): String = BigInteger(
|
|||
1,
|
||||
MessageDigest.getInstance("SHA-256").digest(this.toByteArray())
|
||||
).toString(16).padStart(64, '0')
|
||||
|
||||
fun CoroutineScope.cancel() {
|
||||
coroutineContext[Job]?.cancel()
|
||||
}
|
||||
|
||||
fun View.setSuspendingOnClickListener(
|
||||
coroutineScope: CoroutineScope,
|
||||
clickListener: suspend (v: View) -> Unit
|
||||
) = setOnClickListener { v ->
|
||||
coroutineScope.launch { clickListener(v) }
|
||||
}
|
||||
|
|
|
@ -89,8 +89,7 @@ class MainFragment : Fragment(), CoroutineScope {
|
|||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
(activity as? AppCompatActivity)?.setSupportActionBar(toolbar)
|
||||
showProgress(true)
|
||||
enableButton?.setOnClickListener {
|
||||
launch {
|
||||
enableButton?.setSuspendingOnClickListener(this) {
|
||||
showProgress(true)
|
||||
try {
|
||||
viewModel.enablePiHole()
|
||||
|
@ -98,9 +97,7 @@ class MainFragment : Fragment(), CoroutineScope {
|
|||
Log.e("Pi-Helper", "Failed to enable Pi-Hole", ignored)
|
||||
}
|
||||
}
|
||||
}
|
||||
disable10SecondsButton?.setOnClickListener {
|
||||
launch {
|
||||
disable10SecondsButton?.setSuspendingOnClickListener(this) {
|
||||
showProgress(true)
|
||||
try {
|
||||
viewModel.disablePiHole(10)
|
||||
|
@ -108,9 +105,7 @@ class MainFragment : Fragment(), CoroutineScope {
|
|||
Log.e("Pi-Helper", "Failed to disable Pi-Hole", ignored)
|
||||
}
|
||||
}
|
||||
}
|
||||
disable30SecondsButton?.setOnClickListener {
|
||||
launch {
|
||||
disable30SecondsButton?.setSuspendingOnClickListener(this) {
|
||||
showProgress(true)
|
||||
try {
|
||||
viewModel.disablePiHole(30)
|
||||
|
@ -118,9 +113,7 @@ class MainFragment : Fragment(), CoroutineScope {
|
|||
Log.e("Pi-Helper", "Failed to disable Pi-Hole", ignored)
|
||||
}
|
||||
}
|
||||
}
|
||||
disable5MinutesButton?.setOnClickListener {
|
||||
launch {
|
||||
disable5MinutesButton?.setSuspendingOnClickListener(this) {
|
||||
showProgress(true)
|
||||
try {
|
||||
viewModel.disablePiHole(300)
|
||||
|
@ -128,7 +121,6 @@ class MainFragment : Fragment(), CoroutineScope {
|
|||
Log.e("Pi-Helper", "Failed to disable Pi-Hole", ignored)
|
||||
}
|
||||
}
|
||||
}
|
||||
disableCustomTimeButton?.setOnClickListener {
|
||||
val dialogView = LayoutInflater.from(it.context)
|
||||
.inflate(R.layout.dialog_disable_custom_time, view as ViewGroup, false)
|
||||
|
@ -140,8 +132,7 @@ class MainFragment : Fragment(), CoroutineScope {
|
|||
.create()
|
||||
.apply {
|
||||
setOnShowListener {
|
||||
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
|
||||
launch {
|
||||
getButton(AlertDialog.BUTTON_POSITIVE).setSuspendingOnClickListener(this@MainFragment) {
|
||||
try {
|
||||
val rawTime = dialogView.findViewById<EditText>(R.id.time)
|
||||
.text
|
||||
|
@ -161,11 +152,9 @@ class MainFragment : Fragment(), CoroutineScope {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.show()
|
||||
}
|
||||
disablePermanentlyButton?.setOnClickListener {
|
||||
launch {
|
||||
disablePermanentlyButton?.setSuspendingOnClickListener(this) {
|
||||
showProgress(true)
|
||||
try {
|
||||
viewModel.disablePiHole()
|
||||
|
@ -174,7 +163,6 @@ class MainFragment : Fragment(), CoroutineScope {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
if (item.itemId == R.id.settings) {
|
||||
|
|
|
@ -50,11 +50,15 @@
|
|||
android:layout_height="wrap_content"
|
||||
android:hint="@string/prompt_ip_address">
|
||||
|
||||
<!-- TODO: Figure out how to get this to work for inputting an IP address -->
|
||||
<com.google.android.material.textfield.TextInputEditText
|
||||
android:id="@+id/ipAddress"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" />
|
||||
android:layout_height="wrap_content"
|
||||
android:text="pi.hole"
|
||||
android:inputType="text"
|
||||
android:maxLines="1"
|
||||
android:imeOptions="actionGo"
|
||||
tools:ignore="HardcodedText" />
|
||||
</com.google.android.material.textfield.TextInputLayout>
|
||||
|
||||
<com.google.android.material.button.MaterialButton
|
||||
|
|
Loading…
Reference in a new issue