feat: persist token and show connection state
This commit is contained in:
parent
83153871b2
commit
52099b2e09
8
app/src/main/java/com/example/tvcontroller/Settings.kt
Normal file
8
app/src/main/java/com/example/tvcontroller/Settings.kt
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
package com.example.tvcontroller
|
||||||
|
|
||||||
|
object Settings {
|
||||||
|
enum class ConnectionState {
|
||||||
|
Unregistered,
|
||||||
|
Registered,
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -40,7 +40,9 @@ fun SettingsScreen(deviceService: DeviceService, appViewModel: AppViewModel) {
|
|||||||
style = MaterialTheme.typography.headlineSmall
|
style = MaterialTheme.typography.headlineSmall
|
||||||
)
|
)
|
||||||
Text(
|
Text(
|
||||||
text = stringResource(id = R.string.server_connection_label) + ": " + stringResource(id = R.string.connection_state_disconnected),
|
text = stringResource(id = R.string.server_connection_label) + ": " + getConnectionStateString(
|
||||||
|
viewModel.connectionState
|
||||||
|
),
|
||||||
style = MaterialTheme.typography.bodyMedium
|
style = MaterialTheme.typography.bodyMedium
|
||||||
)
|
)
|
||||||
OutlinedTextField(
|
OutlinedTextField(
|
||||||
@ -84,3 +86,11 @@ fun SettingsScreen(deviceService: DeviceService, appViewModel: AppViewModel) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun getConnectionStateString(state: Settings.ConnectionState): String {
|
||||||
|
return when (state) {
|
||||||
|
Settings.ConnectionState.Unregistered -> stringResource(id = R.string.connection_state_unregistered)
|
||||||
|
Settings.ConnectionState.Registered -> stringResource(id = R.string.connection_state_registered)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -16,12 +16,28 @@ class SettingsViewModel(private val deviceService: DeviceService) : ViewModel()
|
|||||||
private set
|
private set
|
||||||
var registrationCode by mutableStateOf("")
|
var registrationCode by mutableStateOf("")
|
||||||
private set
|
private set
|
||||||
|
var connectionState by mutableStateOf(Settings.ConnectionState.Unregistered)
|
||||||
|
private set
|
||||||
|
|
||||||
|
init {
|
||||||
|
checkConnectionState()
|
||||||
|
}
|
||||||
|
|
||||||
fun connect() {
|
fun connect() {
|
||||||
//Log.i("SettingsScreen", "Save settings: $serverUrl, $deviceName, $registrationCode")
|
//Log.i("SettingsScreen", "Save settings: $serverUrl, $deviceName, $registrationCode")
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
deviceService.setServerAddress(serverAddress)
|
deviceService.setServerAddress(serverAddress)
|
||||||
deviceService.createIntegration(deviceName, registrationCode)
|
deviceService.createIntegration(deviceName, registrationCode)
|
||||||
|
checkConnectionState()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun checkConnectionState() {
|
||||||
|
connectionState = if (deviceService.getToken().isEmpty()) {
|
||||||
|
Settings.ConnectionState.Unregistered
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
Settings.ConnectionState.Registered
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -18,6 +18,7 @@ private const val SHARED_PREFERENCES_NAME = "devices";
|
|||||||
class DeviceService(private val context: Context) {
|
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 = ""
|
||||||
|
|
||||||
init {
|
init {
|
||||||
loadPreferences()
|
loadPreferences()
|
||||||
@ -25,20 +26,23 @@ class DeviceService(private val context: Context) {
|
|||||||
|
|
||||||
suspend fun createIntegration(name: String, code: String) {
|
suspend fun createIntegration(name: String, code: String) {
|
||||||
Log.i("DeviceService", "Creating integration for $name with code $code at $serverAddress")
|
Log.i("DeviceService", "Creating integration for $name with code $code at $serverAddress")
|
||||||
val json = JSONObject()
|
val requestJson = JSONObject()
|
||||||
json.put("name", name)
|
requestJson.put("name", name)
|
||||||
json.put("code", code)
|
requestJson.put("code", code)
|
||||||
try {
|
try {
|
||||||
val response: HttpResponse = client.request("http://$serverAddress/api/integrations") {
|
val response: HttpResponse = client.request("http://$serverAddress/api/integrations") {
|
||||||
method = HttpMethod.Post
|
method = HttpMethod.Post
|
||||||
setBody(json.toString())
|
setBody(requestJson.toString())
|
||||||
headers {
|
headers {
|
||||||
append("Content-Type", "application/json")
|
append("Content-Type", "application/json")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
savePreferences()
|
|
||||||
val body: String = response.body()
|
val body: String = response.body()
|
||||||
|
val responseJson = JSONObject(body)
|
||||||
|
token = responseJson.getString("token")
|
||||||
|
savePreferences()
|
||||||
|
|
||||||
Log.i("DeviceService", "Response: ${response.status.value} $body")
|
Log.i("DeviceService", "Response: ${response.status.value} $body")
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e("DeviceService", "Error creating integration", e)
|
Log.e("DeviceService", "Error creating integration", e)
|
||||||
@ -48,6 +52,8 @@ class DeviceService(private val context: Context) {
|
|||||||
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", "")!!
|
||||||
|
Log.i("DeviceService", "Loaded preferences: $serverAddress $token")
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun savePreferences() {
|
private fun savePreferences() {
|
||||||
@ -55,6 +61,7 @@ class DeviceService(private val context: Context) {
|
|||||||
val editor = sharedPreferences.edit()
|
val editor = sharedPreferences.edit()
|
||||||
editor.apply {
|
editor.apply {
|
||||||
putString("server_address", serverAddress)
|
putString("server_address", serverAddress)
|
||||||
|
putString("token", token)
|
||||||
apply()
|
apply()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -66,4 +73,8 @@ class DeviceService(private val context: Context) {
|
|||||||
fun getServerAddress(): String {
|
fun getServerAddress(): String {
|
||||||
return serverAddress
|
return serverAddress
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getToken(): String {
|
||||||
|
return token
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,4 +11,6 @@
|
|||||||
<string name="save_button_label">Save</string>
|
<string name="save_button_label">Save</string>
|
||||||
<string name="connect_button_label">Connect</string>
|
<string name="connect_button_label">Connect</string>
|
||||||
<string name="server_address_label">Server address</string>
|
<string name="server_address_label">Server address</string>
|
||||||
|
<string name="connection_state_unregistered">unregistered</string>
|
||||||
|
<string name="connection_state_registered">registered</string>
|
||||||
</resources>
|
</resources>
|
||||||
Loading…
Reference in New Issue
Block a user