feat: fetch integration data on app launch

This commit is contained in:
Fritz Heiden 2025-03-20 12:03:15 +01:00
parent 52099b2e09
commit d8395a28a4
3 changed files with 55 additions and 3 deletions

View File

@ -20,7 +20,10 @@ class SettingsViewModel(private val deviceService: DeviceService) : ViewModel()
private set
init {
checkConnectionState()
updateConnectionState()
viewModelScope.launch {
updateDeviceName()
}
}
fun connect() {
@ -28,11 +31,11 @@ class SettingsViewModel(private val deviceService: DeviceService) : ViewModel()
viewModelScope.launch {
deviceService.setServerAddress(serverAddress)
deviceService.createIntegration(deviceName, registrationCode)
checkConnectionState()
updateConnectionState()
}
}
private fun checkConnectionState() {
private fun updateConnectionState() {
connectionState = if (deviceService.getToken().isEmpty()) {
Settings.ConnectionState.Unregistered
return
@ -41,6 +44,12 @@ class SettingsViewModel(private val deviceService: DeviceService) : ViewModel()
}
}
private suspend fun updateDeviceName() {
if (connectionState == Settings.ConnectionState.Unregistered) return
val integration = deviceService.getIntegration() ?: return
deviceName = integration.name
}
fun onServerAddressChanged(url: String) {
serverAddress = url
}

View File

@ -0,0 +1,4 @@
package com.example.tvcontroller.data
data class Integration(var id: String = "", var name: String = "", var token: String = "") {
}

View File

@ -3,6 +3,7 @@ package com.example.tvcontroller.services
import android.content.Context
import android.content.Context.MODE_PRIVATE
import android.util.Log
import com.example.tvcontroller.data.Integration
import io.ktor.client.engine.cio.*
import io.ktor.client.*
import io.ktor.client.call.body
@ -19,6 +20,7 @@ class DeviceService(private val context: Context) {
private var client = HttpClient(CIO)
private var serverAddress: String = ""
private var token: String = ""
private var deviceId: String = ""
init {
loadPreferences()
@ -40,7 +42,13 @@ class DeviceService(private val context: Context) {
val body: String = response.body()
val responseJson = JSONObject(body)
if (responseJson.has("error")) {
val error = responseJson.getString("error")
Log.e("DeviceService", "Error creating integration: $error")
return
}
token = responseJson.getString("token")
deviceId = responseJson.getString("id")
savePreferences()
Log.i("DeviceService", "Response: ${response.status.value} $body")
@ -49,10 +57,40 @@ class DeviceService(private val context: Context) {
}
}
suspend fun getIntegration(): Integration? {
Log.i("DeviceService", "Getting integration $deviceId at $serverAddress")
try {
val response: HttpResponse =
client.request("http://$serverAddress/api/integrations/$deviceId") {
method = HttpMethod.Get
headers {
append("Authorization", "Bearer $token")
}
}
val body: String = response.body()
Log.i("DeviceService", "Response: ${response.status.value} $body")
if (response.status.value != 200) {
return null
}
val responseJson = JSONObject(body)
val integration = Integration(
responseJson.getString("id"),
responseJson.getString("name")
)
Log.i("DeviceService", "Response: ${response.status.value} $body")
return integration
} catch (e: Exception) {
Log.e("DeviceService", "Error getting integration", e)
}
return null
}
private fun loadPreferences() {
val sharedPreferences = context.getSharedPreferences(SHARED_PREFERENCES_NAME, MODE_PRIVATE)
serverAddress = sharedPreferences.getString("server_address", "")!!
token = sharedPreferences.getString("token", "")!!
deviceId = sharedPreferences.getString("device_id", "")!!
Log.i("DeviceService", "Loaded preferences: $serverAddress $token")
}
@ -62,6 +100,7 @@ class DeviceService(private val context: Context) {
editor.apply {
putString("server_address", serverAddress)
putString("token", token)
putString("device_id", deviceId)
apply()
}
}