diff --git a/app/src/main/java/com/example/tvcontroller/SettingsViewModel.kt b/app/src/main/java/com/example/tvcontroller/SettingsViewModel.kt index 069f923..3b0549e 100644 --- a/app/src/main/java/com/example/tvcontroller/SettingsViewModel.kt +++ b/app/src/main/java/com/example/tvcontroller/SettingsViewModel.kt @@ -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 } diff --git a/app/src/main/java/com/example/tvcontroller/data/Integration.kt b/app/src/main/java/com/example/tvcontroller/data/Integration.kt new file mode 100644 index 0000000..a5cc209 --- /dev/null +++ b/app/src/main/java/com/example/tvcontroller/data/Integration.kt @@ -0,0 +1,4 @@ +package com.example.tvcontroller.data + +data class Integration(var id: String = "", var name: String = "", var token: String = "") { +} \ No newline at end of file diff --git a/app/src/main/java/com/example/tvcontroller/services/DeviceService.kt b/app/src/main/java/com/example/tvcontroller/services/DeviceService.kt index 74ddc1f..148acc6 100644 --- a/app/src/main/java/com/example/tvcontroller/services/DeviceService.kt +++ b/app/src/main/java/com/example/tvcontroller/services/DeviceService.kt @@ -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() } }