1
0
mirror of https://github.com/TeamNewPipe/NewPipe.git synced 2024-11-22 11:02:35 +01:00

Use view binding in FeedFragment.

This commit is contained in:
Isira Seneviratne 2020-11-03 13:51:37 +05:30
parent 97672f06de
commit f80e1bd214

View File

@ -37,18 +37,10 @@ import androidx.lifecycle.ViewModelProvider
import androidx.preference.PreferenceManager
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import icepick.State
import kotlinx.android.synthetic.main.error_retry.error_button_retry
import kotlinx.android.synthetic.main.error_retry.error_message_view
import kotlinx.android.synthetic.main.fragment_feed.empty_state_view
import kotlinx.android.synthetic.main.fragment_feed.error_panel
import kotlinx.android.synthetic.main.fragment_feed.items_list
import kotlinx.android.synthetic.main.fragment_feed.loading_progress_bar
import kotlinx.android.synthetic.main.fragment_feed.loading_progress_text
import kotlinx.android.synthetic.main.fragment_feed.refresh_root_view
import kotlinx.android.synthetic.main.fragment_feed.refresh_subtitle_text
import kotlinx.android.synthetic.main.fragment_feed.refresh_text
import org.schabi.newpipe.R
import org.schabi.newpipe.database.feed.model.FeedGroupEntity
import org.schabi.newpipe.databinding.ErrorRetryBinding
import org.schabi.newpipe.databinding.FragmentFeedBinding
import org.schabi.newpipe.fragments.list.BaseListFragment
import org.schabi.newpipe.local.feed.service.FeedLoadService
import org.schabi.newpipe.report.UserAction
@ -57,6 +49,12 @@ import org.schabi.newpipe.util.Localization
import java.util.Calendar
class FeedFragment : BaseListFragment<FeedState, Unit>() {
private var _feedBinding: FragmentFeedBinding? = null
private val feedBinding get() = _feedBinding!!
private var _errorBinding: ErrorRetryBinding? = null
private val errorBinding get() = _errorBinding!!
private lateinit var viewModel: FeedViewModel
private lateinit var swipeRefreshLayout: SwipeRefreshLayout
@State
@ -86,15 +84,17 @@ class FeedFragment : BaseListFragment<FeedState, Unit>() {
override fun onViewCreated(rootView: View, savedInstanceState: Bundle?) {
super.onViewCreated(rootView, savedInstanceState)
swipeRefreshLayout = requireView().findViewById(R.id.swiperefresh)
swipeRefreshLayout.setOnRefreshListener { reloadContent() }
_feedBinding = FragmentFeedBinding.bind(rootView)
_errorBinding = feedBinding.errorPanel
feedBinding.swiperefresh.setOnRefreshListener { reloadContent() }
viewModel = ViewModelProvider(this, FeedViewModel.Factory(requireContext(), groupId)).get(FeedViewModel::class.java)
viewModel.stateLiveData.observe(viewLifecycleOwner, Observer { it?.let(::handleResult) })
}
override fun onPause() {
super.onPause()
listState = items_list?.layoutManager?.onSaveInstanceState()
listState = _feedBinding?.itemsList?.layoutManager?.onSaveInstanceState()
}
override fun onResume() {
@ -112,7 +112,8 @@ class FeedFragment : BaseListFragment<FeedState, Unit>() {
override fun initListeners() {
super.initListeners()
refresh_root_view.setOnClickListener {
// Using the non-null property may result in a NullPointerException
_feedBinding?.refreshRootView?.setOnClickListener {
triggerUpdate()
}
}
@ -169,55 +170,60 @@ class FeedFragment : BaseListFragment<FeedState, Unit>() {
activity?.supportActionBar?.subtitle = null
}
override fun onDestroyView() {
_feedBinding = null
super.onDestroyView()
}
// /////////////////////////////////////////////////////////////////////////
// Handling
// /////////////////////////////////////////////////////////////////////////
override fun showLoading() {
animateView(refresh_root_view, false, 0)
animateView(items_list, false, 0)
animateView(feedBinding.refreshRootView, false, 0)
animateView(feedBinding.itemsList, false, 0)
animateView(loading_progress_bar, true, 200)
animateView(loading_progress_text, true, 200)
animateView(feedBinding.loadingProgressBar, true, 200)
animateView(feedBinding.loadingProgressText, true, 200)
empty_state_view?.let { animateView(it, false, 0) }
animateView(error_panel, false, 0)
animateView(feedBinding.emptyStateView.root, false, 0)
animateView(errorBinding.root, false, 0)
}
override fun hideLoading() {
animateView(refresh_root_view, true, 200)
animateView(items_list, true, 300)
animateView(feedBinding.refreshRootView, true, 200)
animateView(feedBinding.itemsList, true, 300)
animateView(loading_progress_bar, false, 0)
animateView(loading_progress_text, false, 0)
animateView(feedBinding.loadingProgressBar, false, 0)
animateView(feedBinding.loadingProgressText, false, 0)
empty_state_view?.let { animateView(it, false, 0) }
animateView(error_panel, false, 0)
swipeRefreshLayout.isRefreshing = false
animateView(feedBinding.emptyStateView.root, false, 0)
animateView(errorBinding.root, false, 0)
feedBinding.swiperefresh.isRefreshing = false
}
override fun showEmptyState() {
animateView(refresh_root_view, true, 200)
animateView(items_list, false, 0)
animateView(feedBinding.refreshRootView, true, 200)
animateView(feedBinding.itemsList, false, 0)
animateView(loading_progress_bar, false, 0)
animateView(loading_progress_text, false, 0)
animateView(feedBinding.loadingProgressBar, false, 0)
animateView(feedBinding.loadingProgressText, false, 0)
empty_state_view?.let { animateView(it, true, 800) }
animateView(error_panel, false, 0)
animateView(feedBinding.emptyStateView.root, true, 800)
animateView(errorBinding.root, false, 0)
}
override fun showError(message: String, showRetryButton: Boolean) {
infoListAdapter.clearStreamItemList()
animateView(refresh_root_view, false, 120)
animateView(items_list, false, 120)
animateView(feedBinding.refreshRootView, false, 120)
animateView(feedBinding.itemsList, false, 120)
animateView(loading_progress_bar, false, 120)
animateView(loading_progress_text, false, 120)
animateView(feedBinding.loadingProgressBar, false, 120)
animateView(feedBinding.loadingProgressText, false, 120)
error_message_view.text = message
animateView(error_button_retry, showRetryButton, if (showRetryButton) 600 else 0)
animateView(error_panel, true, 300)
errorBinding.errorMessageView.text = message
animateView(errorBinding.errorButtonRetry, showRetryButton, if (showRetryButton) 600 else 0)
animateView(errorBinding.root, true, 300)
}
override fun handleResult(result: FeedState) {
@ -237,33 +243,34 @@ class FeedFragment : BaseListFragment<FeedState, Unit>() {
progressState.maxProgress == -1
if (!isIndeterminate) {
loading_progress_text.text = "${progressState.currentProgress}/${progressState.maxProgress}"
feedBinding.loadingProgressText.text = "${progressState.currentProgress}/${progressState.maxProgress}"
} else if (progressState.progressMessage > 0) {
loading_progress_text?.setText(progressState.progressMessage)
_feedBinding?.loadingProgressText?.setText(progressState.progressMessage)
} else {
loading_progress_text?.text = "∞/∞"
_feedBinding?.loadingProgressText?.text = "∞/∞"
}
loading_progress_bar.isIndeterminate = isIndeterminate ||
(progressState.maxProgress > 0 && progressState.currentProgress == 0)
loading_progress_bar.progress = progressState.currentProgress
feedBinding.loadingProgressBar.isIndeterminate = isIndeterminate ||
(progressState.maxProgress > 0 && progressState.currentProgress == 0)
feedBinding.loadingProgressBar.progress = progressState.currentProgress
loading_progress_bar.max = progressState.maxProgress
feedBinding.loadingProgressBar.max = progressState.maxProgress
}
private fun handleLoadedState(loadedState: FeedState.LoadedState) {
infoListAdapter.setInfoItemList(loadedState.items)
listState?.run {
items_list.layoutManager?.onRestoreInstanceState(listState)
feedBinding.itemsList.layoutManager?.onRestoreInstanceState(listState)
listState = null
}
oldestSubscriptionUpdate = loadedState.oldestUpdate
val loadedCount = loadedState.notLoadedCount > 0
refresh_subtitle_text.isVisible = loadedCount
feedBinding.refreshSubtitleText.isVisible = loadedCount
if (loadedCount) {
refresh_subtitle_text.text = getString(R.string.feed_subscription_not_loaded_count, loadedState.notLoadedCount)
feedBinding.refreshSubtitleText.text = getString(R.string.feed_subscription_not_loaded_count,
loadedState.notLoadedCount)
}
if (loadedState.itemsErrors.isNotEmpty()) {
@ -300,7 +307,7 @@ class FeedFragment : BaseListFragment<FeedState, Unit>() {
else -> ""
}
refresh_text?.text = getString(R.string.feed_oldest_subscription_update, oldestSubscriptionUpdateText)
feedBinding.refreshText.text = getString(R.string.feed_oldest_subscription_update, oldestSubscriptionUpdateText)
}
// /////////////////////////////////////////////////////////////////////////