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
|
||||
)
|
||||
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
|
||||
)
|
||||
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
|
||||
var registrationCode by mutableStateOf("")
|
||||
private set
|
||||
var connectionState by mutableStateOf(Settings.ConnectionState.Unregistered)
|
||||
private set
|
||||
|
||||
init {
|
||||
checkConnectionState()
|
||||
}
|
||||
|
||||
fun connect() {
|
||||
//Log.i("SettingsScreen", "Save settings: $serverUrl, $deviceName, $registrationCode")
|
||||
viewModelScope.launch {
|
||||
deviceService.setServerAddress(serverAddress)
|
||||
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) {
|
||||
private var client = HttpClient(CIO)
|
||||
private var serverAddress: String = ""
|
||||
private var token: String = ""
|
||||
|
||||
init {
|
||||
loadPreferences()
|
||||
@ -25,20 +26,23 @@ class DeviceService(private val context: Context) {
|
||||
|
||||
suspend fun createIntegration(name: String, code: String) {
|
||||
Log.i("DeviceService", "Creating integration for $name with code $code at $serverAddress")
|
||||
val json = JSONObject()
|
||||
json.put("name", name)
|
||||
json.put("code", code)
|
||||
val requestJson = JSONObject()
|
||||
requestJson.put("name", name)
|
||||
requestJson.put("code", code)
|
||||
try {
|
||||
val response: HttpResponse = client.request("http://$serverAddress/api/integrations") {
|
||||
method = HttpMethod.Post
|
||||
setBody(json.toString())
|
||||
setBody(requestJson.toString())
|
||||
headers {
|
||||
append("Content-Type", "application/json")
|
||||
}
|
||||
}
|
||||
|
||||
savePreferences()
|
||||
val body: String = response.body()
|
||||
val responseJson = JSONObject(body)
|
||||
token = responseJson.getString("token")
|
||||
savePreferences()
|
||||
|
||||
Log.i("DeviceService", "Response: ${response.status.value} $body")
|
||||
} catch (e: Exception) {
|
||||
Log.e("DeviceService", "Error creating integration", e)
|
||||
@ -48,6 +52,8 @@ class DeviceService(private val context: Context) {
|
||||
private fun loadPreferences() {
|
||||
val sharedPreferences = context.getSharedPreferences(SHARED_PREFERENCES_NAME, MODE_PRIVATE)
|
||||
serverAddress = sharedPreferences.getString("server_address", "")!!
|
||||
token = sharedPreferences.getString("token", "")!!
|
||||
Log.i("DeviceService", "Loaded preferences: $serverAddress $token")
|
||||
}
|
||||
|
||||
private fun savePreferences() {
|
||||
@ -55,6 +61,7 @@ class DeviceService(private val context: Context) {
|
||||
val editor = sharedPreferences.edit()
|
||||
editor.apply {
|
||||
putString("server_address", serverAddress)
|
||||
putString("token", token)
|
||||
apply()
|
||||
}
|
||||
}
|
||||
@ -66,4 +73,8 @@ class DeviceService(private val context: Context) {
|
||||
fun getServerAddress(): String {
|
||||
return serverAddress
|
||||
}
|
||||
|
||||
fun getToken(): String {
|
||||
return token
|
||||
}
|
||||
}
|
||||
|
||||
@ -11,4 +11,6 @@
|
||||
<string name="save_button_label">Save</string>
|
||||
<string name="connect_button_label">Connect</string>
|
||||
<string name="server_address_label">Server address</string>
|
||||
<string name="connection_state_unregistered">unregistered</string>
|
||||
<string name="connection_state_registered">registered</string>
|
||||
</resources>
|
||||
Loading…
Reference in New Issue
Block a user