feat: add bluetooth enabled state to viewmodel
This commit is contained in:
parent
cb93718491
commit
47e716672d
@ -16,12 +16,13 @@ class BluetoothService(private val context: Context) {
|
||||
getSystemService(context, BluetoothManager::class.java)!!;
|
||||
private var bluetoothAdapter: BluetoothAdapter = bluetoothManager.adapter
|
||||
private var bluetoothStateReceiver: BroadcastReceiver? = null
|
||||
private var bluetoothStateChangedCallbacks: MutableList<(Int) -> Unit> = mutableListOf()
|
||||
|
||||
init {
|
||||
registerBluetoothStateReceiver()
|
||||
}
|
||||
|
||||
fun isEnabled(): Boolean {
|
||||
fun isBluetoothEnabled(): Boolean {
|
||||
return bluetoothAdapter.isEnabled ?: false
|
||||
}
|
||||
|
||||
@ -29,6 +30,7 @@ class BluetoothService(private val context: Context) {
|
||||
bluetoothStateReceiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
val state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR)
|
||||
bluetoothStateChangedCallbacks.forEach { it(state) }
|
||||
when (state) {
|
||||
BluetoothAdapter.STATE_ON -> {
|
||||
// Handle Bluetooth turned on
|
||||
@ -53,6 +55,14 @@ class BluetoothService(private val context: Context) {
|
||||
context.registerReceiver(bluetoothStateReceiver, IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED))
|
||||
}
|
||||
|
||||
fun onBluetoothStateChanged(callback: (Int) -> Unit) {
|
||||
bluetoothStateChangedCallbacks.add(callback)
|
||||
}
|
||||
|
||||
fun offBluetoothStateChanged(callback: (Int) -> Unit) {
|
||||
bluetoothStateChangedCallbacks.remove(callback)
|
||||
}
|
||||
|
||||
fun cleanUp() {
|
||||
context.unregisterReceiver(bluetoothStateReceiver)
|
||||
}
|
||||
|
||||
@ -1,11 +1,13 @@
|
||||
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
|
||||
@ -34,14 +36,17 @@ import com.example.tvcontroller.ui.AppViewModel
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
private lateinit var bluetoothService: BluetoothService
|
||||
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())
|
||||
enableEdgeToEdge()
|
||||
setContent {
|
||||
TVControllerTheme {
|
||||
TvControllerApp()
|
||||
TvControllerApp(appViewModel = appViewModel, bluetoothService = bluetoothService)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -55,7 +60,8 @@ class MainActivity : ComponentActivity() {
|
||||
@Composable
|
||||
fun TvControllerApp(
|
||||
navController: NavHostController = rememberNavController(),
|
||||
appViewModel: AppViewModel = viewModel()
|
||||
appViewModel: AppViewModel = viewModel(),
|
||||
bluetoothService: BluetoothService? = null
|
||||
) {
|
||||
val backStackEntry by navController.currentBackStackEntryAsState()
|
||||
val currentScreen = Screen.valueOf(backStackEntry?.destination?.route ?: Screen.Camera.name)
|
||||
@ -112,7 +118,7 @@ fun TvControllerApp(
|
||||
RemoteScreen()
|
||||
}
|
||||
composable(route = Screen.Settings.name) {
|
||||
SettingsScreen()
|
||||
SettingsScreen(appViewModel = appViewModel)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -147,13 +153,17 @@ fun RemoteScreen(modifier: Modifier = Modifier) {
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun SettingsScreen(modifier: Modifier = Modifier) {
|
||||
fun SettingsScreen(modifier: Modifier = Modifier, appViewModel: AppViewModel = viewModel()) {
|
||||
Column(
|
||||
modifier = modifier
|
||||
.fillMaxSize(),
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.Center
|
||||
) {
|
||||
Text(text = "Settings Screen", modifier = modifier)
|
||||
if (appViewModel.isBluetoothEnabled()) {
|
||||
Text(text = "Settings Screen", modifier = modifier)
|
||||
}else{
|
||||
Text(text = "Bluetooth is disabled. Please enable it in settings.", modifier = modifier)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,9 @@
|
||||
package com.example.tvcontroller.ui
|
||||
|
||||
import android.content.Context
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.lifecycle.ViewModel
|
||||
import com.example.tvcontroller.BluetoothService
|
||||
import com.example.tvcontroller.Screen
|
||||
@ -9,5 +12,14 @@ import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
|
||||
class AppViewModel(private val bluetoothService: BluetoothService) : 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