promviewmodel refactoring

This commit is contained in:
Martin Ptáček
2023-05-31 13:12:10 +02:00
parent 78e1746712
commit 15fe164855
4 changed files with 32 additions and 40 deletions

View File

@ -305,6 +305,9 @@ private fun ConfigFileErrorPage(
){
//TODO implement this
Text("Config File error page")
if (uiState.fileLoadException != null) {
Text(uiState.fileLoadException!!)
}
}
}

View File

@ -1,6 +1,5 @@
package com.birdthedeveloper.prometheus.android.prometheus.android.exporter.compose
import android.content.ComponentName
import android.content.Context
import android.util.Log
import androidx.lifecycle.ViewModel
@ -9,14 +8,10 @@ import androidx.work.Constraints
import androidx.work.Data
import androidx.work.ExistingWorkPolicy
import androidx.work.NetworkType
import androidx.work.OneTimeWorkRequest
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.OutOfQuotaPolicy
import androidx.work.WorkManager
import androidx.work.multiprocess.RemoteListenableWorker
import androidx.work.multiprocess.RemoteWorkerService
import com.birdthedeveloper.prometheus.android.prometheus.android.exporter.worker.PromWorker
import com.birdthedeveloper.prometheus.android.prometheus.android.exporter.worker.PushProxWorker
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
@ -59,7 +54,9 @@ data class PromUiState(
class PromViewModel(): ViewModel() {
private val PROM_UNIQUE_WORK : String = "prom_unique_job"
companion object {
private const val PROM_UNIQUE_WORK : String = "prom_unique_job"
}
private val _uiState = MutableStateFlow(PromUiState())
val uiState : StateFlow<PromUiState> = _uiState.asStateFlow()
@ -69,7 +66,6 @@ class PromViewModel(): ViewModel() {
private fun loadConfigurationFile(){
Log.v(TAG, "Checking for configuration file")
viewModelScope.launch {
Log.v(TAG, getContext().filesDir.absolutePath)
val fileExists = PromConfiguration.configFileExists(context = getContext())
if (fileExists) {
@ -92,13 +88,12 @@ class PromViewModel(): ViewModel() {
)
}
}
}else{
}else {
_uiState.update { current ->
current.copy(configFileState = ConfigFileState.MISSING)
}
}
}
}
fun toggleIsRunning(){
when(_uiState.value.exporterState) {

View File

@ -90,7 +90,7 @@ data class PromConfiguration(
// data/user/0/com.birdthedeveloper.prometheus.android.prometheus.android.exporter/files
private const val filename : String = "config.yaml"
private const val alternativeFilename : String = "config.yml"
suspend fun configFileExists(context : Context): Boolean {
fun configFileExists(context : Context): Boolean {
// using app-specific storage
val file = File(context.filesDir, filename)
val alternativeFile = File(context.filesDir, alternativeFilename)
@ -103,7 +103,7 @@ data class PromConfiguration(
return Json.decodeFromString<PromConfiguration>(jsonString)
}
suspend fun loadFromConfigFile(context : Context): PromConfiguration {
fun loadFromConfigFile(context : Context): PromConfiguration {
Log.v(TAG, context.filesDir.absolutePath)
val file = File(context.filesDir, filename)

View File

@ -4,6 +4,7 @@ import android.content.Context
import android.util.Log
import androidx.work.CoroutineWorker
import androidx.work.WorkerParameters
import com.birdthedeveloper.prometheus.android.prometheus.android.exporter.compose.PromConfiguration
import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO
import io.ktor.client.statement.HttpResponse
@ -19,14 +20,13 @@ class PromWorker(
) : CoroutineWorker(context, parameters) {
override suspend fun doWork(): Result {
//val inputConfiguration : PromConfiguration = PromConfiguration.fromWorkData(inputData)
val inputConfiguration : PromConfiguration = PromConfiguration.fromWorkData(inputData)
while(true){
Log.v(TAG, "Worker is working " + LocalDateTime.now().toString())
//TODO curl localhost
delay(1000L)
curlLocalhost()
}
//TODO implement this asap
@ -34,10 +34,4 @@ class PromWorker(
return Result.success()
}
private suspend fun curlLocalhost(){
val client = HttpClient(CIO)
val response: HttpResponse = client.get("http://localhost:8000")
Log.v(TAG, response.toString())
}
}