feat: check if device still registered on server
This commit is contained in:
parent
d8395a28a4
commit
04c93a0ce7
@ -22,7 +22,7 @@ class SettingsViewModel(private val deviceService: DeviceService) : ViewModel()
|
||||
init {
|
||||
updateConnectionState()
|
||||
viewModelScope.launch {
|
||||
updateDeviceName()
|
||||
updateDeviceInfo()
|
||||
}
|
||||
}
|
||||
|
||||
@ -44,9 +44,13 @@ class SettingsViewModel(private val deviceService: DeviceService) : ViewModel()
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun updateDeviceName() {
|
||||
private suspend fun updateDeviceInfo() {
|
||||
if (connectionState == Settings.ConnectionState.Unregistered) return
|
||||
val integration = deviceService.getIntegration() ?: return
|
||||
val integration = deviceService.getIntegration()
|
||||
if (integration == null) {
|
||||
connectionState = Settings.ConnectionState.Unregistered
|
||||
return
|
||||
}
|
||||
deviceName = integration.name
|
||||
}
|
||||
|
||||
|
||||
@ -11,6 +11,7 @@ import io.ktor.client.request.headers
|
||||
import io.ktor.client.request.request
|
||||
import io.ktor.client.request.setBody
|
||||
import io.ktor.client.statement.HttpResponse
|
||||
import io.ktor.http.HeadersBuilder
|
||||
import io.ktor.http.HttpMethod
|
||||
import org.json.JSONObject
|
||||
|
||||
@ -42,9 +43,9 @@ class DeviceService(private val context: Context) {
|
||||
|
||||
val body: String = response.body()
|
||||
val responseJson = JSONObject(body)
|
||||
if (responseJson.has("error")) {
|
||||
if (response.status.value != 200) {
|
||||
val error = responseJson.getString("error")
|
||||
Log.e("DeviceService", "Error creating integration: $error")
|
||||
Log.e("DeviceService", "Error getting integration: ${response.status.value} $error")
|
||||
return
|
||||
}
|
||||
token = responseJson.getString("token")
|
||||
@ -69,16 +70,16 @@ class DeviceService(private val context: Context) {
|
||||
}
|
||||
|
||||
val body: String = response.body()
|
||||
Log.i("DeviceService", "Response: ${response.status.value} $body")
|
||||
val responseJson = JSONObject(body)
|
||||
if (response.status.value != 200) {
|
||||
val error = responseJson.getString("error")
|
||||
Log.e("DeviceService", "Error getting integration: ${response.status.value} $error")
|
||||
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)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user