From 9de88729ca669d0e7b185afed47235c830c9e1e6 Mon Sep 17 00:00:00 2001 From: Rafhaan Shah Date: Sat, 27 Apr 2024 10:18:51 +0100 Subject: [PATCH] Allow Tasker variables in input #7 --- app/build.gradle | 2 +- .../aggregated/AggregatedHealthDataActionRunner.kt | 6 +++++- .../aggregated/AggregatedHealthDataActivity.kt | 11 +++++------ .../aggregated/AggregatedHealthDataInput.kt | 8 ++++++-- .../aggregated/AggregatedHealthDataOutput.kt | 6 +++++- .../res/layout/activity_aggregated_health_data.xml | 3 +-- build.gradle | 4 ++-- 7 files changed, 25 insertions(+), 15 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 4fc17be..c935203 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -48,7 +48,7 @@ android { } dependencies { - implementation 'androidx.core:core-ktx:1.12.0' + implementation 'androidx.core:core-ktx:1.13.0' implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.navigation:navigation-fragment-ktx:2.7.7' diff --git a/app/src/main/java/com/rafapps/taskerhealthconnect/aggregated/AggregatedHealthDataActionRunner.kt b/app/src/main/java/com/rafapps/taskerhealthconnect/aggregated/AggregatedHealthDataActionRunner.kt index bf6676a..e874c0f 100644 --- a/app/src/main/java/com/rafapps/taskerhealthconnect/aggregated/AggregatedHealthDataActionRunner.kt +++ b/app/src/main/java/com/rafapps/taskerhealthconnect/aggregated/AggregatedHealthDataActionRunner.kt @@ -27,7 +27,11 @@ class AggregatedHealthDataActionRunner : ): TaskerPluginResult { Log.d(TAG, "run: $input") val repository = HealthConnectRepository(context) - val offsetTime = daysToOffsetTime(input.regular.days) + val days = runCatching { input.regular.days.toLong() }.getOrElse { + Log.e(TAG, "invalid input: ${input.regular.days}") + return TaskerPluginResultErrorWithOutput(Throwable(it)) + } + val offsetTime = daysToOffsetTime(days) if (!repository.isAvailable() || runBlocking { !repository.hasPermissions() }) { val errMessage = context.getString(R.string.health_connect_unavailable_or_permissions) diff --git a/app/src/main/java/com/rafapps/taskerhealthconnect/aggregated/AggregatedHealthDataActivity.kt b/app/src/main/java/com/rafapps/taskerhealthconnect/aggregated/AggregatedHealthDataActivity.kt index e2408f9..f283c0d 100644 --- a/app/src/main/java/com/rafapps/taskerhealthconnect/aggregated/AggregatedHealthDataActivity.kt +++ b/app/src/main/java/com/rafapps/taskerhealthconnect/aggregated/AggregatedHealthDataActivity.kt @@ -45,7 +45,7 @@ class AggregatedHealthDataActivity : AppCompatActivity(), ) override fun assignFromInput(input: TaskerInput) { - binding.daysText.editText?.setText(input.regular.days.toString()) + binding.daysText.editText?.setText(input.regular.days) } override fun onCreate(savedInstanceState: Bundle?) { @@ -93,10 +93,8 @@ class AggregatedHealthDataActivity : AppCompatActivity(), } } - private fun getInputDays(): Long { - return runCatching { - binding.daysText.editText?.text.toString().toLong() - }.getOrDefault(0L) + private fun getInputDays(): String { + return binding.daysText.editText?.text.toString() } private fun hideKeyboard() { @@ -110,7 +108,8 @@ class AggregatedHealthDataActivity : AppCompatActivity(), binding.debugButton.isVisible = BuildConfig.DEBUG binding.debugButton.setOnClickListener { lifecycleScope.launch { - val startTime = AggregatedHealthDataActionRunner.daysToOffsetTime(getInputDays()) + val startTime = AggregatedHealthDataActionRunner.daysToOffsetTime( + getInputDays().toLongOrNull() ?: 0L) val endTime = Instant.now() runCatching { val output = repository.getAggregateData(startTime, endTime) diff --git a/app/src/main/java/com/rafapps/taskerhealthconnect/aggregated/AggregatedHealthDataInput.kt b/app/src/main/java/com/rafapps/taskerhealthconnect/aggregated/AggregatedHealthDataInput.kt index 0573b88..0bfefde 100644 --- a/app/src/main/java/com/rafapps/taskerhealthconnect/aggregated/AggregatedHealthDataInput.kt +++ b/app/src/main/java/com/rafapps/taskerhealthconnect/aggregated/AggregatedHealthDataInput.kt @@ -12,5 +12,9 @@ class AggregatedHealthDataInput @JvmOverloads constructor( key = "days", labelResId = R.string.days, descriptionResId = R.string.days_description - ) var days: Long = 0L -) + ) var days: String = "0" +) { + override fun toString(): String { + return "days: $days" + } +} diff --git a/app/src/main/java/com/rafapps/taskerhealthconnect/aggregated/AggregatedHealthDataOutput.kt b/app/src/main/java/com/rafapps/taskerhealthconnect/aggregated/AggregatedHealthDataOutput.kt index 851c92c..c975d63 100644 --- a/app/src/main/java/com/rafapps/taskerhealthconnect/aggregated/AggregatedHealthDataOutput.kt +++ b/app/src/main/java/com/rafapps/taskerhealthconnect/aggregated/AggregatedHealthDataOutput.kt @@ -13,4 +13,8 @@ class AggregatedHealthDataOutput( labelResId = R.string.aggregated_health_data, htmlLabelResId = R.string.aggregated_health_data_description ) val aggregatedHealthData: String = "[]" -) +) { + override fun toString(): String { + return "aggregatedHealthData: $aggregatedHealthData" + } +} diff --git a/app/src/main/res/layout/activity_aggregated_health_data.xml b/app/src/main/res/layout/activity_aggregated_health_data.xml index 657733d..ec84731 100644 --- a/app/src/main/res/layout/activity_aggregated_health_data.xml +++ b/app/src/main/res/layout/activity_aggregated_health_data.xml @@ -26,8 +26,7 @@ + android:layout_height="wrap_content" />