feat: fetch integration data on app launch
This commit is contained in:
parent
52099b2e09
commit
d8395a28a4
@ -20,7 +20,10 @@ class SettingsViewModel(private val deviceService: DeviceService) : ViewModel()
|
|||||||
private set
|
private set
|
||||||
|
|
||||||
init {
|
init {
|
||||||
checkConnectionState()
|
updateConnectionState()
|
||||||
|
viewModelScope.launch {
|
||||||
|
updateDeviceName()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun connect() {
|
fun connect() {
|
||||||
@ -28,11 +31,11 @@ class SettingsViewModel(private val deviceService: DeviceService) : ViewModel()
|
|||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
deviceService.setServerAddress(serverAddress)
|
deviceService.setServerAddress(serverAddress)
|
||||||
deviceService.createIntegration(deviceName, registrationCode)
|
deviceService.createIntegration(deviceName, registrationCode)
|
||||||
checkConnectionState()
|
updateConnectionState()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkConnectionState() {
|
private fun updateConnectionState() {
|
||||||
connectionState = if (deviceService.getToken().isEmpty()) {
|
connectionState = if (deviceService.getToken().isEmpty()) {
|
||||||
Settings.ConnectionState.Unregistered
|
Settings.ConnectionState.Unregistered
|
||||||
return
|
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) {
|
fun onServerAddressChanged(url: String) {
|
||||||
serverAddress = url
|
serverAddress = url
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,4 @@
|
|||||||
|
package com.example.tvcontroller.data
|
||||||
|
|
||||||
|
data class Integration(var id: String = "", var name: String = "", var token: String = "") {
|
||||||
|
}
|
||||||
@ -3,6 +3,7 @@ package com.example.tvcontroller.services
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Context.MODE_PRIVATE
|
import android.content.Context.MODE_PRIVATE
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import com.example.tvcontroller.data.Integration
|
||||||
import io.ktor.client.engine.cio.*
|
import io.ktor.client.engine.cio.*
|
||||||
import io.ktor.client.*
|
import io.ktor.client.*
|
||||||
import io.ktor.client.call.body
|
import io.ktor.client.call.body
|
||||||
@ -19,6 +20,7 @@ class DeviceService(private val context: Context) {
|
|||||||
private var client = HttpClient(CIO)
|
private var client = HttpClient(CIO)
|
||||||
private var serverAddress: String = ""
|
private var serverAddress: String = ""
|
||||||
private var token: String = ""
|
private var token: String = ""
|
||||||
|
private var deviceId: String = ""
|
||||||
|
|
||||||
init {
|
init {
|
||||||
loadPreferences()
|
loadPreferences()
|
||||||
@ -40,7 +42,13 @@ class DeviceService(private val context: Context) {
|
|||||||
|
|
||||||
val body: String = response.body()
|
val body: String = response.body()
|
||||||
val responseJson = JSONObject(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")
|
token = responseJson.getString("token")
|
||||||
|
deviceId = responseJson.getString("id")
|
||||||
savePreferences()
|
savePreferences()
|
||||||
|
|
||||||
Log.i("DeviceService", "Response: ${response.status.value} $body")
|
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() {
|
private fun loadPreferences() {
|
||||||
val sharedPreferences = context.getSharedPreferences(SHARED_PREFERENCES_NAME, MODE_PRIVATE)
|
val sharedPreferences = context.getSharedPreferences(SHARED_PREFERENCES_NAME, MODE_PRIVATE)
|
||||||
serverAddress = sharedPreferences.getString("server_address", "")!!
|
serverAddress = sharedPreferences.getString("server_address", "")!!
|
||||||
token = sharedPreferences.getString("token", "")!!
|
token = sharedPreferences.getString("token", "")!!
|
||||||
|
deviceId = sharedPreferences.getString("device_id", "")!!
|
||||||
Log.i("DeviceService", "Loaded preferences: $serverAddress $token")
|
Log.i("DeviceService", "Loaded preferences: $serverAddress $token")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,6 +100,7 @@ class DeviceService(private val context: Context) {
|
|||||||
editor.apply {
|
editor.apply {
|
||||||
putString("server_address", serverAddress)
|
putString("server_address", serverAddress)
|
||||||
putString("token", token)
|
putString("token", token)
|
||||||
|
putString("device_id", deviceId)
|
||||||
apply()
|
apply()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user