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:
parent
cccf1e6b33
commit
14eebe0c3d
4 changed files with 29 additions and 31 deletions
|
@ -1 +1 @@
|
|||
Pi-Helper
|
||||
Pi-helper
|
|
@ -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<String?>()
|
||||
val scanningIp = MutableLiveData<String?>()
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue