2019-04-28 22:43:54 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2019 Mauricio Colli <mauriciocolli@outlook.com>
|
|
|
|
* FeedFragment.kt is part of NewPipe
|
|
|
|
*
|
|
|
|
* License: GPL-3.0+
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.schabi.newpipe.local.feed
|
|
|
|
|
2020-04-05 16:11:03 +02:00
|
|
|
import android.annotation.SuppressLint
|
|
|
|
import android.app.Activity
|
2019-04-28 22:43:54 +02:00
|
|
|
import android.content.Intent
|
2021-06-15 18:40:25 +02:00
|
|
|
import android.content.SharedPreferences
|
2019-04-28 22:43:54 +02:00
|
|
|
import android.os.Bundle
|
|
|
|
import android.os.Parcelable
|
2020-05-01 20:13:01 +02:00
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.Menu
|
|
|
|
import android.view.MenuInflater
|
|
|
|
import android.view.MenuItem
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2021-04-02 21:41:06 +02:00
|
|
|
import androidx.annotation.Nullable
|
2020-03-14 04:11:30 +01:00
|
|
|
import androidx.appcompat.app.AlertDialog
|
2020-04-05 16:11:03 +02:00
|
|
|
import androidx.appcompat.content.res.AppCompatResources
|
2020-11-18 23:29:58 +01:00
|
|
|
import androidx.core.content.edit
|
2020-10-17 12:08:45 +02:00
|
|
|
import androidx.core.os.bundleOf
|
2020-10-17 12:24:35 +02:00
|
|
|
import androidx.core.view.isVisible
|
2020-08-27 22:56:12 +02:00
|
|
|
import androidx.lifecycle.ViewModelProvider
|
2020-03-14 04:11:30 +01:00
|
|
|
import androidx.preference.PreferenceManager
|
2020-04-05 16:11:03 +02:00
|
|
|
import androidx.recyclerview.widget.GridLayoutManager
|
|
|
|
import com.xwray.groupie.GroupAdapter
|
|
|
|
import com.xwray.groupie.GroupieViewHolder
|
|
|
|
import com.xwray.groupie.Item
|
|
|
|
import com.xwray.groupie.OnItemClickListener
|
|
|
|
import com.xwray.groupie.OnItemLongClickListener
|
2019-04-28 22:43:54 +02:00
|
|
|
import icepick.State
|
2021-04-02 21:41:06 +02:00
|
|
|
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
|
|
|
|
import io.reactivex.rxjava3.core.Single
|
|
|
|
import io.reactivex.rxjava3.disposables.CompositeDisposable
|
|
|
|
import io.reactivex.rxjava3.schedulers.Schedulers
|
|
|
|
import org.schabi.newpipe.NewPipeDatabase
|
2019-04-28 22:43:54 +02:00
|
|
|
import org.schabi.newpipe.R
|
2020-01-28 06:59:49 +01:00
|
|
|
import org.schabi.newpipe.database.feed.model.FeedGroupEntity
|
2021-04-02 21:41:06 +02:00
|
|
|
import org.schabi.newpipe.database.subscription.SubscriptionEntity
|
2020-11-03 09:21:37 +01:00
|
|
|
import org.schabi.newpipe.databinding.FragmentFeedBinding
|
2020-12-11 14:55:47 +01:00
|
|
|
import org.schabi.newpipe.error.ErrorInfo
|
2020-12-09 12:42:01 +01:00
|
|
|
import org.schabi.newpipe.error.UserAction
|
2021-04-02 21:41:06 +02:00
|
|
|
import org.schabi.newpipe.extractor.exceptions.AccountTerminatedException
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException
|
2020-04-05 16:11:03 +02:00
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfoItem
|
|
|
|
import org.schabi.newpipe.extractor.stream.StreamType
|
2021-03-31 23:32:53 +02:00
|
|
|
import org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty
|
2020-04-05 16:11:03 +02:00
|
|
|
import org.schabi.newpipe.fragments.BaseStateFragment
|
|
|
|
import org.schabi.newpipe.info_list.InfoItemDialog
|
2021-01-16 04:32:01 +01:00
|
|
|
import org.schabi.newpipe.ktx.animate
|
2021-01-15 22:29:18 +01:00
|
|
|
import org.schabi.newpipe.ktx.animateHideRecyclerViewAllowingScrolling
|
2020-04-05 16:11:03 +02:00
|
|
|
import org.schabi.newpipe.local.feed.item.StreamItem
|
2019-04-28 22:43:54 +02:00
|
|
|
import org.schabi.newpipe.local.feed.service.FeedLoadService
|
2021-04-02 21:41:06 +02:00
|
|
|
import org.schabi.newpipe.local.subscription.SubscriptionManager
|
2020-04-05 16:11:03 +02:00
|
|
|
import org.schabi.newpipe.player.helper.PlayerHolder
|
2019-04-28 22:43:54 +02:00
|
|
|
import org.schabi.newpipe.util.Localization
|
2020-04-05 16:11:03 +02:00
|
|
|
import org.schabi.newpipe.util.NavigationHelper
|
|
|
|
import org.schabi.newpipe.util.StreamDialogEntry
|
2021-07-31 10:51:59 +02:00
|
|
|
import org.schabi.newpipe.util.ThemeHelper.getGridSpanCountStreams
|
2021-07-19 20:47:50 +02:00
|
|
|
import org.schabi.newpipe.util.ThemeHelper.shouldUseGridLayout
|
2020-12-20 05:23:05 +01:00
|
|
|
import java.time.OffsetDateTime
|
2020-04-05 16:11:03 +02:00
|
|
|
import java.util.ArrayList
|
2019-04-28 22:43:54 +02:00
|
|
|
|
2020-04-05 16:11:03 +02:00
|
|
|
class FeedFragment : BaseStateFragment<FeedState>() {
|
2020-11-03 09:21:37 +01:00
|
|
|
private var _feedBinding: FragmentFeedBinding? = null
|
|
|
|
private val feedBinding get() = _feedBinding!!
|
|
|
|
|
2021-04-02 21:41:06 +02:00
|
|
|
private val disposables = CompositeDisposable()
|
|
|
|
|
2019-04-28 22:43:54 +02:00
|
|
|
private lateinit var viewModel: FeedViewModel
|
2020-04-05 16:11:03 +02:00
|
|
|
@State @JvmField var listState: Parcelable? = null
|
2019-04-28 22:43:54 +02:00
|
|
|
|
2020-01-28 06:59:49 +01:00
|
|
|
private var groupId = FeedGroupEntity.GROUP_ALL_ID
|
2019-04-28 22:43:54 +02:00
|
|
|
private var groupName = ""
|
2020-12-20 05:23:05 +01:00
|
|
|
private var oldestSubscriptionUpdate: OffsetDateTime? = null
|
2019-04-28 22:43:54 +02:00
|
|
|
|
2020-04-05 16:11:03 +02:00
|
|
|
private lateinit var groupAdapter: GroupAdapter<GroupieViewHolder>
|
|
|
|
@State @JvmField var showPlayedItems: Boolean = true
|
|
|
|
|
2021-06-15 18:40:25 +02:00
|
|
|
private var onSettingsChangeListener: SharedPreferences.OnSharedPreferenceChangeListener? = null
|
|
|
|
private var updateListViewModeOnResume = false
|
2021-07-11 04:00:32 +02:00
|
|
|
private var isRefreshing = false
|
2021-06-15 18:40:25 +02:00
|
|
|
|
2019-04-28 22:43:54 +02:00
|
|
|
init {
|
|
|
|
setHasOptionsMenu(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
groupId = arguments?.getLong(KEY_GROUP_ID, FeedGroupEntity.GROUP_ALL_ID)
|
2020-10-31 21:55:45 +01:00
|
|
|
?: FeedGroupEntity.GROUP_ALL_ID
|
2019-04-28 22:43:54 +02:00
|
|
|
groupName = arguments?.getString(KEY_GROUP_NAME) ?: ""
|
2021-06-15 18:40:25 +02:00
|
|
|
|
|
|
|
onSettingsChangeListener = SharedPreferences.OnSharedPreferenceChangeListener { _, key ->
|
|
|
|
if (key.equals(getString(R.string.list_view_mode_key))) {
|
|
|
|
updateListViewModeOnResume = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PreferenceManager.getDefaultSharedPreferences(activity)
|
|
|
|
.registerOnSharedPreferenceChangeListener(onSettingsChangeListener)
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
|
|
|
|
return inflater.inflate(R.layout.fragment_feed, container, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onViewCreated(rootView: View, savedInstanceState: Bundle?) {
|
2021-01-16 18:38:29 +01:00
|
|
|
// super.onViewCreated() calls initListeners() which require the binding to be initialized
|
2020-11-03 09:21:37 +01:00
|
|
|
_feedBinding = FragmentFeedBinding.bind(rootView)
|
2021-01-16 18:38:29 +01:00
|
|
|
super.onViewCreated(rootView, savedInstanceState)
|
2020-11-03 09:21:37 +01:00
|
|
|
|
2020-04-05 16:11:03 +02:00
|
|
|
val factory = FeedViewModel.Factory(requireContext(), groupId, showPlayedItems)
|
|
|
|
viewModel = ViewModelProvider(this, factory).get(FeedViewModel::class.java)
|
|
|
|
viewModel.stateLiveData.observe(viewLifecycleOwner, { it?.let(::handleResult) })
|
|
|
|
|
|
|
|
groupAdapter = GroupAdapter<GroupieViewHolder>().apply {
|
|
|
|
setOnItemClickListener(listenerStreamItem)
|
|
|
|
setOnItemLongClickListener(listenerStreamItem)
|
|
|
|
}
|
|
|
|
|
2021-06-15 18:40:25 +02:00
|
|
|
feedBinding.itemsList.adapter = groupAdapter
|
|
|
|
setupListViewMode()
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onPause() {
|
|
|
|
super.onPause()
|
2021-01-18 11:35:45 +01:00
|
|
|
listState = feedBinding.itemsList.layoutManager?.onSaveInstanceState()
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
|
|
|
updateRelativeTimeViews()
|
2021-06-15 18:40:25 +02:00
|
|
|
|
|
|
|
if (updateListViewModeOnResume) {
|
|
|
|
updateListViewModeOnResume = false
|
|
|
|
|
|
|
|
setupListViewMode()
|
|
|
|
if (viewModel.stateLiveData.value != null) {
|
|
|
|
handleResult(viewModel.stateLiveData.value!!)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun setupListViewMode() {
|
|
|
|
// does everything needed to setup the layouts for grid or list modes
|
2021-07-31 10:51:59 +02:00
|
|
|
groupAdapter.spanCount = if (shouldUseGridLayout(context)) getGridSpanCountStreams(context) else 1
|
2021-06-15 18:40:25 +02:00
|
|
|
feedBinding.itemsList.layoutManager = GridLayoutManager(requireContext(), groupAdapter.spanCount).apply {
|
|
|
|
spanSizeLookup = groupAdapter.spanSizeLookup
|
|
|
|
}
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun initListeners() {
|
|
|
|
super.initListeners()
|
2021-01-16 18:38:29 +01:00
|
|
|
feedBinding.refreshRootView.setOnClickListener { reloadContent() }
|
2021-01-15 22:29:18 +01:00
|
|
|
feedBinding.swipeRefreshLayout.setOnRefreshListener { reloadContent() }
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|
|
|
|
|
2020-05-01 20:13:21 +02:00
|
|
|
// /////////////////////////////////////////////////////////////////////////
|
2019-04-28 22:43:54 +02:00
|
|
|
// Menu
|
2020-05-01 20:13:21 +02:00
|
|
|
// /////////////////////////////////////////////////////////////////////////
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
|
|
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
|
|
|
|
super.onCreateOptionsMenu(menu, inflater)
|
2020-04-05 16:11:03 +02:00
|
|
|
|
|
|
|
activity.supportActionBar?.setDisplayShowTitleEnabled(true)
|
2019-10-11 06:09:28 +02:00
|
|
|
activity.supportActionBar?.setTitle(R.string.fragment_feed_title)
|
2019-04-28 22:43:54 +02:00
|
|
|
activity.supportActionBar?.subtitle = groupName
|
2020-03-14 04:11:30 +01:00
|
|
|
|
|
|
|
inflater.inflate(R.menu.menu_feed_fragment, menu)
|
2021-06-09 16:14:03 +02:00
|
|
|
updateTogglePlayedItemsButton(menu.findItem(R.id.menu_item_feed_toggle_played_items))
|
2020-03-14 04:11:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
|
|
|
if (item.itemId == R.id.menu_item_feed_help) {
|
|
|
|
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
|
|
|
|
|
2020-04-05 16:11:03 +02:00
|
|
|
val usingDedicatedMethod = sharedPreferences
|
|
|
|
.getBoolean(getString(R.string.feed_use_dedicated_fetch_method_key), false)
|
2020-03-14 04:11:30 +01:00
|
|
|
val enableDisableButtonText = when {
|
|
|
|
usingDedicatedMethod -> R.string.feed_use_dedicated_fetch_method_disable_button
|
|
|
|
else -> R.string.feed_use_dedicated_fetch_method_enable_button
|
|
|
|
}
|
|
|
|
|
|
|
|
AlertDialog.Builder(requireContext())
|
2020-10-31 21:55:45 +01:00
|
|
|
.setMessage(R.string.feed_use_dedicated_fetch_method_help_text)
|
|
|
|
.setNeutralButton(enableDisableButtonText) { _, _ ->
|
|
|
|
sharedPreferences.edit {
|
|
|
|
putBoolean(getString(R.string.feed_use_dedicated_fetch_method_key), !usingDedicatedMethod)
|
2020-03-14 04:11:30 +01:00
|
|
|
}
|
2020-10-31 21:55:45 +01:00
|
|
|
}
|
2021-08-30 16:15:26 +02:00
|
|
|
.setPositiveButton(resources.getString(R.string.ok), null)
|
2020-10-31 21:55:45 +01:00
|
|
|
.create()
|
|
|
|
.show()
|
2020-03-14 04:11:30 +01:00
|
|
|
return true
|
2020-04-05 16:11:03 +02:00
|
|
|
} else if (item.itemId == R.id.menu_item_feed_toggle_played_items) {
|
|
|
|
showPlayedItems = !item.isChecked
|
|
|
|
updateTogglePlayedItemsButton(item)
|
|
|
|
viewModel.togglePlayedItems(showPlayedItems)
|
2020-03-14 04:11:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return super.onOptionsItemSelected(item)
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDestroyOptionsMenu() {
|
|
|
|
super.onDestroyOptionsMenu()
|
2019-10-09 04:59:11 +02:00
|
|
|
activity?.supportActionBar?.subtitle = null
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDestroy() {
|
2021-04-02 21:41:06 +02:00
|
|
|
disposables.dispose()
|
2021-06-15 18:40:25 +02:00
|
|
|
if (onSettingsChangeListener != null) {
|
|
|
|
PreferenceManager.getDefaultSharedPreferences(activity)
|
|
|
|
.unregisterOnSharedPreferenceChangeListener(onSettingsChangeListener)
|
|
|
|
onSettingsChangeListener = null
|
|
|
|
}
|
|
|
|
|
2019-10-09 04:59:11 +02:00
|
|
|
super.onDestroy()
|
|
|
|
activity?.supportActionBar?.subtitle = null
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 09:21:37 +01:00
|
|
|
override fun onDestroyView() {
|
2020-04-05 16:11:03 +02:00
|
|
|
feedBinding.itemsList.adapter = null
|
2020-11-03 09:21:37 +01:00
|
|
|
_feedBinding = null
|
|
|
|
super.onDestroyView()
|
|
|
|
}
|
|
|
|
|
2020-04-05 16:11:03 +02:00
|
|
|
private fun updateTogglePlayedItemsButton(menuItem: MenuItem) {
|
|
|
|
menuItem.isChecked = showPlayedItems
|
|
|
|
menuItem.icon = AppCompatResources.getDrawable(
|
|
|
|
requireContext(),
|
|
|
|
if (showPlayedItems) R.drawable.ic_visibility_on else R.drawable.ic_visibility_off
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// //////////////////////////////////////////////////////////////////////////
|
2019-04-28 22:43:54 +02:00
|
|
|
// Handling
|
2020-04-05 16:11:03 +02:00
|
|
|
// //////////////////////////////////////////////////////////////////////////
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
|
|
override fun showLoading() {
|
2020-12-11 14:55:47 +01:00
|
|
|
super.showLoading()
|
2021-01-15 22:29:18 +01:00
|
|
|
feedBinding.itemsList.animateHideRecyclerViewAllowingScrolling()
|
2021-01-16 04:32:01 +01:00
|
|
|
feedBinding.refreshRootView.animate(false, 0)
|
|
|
|
feedBinding.loadingProgressText.animate(true, 200)
|
2021-01-15 22:29:18 +01:00
|
|
|
feedBinding.swipeRefreshLayout.isRefreshing = true
|
2021-07-11 04:00:32 +02:00
|
|
|
isRefreshing = true
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun hideLoading() {
|
2020-12-11 14:55:47 +01:00
|
|
|
super.hideLoading()
|
2020-04-05 16:11:03 +02:00
|
|
|
feedBinding.itemsList.animate(true, 0)
|
2021-01-16 04:32:01 +01:00
|
|
|
feedBinding.refreshRootView.animate(true, 200)
|
|
|
|
feedBinding.loadingProgressText.animate(false, 0)
|
2021-01-15 22:29:18 +01:00
|
|
|
feedBinding.swipeRefreshLayout.isRefreshing = false
|
2021-07-11 04:00:32 +02:00
|
|
|
isRefreshing = false
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun showEmptyState() {
|
2020-12-11 14:55:47 +01:00
|
|
|
super.showEmptyState()
|
2021-01-15 22:29:18 +01:00
|
|
|
feedBinding.itemsList.animateHideRecyclerViewAllowingScrolling()
|
2021-01-16 04:32:01 +01:00
|
|
|
feedBinding.refreshRootView.animate(true, 200)
|
|
|
|
feedBinding.loadingProgressText.animate(false, 0)
|
2021-01-15 22:29:18 +01:00
|
|
|
feedBinding.swipeRefreshLayout.isRefreshing = false
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun handleResult(result: FeedState) {
|
|
|
|
when (result) {
|
|
|
|
is FeedState.ProgressState -> handleProgressState(result)
|
|
|
|
is FeedState.LoadedState -> handleLoadedState(result)
|
|
|
|
is FeedState.ErrorState -> if (handleErrorState(result)) return
|
|
|
|
}
|
|
|
|
|
|
|
|
updateRefreshViewState()
|
|
|
|
}
|
|
|
|
|
2020-12-11 14:55:47 +01:00
|
|
|
override fun handleError() {
|
|
|
|
super.handleError()
|
2021-01-15 22:29:18 +01:00
|
|
|
feedBinding.itemsList.animateHideRecyclerViewAllowingScrolling()
|
|
|
|
feedBinding.refreshRootView.animate(false, 0)
|
|
|
|
feedBinding.loadingProgressText.animate(false, 0)
|
|
|
|
feedBinding.swipeRefreshLayout.isRefreshing = false
|
2021-07-11 04:00:32 +02:00
|
|
|
isRefreshing = false
|
2020-12-11 14:55:47 +01:00
|
|
|
}
|
|
|
|
|
2019-04-28 22:43:54 +02:00
|
|
|
private fun handleProgressState(progressState: FeedState.ProgressState) {
|
|
|
|
showLoading()
|
|
|
|
|
|
|
|
val isIndeterminate = progressState.currentProgress == -1 &&
|
2020-10-31 21:55:45 +01:00
|
|
|
progressState.maxProgress == -1
|
2019-04-28 22:43:54 +02:00
|
|
|
|
2021-01-18 11:35:45 +01:00
|
|
|
feedBinding.loadingProgressText.text = if (!isIndeterminate) {
|
|
|
|
"${progressState.currentProgress}/${progressState.maxProgress}"
|
2019-04-28 22:43:54 +02:00
|
|
|
} else if (progressState.progressMessage > 0) {
|
2021-01-20 10:44:44 +01:00
|
|
|
getString(progressState.progressMessage)
|
2019-04-28 22:43:54 +02:00
|
|
|
} else {
|
2021-01-18 11:35:45 +01:00
|
|
|
"∞/∞"
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 09:21:37 +01:00
|
|
|
feedBinding.loadingProgressBar.isIndeterminate = isIndeterminate ||
|
2021-01-15 02:15:07 +01:00
|
|
|
(progressState.maxProgress > 0 && progressState.currentProgress == 0)
|
2020-11-03 09:21:37 +01:00
|
|
|
feedBinding.loadingProgressBar.progress = progressState.currentProgress
|
2019-04-28 22:43:54 +02:00
|
|
|
|
2020-11-03 09:21:37 +01:00
|
|
|
feedBinding.loadingProgressBar.max = progressState.maxProgress
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|
|
|
|
|
2020-04-05 16:11:03 +02:00
|
|
|
private fun showStreamDialog(item: StreamInfoItem) {
|
|
|
|
val context = context
|
|
|
|
val activity: Activity? = getActivity()
|
|
|
|
if (context == null || context.resources == null || activity == null) return
|
|
|
|
|
|
|
|
val entries = ArrayList<StreamDialogEntry>()
|
Add play next to long press menu & refactor enqueue methods (#6872)
* added mvp play next button in long press menu; new intent handling, new long press dialog entry, new dialog functions, new strings
* changed line length for checkstyle pass
* cleaned comments, moved strings
* Update app/src/main/res/values/strings.xml
to make long press entry more descriptive
Co-authored-by: opusforlife2 <53176348+opusforlife2@users.noreply.github.com>
* Update app/src/main/res/values/strings.xml
Co-authored-by: Stypox <stypox@pm.me>
* replace redundant nextOnVideoPlayer methods
Co-authored-by: Stypox <stypox@pm.me>
* add enqueueNextOnPlayer and enqueueOnPlayer without selectOnAppend and RESUME_PLAYBACK/ deprecate enqueueNextOn*Player and enqueueOn*Player methods
add getPlayerIntent, getPlayerEnqueueIntent and getPlayerEnqueueNextIntent without selectOnAppend and RESUME_PLAYBACK/ deprecate those with
add section comments
* removed deprecated methods
removed redundant methods
* removed deprecated methods
removed redundant methods
* replaced APPEND_ONLY, removed SELECT_ON_APPEND / replaced remaining enqueueOn*Player methods
* now works with playlists
* renamed dialog entry
* checking for >1 items in the queue using the PlayerHolder
* making enqueue*OnPlayer safe to call when no video is playing (defaulting to audio)
* corrected strings
* improve getQueueSize in PlayerHolder
* long press to enqueue only if queue isnt empty
* add Whitespace
Co-authored-by: Stypox <stypox@pm.me>
* clarify comments / add spaces
* PlayerType as parameter of the enqueueOnPlayer method
add Helper method
* using the helper function everywhere (except for the background and popup long-press actions (also on playlists, history, ...)), so basically nowhere
/ passing checkstyle
* assimilated the enqueue*OnPlayer methods
* removed redundant comment, variable
* simplify code line
Co-authored-by: Stypox <stypox@pm.me>
* move if
* replace workaround for isPlayerOpen()
Co-authored-by: Stypox <stypox@pm.me>
* replaced workarounds (getType), corrected static access with getInstance
* remove unused imports
* changed method call to original, new method doesnt exist yet.
* Use getter method instead of property access syntax.
* improve conditional for play next entry
Co-authored-by: Stypox <stypox@pm.me>
* show play next btn in feed fragment
Co-authored-by: Stypox <stypox@pm.me>
* add play next to local playlist and statistics fragment
Co-authored-by: Stypox <stypox@pm.me>
* formating
Co-authored-by: Stypox <stypox@pm.me>
* correcting logic
Co-authored-by: Stypox <stypox@pm.me>
* remove 2 year old unused string, formating
Co-authored-by: Stypox <stypox@pm.me>
* correct enqueue (next) conditionals, default to background if no player is open. Dont generally default to background play.
* remove player open checks from button long press enqueue actions
* improve log msg
* Rename next to enqueue_next
* Refactor kotlin
Co-authored-by: opusforlife2 <53176348+opusforlife2@users.noreply.github.com>
Co-authored-by: Stypox <stypox@pm.me>
2021-09-18 11:22:49 +02:00
|
|
|
if (PlayerHolder.getInstance().isPlayerOpen) {
|
2020-04-05 16:11:03 +02:00
|
|
|
entries.add(StreamDialogEntry.enqueue)
|
Add play next to long press menu & refactor enqueue methods (#6872)
* added mvp play next button in long press menu; new intent handling, new long press dialog entry, new dialog functions, new strings
* changed line length for checkstyle pass
* cleaned comments, moved strings
* Update app/src/main/res/values/strings.xml
to make long press entry more descriptive
Co-authored-by: opusforlife2 <53176348+opusforlife2@users.noreply.github.com>
* Update app/src/main/res/values/strings.xml
Co-authored-by: Stypox <stypox@pm.me>
* replace redundant nextOnVideoPlayer methods
Co-authored-by: Stypox <stypox@pm.me>
* add enqueueNextOnPlayer and enqueueOnPlayer without selectOnAppend and RESUME_PLAYBACK/ deprecate enqueueNextOn*Player and enqueueOn*Player methods
add getPlayerIntent, getPlayerEnqueueIntent and getPlayerEnqueueNextIntent without selectOnAppend and RESUME_PLAYBACK/ deprecate those with
add section comments
* removed deprecated methods
removed redundant methods
* removed deprecated methods
removed redundant methods
* replaced APPEND_ONLY, removed SELECT_ON_APPEND / replaced remaining enqueueOn*Player methods
* now works with playlists
* renamed dialog entry
* checking for >1 items in the queue using the PlayerHolder
* making enqueue*OnPlayer safe to call when no video is playing (defaulting to audio)
* corrected strings
* improve getQueueSize in PlayerHolder
* long press to enqueue only if queue isnt empty
* add Whitespace
Co-authored-by: Stypox <stypox@pm.me>
* clarify comments / add spaces
* PlayerType as parameter of the enqueueOnPlayer method
add Helper method
* using the helper function everywhere (except for the background and popup long-press actions (also on playlists, history, ...)), so basically nowhere
/ passing checkstyle
* assimilated the enqueue*OnPlayer methods
* removed redundant comment, variable
* simplify code line
Co-authored-by: Stypox <stypox@pm.me>
* move if
* replace workaround for isPlayerOpen()
Co-authored-by: Stypox <stypox@pm.me>
* replaced workarounds (getType), corrected static access with getInstance
* remove unused imports
* changed method call to original, new method doesnt exist yet.
* Use getter method instead of property access syntax.
* improve conditional for play next entry
Co-authored-by: Stypox <stypox@pm.me>
* show play next btn in feed fragment
Co-authored-by: Stypox <stypox@pm.me>
* add play next to local playlist and statistics fragment
Co-authored-by: Stypox <stypox@pm.me>
* formating
Co-authored-by: Stypox <stypox@pm.me>
* correcting logic
Co-authored-by: Stypox <stypox@pm.me>
* remove 2 year old unused string, formating
Co-authored-by: Stypox <stypox@pm.me>
* correct enqueue (next) conditionals, default to background if no player is open. Dont generally default to background play.
* remove player open checks from button long press enqueue actions
* improve log msg
* Rename next to enqueue_next
* Refactor kotlin
Co-authored-by: opusforlife2 <53176348+opusforlife2@users.noreply.github.com>
Co-authored-by: Stypox <stypox@pm.me>
2021-09-18 11:22:49 +02:00
|
|
|
|
|
|
|
if (PlayerHolder.getInstance().queueSize > 1) {
|
|
|
|
entries.add(StreamDialogEntry.enqueue_next)
|
|
|
|
}
|
2020-04-05 16:11:03 +02:00
|
|
|
}
|
Add play next to long press menu & refactor enqueue methods (#6872)
* added mvp play next button in long press menu; new intent handling, new long press dialog entry, new dialog functions, new strings
* changed line length for checkstyle pass
* cleaned comments, moved strings
* Update app/src/main/res/values/strings.xml
to make long press entry more descriptive
Co-authored-by: opusforlife2 <53176348+opusforlife2@users.noreply.github.com>
* Update app/src/main/res/values/strings.xml
Co-authored-by: Stypox <stypox@pm.me>
* replace redundant nextOnVideoPlayer methods
Co-authored-by: Stypox <stypox@pm.me>
* add enqueueNextOnPlayer and enqueueOnPlayer without selectOnAppend and RESUME_PLAYBACK/ deprecate enqueueNextOn*Player and enqueueOn*Player methods
add getPlayerIntent, getPlayerEnqueueIntent and getPlayerEnqueueNextIntent without selectOnAppend and RESUME_PLAYBACK/ deprecate those with
add section comments
* removed deprecated methods
removed redundant methods
* removed deprecated methods
removed redundant methods
* replaced APPEND_ONLY, removed SELECT_ON_APPEND / replaced remaining enqueueOn*Player methods
* now works with playlists
* renamed dialog entry
* checking for >1 items in the queue using the PlayerHolder
* making enqueue*OnPlayer safe to call when no video is playing (defaulting to audio)
* corrected strings
* improve getQueueSize in PlayerHolder
* long press to enqueue only if queue isnt empty
* add Whitespace
Co-authored-by: Stypox <stypox@pm.me>
* clarify comments / add spaces
* PlayerType as parameter of the enqueueOnPlayer method
add Helper method
* using the helper function everywhere (except for the background and popup long-press actions (also on playlists, history, ...)), so basically nowhere
/ passing checkstyle
* assimilated the enqueue*OnPlayer methods
* removed redundant comment, variable
* simplify code line
Co-authored-by: Stypox <stypox@pm.me>
* move if
* replace workaround for isPlayerOpen()
Co-authored-by: Stypox <stypox@pm.me>
* replaced workarounds (getType), corrected static access with getInstance
* remove unused imports
* changed method call to original, new method doesnt exist yet.
* Use getter method instead of property access syntax.
* improve conditional for play next entry
Co-authored-by: Stypox <stypox@pm.me>
* show play next btn in feed fragment
Co-authored-by: Stypox <stypox@pm.me>
* add play next to local playlist and statistics fragment
Co-authored-by: Stypox <stypox@pm.me>
* formating
Co-authored-by: Stypox <stypox@pm.me>
* correcting logic
Co-authored-by: Stypox <stypox@pm.me>
* remove 2 year old unused string, formating
Co-authored-by: Stypox <stypox@pm.me>
* correct enqueue (next) conditionals, default to background if no player is open. Dont generally default to background play.
* remove player open checks from button long press enqueue actions
* improve log msg
* Rename next to enqueue_next
* Refactor kotlin
Co-authored-by: opusforlife2 <53176348+opusforlife2@users.noreply.github.com>
Co-authored-by: Stypox <stypox@pm.me>
2021-09-18 11:22:49 +02:00
|
|
|
|
2020-04-05 16:11:03 +02:00
|
|
|
if (item.streamType == StreamType.AUDIO_STREAM) {
|
|
|
|
entries.addAll(
|
|
|
|
listOf(
|
|
|
|
StreamDialogEntry.start_here_on_background,
|
|
|
|
StreamDialogEntry.append_playlist,
|
2021-06-09 16:15:04 +02:00
|
|
|
StreamDialogEntry.share,
|
2021-07-27 21:26:51 +02:00
|
|
|
StreamDialogEntry.open_in_browser
|
2020-04-05 16:11:03 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
entries.addAll(
|
|
|
|
listOf(
|
|
|
|
StreamDialogEntry.start_here_on_background,
|
|
|
|
StreamDialogEntry.start_here_on_popup,
|
|
|
|
StreamDialogEntry.append_playlist,
|
2021-06-09 16:15:04 +02:00
|
|
|
StreamDialogEntry.share,
|
2021-07-27 21:26:51 +02:00
|
|
|
StreamDialogEntry.open_in_browser
|
2020-04-05 16:11:03 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
2021-08-22 19:39:13 +02:00
|
|
|
|
|
|
|
// show "mark as watched" only when watch history is enabled
|
|
|
|
val isWatchHistoryEnabled = PreferenceManager
|
|
|
|
.getDefaultSharedPreferences(context)
|
|
|
|
.getBoolean(getString(R.string.enable_watch_history_key), false)
|
|
|
|
if (item.streamType != StreamType.AUDIO_LIVE_STREAM &&
|
|
|
|
item.streamType != StreamType.LIVE_STREAM &&
|
|
|
|
isWatchHistoryEnabled
|
|
|
|
) {
|
2021-07-27 21:26:51 +02:00
|
|
|
entries.add(
|
|
|
|
StreamDialogEntry.mark_as_watched
|
|
|
|
)
|
|
|
|
}
|
2021-08-13 10:58:01 +02:00
|
|
|
entries.add(StreamDialogEntry.show_channel_details)
|
2020-04-05 16:11:03 +02:00
|
|
|
|
2021-06-09 16:15:04 +02:00
|
|
|
StreamDialogEntry.setEnabledEntries(entries)
|
2020-04-05 16:11:03 +02:00
|
|
|
InfoItemDialog(activity, item, StreamDialogEntry.getCommands(context)) { _, which ->
|
|
|
|
StreamDialogEntry.clickOn(which, this, item)
|
|
|
|
}.show()
|
|
|
|
}
|
|
|
|
|
|
|
|
private val listenerStreamItem = object : OnItemClickListener, OnItemLongClickListener {
|
|
|
|
override fun onItemClick(item: Item<*>, view: View) {
|
2021-07-11 04:00:32 +02:00
|
|
|
if (item is StreamItem && !isRefreshing) {
|
2020-04-05 16:11:03 +02:00
|
|
|
val stream = item.streamWithState.stream
|
|
|
|
NavigationHelper.openVideoDetailFragment(
|
|
|
|
requireContext(), fm,
|
|
|
|
stream.serviceId, stream.url, stream.title, null, false
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onItemLongClick(item: Item<*>, view: View): Boolean {
|
2021-07-11 04:00:32 +02:00
|
|
|
if (item is StreamItem && !isRefreshing) {
|
2020-04-05 16:11:03 +02:00
|
|
|
showStreamDialog(item.streamWithState.stream.toStreamInfoItem())
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressLint("StringFormatMatches")
|
2019-04-28 22:43:54 +02:00
|
|
|
private fun handleLoadedState(loadedState: FeedState.LoadedState) {
|
2020-04-05 16:11:03 +02:00
|
|
|
|
2021-07-19 20:47:50 +02:00
|
|
|
val itemVersion = if (shouldUseGridLayout(context)) {
|
2020-04-05 16:11:03 +02:00
|
|
|
StreamItem.ItemVersion.GRID
|
|
|
|
} else {
|
|
|
|
StreamItem.ItemVersion.NORMAL
|
|
|
|
}
|
|
|
|
loadedState.items.forEach { it.itemVersion = itemVersion }
|
|
|
|
|
|
|
|
groupAdapter.updateAsync(loadedState.items, false, null)
|
|
|
|
|
2019-04-28 22:43:54 +02:00
|
|
|
listState?.run {
|
2020-11-03 09:21:37 +01:00
|
|
|
feedBinding.itemsList.layoutManager?.onRestoreInstanceState(listState)
|
2019-04-28 22:43:54 +02:00
|
|
|
listState = null
|
|
|
|
}
|
|
|
|
|
2021-04-02 21:41:06 +02:00
|
|
|
val feedsNotLoaded = loadedState.notLoadedCount > 0
|
|
|
|
feedBinding.refreshSubtitleText.isVisible = feedsNotLoaded
|
|
|
|
if (feedsNotLoaded) {
|
2021-01-15 02:15:07 +01:00
|
|
|
feedBinding.refreshSubtitleText.text = getString(
|
|
|
|
R.string.feed_subscription_not_loaded_count,
|
|
|
|
loadedState.notLoadedCount
|
|
|
|
)
|
2019-12-16 08:36:04 +01:00
|
|
|
}
|
|
|
|
|
2021-04-02 21:42:40 +02:00
|
|
|
if (oldestSubscriptionUpdate != loadedState.oldestUpdate ||
|
|
|
|
(oldestSubscriptionUpdate == null && loadedState.oldestUpdate == null)
|
|
|
|
) {
|
2021-04-02 18:16:24 +02:00
|
|
|
// ignore errors if they have already been handled for the current update
|
|
|
|
handleItemsErrors(loadedState.itemsErrors)
|
|
|
|
}
|
|
|
|
oldestSubscriptionUpdate = loadedState.oldestUpdate
|
|
|
|
|
2019-04-28 22:43:54 +02:00
|
|
|
if (loadedState.items.isEmpty()) {
|
|
|
|
showEmptyState()
|
|
|
|
} else {
|
|
|
|
hideLoading()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun handleErrorState(errorState: FeedState.ErrorState): Boolean {
|
2020-12-11 14:55:47 +01:00
|
|
|
return if (errorState.error == null) {
|
|
|
|
hideLoading()
|
|
|
|
false
|
|
|
|
} else {
|
2021-04-02 18:16:24 +02:00
|
|
|
showError(ErrorInfo(errorState.error, UserAction.REQUESTED_FEED, "Loading feed"))
|
2020-12-11 14:55:47 +01:00
|
|
|
true
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 18:16:24 +02:00
|
|
|
private fun handleItemsErrors(errors: List<Throwable>) {
|
2021-06-09 16:15:04 +02:00
|
|
|
errors.forEachIndexed { i, t ->
|
2021-04-02 18:16:24 +02:00
|
|
|
if (t is FeedLoadService.RequestException &&
|
|
|
|
t.cause is ContentNotAvailableException
|
|
|
|
) {
|
|
|
|
Single.fromCallable {
|
|
|
|
NewPipeDatabase.getInstance(requireContext()).subscriptionDAO()
|
|
|
|
.getSubscription(t.subscriptionId)
|
|
|
|
}.subscribeOn(Schedulers.io())
|
|
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
|
|
.subscribe(
|
|
|
|
{
|
|
|
|
subscriptionEntity ->
|
|
|
|
handleFeedNotAvailable(
|
|
|
|
subscriptionEntity,
|
2021-04-02 21:42:40 +02:00
|
|
|
t.cause,
|
2021-04-02 18:16:24 +02:00
|
|
|
errors.subList(i + 1, errors.size)
|
|
|
|
)
|
|
|
|
},
|
|
|
|
{ throwable -> throwable.printStackTrace() }
|
|
|
|
)
|
|
|
|
return // this will be called on the remaining errors by handleFeedNotAvailable()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 21:41:06 +02:00
|
|
|
private fun handleFeedNotAvailable(
|
|
|
|
subscriptionEntity: SubscriptionEntity,
|
2021-04-02 18:16:24 +02:00
|
|
|
@Nullable cause: Throwable?,
|
|
|
|
nextItemsErrors: List<Throwable>
|
2021-04-02 21:41:06 +02:00
|
|
|
) {
|
|
|
|
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(requireContext())
|
|
|
|
val isFastFeedModeEnabled = sharedPreferences.getBoolean(
|
|
|
|
getString(R.string.feed_use_dedicated_fetch_method_key), false
|
|
|
|
)
|
2021-04-02 18:16:24 +02:00
|
|
|
|
2021-04-02 21:41:06 +02:00
|
|
|
val builder = AlertDialog.Builder(requireContext())
|
|
|
|
.setTitle(R.string.feed_load_error)
|
|
|
|
.setPositiveButton(
|
2021-04-02 18:16:24 +02:00
|
|
|
R.string.unsubscribe
|
|
|
|
) { _, _ ->
|
|
|
|
SubscriptionManager(requireContext()).deleteSubscription(
|
|
|
|
subscriptionEntity.serviceId, subscriptionEntity.url
|
|
|
|
).subscribe()
|
|
|
|
handleItemsErrors(nextItemsErrors)
|
|
|
|
}
|
|
|
|
.setNegativeButton(R.string.cancel) { _, _ -> }
|
|
|
|
|
2021-03-31 23:32:53 +02:00
|
|
|
var message = getString(R.string.feed_load_error_account_info, subscriptionEntity.name)
|
2021-04-02 21:41:06 +02:00
|
|
|
if (cause is AccountTerminatedException) {
|
2021-03-31 23:32:53 +02:00
|
|
|
message += "\n" + getString(R.string.feed_load_error_terminated)
|
|
|
|
} else if (cause is ContentNotAvailableException) {
|
|
|
|
if (isFastFeedModeEnabled) {
|
|
|
|
message += "\n" + getString(R.string.feed_load_error_fast_unknown)
|
|
|
|
builder.setNeutralButton(R.string.feed_use_dedicated_fetch_method_disable_button) { _, _ ->
|
2021-04-02 21:41:06 +02:00
|
|
|
sharedPreferences.edit {
|
|
|
|
putBoolean(getString(R.string.feed_use_dedicated_fetch_method_key), false)
|
|
|
|
}
|
|
|
|
}
|
2021-03-31 23:32:53 +02:00
|
|
|
} else if (!isNullOrEmpty(cause.message)) {
|
|
|
|
message += "\n" + cause.message
|
|
|
|
}
|
2021-04-02 21:41:06 +02:00
|
|
|
}
|
2021-03-31 23:32:53 +02:00
|
|
|
builder.setMessage(message).create().show()
|
2021-04-02 21:41:06 +02:00
|
|
|
}
|
|
|
|
|
2019-04-28 22:43:54 +02:00
|
|
|
private fun updateRelativeTimeViews() {
|
|
|
|
updateRefreshViewState()
|
2020-04-05 16:11:03 +02:00
|
|
|
groupAdapter.notifyItemRangeChanged(
|
|
|
|
0, groupAdapter.itemCount,
|
|
|
|
StreamItem.UPDATE_RELATIVE_TIME
|
|
|
|
)
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun updateRefreshViewState() {
|
2020-12-20 05:23:05 +01:00
|
|
|
feedBinding.refreshText.text = getString(
|
|
|
|
R.string.feed_oldest_subscription_update,
|
|
|
|
oldestSubscriptionUpdate?.let { Localization.relativeTime(it) } ?: "—"
|
|
|
|
)
|
2019-04-28 22:43:54 +02:00
|
|
|
}
|
|
|
|
|
2020-05-01 20:13:21 +02:00
|
|
|
// /////////////////////////////////////////////////////////////////////////
|
2019-04-28 22:43:54 +02:00
|
|
|
// Load Service Handling
|
2020-05-01 20:13:21 +02:00
|
|
|
// /////////////////////////////////////////////////////////////////////////
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
|
|
override fun doInitialLoadLogic() {}
|
|
|
|
|
2021-01-16 18:38:29 +01:00
|
|
|
override fun reloadContent() {
|
2020-10-31 21:55:45 +01:00
|
|
|
getActivity()?.startService(
|
|
|
|
Intent(requireContext(), FeedLoadService::class.java).apply {
|
|
|
|
putExtra(FeedLoadService.EXTRA_GROUP_ID, groupId)
|
|
|
|
}
|
|
|
|
)
|
2019-04-28 22:43:54 +02:00
|
|
|
listState = null
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
const val KEY_GROUP_ID = "ARG_GROUP_ID"
|
|
|
|
const val KEY_GROUP_NAME = "ARG_GROUP_NAME"
|
|
|
|
|
|
|
|
@JvmStatic
|
2020-01-28 06:59:49 +01:00
|
|
|
fun newInstance(groupId: Long = FeedGroupEntity.GROUP_ALL_ID, groupName: String? = null): FeedFragment {
|
2019-04-28 22:43:54 +02:00
|
|
|
val feedFragment = FeedFragment()
|
2020-10-17 12:08:45 +02:00
|
|
|
feedFragment.arguments = bundleOf(KEY_GROUP_ID to groupId, KEY_GROUP_NAME to groupName)
|
2019-04-28 22:43:54 +02:00
|
|
|
return feedFragment
|
|
|
|
}
|
|
|
|
}
|
2020-03-31 19:20:15 +02:00
|
|
|
}
|