Use view binding in FeedFragment.

This commit is contained in:
Isira Seneviratne 2020-11-03 13:51:37 +05:30
parent 97672f06de
commit f80e1bd214
1 changed files with 58 additions and 51 deletions

View File

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