rentedinstance on instances callback and dashboard view model

This commit is contained in:
Minecon724 2024-08-01 14:07:14 +02:00
parent 3ae52c6638
commit 0a85bb20e0
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
2 changed files with 7 additions and 7 deletions

View file

@ -5,7 +5,7 @@ import eu.m724.vastapp.vastai.ApiRoute
import eu.m724.vastapp.vastai.VastApi import eu.m724.vastapp.vastai.VastApi
import eu.m724.vastapp.vastai.api.InstancesUrlRequestCallback import eu.m724.vastapp.vastai.api.InstancesUrlRequestCallback
import eu.m724.vastapp.vastai.api.UserUrlRequestCallback import eu.m724.vastapp.vastai.api.UserUrlRequestCallback
import eu.m724.vastapp.vastai.data.Instance import eu.m724.vastapp.vastai.data.RentedInstance
import eu.m724.vastapp.vastai.data.User import eu.m724.vastapp.vastai.data.User
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
@ -19,8 +19,8 @@ class DashboardViewModel(initialUser: User, private val vastApi: VastApi) : View
val uiState: StateFlow<DashboardUiState> = val uiState: StateFlow<DashboardUiState> =
_uiState.asStateFlow() _uiState.asStateFlow()
private val _rentedInstances: MutableStateFlow<List<Instance>> = MutableStateFlow(emptyList()) private val _rentedInstances: MutableStateFlow<List<RentedInstance>> = MutableStateFlow(emptyList())
val rentedInstances: StateFlow<List<Instance>> = _rentedInstances.asStateFlow() val rentedInstances: StateFlow<List<RentedInstance>> = _rentedInstances.asStateFlow()
private val _user: MutableStateFlow<User> = MutableStateFlow(initialUser) private val _user: MutableStateFlow<User> = MutableStateFlow(initialUser)
val user: StateFlow<User> = _user.asStateFlow() val user: StateFlow<User> = _user.asStateFlow()

View file

@ -1,7 +1,7 @@
package eu.m724.vastapp.vastai.api package eu.m724.vastapp.vastai.api
import eu.m724.vastapp.vastai.ApiFailure import eu.m724.vastapp.vastai.ApiFailure
import eu.m724.vastapp.vastai.data.Instance import eu.m724.vastapp.vastai.data.RentedInstance
import org.chromium.net.CronetException import org.chromium.net.CronetException
import org.chromium.net.UrlRequest import org.chromium.net.UrlRequest
import org.chromium.net.UrlResponseInfo import org.chromium.net.UrlResponseInfo
@ -10,7 +10,7 @@ import java.nio.ByteBuffer
import java.nio.charset.CodingErrorAction import java.nio.charset.CodingErrorAction
class InstancesUrlRequestCallback( class InstancesUrlRequestCallback(
val onSuccess: (List<Instance>) -> Unit, val onSuccess: (List<RentedInstance>) -> Unit,
val onFailure: (ApiFailure) -> Unit val onFailure: (ApiFailure) -> Unit
) : UrlRequest.Callback() { ) : UrlRequest.Callback() {
@ -43,11 +43,11 @@ class InstancesUrlRequestCallback(
println(stringResponse) // TODO don't do that println(stringResponse) // TODO don't do that
if (info?.httpStatusCode == 200) { if (info?.httpStatusCode == 200) {
val jsonResponse = JSONObject(stringResponse.toString()) val jsonResponse = JSONObject(stringResponse.toString())
val instances = ArrayList<Instance>() val instances = ArrayList<RentedInstance>()
val instancesJson = jsonResponse.getJSONArray("instances") val instancesJson = jsonResponse.getJSONArray("instances")
for (i in 0..<instancesJson.length()) { for (i in 0..<instancesJson.length()) {
instances.add(Instance.fromJson(instancesJson.getJSONObject(i))) instances.add(RentedInstance.fromJson(instancesJson.getJSONObject(i)))
} }
onSuccess(instances) // TODO handle json errors onSuccess(instances) // TODO handle json errors