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.
This commit is contained in:
William Brawner 2021-02-23 17:05:11 -07:00
parent cccf1e6b33
commit 14eebe0c3d
4 changed files with 29 additions and 31 deletions

View file

@ -1 +1 @@
Pi-Helper Pi-helper

View file

@ -19,9 +19,8 @@ class AddPiHelperViewModel(
private val configHelper: ConfigPersistenceHelper private val configHelper: ConfigPersistenceHelper
) : ViewModel() { ) : ViewModel() {
suspend fun getBaseUrl() = configHelper.getHost() val baseUrl: String? = configHelper.host
val apiKey: String? = configHelper.apiKey
suspend fun getApiKey() = configHelper.getApiKey()
val piHoleIpAddress = MutableLiveData<String?>() val piHoleIpAddress = MutableLiveData<String?>()
val scanningIp = MutableLiveData<String?>() val scanningIp = MutableLiveData<String?>()
@ -67,7 +66,7 @@ class AddPiHelperViewModel(
suspend fun connectToIpAddress(ipAddress: String): Boolean { suspend fun connectToIpAddress(ipAddress: String): Boolean {
val status: Status? = withContext(Dispatchers.IO) { val status: Status? = withContext(Dispatchers.IO) {
try { try {
configHelper.setHost(ipAddress) configHelper.host = ipAddress
apiService.getStatus() apiService.getStatus()
} catch (ignored: ConnectException) { } catch (ignored: ConnectException) {
null null
@ -87,19 +86,19 @@ class AddPiHelperViewModel(
} }
} }
suspend fun authenticateWithPassword(password: String) { fun authenticateWithPassword(password: String) {
configHelper.setPassword(password) configHelper.setPassword(password)
configHelper.saveConfig() configHelper.saveConfig()
authenticate() authenticate()
} }
suspend fun authenticateWithApiKey(apiKey: String) { fun authenticateWithApiKey(apiKey: String) {
configHelper.setApiKey(apiKey) configHelper.apiKey = apiKey
configHelper.saveConfig() configHelper.saveConfig()
authenticate() authenticate()
} }
private suspend fun authenticate() { private fun authenticate() {
try { try {
// This uses the topItems endpoint to test that the API key is working since it requires // 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 // 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() configHelper.deleteConfig()
} }
} }

View file

@ -1,12 +1,10 @@
package com.wbrawner.piholeclient package com.wbrawner.piholeclient
interface ConfigPersistenceHelper { interface ConfigPersistenceHelper {
suspend fun getHost(): String? var host: String?
suspend fun setHost(host: String?) var apiKey: String?
suspend fun getApiKey(): String? fun setPassword(password: String?)
suspend fun setApiKey(apiKey: String?) fun saveConfig()
suspend fun setPassword(password: String?) fun deleteConfig()
suspend fun saveConfig()
suspend fun deleteConfig()
} }

View file

@ -3,7 +3,8 @@ package com.wbrawner.piholeclient
import com.wbrawner.libpihelper.PiHelperNative import com.wbrawner.libpihelper.PiHelperNative
import java.io.File import java.io.File
class NativePiHelperConfigPersistenceHelper(private val configFile: File): ConfigPersistenceHelper { class NativePiHelperConfigPersistenceHelper(private val configFile: File) :
ConfigPersistenceHelper {
init { init {
if (configFile.exists()) { if (configFile.exists()) {
PiHelperNative.readConfig(configFile.absolutePath) 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()
override suspend fun setHost(host: String?) { set(value) {
PiHelperNative.setHost(host) PiHelperNative.setHost(value)
} }
override suspend fun getApiKey(): String? = PiHelperNative.getApiKey() override var apiKey: String?
get() = PiHelperNative.getApiKey()
override suspend fun setApiKey(apiKey: String?) { set(value) {
PiHelperNative.setApiKey(apiKey) PiHelperNative.setApiKey(value)
} }
override suspend fun setPassword(password: String?) { override fun setPassword(password: String?) {
PiHelperNative.setPassword(password) PiHelperNative.setPassword(password)
} }
override suspend fun saveConfig() { override fun saveConfig() {
PiHelperNative.saveConfig(configFile.absolutePath) PiHelperNative.saveConfig(configFile.absolutePath)
} }
override suspend fun deleteConfig() { override fun deleteConfig() {
PiHelperNative.cleanup() PiHelperNative.cleanup()
configFile.delete() configFile.delete()
PiHelperNative.initConfig() PiHelperNative.initConfig()