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