mirror of
https://github.com/Michatec/Radio.git
synced 2026-04-01 07:56:27 +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 */
|
||||
private fun handleStartIntent() {
|
||||
if ((activity as Activity).intent.action != null) {
|
||||
when ((activity as Activity).intent.action) {
|
||||
Keys.ACTION_SHOW_PLAYER -> handleShowPlayer()
|
||||
Intent.ACTION_VIEW -> handleViewIntent()
|
||||
Keys.ACTION_START -> handleStartPlayer()
|
||||
val intent = (activity as Activity).intent
|
||||
if (intent.action != null && intent.action?.isNotEmpty() == true) {
|
||||
var handled = false
|
||||
when (intent.action) {
|
||||
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
|
||||
intent.action = ""
|
||||
}
|
||||
}
|
||||
// clear intent action to prevent double calls
|
||||
(activity as Activity).intent.action = ""
|
||||
}
|
||||
|
||||
|
||||
@@ -590,12 +602,12 @@ class PlayerFragment : Fragment(),
|
||||
val scheme: String = intentUri.scheme ?: String()
|
||||
// CASE: intent is a web link
|
||||
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()))
|
||||
}
|
||||
// CASE: intent is a local file
|
||||
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))
|
||||
}
|
||||
withContext(Main) {
|
||||
@@ -612,17 +624,30 @@ class PlayerFragment : Fragment(),
|
||||
|
||||
|
||||
/* 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
|
||||
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)) {
|
||||
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)) {
|
||||
val streamUri: String = intent.getStringExtra(Keys.EXTRA_STREAM_URI) ?: String()
|
||||
controller?.playStreamDirectly(streamUri)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
@@ -640,9 +665,9 @@ class PlayerFragment : Fragment(),
|
||||
collection = it
|
||||
// updates current station in player views
|
||||
playerState = PreferencesHelper.loadPlayerState()
|
||||
// // get station
|
||||
// get station
|
||||
val station: Station = CollectionHelper.getStation(collection, playerState.stationUuid)
|
||||
// // update player views
|
||||
// update player views
|
||||
layout.updatePlayerViews(activity as Context, station, playerState.isPlaying)
|
||||
// handle start intent
|
||||
handleStartIntent()
|
||||
|
||||
@@ -241,11 +241,6 @@ class PlayerService : MediaLibraryService() {
|
||||
async(Dispatchers.Default) { FileHelper.readCollectionSuspended(context) }
|
||||
// wait for result and update collection
|
||||
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,
|
||||
isPlaying
|
||||
)
|
||||
//updatePlayerState(station, playbackState)
|
||||
|
||||
if (isPlaying) {
|
||||
// playback is active
|
||||
@@ -506,10 +500,6 @@ class PlayerService : MediaLibraryService() {
|
||||
// try to reconnect every 5 seconds - up to 20 times
|
||||
if (loadErrorInfo.errorCount <= Keys.DEFAULT_MAX_RECONNECTION_COUNT && loadErrorInfo.exception is HttpDataSource.HttpDataSourceException) {
|
||||
return Keys.RECONNECTION_WAIT_INTERVAL
|
||||
// } else {
|
||||
// CoroutineScope(Main).launch {
|
||||
// player.stop()
|
||||
// }
|
||||
}
|
||||
return C.TIME_UNSET
|
||||
}
|
||||
|
||||
@@ -245,7 +245,7 @@ class SettingsFragment : PreferenceFragmentCompat(), YesNoDialog.YesNoDialogList
|
||||
val preferenceCategoryMaintenance = PreferenceCategory(activity as Context)
|
||||
preferenceCategoryMaintenance.title = getString(R.string.pref_maintenance_title)
|
||||
preferenceCategoryMaintenance.contains(preferenceUpdateStationImages)
|
||||
// preferenceCategoryMaintenance.contains(preferenceUpdateCollection)
|
||||
preferenceCategoryMaintenance.contains(preferenceUpdateCollection)
|
||||
|
||||
val preferenceCategoryImportExport = PreferenceCategory(activity as Context)
|
||||
preferenceCategoryImportExport.title = getString(R.string.pref_backup_import_export_title)
|
||||
@@ -549,7 +549,6 @@ class SettingsFragment : PreferenceFragmentCompat(), YesNoDialog.YesNoDialogList
|
||||
requestRestoreCollectionLauncher.launch(intent)
|
||||
} catch (exception: 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.lifecycle.LifecycleOwner
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.media3.common.text.TextAnnotation
|
||||
import androidx.navigation.findNavController
|
||||
import androidx.recyclerview.widget.DiffUtil
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
@@ -190,7 +189,11 @@ class CollectionAdapter(
|
||||
|
||||
// highlight if reordering
|
||||
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
|
||||
@@ -369,12 +372,16 @@ class CollectionAdapter(
|
||||
when (station.starred) {
|
||||
true -> {
|
||||
if (station.imageColor != -1) {
|
||||
// stationViewHolder.stationCardView.setCardBackgroundColor(station.imageColor)
|
||||
stationViewHolder.stationStarredView.setColorFilter(station.imageColor)
|
||||
} else {
|
||||
stationViewHolder.stationStarredView.clearColorFilter()
|
||||
}
|
||||
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 */
|
||||
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)) {
|
||||
val shortcut: ShortcutInfoCompat = ShortcutInfoCompat.Builder(context, station.name)
|
||||
.setShortLabel(station.name)
|
||||
|
||||
@@ -128,7 +128,6 @@ data class LayoutHolder(var rootView: View) {
|
||||
if (!isPlaying) {
|
||||
metadataView?.text = station.name
|
||||
sheetMetadataHistoryView?.text = station.name
|
||||
// sheetMetadataHistoryView.isSelected = true
|
||||
}
|
||||
|
||||
// update name
|
||||
@@ -289,11 +288,9 @@ data class LayoutHolder(var rootView: View) {
|
||||
val animatedVectorDrawable = playButtonView.drawable as? AnimatedVectorDrawable
|
||||
animatedVectorDrawable?.start()
|
||||
sheetSleepTimerStartButtonView?.isVisible = true
|
||||
// bufferingIndicator.isVisible = false
|
||||
} else {
|
||||
playButtonView.setImageResource(R.drawable.ic_player_play_symbol_42dp)
|
||||
sheetSleepTimerStartButtonView?.isVisible = false
|
||||
// bufferingIndicator.isVisible = isBuffering
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user