mirror of
https://github.com/Michatec/Radio.git
synced 2026-11-17 05:06:53 +01:00
feat(collection): add search and favorite filtering
Implemented a search bar and favorites filter for the station list. Added a search button to the station collection card, updated the adapter to handle filtered datasets, and integrated the search UI into both mobile and Android TV layouts. Includes localized strings for multiple languages.
This commit is contained in:
1 parent
cb1ca8ef80
commit
ba404c04d3
17 files changed
+318
-27
No files matched your search
@@ -10,10 +10,13 @@ import android.content.pm.PackageManager
|
||||
import android.media.AudioManager
|
||||
import android.net.Uri
|
||||
import android.os.*
|
||||
import android.text.Editable
|
||||
import android.text.TextWatcher
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.view.inputmethod.InputMethodManager
|
||||
import android.widget.Toast
|
||||
import androidx.activity.OnBackPressedCallback
|
||||
import androidx.activity.result.ActivityResultLauncher
|
||||
@@ -97,6 +100,7 @@ class PlayerFragment : Fragment(),
|
||||
private var tempStationUuid: String = String()
|
||||
private var itemTouchHelper: ItemTouchHelper? = null
|
||||
private var shaderEffectView: ShaderEffectView? = null
|
||||
private var isSearchActive: Boolean = false
|
||||
|
||||
// Check if the device running the app is an Android TV instance
|
||||
private val isAndroidTV: Boolean by lazy {
|
||||
@@ -181,6 +185,7 @@ class PlayerFragment : Fragment(),
|
||||
// find views and set them up
|
||||
val rootView: View = inflater.inflate(R.layout.fragment_player, container, false)
|
||||
layout = LayoutHolder(rootView)
|
||||
isSearchActive = false
|
||||
|
||||
shaderEffectView = rootView.findViewById(R.id.player_shader_effect)
|
||||
initializeViews()
|
||||
@@ -381,6 +386,20 @@ class PlayerFragment : Fragment(),
|
||||
pickImage()
|
||||
}
|
||||
|
||||
/* Overrides onSearchButtonTapped from CollectionAdapterListener */
|
||||
override fun onSearchButtonTapped() {
|
||||
isSearchActive = !isSearchActive
|
||||
layout.toggleSearchLayout(isSearchActive)
|
||||
if (!isSearchActive) {
|
||||
collectionAdapter.filter("", false)
|
||||
} else if (isAndroidTV) {
|
||||
// TV: focus search field and show keyboard
|
||||
layout.searchInputEditText?.requestFocus()
|
||||
val imm = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as? InputMethodManager
|
||||
imm?.showSoftInput(layout.searchInputEditText, 0)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Overrides onYesNoDialog from YesNoDialogListener */
|
||||
override fun onYesNoDialog(
|
||||
@@ -518,7 +537,7 @@ class PlayerFragment : Fragment(),
|
||||
Snackbar.LENGTH_SHORT
|
||||
)
|
||||
if (!isAndroidTV) {
|
||||
snackbar.setAnchorView(layout.bottomSheet)
|
||||
snackbar.anchorView = layout.bottomSheet
|
||||
}
|
||||
snackbar.show()
|
||||
}
|
||||
@@ -532,6 +551,20 @@ class PlayerFragment : Fragment(),
|
||||
togglePeriodicSleepTimerUpdateRequest()
|
||||
}
|
||||
|
||||
// set up search input
|
||||
layout.searchInputEditText?.addTextChangedListener(object : TextWatcher {
|
||||
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
|
||||
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
|
||||
override fun afterTextChanged(s: Editable?) {
|
||||
performSearch()
|
||||
}
|
||||
})
|
||||
|
||||
// set up favorites filter
|
||||
layout.searchFilterFavoritesSwitch?.setOnCheckedChangeListener { _, _ ->
|
||||
performSearch()
|
||||
}
|
||||
|
||||
// set up TV station navigation
|
||||
layout.playerPrevButtonView?.setOnClickListener {
|
||||
val currentPosition = CollectionHelper.getStationPosition(collection, playerState.stationUuid)
|
||||
@@ -607,6 +640,14 @@ class PlayerFragment : Fragment(),
|
||||
pickSingleMediaLauncher.launch(PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly))
|
||||
}
|
||||
|
||||
/* Performs search and updates list */
|
||||
private fun performSearch() {
|
||||
val query = layout.searchInputEditText?.text?.toString() ?: ""
|
||||
val onlyFavorites = layout.searchFilterFavoritesSwitch?.isChecked ?: false
|
||||
collectionAdapter.filter(query, onlyFavorites)
|
||||
layout.toggleSearchNoResults(collectionAdapter.itemCount == 1 && (query.isNotEmpty() || onlyFavorites))
|
||||
}
|
||||
|
||||
/* Handles this activity's start intent */
|
||||
private fun handleStartIntent() {
|
||||
val intent = (activity as Activity).intent
|
||||
@@ -724,9 +765,9 @@ class PlayerFragment : Fragment(),
|
||||
// handle navigation arguments
|
||||
handleNavigationArguments()
|
||||
}
|
||||
collectionViewModel.collectionSizeLiveData.observe(this) {
|
||||
collectionViewModel.collectionSizeLiveData.observe(this) { size ->
|
||||
// size of collection changed
|
||||
layout.toggleOnboarding(activity as Context, collection.stations.size)
|
||||
layout.toggleOnboarding(activity as Context, size)
|
||||
updatePlayerViews()
|
||||
CollectionHelper.exportCollectionM3u(activity as Context, collection)
|
||||
CollectionHelper.exportCollectionPls(activity as Context, collection)
|
||||
|
||||
@@ -50,12 +50,15 @@ class CollectionAdapter(
|
||||
/* Main class variables */
|
||||
private lateinit var collectionViewModel: CollectionViewModel
|
||||
private var collection: Collection = Collection()
|
||||
private var filteredStations: MutableList<Station> = mutableListOf()
|
||||
private var editStationsEnabled: Boolean = PreferencesHelper.loadEditStationsEnabled(context)
|
||||
private var editStationStreamsEnabled: Boolean = PreferencesHelper.loadEditStreamUrisEnabled(context)
|
||||
private var expandedStationUuid: String = PreferencesHelper.loadStationListStreamUuid()
|
||||
private var expandedStationPosition: Int = -1
|
||||
var isExpandedForEdit: Boolean = false
|
||||
private var reorderStationUuid: String = ""
|
||||
private var currentSearchQuery: String = ""
|
||||
private var currentOnlyFavorites: Boolean = false
|
||||
|
||||
|
||||
/* Listener Interface */
|
||||
@@ -63,6 +66,7 @@ class CollectionAdapter(
|
||||
fun onPlayButtonTapped(stationUuid: String)
|
||||
fun onAddNewButtonTapped()
|
||||
fun onChangeImageButtonTapped(stationUuid: String)
|
||||
fun onSearchButtonTapped()
|
||||
}
|
||||
|
||||
|
||||
@@ -108,8 +112,7 @@ class CollectionAdapter(
|
||||
|
||||
/* Implement the method to handle item move */
|
||||
fun onItemMove(fromPosition: Int, toPosition: Int) {
|
||||
// Do nothing if in "edit" mode
|
||||
if (isExpandedForEdit) {
|
||||
if (isExpandedForEdit || filteredStations.size != collection.stations.size) {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -141,8 +144,12 @@ class CollectionAdapter(
|
||||
|
||||
/* Implement the method to handle item dismissal */
|
||||
fun onItemDismiss(position: Int) {
|
||||
// Remove the item at the given position from your data collection
|
||||
collection.stations.removeAt(position)
|
||||
val station = filteredStations[position]
|
||||
val originalPosition = collection.stations.indexOfFirst { it.uuid == station.uuid }
|
||||
if (originalPosition != -1) {
|
||||
collection.stations.removeAt(originalPosition)
|
||||
}
|
||||
filteredStations.removeAt(position)
|
||||
notifyItemRemoved(position)
|
||||
}
|
||||
|
||||
@@ -154,6 +161,27 @@ class CollectionAdapter(
|
||||
}
|
||||
|
||||
|
||||
/* Filters the station list based on query and favorite status */
|
||||
fun filter(query: String, onlyFavorites: Boolean) {
|
||||
currentSearchQuery = query
|
||||
currentOnlyFavorites = onlyFavorites
|
||||
applyFilter()
|
||||
}
|
||||
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
private fun applyFilter() {
|
||||
filteredStations = if (currentSearchQuery.isEmpty() && !currentOnlyFavorites) {
|
||||
collection.stations.toMutableList()
|
||||
} else {
|
||||
collection.stations.filter { station ->
|
||||
val matchesQuery = station.name.contains(currentSearchQuery, ignoreCase = true)
|
||||
val matchesFavorite = !currentOnlyFavorites || station.starred
|
||||
matchesQuery && matchesFavorite
|
||||
}.toMutableList()
|
||||
}
|
||||
notifyDataSetChanged()
|
||||
}
|
||||
|
||||
/* Overrides onBindViewHolder */
|
||||
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
|
||||
when (holder) {
|
||||
@@ -171,11 +199,15 @@ class CollectionAdapter(
|
||||
addNewViewHolder.visualizerButtonView.setOnClickListener {
|
||||
it.findNavController().navigate(R.id.visualizer_destination)
|
||||
}
|
||||
addNewViewHolder.playerSearchButtonView.setOnClickListener {
|
||||
collectionAdapterListener.onSearchButtonTapped()
|
||||
}
|
||||
addNewViewHolder.playerSearchButtonView.isVisible = collection.stations.isNotEmpty()
|
||||
}
|
||||
// CASE STATION CARD
|
||||
is StationViewHolder -> {
|
||||
// get station from position
|
||||
val station: Station = collection.stations[position]
|
||||
val station: Station = filteredStations[position]
|
||||
|
||||
// get reference to StationViewHolder
|
||||
val stationViewHolder: StationViewHolder = holder
|
||||
@@ -583,7 +615,7 @@ class CollectionAdapter(
|
||||
|
||||
} else if (holder is StationViewHolder) {
|
||||
// get station from position
|
||||
val station: Station = collection.stations[holder.bindingAdapterPosition]
|
||||
val station: Station = filteredStations[holder.bindingAdapterPosition]
|
||||
|
||||
for (data in payloads) {
|
||||
when (data as Int) {
|
||||
@@ -621,19 +653,24 @@ class CollectionAdapter(
|
||||
/* Overrides getItemCount */
|
||||
override fun getItemCount(): Int {
|
||||
// +1 ==> the add station card
|
||||
return collection.stations.size + 1
|
||||
return filteredStations.size + 1
|
||||
}
|
||||
|
||||
|
||||
/* Removes a station from collection */
|
||||
fun removeStation(context: Context, position: Int) {
|
||||
val station = filteredStations[position]
|
||||
val originalPosition = collection.stations.indexOfFirst { it.uuid == station.uuid }
|
||||
if (originalPosition == -1) return
|
||||
|
||||
val newCollection = collection.deepCopy()
|
||||
// delete images assets
|
||||
CollectionHelper.deleteStationImages(context, newCollection.stations[position])
|
||||
CollectionHelper.deleteStationImages(context, newCollection.stations[originalPosition])
|
||||
// remove station from collection
|
||||
newCollection.stations.removeAt(position)
|
||||
newCollection.stations.removeAt(originalPosition)
|
||||
collection = newCollection
|
||||
// update list
|
||||
filteredStations.removeAt(position)
|
||||
notifyItemRemoved(position)
|
||||
// save collection and broadcast changes
|
||||
CollectionHelper.saveCollection(context, newCollection)
|
||||
@@ -642,15 +679,16 @@ class CollectionAdapter(
|
||||
|
||||
/* Toggles starred status of a station */
|
||||
fun toggleStarredStation(context: Context, position: Int) {
|
||||
val station = filteredStations[position]
|
||||
val originalPosition = collection.stations.indexOfFirst { it.uuid == station.uuid }
|
||||
if (originalPosition == -1) return
|
||||
|
||||
// update view (reset "swipe" state of station card)
|
||||
notifyItemChanged(position)
|
||||
// mark starred
|
||||
val stationUuid: String = collection.stations[position].uuid
|
||||
collection.stations[position].apply { starred = !starred }
|
||||
collection.stations[originalPosition].apply { starred = !starred }
|
||||
// sort collection
|
||||
collection = CollectionHelper.sortCollection(collection)
|
||||
// update list
|
||||
notifyItemMoved(position, CollectionHelper.getStationPosition(collection, stationUuid))
|
||||
// save collection and broadcast changes
|
||||
CollectionHelper.saveCollection(context, collection)
|
||||
}
|
||||
@@ -690,7 +728,7 @@ class CollectionAdapter(
|
||||
|
||||
/* Determines if position is last */
|
||||
private fun isPositionFooter(position: Int): Boolean {
|
||||
return position == collection.stations.size
|
||||
return position == filteredStations.size
|
||||
}
|
||||
|
||||
|
||||
@@ -698,6 +736,7 @@ class CollectionAdapter(
|
||||
@SuppressLint("NotifyDataSetChanged")
|
||||
private fun updateRecyclerView(oldCollection: Collection, newCollection: Collection) {
|
||||
collection = newCollection
|
||||
applyFilter()
|
||||
if (oldCollection.stations.isEmpty() && newCollection.stations.isNotEmpty()) {
|
||||
// data set has been initialized - redraw the whole list
|
||||
notifyDataSetChanged()
|
||||
@@ -757,6 +796,8 @@ class CollectionAdapter(
|
||||
listItemAddNewLayout.findViewById(R.id.card_settings)
|
||||
val visualizerButtonView: ExtendedFloatingActionButton =
|
||||
listItemAddNewLayout.findViewById(R.id.card_visualizer)
|
||||
val playerSearchButtonView: ExtendedFloatingActionButton =
|
||||
listItemAddNewLayout.findViewById(R.id.player_search_button)
|
||||
}
|
||||
/*
|
||||
* End of inner class
|
||||
|
||||
@@ -6,7 +6,10 @@ import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.graphics.drawable.AnimatedVectorDrawable
|
||||
import android.transition.ChangeBounds
|
||||
import android.transition.TransitionManager
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.view.animation.Animation
|
||||
import android.view.animation.AnimationUtils
|
||||
import android.widget.ImageButton
|
||||
@@ -23,7 +26,9 @@ import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.google.android.gms.cast.framework.CastButtonFactory
|
||||
import com.google.android.material.bottomsheet.BottomSheetBehavior
|
||||
import com.google.android.material.materialswitch.MaterialSwitch
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
import com.google.android.material.textfield.TextInputEditText
|
||||
import com.michatec.radio.Keys
|
||||
import com.michatec.radio.R
|
||||
import com.michatec.radio.core.Station
|
||||
@@ -56,6 +61,10 @@ data class LayoutHolder(var rootView: View) {
|
||||
var playerPrevButtonView: ImageButton? = rootView.findViewById(R.id.player_prev_button)
|
||||
var playerNextButtonView: ImageButton? = rootView.findViewById(R.id.player_next_button)
|
||||
private var bufferingIndicator: ProgressBar = rootView.findViewById(R.id.player_buffering_indicator)
|
||||
private var searchLayout: View? = rootView.findViewById(R.id.search_layout)
|
||||
var searchInputEditText: TextInputEditText? = rootView.findViewById(R.id.search_input_edit_text)
|
||||
var searchFilterFavoritesSwitch: MaterialSwitch? = rootView.findViewById(R.id.search_filter_favorites_switch)
|
||||
private var searchNoResultsView: View? = rootView.findViewById(R.id.search_no_results_view)
|
||||
private var sheetStreamingLinkHeadline: TextView? = rootView.findViewById(R.id.sheet_streaming_link_headline)
|
||||
private var sheetStreamingLinkView: TextView? = rootView.findViewById(R.id.sheet_streaming_link)
|
||||
private var sheetMetadataHistoryHeadline: TextView? = rootView.findViewById(R.id.sheet_metadata_headline)
|
||||
@@ -362,6 +371,27 @@ data class LayoutHolder(var rootView: View) {
|
||||
}
|
||||
}
|
||||
|
||||
/* Toggles visibility of the search layout */
|
||||
fun toggleSearchLayout(visible: Boolean) {
|
||||
val parent = searchLayout?.parent as? ViewGroup
|
||||
if (parent != null) {
|
||||
val transition = ChangeBounds()
|
||||
transition.duration = 300
|
||||
TransitionManager.beginDelayedTransition(parent, transition)
|
||||
}
|
||||
searchLayout?.isVisible = visible
|
||||
if (!visible) {
|
||||
searchInputEditText?.text?.clear()
|
||||
searchFilterFavoritesSwitch?.isChecked = false
|
||||
searchNoResultsView?.isGone = true
|
||||
}
|
||||
}
|
||||
|
||||
/* Shows/hides no results message */
|
||||
fun toggleSearchNoResults(show: Boolean) {
|
||||
searchNoResultsView?.isVisible = show
|
||||
}
|
||||
|
||||
|
||||
/* Toggles visibility of the onboarding screen */
|
||||
fun toggleOnboarding(context: Context, collectionSize: Int): Boolean {
|
||||
|
||||
Reference in new issue
Block a user