diff --git a/app/src/main/java/com/michatec/radio/fragments/SettingsFragment.kt b/app/src/main/java/com/michatec/radio/fragments/SettingsFragment.kt index 1194afa..ca244c0 100644 --- a/app/src/main/java/com/michatec/radio/fragments/SettingsFragment.kt +++ b/app/src/main/java/com/michatec/radio/fragments/SettingsFragment.kt @@ -417,7 +417,7 @@ class SettingsFragment : PreferenceFragmentCompat(), SharedPreferences.OnSharedP // set up "Shader Effect" preference val preferenceShaderEffect = MarqueeSwitchPreference(context) preferenceShaderEffect.title = getString(R.string.pref_shader_effect_title) - preferenceShaderEffect.setIcon(R.drawable.ic_brush_24dp) + preferenceShaderEffect.setIcon(R.drawable.ic_shader_24dp) preferenceShaderEffect.key = Keys.PREF_SHADER_EFFECT preferenceShaderEffect.summary = getString(R.string.pref_shader_effect_summary) preferenceShaderEffect.setDefaultValue(true) diff --git a/app/src/main/java/com/michatec/radio/ui/CustomCastChooserDialogFragment.kt b/app/src/main/java/com/michatec/radio/ui/CustomCastChooserDialogFragment.kt index 8af4215..e7ef4ae 100644 --- a/app/src/main/java/com/michatec/radio/ui/CustomCastChooserDialogFragment.kt +++ b/app/src/main/java/com/michatec/radio/ui/CustomCastChooserDialogFragment.kt @@ -2,20 +2,19 @@ package com.michatec.radio.ui import android.app.Dialog import android.os.Bundle -import android.view.LayoutInflater -import android.view.ViewGroup -import androidx.core.view.isVisible +import androidx.compose.runtime.getValue +import androidx.compose.runtime.produceState +import androidx.compose.ui.platform.ComposeView import androidx.mediarouter.app.MediaRouteChooserDialogFragment import androidx.mediarouter.media.MediaControlIntent import androidx.mediarouter.media.MediaRouteSelector import androidx.mediarouter.media.MediaRouter -import androidx.recyclerview.widget.DiffUtil -import androidx.recyclerview.widget.LinearLayoutManager -import androidx.recyclerview.widget.ListAdapter -import androidx.recyclerview.widget.RecyclerView import com.google.android.material.dialog.MaterialAlertDialogBuilder -import com.michatec.radio.databinding.DialogCastChooserBinding -import com.michatec.radio.databinding.ItemCastDeviceBinding +import com.michatec.radio.ui.screens.CastChooserContent +import com.michatec.radio.ui.theme.RadioTheme +import kotlinx.coroutines.channels.awaitClose +import kotlinx.coroutines.flow.callbackFlow +import kotlinx.coroutines.flow.collectLatest /** * A custom DialogFragment that replaces the default Google Cast device selection dialog. @@ -24,35 +23,6 @@ class CustomCastChooserDialogFragment : MediaRouteChooserDialogFragment() { private lateinit var router: MediaRouter private lateinit var selector: MediaRouteSelector - private var adapter: CastDeviceAdapter? = null - private var _binding: DialogCastChooserBinding? = null - private val binding get() = _binding!! - - private val callback = object : MediaRouter.Callback() { - override fun onRouteAdded(router: MediaRouter, route: MediaRouter.RouteInfo) { - updateRouteList() - } - - override fun onRouteRemoved(router: MediaRouter, route: MediaRouter.RouteInfo) { - updateRouteList() - } - - override fun onRouteChanged(router: MediaRouter, route: MediaRouter.RouteInfo) { - updateRouteList() - } - } - - /** - * Data class to hold route information safely for background diffing. - * MediaRouter.RouteInfo must only be accessed on the main thread. - */ - private data class RouteItem( - val id: String, - val name: String, - val description: String?, - val isSelected: Boolean, - val originalRoute: MediaRouter.RouteInfo - ) override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val context = requireContext() @@ -62,95 +32,50 @@ class CustomCastChooserDialogFragment : MediaRouteChooserDialogFragment() { .addControlCategory(MediaControlIntent.CATEGORY_REMOTE_PLAYBACK) .build() - _binding = DialogCastChooserBinding.inflate(LayoutInflater.from(context)) + val composeView = ComposeView(context).apply { + setContent { + RadioTheme { + val routes by produceState(initialValue = emptyList()) { + val flow = callbackFlow { + val updateList = { + val currentRoutes = router.routes.filter { + it.matchesSelector(selector) && !it.isDefault + } + trySend(currentRoutes) + } - binding.recyclerViewDevices.layoutManager = LinearLayoutManager(context) - adapter = CastDeviceAdapter { routeItem -> - routeItem.originalRoute.select() - dismiss() + val callback = object : MediaRouter.Callback() { + override fun onRouteAdded(router: MediaRouter, route: MediaRouter.RouteInfo) { updateList() } + override fun onRouteRemoved(router: MediaRouter, route: MediaRouter.RouteInfo) { updateList() } + override fun onRouteChanged(router: MediaRouter, route: MediaRouter.RouteInfo) { updateList() } + } + + router.addCallback(selector, callback, MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY) + updateList() + + awaitClose { + router.removeCallback(callback) + } + } + + flow.collectLatest { + value = it + } + } + + CastChooserContent( + routes = routes, + onRouteSelected = { route -> + route.select() + dismiss() + } + ) + } + } } - binding.recyclerViewDevices.adapter = adapter - - updateRouteList() return MaterialAlertDialogBuilder(context) - .setView(binding.root) + .setView(composeView) .create() } - - override fun onDestroyView() { - super.onDestroyView() - _binding = null - } - - override fun onStart() { - super.onStart() - router.addCallback(selector, callback, MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY) - } - - override fun onStop() { - router.removeCallback(callback) - super.onStop() - } - - private fun updateRouteList() { - val routes = router.routes - .filter { it.matchesSelector(selector) && !it.isDefault } - .map { route -> - RouteItem( - id = route.id, - name = route.name, - description = route.description, - isSelected = route.isSelected, - originalRoute = route - ) - } - - adapter?.submitList(routes) - - val hasDevices = routes.isNotEmpty() - binding.recyclerViewDevices.isVisible = hasDevices - binding.textViewNoDevices.isVisible = !hasDevices - } - - /** - * Inner adapter for the Cast device list - */ - private class CastDeviceAdapter(private val onRouteSelected: (RouteItem) -> Unit) : - ListAdapter(RouteDiffCallback()) { - - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { - val binding = ItemCastDeviceBinding.inflate(LayoutInflater.from(parent.context), parent, false) - return ViewHolder(binding) - } - - override fun onBindViewHolder(holder: ViewHolder, position: Int) { - val item = getItem(position) - holder.binding.textViewName.text = item.name - - val description = item.description - if (!description.isNullOrEmpty()) { - holder.binding.textViewDescription.text = description - holder.binding.textViewDescription.isVisible = true - } else { - holder.binding.textViewDescription.isVisible = false - } - - holder.itemView.setOnClickListener { onRouteSelected(item) } - } - - class ViewHolder(val binding: ItemCastDeviceBinding) : RecyclerView.ViewHolder(binding.root) - - private class RouteDiffCallback : DiffUtil.ItemCallback() { - override fun areItemsTheSame(oldItem: RouteItem, newItem: RouteItem): Boolean { - return oldItem.id == newItem.id - } - - override fun areContentsTheSame(oldItem: RouteItem, newItem: RouteItem): Boolean { - return oldItem.name == newItem.name && - oldItem.description == newItem.description && - oldItem.isSelected == newItem.isSelected - } - } - } } \ No newline at end of file diff --git a/app/src/main/java/com/michatec/radio/ui/CustomCastControllerDialogFragment.kt b/app/src/main/java/com/michatec/radio/ui/CustomCastControllerDialogFragment.kt index 68d308c..991ec92 100644 --- a/app/src/main/java/com/michatec/radio/ui/CustomCastControllerDialogFragment.kt +++ b/app/src/main/java/com/michatec/radio/ui/CustomCastControllerDialogFragment.kt @@ -2,15 +2,15 @@ package com.michatec.radio.ui import android.app.Dialog import android.os.Bundle -import android.view.LayoutInflater -import android.widget.SeekBar +import androidx.compose.runtime.* +import androidx.compose.ui.platform.ComposeView import androidx.mediarouter.app.MediaRouteControllerDialogFragment import com.google.android.gms.cast.Cast import com.google.android.gms.cast.framework.CastContext -import com.google.android.gms.cast.framework.CastSession import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.michatec.radio.R -import com.michatec.radio.databinding.DialogCastConnectedBinding +import com.michatec.radio.ui.screens.CastControllerContent +import com.michatec.radio.ui.theme.RadioTheme /** * A custom DialogFragment that replaces the default Google Cast controller dialog. @@ -18,74 +18,53 @@ import com.michatec.radio.databinding.DialogCastConnectedBinding */ class CustomCastControllerDialogFragment : MediaRouteControllerDialogFragment() { - private var _binding: DialogCastConnectedBinding? = null - private val binding get() = _binding!! - private var castSession: CastSession? = null - - private val castListener = object : Cast.Listener() { - override fun onVolumeChanged() { - updateVolumeSeekBar() - } - } - override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val context = requireContext() - - _binding = DialogCastConnectedBinding.inflate(LayoutInflater.from(context)) - val castContext = CastContext.getSharedInstance(context) - castSession = castContext.sessionManager.currentCastSession - val deviceName = castSession?.castDevice?.friendlyName ?: context.getString(R.string.media_route_menu_title) - binding.textViewDeviceName.text = deviceName + val composeView = ComposeView(context).apply { + setContent { + RadioTheme { + val castSession = remember { castContext.sessionManager.currentCastSession } + var volume by remember { mutableFloatStateOf(castSession?.volume?.toFloat() ?: 0f) } + var isMute by remember { mutableStateOf(castSession?.isMute ?: false) } + val deviceName = remember { castSession?.castDevice?.friendlyName ?: context.getString(R.string.media_route_menu_title) } - updateVolumeSeekBar() - binding.seekBarVolume.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener { - override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) { - if (fromUser) { - castSession?.volume = progress / 100.0 + DisposableEffect(castSession) { + val listener = object : Cast.Listener() { + override fun onVolumeChanged() { + volume = castSession?.volume?.toFloat() ?: 0f + isMute = castSession?.isMute ?: false + } + } + castSession?.addCastListener(listener) + onDispose { + castSession?.removeCastListener(listener) + } + } + + CastControllerContent( + deviceName = deviceName, + volume = volume, + isMute = isMute, + onVolumeChanged = { newVolume -> + castSession?.volume = newVolume.toDouble() + volume = newVolume + }, + onStopCasting = { + castContext.sessionManager.endCurrentSession(true) + dismiss() + }, + onCancel = { + dismiss() + } + ) } } + } - override fun onStartTrackingTouch(seekBar: SeekBar?) {} - override fun onStopTrackingTouch(seekBar: SeekBar?) {} - }) - - val dialog = MaterialAlertDialogBuilder(context) - .setView(binding.root) + return MaterialAlertDialogBuilder(context) + .setView(composeView) .create() - - binding.buttonStopCasting.setOnClickListener { - castContext.sessionManager.endCurrentSession(true) - dialog.dismiss() - } - - binding.buttonCancel.setOnClickListener { - dialog.dismiss() - } - - return dialog - } - - override fun onStart() { - super.onStart() - castSession?.addCastListener(castListener) - updateVolumeSeekBar() - } - - override fun onStop() { - super.onStop() - castSession?.removeCastListener(castListener) - } - - private fun updateVolumeSeekBar() { - val volume = castSession?.volume ?: 0.0 - binding.seekBarVolume.progress = (volume * 100).toInt() - binding.seekBarVolume.isEnabled = castSession?.isMute == false - } - - override fun onDestroyView() { - super.onDestroyView() - _binding = null } } \ No newline at end of file diff --git a/app/src/main/java/com/michatec/radio/ui/screens/CastComposables.kt b/app/src/main/java/com/michatec/radio/ui/screens/CastComposables.kt new file mode 100644 index 0000000..b0dce76 --- /dev/null +++ b/app/src/main/java/com/michatec/radio/ui/screens/CastComposables.kt @@ -0,0 +1,194 @@ +package com.michatec.radio.ui.screens + +import androidx.compose.foundation.clickable +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.heightIn +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.layout.width +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.items +import androidx.compose.material3.Button +import androidx.compose.material3.Icon +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Slider +import androidx.compose.material3.Text +import androidx.compose.material3.TextButton +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.painterResource +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.dp +import androidx.mediarouter.media.MediaRouter +import com.michatec.radio.R + +@Composable +fun CastChooserContent( + routes: List, + onRouteSelected: (MediaRouter.RouteInfo) -> Unit +) { + Column( + modifier = Modifier + .fillMaxWidth() + .padding(top = 10.dp) + ) { + Text( + text = stringResource(id = R.string.cast_chooser_title), + style = MaterialTheme.typography.titleLarge, + color = MaterialTheme.colorScheme.onSurface, + modifier = Modifier.padding(horizontal = 24.dp, vertical = 16.dp) + ) + + if (routes.isEmpty()) { + Box( + modifier = Modifier + .fillMaxWidth() + .padding(32.dp), + contentAlignment = Alignment.Center + ) { + Text( + text = stringResource(id = R.string.cast_no_devices), + style = MaterialTheme.typography.bodyLarge, + color = MaterialTheme.colorScheme.onSurface + ) + } + } else { + LazyColumn( + modifier = Modifier + .fillMaxWidth() + .heightIn(min = 100.dp, max = 400.dp) + ) { + items(routes) { route -> + CastDeviceItem(route = route, onClick = { onRouteSelected(route) }) + } + } + } + } +} + +@Composable +fun CastDeviceItem( + route: MediaRouter.RouteInfo, + onClick: () -> Unit +) { + Row( + modifier = Modifier + .fillMaxWidth() + .clickable(onClick = onClick) + .padding(14.dp), + verticalAlignment = Alignment.CenterVertically + ) { + Icon( + painter = painterResource(id = R.drawable.ic_cast_24dp), + contentDescription = null, + tint = MaterialTheme.colorScheme.onSurfaceVariant + ) + Spacer(modifier = Modifier.width(16.dp)) + Column { + Text( + text = route.name, + style = MaterialTheme.typography.bodyLarge, + color = MaterialTheme.colorScheme.onSurface + ) + route.description?.let { description -> + if (description.isNotEmpty()) { + Text( + text = description, + style = MaterialTheme.typography.bodySmall, + color = MaterialTheme.colorScheme.onSurfaceVariant + ) + } + } + } + } +} + +@Composable +fun CastControllerContent( + deviceName: String, + volume: Float, + isMute: Boolean, + onVolumeChanged: (Float) -> Unit, + onStopCasting: () -> Unit, + onCancel: () -> Unit +) { + Column( + modifier = Modifier + .fillMaxWidth() + .padding(24.dp), + horizontalAlignment = Alignment.CenterHorizontally + ) { + Icon( + painter = painterResource(id = R.drawable.ic_cast_24dp), + contentDescription = null, + tint = MaterialTheme.colorScheme.primary, + modifier = Modifier + .size(48.dp) + .padding(bottom = 16.dp) + ) + + Text( + text = stringResource(id = R.string.cast_connected_to), + style = MaterialTheme.typography.titleLarge, + color = MaterialTheme.colorScheme.onSurface + ) + + Text( + text = deviceName, + style = MaterialTheme.typography.bodyLarge, + fontWeight = FontWeight.Bold, + color = MaterialTheme.colorScheme.onSurfaceVariant, + modifier = Modifier.padding(top = 8.dp, bottom = 16.dp) + ) + + Row( + modifier = Modifier + .fillMaxWidth() + .padding(bottom = 24.dp), + verticalAlignment = Alignment.CenterVertically + ) { + Icon( + painter = painterResource(id = R.drawable.ic_volume_down_24dp), + contentDescription = null, + tint = MaterialTheme.colorScheme.onSurfaceVariant + ) + Slider( + value = volume, + onValueChange = onVolumeChanged, + enabled = !isMute, + modifier = Modifier + .weight(1f) + .padding(horizontal = 8.dp) + ) + Icon( + painter = painterResource(id = R.drawable.ic_volume_up_24dp), + contentDescription = null, + tint = MaterialTheme.colorScheme.onSurfaceVariant + ) + } + + Button( + onClick = onStopCasting, + modifier = Modifier + .fillMaxWidth() + .padding(bottom = 8.dp), + shape = MaterialTheme.shapes.medium + ) { + Text(text = stringResource(id = R.string.cast_stop_casting)) + } + + TextButton( + onClick = onCancel, + modifier = Modifier.fillMaxWidth(), + shape = MaterialTheme.shapes.medium + ) { + Text(text = stringResource(id = R.string.cast_cancel)) + } + } +} diff --git a/app/src/main/java/com/michatec/radio/ui/theme/AppTheme.kt b/app/src/main/java/com/michatec/radio/ui/theme/AppTheme.kt index c3b4ad0..6a78854 100644 --- a/app/src/main/java/com/michatec/radio/ui/theme/AppTheme.kt +++ b/app/src/main/java/com/michatec/radio/ui/theme/AppTheme.kt @@ -2,6 +2,7 @@ package com.michatec.radio.ui.theme import androidx.compose.foundation.isSystemInDarkTheme import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Surface import androidx.compose.material3.darkColorScheme import androidx.compose.material3.lightColorScheme import androidx.compose.runtime.Composable @@ -17,10 +18,8 @@ fun RadioTheme( ) { val context = LocalContext.current val customThemeEnabled = PreferencesHelper.loadCustomThemeEnabled() - val isDark: Boolean - val backgroundColor: Color - if (customThemeEnabled) { + val colorScheme = if (customThemeEnabled) { var colorInt = PreferencesHelper.loadCustomThemeColor(context) val index = PreferencesHelper.loadCustomThemeIndex() if (index != -1) { @@ -29,27 +28,32 @@ fun RadioTheme( colorInt = colors[index] } } - backgroundColor = Color(colorInt) - isDark = backgroundColor.luminance() < 0.5f + val backgroundColor = Color(colorInt) + val isDark = backgroundColor.luminance() < 0.5f + if (isDark) { + darkColorScheme( + background = backgroundColor, + surface = backgroundColor + ) + } else { + lightColorScheme( + background = backgroundColor, + surface = backgroundColor + ) + } } else { - isDark = isSystemInDarkTheme() - backgroundColor = if (isDark) Color(0xFF1C1B1F) else Color(0xFFFFFBFE) - } - - val colorScheme = if (isDark) { - darkColorScheme( - background = backgroundColor, - surface = backgroundColor - ) - } else { - lightColorScheme( - background = backgroundColor, - surface = backgroundColor - ) + val isDark = isSystemInDarkTheme() + if (isDark) { + darkColorScheme() + } else { + lightColorScheme() + } } MaterialTheme( colorScheme = colorScheme, - content = content + content = { + Surface(content = content) + } ) } diff --git a/app/src/main/res/drawable/ic_shader_24dp.xml b/app/src/main/res/drawable/ic_shader_24dp.xml new file mode 100644 index 0000000..31cd120 --- /dev/null +++ b/app/src/main/res/drawable/ic_shader_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/dialog_cast_chooser.xml b/app/src/main/res/layout/dialog_cast_chooser.xml deleted file mode 100644 index 0b39180..0000000 --- a/app/src/main/res/layout/dialog_cast_chooser.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_cast_connected.xml b/app/src/main/res/layout/dialog_cast_connected.xml deleted file mode 100644 index 870472f..0000000 --- a/app/src/main/res/layout/dialog_cast_connected.xml +++ /dev/null @@ -1,85 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/item_cast_device.xml b/app/src/main/res/layout/item_cast_device.xml deleted file mode 100644 index 4c3ec83..0000000 --- a/app/src/main/res/layout/item_cast_device.xml +++ /dev/null @@ -1,42 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/values-night-v31/styles.xml b/app/src/main/res/values-night-v31/styles.xml index df63d00..b0b0e67 100644 --- a/app/src/main/res/values-night-v31/styles.xml +++ b/app/src/main/res/values-night-v31/styles.xml @@ -29,6 +29,8 @@ @style/ThemeOverlay.App.TimePicker + false + false @android:color/transparent diff --git a/app/src/main/res/values-night/styles.xml b/app/src/main/res/values-night/styles.xml index 488f5a6..8c4ad1b 100644 --- a/app/src/main/res/values-night/styles.xml +++ b/app/src/main/res/values-night/styles.xml @@ -25,6 +25,7 @@ @style/ThemeOverlay.App.TimePicker + false @android:color/transparent