mirror of
https://github.com/Michatec/Radio.git
synced 2026-03-31 23:46:28 +02:00
refactor(ui): improve intent handling and collection view state
- Update `handleStartIntent` and `handleStartPlayer` in `PlayerFragment` to ensure intents are only cleared if successfully handled and stations are valid. - Refine star icon rendering in `CollectionAdapter` by clearing color filters when appropriate. - Remove obsolete commented-out code and update log messages in `PlayerFragment`, `PlayerService`, and `LayoutHolder`. - Uncomment maintenance preference updates in `SettingsFragment`. - Reformat code for better readability in `CollectionAdapter`.
This commit is contained in:
@@ -561,15 +561,27 @@ class PlayerFragment : Fragment(),
|
|||||||
|
|
||||||
/* Handles this activity's start intent */
|
/* Handles this activity's start intent */
|
||||||
private fun handleStartIntent() {
|
private fun handleStartIntent() {
|
||||||
if ((activity as Activity).intent.action != null) {
|
val intent = (activity as Activity).intent
|
||||||
when ((activity as Activity).intent.action) {
|
if (intent.action != null && intent.action?.isNotEmpty() == true) {
|
||||||
Keys.ACTION_SHOW_PLAYER -> handleShowPlayer()
|
var handled = false
|
||||||
Intent.ACTION_VIEW -> handleViewIntent()
|
when (intent.action) {
|
||||||
Keys.ACTION_START -> handleStartPlayer()
|
Keys.ACTION_SHOW_PLAYER -> {
|
||||||
|
handleShowPlayer()
|
||||||
|
handled = true
|
||||||
|
}
|
||||||
|
Intent.ACTION_VIEW -> {
|
||||||
|
handleViewIntent()
|
||||||
|
handled = true
|
||||||
|
}
|
||||||
|
Keys.ACTION_START -> {
|
||||||
|
handled = handleStartPlayer()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (handled) {
|
||||||
// clear intent action to prevent double calls
|
// clear intent action to prevent double calls
|
||||||
(activity as Activity).intent.action = ""
|
intent.action = ""
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -590,12 +602,12 @@ class PlayerFragment : Fragment(),
|
|||||||
val scheme: String = intentUri.scheme ?: String()
|
val scheme: String = intentUri.scheme ?: String()
|
||||||
// CASE: intent is a web link
|
// CASE: intent is a web link
|
||||||
if (scheme.startsWith("http")) {
|
if (scheme.startsWith("http")) {
|
||||||
Log.i(TAG, "Transistor was started to handle a web link.")
|
Log.i(TAG, "Radio was started to handle a web link.")
|
||||||
stationList.addAll(CollectionHelper.createStationsFromUrl(intentUri.toString()))
|
stationList.addAll(CollectionHelper.createStationsFromUrl(intentUri.toString()))
|
||||||
}
|
}
|
||||||
// CASE: intent is a local file
|
// CASE: intent is a local file
|
||||||
else if (scheme.startsWith("content")) {
|
else if (scheme.startsWith("content")) {
|
||||||
Log.i(TAG, "Transistor was started to handle a local audio playlist.")
|
Log.i(TAG, "Radio was started to handle a local audio playlist.")
|
||||||
stationList.addAll(CollectionHelper.createStationListFromContentUri(activity as Context, intentUri))
|
stationList.addAll(CollectionHelper.createStationListFromContentUri(activity as Context, intentUri))
|
||||||
}
|
}
|
||||||
withContext(Main) {
|
withContext(Main) {
|
||||||
@@ -612,17 +624,30 @@ class PlayerFragment : Fragment(),
|
|||||||
|
|
||||||
|
|
||||||
/* Handles START_PLAYER_SERVICE request from App Shortcut */
|
/* Handles START_PLAYER_SERVICE request from App Shortcut */
|
||||||
private fun handleStartPlayer() {
|
private fun handleStartPlayer(): Boolean {
|
||||||
|
if (controller == null || collection.stations.isEmpty()) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
val intent: Intent = (activity as Activity).intent
|
val intent: Intent = (activity as Activity).intent
|
||||||
if (intent.hasExtra(Keys.EXTRA_START_LAST_PLAYED_STATION)) {
|
if (intent.hasExtra(Keys.EXTRA_START_LAST_PLAYED_STATION)) {
|
||||||
controller?.play(activity as Context, CollectionHelper.getStation(collection, playerState.stationUuid))
|
val station = CollectionHelper.getStation(collection, playerState.stationUuid)
|
||||||
|
if (station.isValid()) {
|
||||||
|
controller?.play(activity as Context, station)
|
||||||
|
return true
|
||||||
|
}
|
||||||
} else if (intent.hasExtra(Keys.EXTRA_STATION_UUID)) {
|
} else if (intent.hasExtra(Keys.EXTRA_STATION_UUID)) {
|
||||||
val uuid: String = intent.getStringExtra(Keys.EXTRA_STATION_UUID) ?: String()
|
val uuid: String = intent.getStringExtra(Keys.EXTRA_STATION_UUID) ?: String()
|
||||||
controller?.play(activity as Context, CollectionHelper.getStation(collection, uuid))
|
val station = CollectionHelper.getStation(collection, uuid)
|
||||||
|
if (station.isValid()) {
|
||||||
|
controller?.play(activity as Context, station)
|
||||||
|
return true
|
||||||
|
}
|
||||||
} else if (intent.hasExtra(Keys.EXTRA_STREAM_URI)) {
|
} else if (intent.hasExtra(Keys.EXTRA_STREAM_URI)) {
|
||||||
val streamUri: String = intent.getStringExtra(Keys.EXTRA_STREAM_URI) ?: String()
|
val streamUri: String = intent.getStringExtra(Keys.EXTRA_STREAM_URI) ?: String()
|
||||||
controller?.playStreamDirectly(streamUri)
|
controller?.playStreamDirectly(streamUri)
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -640,9 +665,9 @@ class PlayerFragment : Fragment(),
|
|||||||
collection = it
|
collection = it
|
||||||
// updates current station in player views
|
// updates current station in player views
|
||||||
playerState = PreferencesHelper.loadPlayerState()
|
playerState = PreferencesHelper.loadPlayerState()
|
||||||
// // get station
|
// get station
|
||||||
val station: Station = CollectionHelper.getStation(collection, playerState.stationUuid)
|
val station: Station = CollectionHelper.getStation(collection, playerState.stationUuid)
|
||||||
// // update player views
|
// update player views
|
||||||
layout.updatePlayerViews(activity as Context, station, playerState.isPlaying)
|
layout.updatePlayerViews(activity as Context, station, playerState.isPlaying)
|
||||||
// handle start intent
|
// handle start intent
|
||||||
handleStartIntent()
|
handleStartIntent()
|
||||||
|
|||||||
@@ -241,11 +241,6 @@ class PlayerService : MediaLibraryService() {
|
|||||||
async(Dispatchers.Default) { FileHelper.readCollectionSuspended(context) }
|
async(Dispatchers.Default) { FileHelper.readCollectionSuspended(context) }
|
||||||
// wait for result and update collection
|
// wait for result and update collection
|
||||||
collection = deferred.await()
|
collection = deferred.await()
|
||||||
// // special case: trigger metadata view update for stations that have no metadata
|
|
||||||
// if (player.isPlaying && station.name == getCurrentMetadata()) {
|
|
||||||
// station = CollectionHelper.getStation(collection, station.uuid)
|
|
||||||
// updateMetadata(null)
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -429,7 +424,6 @@ class PlayerService : MediaLibraryService() {
|
|||||||
currentMediaId,
|
currentMediaId,
|
||||||
isPlaying
|
isPlaying
|
||||||
)
|
)
|
||||||
//updatePlayerState(station, playbackState)
|
|
||||||
|
|
||||||
if (isPlaying) {
|
if (isPlaying) {
|
||||||
// playback is active
|
// playback is active
|
||||||
@@ -506,10 +500,6 @@ class PlayerService : MediaLibraryService() {
|
|||||||
// try to reconnect every 5 seconds - up to 20 times
|
// try to reconnect every 5 seconds - up to 20 times
|
||||||
if (loadErrorInfo.errorCount <= Keys.DEFAULT_MAX_RECONNECTION_COUNT && loadErrorInfo.exception is HttpDataSource.HttpDataSourceException) {
|
if (loadErrorInfo.errorCount <= Keys.DEFAULT_MAX_RECONNECTION_COUNT && loadErrorInfo.exception is HttpDataSource.HttpDataSourceException) {
|
||||||
return Keys.RECONNECTION_WAIT_INTERVAL
|
return Keys.RECONNECTION_WAIT_INTERVAL
|
||||||
// } else {
|
|
||||||
// CoroutineScope(Main).launch {
|
|
||||||
// player.stop()
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
return C.TIME_UNSET
|
return C.TIME_UNSET
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -245,7 +245,7 @@ class SettingsFragment : PreferenceFragmentCompat(), YesNoDialog.YesNoDialogList
|
|||||||
val preferenceCategoryMaintenance = PreferenceCategory(activity as Context)
|
val preferenceCategoryMaintenance = PreferenceCategory(activity as Context)
|
||||||
preferenceCategoryMaintenance.title = getString(R.string.pref_maintenance_title)
|
preferenceCategoryMaintenance.title = getString(R.string.pref_maintenance_title)
|
||||||
preferenceCategoryMaintenance.contains(preferenceUpdateStationImages)
|
preferenceCategoryMaintenance.contains(preferenceUpdateStationImages)
|
||||||
// preferenceCategoryMaintenance.contains(preferenceUpdateCollection)
|
preferenceCategoryMaintenance.contains(preferenceUpdateCollection)
|
||||||
|
|
||||||
val preferenceCategoryImportExport = PreferenceCategory(activity as Context)
|
val preferenceCategoryImportExport = PreferenceCategory(activity as Context)
|
||||||
preferenceCategoryImportExport.title = getString(R.string.pref_backup_import_export_title)
|
preferenceCategoryImportExport.title = getString(R.string.pref_backup_import_export_title)
|
||||||
@@ -549,7 +549,6 @@ class SettingsFragment : PreferenceFragmentCompat(), YesNoDialog.YesNoDialogList
|
|||||||
requestRestoreCollectionLauncher.launch(intent)
|
requestRestoreCollectionLauncher.launch(intent)
|
||||||
} catch (exception: Exception) {
|
} catch (exception: Exception) {
|
||||||
Log.e(TAG, "Unable to open file picker for ZIP.\n$exception")
|
Log.e(TAG, "Unable to open file picker for ZIP.\n$exception")
|
||||||
// Toast.makeText(activity as Context, R.string.toast_message_install_file_helper, Toast.LENGTH_LONG).show()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ import androidx.core.view.isGone
|
|||||||
import androidx.core.view.isVisible
|
import androidx.core.view.isVisible
|
||||||
import androidx.lifecycle.LifecycleOwner
|
import androidx.lifecycle.LifecycleOwner
|
||||||
import androidx.lifecycle.ViewModelProvider
|
import androidx.lifecycle.ViewModelProvider
|
||||||
import androidx.media3.common.text.TextAnnotation
|
|
||||||
import androidx.navigation.findNavController
|
import androidx.navigation.findNavController
|
||||||
import androidx.recyclerview.widget.DiffUtil
|
import androidx.recyclerview.widget.DiffUtil
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
@@ -190,7 +189,11 @@ class CollectionAdapter(
|
|||||||
|
|
||||||
// highlight if reordering
|
// highlight if reordering
|
||||||
if (reorderStationUuid == station.uuid) {
|
if (reorderStationUuid == station.uuid) {
|
||||||
stationViewHolder.stationCardView.setStrokeColor(ColorStateList.valueOf(ContextCompat.getColor(context, R.color.cardview_reordering)))
|
stationViewHolder.stationCardView.setStrokeColor(
|
||||||
|
ColorStateList.valueOf(
|
||||||
|
ContextCompat.getColor(context, R.color.cardview_reordering)
|
||||||
|
)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// show / hide edit views
|
// show / hide edit views
|
||||||
@@ -369,12 +372,16 @@ class CollectionAdapter(
|
|||||||
when (station.starred) {
|
when (station.starred) {
|
||||||
true -> {
|
true -> {
|
||||||
if (station.imageColor != -1) {
|
if (station.imageColor != -1) {
|
||||||
// stationViewHolder.stationCardView.setCardBackgroundColor(station.imageColor)
|
|
||||||
stationViewHolder.stationStarredView.setColorFilter(station.imageColor)
|
stationViewHolder.stationStarredView.setColorFilter(station.imageColor)
|
||||||
|
} else {
|
||||||
|
stationViewHolder.stationStarredView.clearColorFilter()
|
||||||
}
|
}
|
||||||
stationViewHolder.stationStarredView.isVisible = true
|
stationViewHolder.stationStarredView.isVisible = true
|
||||||
}
|
}
|
||||||
false -> stationViewHolder.stationStarredView.isGone = true
|
false -> {
|
||||||
|
stationViewHolder.stationStarredView.clearColorFilter()
|
||||||
|
stationViewHolder.stationStarredView.isGone = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ object ShortcutHelper {
|
|||||||
|
|
||||||
/* Places shortcut on Home screen */
|
/* Places shortcut on Home screen */
|
||||||
fun placeShortcut(context: Context, station: Station) {
|
fun placeShortcut(context: Context, station: Station) {
|
||||||
// credit: https://medium.com/@BladeCoder/using-support-library-26-0-0-you-can-do-bb75911e01e8
|
|
||||||
if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
|
if (ShortcutManagerCompat.isRequestPinShortcutSupported(context)) {
|
||||||
val shortcut: ShortcutInfoCompat = ShortcutInfoCompat.Builder(context, station.name)
|
val shortcut: ShortcutInfoCompat = ShortcutInfoCompat.Builder(context, station.name)
|
||||||
.setShortLabel(station.name)
|
.setShortLabel(station.name)
|
||||||
|
|||||||
@@ -128,7 +128,6 @@ data class LayoutHolder(var rootView: View) {
|
|||||||
if (!isPlaying) {
|
if (!isPlaying) {
|
||||||
metadataView?.text = station.name
|
metadataView?.text = station.name
|
||||||
sheetMetadataHistoryView?.text = station.name
|
sheetMetadataHistoryView?.text = station.name
|
||||||
// sheetMetadataHistoryView.isSelected = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// update name
|
// update name
|
||||||
@@ -289,11 +288,9 @@ data class LayoutHolder(var rootView: View) {
|
|||||||
val animatedVectorDrawable = playButtonView.drawable as? AnimatedVectorDrawable
|
val animatedVectorDrawable = playButtonView.drawable as? AnimatedVectorDrawable
|
||||||
animatedVectorDrawable?.start()
|
animatedVectorDrawable?.start()
|
||||||
sheetSleepTimerStartButtonView?.isVisible = true
|
sheetSleepTimerStartButtonView?.isVisible = true
|
||||||
// bufferingIndicator.isVisible = false
|
|
||||||
} else {
|
} else {
|
||||||
playButtonView.setImageResource(R.drawable.ic_player_play_symbol_42dp)
|
playButtonView.setImageResource(R.drawable.ic_player_play_symbol_42dp)
|
||||||
sheetSleepTimerStartButtonView?.isVisible = false
|
sheetSleepTimerStartButtonView?.isVisible = false
|
||||||
// bufferingIndicator.isVisible = isBuffering
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user