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(),
PendingUploadsContract.View,
PendingUploadsAdapter.Callback {
//fix:public and nullable to allow the dagger injection and prevent crash on the rotation.
@Inject
lateinit var pendingUploadsPresenter: PendingUploadsPresenter
@JvmField
var pendingUploadsPresenter: PendingUploadsPresenter? = null
private lateinit var binding: FragmentPendingUploadsBinding
@ -55,15 +58,11 @@ class PendingUploadsFragment :
): View {
super.onCreate(savedInstanceState)
binding = FragmentPendingUploadsBinding.inflate(inflater, container, false)
pendingUploadsPresenter.onAttachView(this)
pendingUploadsPresenter?.onAttachView(this)
initAdapter()
return binding.root
}
fun initAdapter() {
adapter = PendingUploadsAdapter(this)
}
override fun onViewCreated(
view: View,
savedInstanceState: Bundle?,
@ -72,15 +71,19 @@ class PendingUploadsFragment :
initRecyclerView()
}
fun initAdapter() {
adapter = PendingUploadsAdapter(this)
}
/**
* Initializes the recycler view.
*/
private fun initRecyclerView() {
binding.pendingUploadsRecyclerView.setLayoutManager(LinearLayoutManager(this.context))
binding.pendingUploadsRecyclerView.adapter = adapter
pendingUploadsPresenter.setup()
pendingUploadsPresenter.totalContributionList
.observe(viewLifecycleOwner) { list: PagedList<Contribution> ->
pendingUploadsPresenter?.setup()
pendingUploadsPresenter?.totalContributionList
?.observe(viewLifecycleOwner) { list: PagedList<Contribution> ->
contributionsSize = list.size
contributionsList = mutableListOf()
var pausedOrQueuedUploads = 0
@ -129,7 +132,8 @@ class PendingUploadsFragment :
String.format(locale, activity.getString(R.string.no)),
{
ViewUtil.showShortToast(context, R.string.cancelling_upload)
pendingUploadsPresenter.deleteUpload(
// uses the safe call directly
pendingUploadsPresenter?.deleteUpload(
contribution, requireContext().applicationContext,
)
},
@ -140,19 +144,24 @@ class PendingUploadsFragment :
/**
* Restarts all the paused uploads.
*/
fun restartUploads() = pendingUploadsPresenter.restartUploads(
fun restartUploads() {
pendingUploadsPresenter?.restartUploads(
contributionsList, 0, requireContext().applicationContext
)
}
/**
* 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.
*/
fun deleteUploads() {
pendingUploadsPresenter ?: return
val activity = requireActivity()
val locale = Locale.getDefault()
showAlertDialog(
@ -164,7 +173,7 @@ class PendingUploadsFragment :
{
ViewUtil.showShortToast(context, R.string.cancelling_upload)
uploadProgressActivity.hidePendingIcons()
pendingUploadsPresenter.deleteUploads(
pendingUploadsPresenter?.deleteUploads(
listOf(
STATE_QUEUED,
STATE_IN_PROGRESS,