diff --git a/app/src/main/java/com/github/libretube/ui/adapters/SearchAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/SearchAdapter.kt index 2ac2f47c9..75f018e29 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/SearchAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/SearchAdapter.kt @@ -30,7 +30,8 @@ import com.github.libretube.util.TextUtils import kotlinx.serialization.encodeToString class SearchAdapter( - private val isChannelAdapter: Boolean = false + private val isChannelAdapter: Boolean = false, + private val timeStamp: Long = 0, ) : ListAdapter(SearchCallback) { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SearchViewHolder { val layoutInflater = LayoutInflater.from(parent.context) @@ -97,7 +98,7 @@ class SearchAdapter( ImageHelper.loadImage(item.uploaderAvatar, channelImage) } root.setOnClickListener { - NavigationHelper.navigateVideo(root.context, item.url) + NavigationHelper.navigateVideo(root.context, item.url, timestamp = timeStamp) } val videoId = item.url.toID() diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index 7f83c804b..0d2eb56ca 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -1127,7 +1127,7 @@ class PlayerFragment : Fragment(), OnlinePlayerOptions { */ private fun handleLink(link: String) { // get video id if the link is a valid youtube video link - val videoId = TextUtils.getVideoIdFromUri(link) + val videoId = TextUtils.getVideoIdFromUrl(link) if (videoId.isNullOrEmpty()) { // not a YouTube video link, thus handle normally val intent = Intent(Intent.ACTION_VIEW, link.toUri()) diff --git a/app/src/main/java/com/github/libretube/ui/fragments/SearchResultFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/SearchResultFragment.kt index 286d88289..9ebd8cbc6 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/SearchResultFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/SearchResultFragment.kt @@ -21,13 +21,18 @@ import com.github.libretube.db.DatabaseHelper import com.github.libretube.db.obj.SearchHistoryItem import com.github.libretube.extensions.TAG import com.github.libretube.extensions.hideKeyboard +import com.github.libretube.extensions.toID import com.github.libretube.extensions.toastFromMainDispatcher import com.github.libretube.helpers.PreferenceHelper import com.github.libretube.ui.adapters.SearchAdapter +import com.github.libretube.ui.dialogs.ShareDialog +import com.github.libretube.util.TextUtils +import com.github.libretube.util.TextUtils.toTimeInSeconds import com.github.libretube.util.deArrow import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import okhttp3.HttpUrl.Companion.toHttpUrlOrNull class SearchResultFragment : Fragment() { private var _binding: FragmentSearchResultBinding? = null @@ -37,7 +42,7 @@ class SearchResultFragment : Fragment() { private var query = "" private lateinit var searchAdapter: SearchAdapter - private var apiSearchFilter = "all" + private var searchFilter = "all" override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -63,7 +68,7 @@ class SearchResultFragment : Fragment() { // filter options binding.filterChipGroup.setOnCheckedStateChangeListener { _, _ -> - apiSearchFilter = when ( + searchFilter = when ( binding.filterChipGroup.checkedChipId ) { R.id.chip_all -> "all" @@ -95,11 +100,20 @@ class SearchResultFragment : Fragment() { _binding?.searchResultsLayout?.isGone = true lifecycleScope.launch { + var timeStamp: Long? = null + + // parse search URLs from YouTube entered in the search bar + val searchQuery = query.toHttpUrlOrNull()?.let { + val videoId = TextUtils.getVideoIdFromUrl(it.toString()) ?: query + timeStamp = it.queryParameter("t")?.toTimeInSeconds() + "${ShareDialog.YOUTUBE_FRONTEND_URL}/watch?v=${videoId}" + } ?: query + repeatOnLifecycle(Lifecycle.State.CREATED) { view?.let { context?.hideKeyboard(it) } val response = try { withContext(Dispatchers.IO) { - RetrofitInstance.api.getSearchResults(query, apiSearchFilter).apply { + RetrofitInstance.api.getSearchResults(searchQuery, searchFilter).apply { items = items.deArrow() } } @@ -110,9 +124,10 @@ class SearchResultFragment : Fragment() { } val binding = _binding ?: return@repeatOnLifecycle - searchAdapter = SearchAdapter() + searchAdapter = SearchAdapter(timeStamp = timeStamp ?: 0) binding.searchRecycler.adapter = searchAdapter searchAdapter.submitList(response.items) + binding.searchResultsLayout.isVisible = true binding.progress.isGone = true binding.noSearchResult.isVisible = response.items.isEmpty() @@ -129,7 +144,7 @@ class SearchResultFragment : Fragment() { withContext(Dispatchers.IO) { RetrofitInstance.api.getSearchResultsNextPage( query, - apiSearchFilter, + searchFilter, nextPage!! ).apply { items = items.deArrow() diff --git a/app/src/main/java/com/github/libretube/util/TextUtils.kt b/app/src/main/java/com/github/libretube/util/TextUtils.kt index 59af28614..1b1a5e21c 100644 --- a/app/src/main/java/com/github/libretube/util/TextUtils.kt +++ b/app/src/main/java/com/github/libretube/util/TextUtils.kt @@ -52,10 +52,10 @@ object TextUtils { /** * Get video id if the link is a valid youtube video link */ - fun getVideoIdFromUri(link: String): String? { + fun getVideoIdFromUrl(link: String): String? { return link.toHttpUrlOrNull()?.let { when (it.host) { - "www.youtube.com" -> it.queryParameter("v") + "www.youtube.com", "m.youtube.com" -> it.queryParameter("v") "youtu.be" -> it.pathSegments.lastOrNull() else -> null }