From 14eebe0c3d8ab8bb97eda978fbf89eca4596d278 Mon Sep 17 00:00:00 2001 From: William Brawner Date: Tue, 23 Feb 2021 17:05:11 -0700 Subject: [PATCH] Remove unnecessary coroutine interactions Only reading, saving, and deleting the config actually involve disk operations, and even the network operations have a 500ms timeout, so it doesn't make sense to have all of the methods support concurrency. The increased complexity outweighs the benefits of following the best practice of keeping disk and network operations off the main thread. --- .idea/.name | 2 +- .../wbrawner/pihelper/AddPiHelperViewModel.kt | 17 +++++------ .../piholeclient/ConfigPersistenceHelper.kt | 12 ++++---- .../NativePiHelperConfigPersistenceHelper.kt | 29 ++++++++++--------- 4 files changed, 29 insertions(+), 31 deletions(-) diff --git a/.idea/.name b/.idea/.name index db548ff..486df6e 100644 --- a/.idea/.name +++ b/.idea/.name @@ -1 +1 @@ -Pi-Helper \ No newline at end of file +Pi-helper \ No newline at end of file diff --git a/app/src/main/java/com/wbrawner/pihelper/AddPiHelperViewModel.kt b/app/src/main/java/com/wbrawner/pihelper/AddPiHelperViewModel.kt index c17353a..10f7abc 100644 --- a/app/src/main/java/com/wbrawner/pihelper/AddPiHelperViewModel.kt +++ b/app/src/main/java/com/wbrawner/pihelper/AddPiHelperViewModel.kt @@ -19,9 +19,8 @@ class AddPiHelperViewModel( private val configHelper: ConfigPersistenceHelper ) : ViewModel() { - suspend fun getBaseUrl() = configHelper.getHost() - - suspend fun getApiKey() = configHelper.getApiKey() + val baseUrl: String? = configHelper.host + val apiKey: String? = configHelper.apiKey val piHoleIpAddress = MutableLiveData() val scanningIp = MutableLiveData() @@ -67,7 +66,7 @@ class AddPiHelperViewModel( suspend fun connectToIpAddress(ipAddress: String): Boolean { val status: Status? = withContext(Dispatchers.IO) { try { - configHelper.setHost(ipAddress) + configHelper.host = ipAddress apiService.getStatus() } catch (ignored: ConnectException) { null @@ -87,19 +86,19 @@ class AddPiHelperViewModel( } } - suspend fun authenticateWithPassword(password: String) { + fun authenticateWithPassword(password: String) { configHelper.setPassword(password) configHelper.saveConfig() authenticate() } - suspend fun authenticateWithApiKey(apiKey: String) { - configHelper.setApiKey(apiKey) + fun authenticateWithApiKey(apiKey: String) { + configHelper.apiKey = apiKey configHelper.saveConfig() authenticate() } - private suspend fun authenticate() { + private fun authenticate() { try { // This uses the topItems endpoint to test that the API key is working since it requires // authentication and is fairly simple to determine whether or not the request was @@ -113,7 +112,7 @@ class AddPiHelperViewModel( } } - suspend fun forgetPihole() { + fun forgetPihole() { configHelper.deleteConfig() } } diff --git a/piholeclient/src/main/java/com/wbrawner/piholeclient/ConfigPersistenceHelper.kt b/piholeclient/src/main/java/com/wbrawner/piholeclient/ConfigPersistenceHelper.kt index 8554e7e..298fa42 100644 --- a/piholeclient/src/main/java/com/wbrawner/piholeclient/ConfigPersistenceHelper.kt +++ b/piholeclient/src/main/java/com/wbrawner/piholeclient/ConfigPersistenceHelper.kt @@ -1,12 +1,10 @@ package com.wbrawner.piholeclient interface ConfigPersistenceHelper { - suspend fun getHost(): String? - suspend fun setHost(host: String?) - suspend fun getApiKey(): String? - suspend fun setApiKey(apiKey: String?) - suspend fun setPassword(password: String?) - suspend fun saveConfig() - suspend fun deleteConfig() + var host: String? + var apiKey: String? + fun setPassword(password: String?) + fun saveConfig() + fun deleteConfig() } diff --git a/piholeclient/src/main/java/com/wbrawner/piholeclient/NativePiHelperConfigPersistenceHelper.kt b/piholeclient/src/main/java/com/wbrawner/piholeclient/NativePiHelperConfigPersistenceHelper.kt index 83dd1cf..6eeed94 100644 --- a/piholeclient/src/main/java/com/wbrawner/piholeclient/NativePiHelperConfigPersistenceHelper.kt +++ b/piholeclient/src/main/java/com/wbrawner/piholeclient/NativePiHelperConfigPersistenceHelper.kt @@ -3,7 +3,8 @@ package com.wbrawner.piholeclient import com.wbrawner.libpihelper.PiHelperNative import java.io.File -class NativePiHelperConfigPersistenceHelper(private val configFile: File): ConfigPersistenceHelper { +class NativePiHelperConfigPersistenceHelper(private val configFile: File) : + ConfigPersistenceHelper { init { if (configFile.exists()) { PiHelperNative.readConfig(configFile.absolutePath) @@ -12,27 +13,27 @@ class NativePiHelperConfigPersistenceHelper(private val configFile: File): Confi } } - override suspend fun getHost(): String? = PiHelperNative.getHost() + override var host: String? + get() = PiHelperNative.getHost() + set(value) { + PiHelperNative.setHost(value) + } - override suspend fun setHost(host: String?) { - PiHelperNative.setHost(host) - } + override var apiKey: String? + get() = PiHelperNative.getApiKey() + set(value) { + PiHelperNative.setApiKey(value) + } - override suspend fun getApiKey(): String? = PiHelperNative.getApiKey() - - override suspend fun setApiKey(apiKey: String?) { - PiHelperNative.setApiKey(apiKey) - } - - override suspend fun setPassword(password: String?) { + override fun setPassword(password: String?) { PiHelperNative.setPassword(password) } - override suspend fun saveConfig() { + override fun saveConfig() { PiHelperNative.saveConfig(configFile.absolutePath) } - override suspend fun deleteConfig() { + override fun deleteConfig() { PiHelperNative.cleanup() configFile.delete() PiHelperNative.initConfig()