From b6bf045fd06e175515f44af400d5606d6f3ba8f1 Mon Sep 17 00:00:00 2001 From: William Brawner Date: Sun, 10 Jan 2021 14:11:39 -0700 Subject: [PATCH] Add option to disable for hours Signed-off-by: William Brawner --- .../com/wbrawner/pihelper/MainFragment.kt | 35 ++++++++++--------- .../res/layout/dialog_disable_custom_time.xml | 6 ++++ app/src/main/res/values/strings.xml | 1 + 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/wbrawner/pihelper/MainFragment.kt b/app/src/main/java/com/wbrawner/pihelper/MainFragment.kt index e631084..5acfcff 100644 --- a/app/src/main/java/com/wbrawner/pihelper/MainFragment.kt +++ b/app/src/main/java/com/wbrawner/pihelper/MainFragment.kt @@ -132,24 +132,25 @@ class MainFragment : Fragment() { .create() .apply { setOnShowListener { - getButton(AlertDialog.BUTTON_POSITIVE).setSuspendingOnClickListener( - lifecycleScope - ) { - try { - val rawTime = dialogView.time - .text - .toString() - .toLong() - val checkedId = - dialogView.timeUnit.checkedRadioButtonId - val computedTime = if (checkedId == R.id.seconds) rawTime - else rawTime * 60 - viewModel.disablePiHole(computedTime) - dismiss() - } catch (e: Exception) { - dialogView.time.error = "Failed to disable Pi-hole" + getButton(AlertDialog.BUTTON_POSITIVE) + .setSuspendingOnClickListener(lifecycleScope) { + try { + val rawTime = dialogView.time + .text + .toString() + .toLong() + val computedTime = + when (dialogView.timeUnit.checkedRadioButtonId) { + R.id.seconds -> rawTime + R.id.minutes -> rawTime * 60 + else -> rawTime * 3600 + } + viewModel.disablePiHole(computedTime) + dismiss() + } catch (e: Exception) { + dialogView.time.error = "Failed to disable Pi-hole" + } } - } } } .show() diff --git a/app/src/main/res/layout/dialog_disable_custom_time.xml b/app/src/main/res/layout/dialog_disable_custom_time.xml index 5f6b7e9..dc6c14a 100644 --- a/app/src/main/res/layout/dialog_disable_custom_time.xml +++ b/app/src/main/res/layout/dialog_disable_custom_time.xml @@ -40,5 +40,11 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/duration_minutes" /> + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3f240bf..5106b73 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -46,4 +46,5 @@ Authenticate with API Key Connecting to Pi-holeā€¦ Cancel + Hours