Compare commits
1 Commits
e3623cb128
...
aefa7ebca7
| Author | SHA1 | Date | |
|---|---|---|---|
| aefa7ebca7 |
@ -1,13 +1,11 @@
|
||||
package com.example.tvcontroller
|
||||
|
||||
import android.bluetooth.BluetoothAdapter
|
||||
import android.content.ContentValues.TAG
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.activity.viewModels
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
@ -29,28 +27,22 @@ import com.example.tvcontroller.ui.theme.TVControllerTheme
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import com.example.tvcontroller.services.BluetoothService
|
||||
import com.example.tvcontroller.services.DeviceService
|
||||
import com.example.tvcontroller.ui.AppViewModel
|
||||
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
private lateinit var bluetoothService: BluetoothService
|
||||
private lateinit var deviceService: DeviceService
|
||||
private val appViewModel by viewModels<AppViewModel>()
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
bluetoothService = BluetoothService(this.applicationContext)
|
||||
bluetoothService.onBluetoothStateChanged { state -> appViewModel.setBluetoothEnabled(state == BluetoothAdapter.STATE_ON) }
|
||||
appViewModel.setBluetoothEnabled(bluetoothService.isBluetoothEnabled())
|
||||
deviceService = DeviceService(this.applicationContext)
|
||||
enableEdgeToEdge()
|
||||
setContent {
|
||||
TVControllerTheme {
|
||||
TvControllerApp(
|
||||
appViewModel = appViewModel,
|
||||
bluetoothService = bluetoothService,
|
||||
deviceService = deviceService
|
||||
)
|
||||
@ -62,8 +54,7 @@ class MainActivity : ComponentActivity() {
|
||||
@Composable
|
||||
fun TvControllerApp(
|
||||
navController: NavHostController = rememberNavController(),
|
||||
appViewModel: AppViewModel = viewModel(),
|
||||
bluetoothService: BluetoothService? = null,
|
||||
bluetoothService: BluetoothService,
|
||||
deviceService: DeviceService
|
||||
) {
|
||||
val backStackEntry by navController.currentBackStackEntryAsState()
|
||||
@ -121,7 +112,10 @@ fun TvControllerApp(
|
||||
RemoteScreen()
|
||||
}
|
||||
composable(route = Screen.Settings.name) {
|
||||
SettingsScreen(appViewModel = appViewModel, deviceService = deviceService)
|
||||
SettingsScreen(
|
||||
deviceService = deviceService,
|
||||
bluetoothService = bluetoothService
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -16,13 +16,16 @@ import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import com.example.tvcontroller.services.BluetoothService
|
||||
import com.example.tvcontroller.services.DeviceService
|
||||
import com.example.tvcontroller.ui.AppViewModel
|
||||
|
||||
@Composable
|
||||
fun SettingsScreen(deviceService: DeviceService, appViewModel: AppViewModel) {
|
||||
fun SettingsScreen(
|
||||
deviceService: DeviceService,
|
||||
bluetoothService: BluetoothService
|
||||
) {
|
||||
val viewModel =
|
||||
viewModel<SettingsViewModel>(factory = SettingsViewModel.provideFactory(deviceService))
|
||||
viewModel<SettingsViewModel>(factory = SettingsViewModel.provideFactory(deviceService, bluetoothService))
|
||||
|
||||
Column(
|
||||
modifier = Modifier
|
||||
@ -73,7 +76,7 @@ fun SettingsScreen(deviceService: DeviceService, appViewModel: AppViewModel) {
|
||||
text = stringResource(id = R.string.controller_settings_heading),
|
||||
style = MaterialTheme.typography.headlineSmall
|
||||
)
|
||||
if (appViewModel.isBluetoothEnabled()) {
|
||||
if (viewModel.bluetoothEnabled) {
|
||||
Text(
|
||||
text = "Controller settings: Bluetooth is enabled.",
|
||||
style = MaterialTheme.typography.bodyMedium
|
||||
|
||||
@ -1,15 +1,20 @@
|
||||
package com.example.tvcontroller
|
||||
|
||||
import android.bluetooth.BluetoothAdapter
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.example.tvcontroller.services.BluetoothService
|
||||
import com.example.tvcontroller.services.DeviceService
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class SettingsViewModel(private val deviceService: DeviceService) : ViewModel() {
|
||||
class SettingsViewModel(
|
||||
private val deviceService: DeviceService,
|
||||
private val bluetoothService: BluetoothService
|
||||
) : ViewModel() {
|
||||
var serverAddress by mutableStateOf(deviceService.getServerAddress())
|
||||
private set
|
||||
var deviceName by mutableStateOf(android.os.Build.MANUFACTURER + " " + android.os.Build.MODEL)
|
||||
@ -18,12 +23,17 @@ class SettingsViewModel(private val deviceService: DeviceService) : ViewModel()
|
||||
private set
|
||||
var connectionState by mutableStateOf(Settings.ConnectionState.Unregistered)
|
||||
private set
|
||||
var bluetoothEnabled by mutableStateOf(bluetoothService.isBluetoothEnabled())
|
||||
private set
|
||||
|
||||
init {
|
||||
updateConnectionState()
|
||||
viewModelScope.launch {
|
||||
updateDeviceInfo()
|
||||
}
|
||||
bluetoothService.onBluetoothStateChanged {
|
||||
bluetoothEnabled = it == BluetoothAdapter.STATE_ON
|
||||
}
|
||||
}
|
||||
|
||||
fun connect() {
|
||||
@ -69,10 +79,11 @@ class SettingsViewModel(private val deviceService: DeviceService) : ViewModel()
|
||||
companion object {
|
||||
fun provideFactory(
|
||||
deviceService: DeviceService,
|
||||
bluetoothService: BluetoothService,
|
||||
) = object : ViewModelProvider.Factory {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override fun <T : ViewModel> create(modelClass: Class<T>): T {
|
||||
return SettingsViewModel(deviceService) as T
|
||||
return SettingsViewModel(deviceService, bluetoothService) as T
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +0,0 @@
|
||||
package com.example.tvcontroller.ui
|
||||
|
||||
import com.example.tvcontroller.Screen
|
||||
|
||||
data class AppUiState(
|
||||
val currentScreen: Screen = Screen.Settings,
|
||||
)
|
||||
@ -1,16 +0,0 @@
|
||||
package com.example.tvcontroller.ui
|
||||
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.lifecycle.ViewModel
|
||||
|
||||
class AppViewModel() : ViewModel() {
|
||||
private var isBluetoothEnabled = mutableStateOf(false)
|
||||
|
||||
fun setBluetoothEnabled(enabled: Boolean) {
|
||||
isBluetoothEnabled.value = enabled
|
||||
}
|
||||
|
||||
fun isBluetoothEnabled(): Boolean {
|
||||
return isBluetoothEnabled.value
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user