fix:gracefully handle the missing presenter during configuration change

This commit is contained in:
Kota-Jagadeesh 2025-10-19 23:01:40 +05:30
parent 3a55583460
commit 8352bc5925

View file

@ -28,8 +28,11 @@ class PendingUploadsFragment :
CommonsDaggerSupportFragment(), CommonsDaggerSupportFragment(),
PendingUploadsContract.View, PendingUploadsContract.View,
PendingUploadsAdapter.Callback { PendingUploadsAdapter.Callback {
//fix:public and nullable to allow the dagger injection and prevent crash on the rotation.
@Inject @Inject
lateinit var pendingUploadsPresenter: PendingUploadsPresenter @JvmField
var pendingUploadsPresenter: PendingUploadsPresenter? = null
private lateinit var binding: FragmentPendingUploadsBinding private lateinit var binding: FragmentPendingUploadsBinding
@ -55,15 +58,11 @@ class PendingUploadsFragment :
): View { ): View {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
binding = FragmentPendingUploadsBinding.inflate(inflater, container, false) binding = FragmentPendingUploadsBinding.inflate(inflater, container, false)
pendingUploadsPresenter.onAttachView(this) pendingUploadsPresenter?.onAttachView(this)
initAdapter() initAdapter()
return binding.root return binding.root
} }
fun initAdapter() {
adapter = PendingUploadsAdapter(this)
}
override fun onViewCreated( override fun onViewCreated(
view: View, view: View,
savedInstanceState: Bundle?, savedInstanceState: Bundle?,
@ -72,47 +71,51 @@ class PendingUploadsFragment :
initRecyclerView() initRecyclerView()
} }
fun initAdapter() {
adapter = PendingUploadsAdapter(this)
}
/** /**
* Initializes the recycler view. * Initializes the recycler view.
*/ */
private fun initRecyclerView() { private fun initRecyclerView() {
binding.pendingUploadsRecyclerView.setLayoutManager(LinearLayoutManager(this.context)) binding.pendingUploadsRecyclerView.setLayoutManager(LinearLayoutManager(this.context))
binding.pendingUploadsRecyclerView.adapter = adapter binding.pendingUploadsRecyclerView.adapter = adapter
pendingUploadsPresenter.setup() pendingUploadsPresenter?.setup()
pendingUploadsPresenter.totalContributionList pendingUploadsPresenter?.totalContributionList
.observe(viewLifecycleOwner) { list: PagedList<Contribution> -> ?.observe(viewLifecycleOwner) { list: PagedList<Contribution> ->
contributionsSize = list.size contributionsSize = list.size
contributionsList = mutableListOf() contributionsList = mutableListOf()
var pausedOrQueuedUploads = 0 var pausedOrQueuedUploads = 0
list.forEach { list.forEach {
if (it != null) { if (it != null) {
if (it.state == STATE_PAUSED || if (it.state == STATE_PAUSED ||
it.state == STATE_QUEUED || it.state == STATE_QUEUED ||
it.state == STATE_IN_PROGRESS it.state == STATE_IN_PROGRESS
) { ) {
contributionsList.add(it) contributionsList.add(it)
} }
if (it.state == STATE_PAUSED || it.state == STATE_QUEUED) { if (it.state == STATE_PAUSED || it.state == STATE_QUEUED) {
pausedOrQueuedUploads++ pausedOrQueuedUploads++
}
} }
} }
} if (contributionsSize == 0) {
if (contributionsSize == 0) { binding.nopendingTextView.visibility = View.VISIBLE
binding.nopendingTextView.visibility = View.VISIBLE binding.pendingUplaodsLl.visibility = View.GONE
binding.pendingUplaodsLl.visibility = View.GONE uploadProgressActivity.hidePendingIcons()
uploadProgressActivity.hidePendingIcons()
} else {
binding.nopendingTextView.visibility = View.GONE
binding.pendingUplaodsLl.visibility = View.VISIBLE
adapter.submitList(list)
binding.progressTextView.setText("$contributionsSize uploads left")
if ((pausedOrQueuedUploads == contributionsSize) || CommonsApplication.isPaused) {
uploadProgressActivity.setPausedIcon(true)
} else { } else {
uploadProgressActivity.setPausedIcon(false) binding.nopendingTextView.visibility = View.GONE
binding.pendingUplaodsLl.visibility = View.VISIBLE
adapter.submitList(list)
binding.progressTextView.setText("$contributionsSize uploads left")
if ((pausedOrQueuedUploads == contributionsSize) || CommonsApplication.isPaused) {
uploadProgressActivity.setPausedIcon(true)
} else {
uploadProgressActivity.setPausedIcon(false)
}
} }
} }
}
} }
/** /**
@ -129,7 +132,8 @@ class PendingUploadsFragment :
String.format(locale, activity.getString(R.string.no)), String.format(locale, activity.getString(R.string.no)),
{ {
ViewUtil.showShortToast(context, R.string.cancelling_upload) ViewUtil.showShortToast(context, R.string.cancelling_upload)
pendingUploadsPresenter.deleteUpload( // uses the safe call directly
pendingUploadsPresenter?.deleteUpload(
contribution, requireContext().applicationContext, contribution, requireContext().applicationContext,
) )
}, },
@ -140,19 +144,24 @@ class PendingUploadsFragment :
/** /**
* Restarts all the paused uploads. * Restarts all the paused uploads.
*/ */
fun restartUploads() = pendingUploadsPresenter.restartUploads( fun restartUploads() {
contributionsList, 0, requireContext().applicationContext pendingUploadsPresenter?.restartUploads(
) contributionsList, 0, requireContext().applicationContext
)
}
/** /**
* Pauses all the ongoing uploads. * Pauses all the ongoing uploads.
*/ */
fun pauseUploads() = pendingUploadsPresenter.pauseUploads() fun pauseUploads() { pendingUploadsPresenter?.pauseUploads()
}
/** /**
* Cancels all the uploads after getting a confirmation from the user using Dialog. * Cancels all the uploads after getting a confirmation from the user using Dialog.
*/ */
fun deleteUploads() { fun deleteUploads() {
pendingUploadsPresenter ?: return
val activity = requireActivity() val activity = requireActivity()
val locale = Locale.getDefault() val locale = Locale.getDefault()
showAlertDialog( showAlertDialog(
@ -164,7 +173,7 @@ class PendingUploadsFragment :
{ {
ViewUtil.showShortToast(context, R.string.cancelling_upload) ViewUtil.showShortToast(context, R.string.cancelling_upload)
uploadProgressActivity.hidePendingIcons() uploadProgressActivity.hidePendingIcons()
pendingUploadsPresenter.deleteUploads( pendingUploadsPresenter?.deleteUploads(
listOf( listOf(
STATE_QUEUED, STATE_QUEUED,
STATE_IN_PROGRESS, STATE_IN_PROGRESS,