fix: null context issue by removing context getter override (#6218)

Refactored related code to resolve build issues
This commit is contained in:
Rohit Verma 2025-03-08 18:32:28 +05:30 committed by GitHub
parent d11439f85d
commit 32d485cc51
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 23 additions and 25 deletions

View file

@ -10,7 +10,7 @@ interface ContributionsContract {
interface View { interface View {
fun showMessage(localizedMessage: String) fun showMessage(localizedMessage: String)
fun getContext(): Context fun getContext(): Context?
} }
interface UserActionListener : BasePresenter<View> { interface UserActionListener : BasePresenter<View> {

View file

@ -74,12 +74,9 @@ import java.util.Date
import javax.inject.Inject import javax.inject.Inject
import javax.inject.Named import javax.inject.Named
class ContributionsFragment class ContributionsFragment : CommonsDaggerSupportFragment(), FragmentManager.OnBackStackChangedListener,
: CommonsDaggerSupportFragment(), FragmentManager.OnBackStackChangedListener,
LocationUpdateListener, MediaDetailProvider, SensorEventListener, ICampaignsView, LocationUpdateListener, MediaDetailProvider, SensorEventListener, ICampaignsView,
ContributionsContract.View, ContributionsContract.View, ContributionsListFragment.Callback {
ContributionsListFragment.Callback {
@JvmField @JvmField
@Inject @Inject
@Named("default_preferences") @Named("default_preferences")
@ -307,9 +304,11 @@ class ContributionsFragment
} }
} }
notification.setOnClickListener { view: View? -> notification.setOnClickListener { view: View? ->
startYourself( context?.let {
context, "unread" startYourself(
) it, "unread"
)
}
} }
} }
@ -889,14 +888,16 @@ class ContributionsFragment
* this function updates the number of contributions * this function updates the number of contributions
*/ */
fun upDateUploadCount() { fun upDateUploadCount() {
WorkManager.getInstance(context) context?.let {
.getWorkInfosForUniqueWorkLiveData(UploadWorker::class.java.simpleName).observe( WorkManager.getInstance(it)
viewLifecycleOwner .getWorkInfosForUniqueWorkLiveData(UploadWorker::class.java.simpleName).observe(
) { workInfos: List<WorkInfo?> -> viewLifecycleOwner
if (workInfos.size > 0) { ) { workInfos: List<WorkInfo?> ->
setUploadCount() if (workInfos.size > 0) {
setUploadCount()
}
} }
} }
} }
@ -953,7 +954,7 @@ class ContributionsFragment
Timber.d("Skipping re-upload for non-failed %s", contribution.toString()) Timber.d("Skipping re-upload for non-failed %s", contribution.toString())
} }
} else { } else {
showLongToast(context, R.string.this_function_needs_network_connection) context?.let { showLongToast(it, R.string.this_function_needs_network_connection) }
} }
} }

View file

@ -80,9 +80,11 @@ class ContributionsPresenter @Inject internal constructor(
.save(contribution) .save(contribution)
.subscribeOn(ioThreadScheduler) .subscribeOn(ioThreadScheduler)
.subscribe { .subscribe {
makeOneTimeWorkRequest( view!!.getContext()?.applicationContext?.let {
view!!.getContext().applicationContext, ExistingWorkPolicy.KEEP makeOneTimeWorkRequest(
) it, ExistingWorkPolicy.KEEP
)
}
}) })
} }
} }

View file

@ -63,9 +63,4 @@ abstract class CommonsDaggerSupportFragment : Fragment(), HasSupportFragmentInje
return getInstance(activity.applicationContext) return getInstance(activity.applicationContext)
} }
// Ensure getContext() returns a non-null Context
override fun getContext(): Context {
return super.getContext() ?: throw IllegalStateException("Context is null")
}
} }