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 {
fun showMessage(localizedMessage: String)
fun getContext(): Context
fun getContext(): Context?
}
interface UserActionListener : BasePresenter<View> {

View file

@ -74,12 +74,9 @@ import java.util.Date
import javax.inject.Inject
import javax.inject.Named
class ContributionsFragment
: CommonsDaggerSupportFragment(), FragmentManager.OnBackStackChangedListener,
class ContributionsFragment : CommonsDaggerSupportFragment(), FragmentManager.OnBackStackChangedListener,
LocationUpdateListener, MediaDetailProvider, SensorEventListener, ICampaignsView,
ContributionsContract.View,
ContributionsListFragment.Callback {
ContributionsContract.View, ContributionsListFragment.Callback {
@JvmField
@Inject
@Named("default_preferences")
@ -307,11 +304,13 @@ class ContributionsFragment
}
}
notification.setOnClickListener { view: View? ->
context?.let {
startYourself(
context, "unread"
it, "unread"
)
}
}
}
@SuppressLint("CheckResult")
fun setNotificationCount() {
@ -889,7 +888,8 @@ class ContributionsFragment
* this function updates the number of contributions
*/
fun upDateUploadCount() {
WorkManager.getInstance(context)
context?.let {
WorkManager.getInstance(it)
.getWorkInfosForUniqueWorkLiveData(UploadWorker::class.java.simpleName).observe(
viewLifecycleOwner
) { workInfos: List<WorkInfo?> ->
@ -898,6 +898,7 @@ class ContributionsFragment
}
}
}
}
/**
@ -953,7 +954,7 @@ class ContributionsFragment
Timber.d("Skipping re-upload for non-failed %s", contribution.toString())
}
} 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)
.subscribeOn(ioThreadScheduler)
.subscribe {
view!!.getContext()?.applicationContext?.let {
makeOneTimeWorkRequest(
view!!.getContext().applicationContext, ExistingWorkPolicy.KEEP
it, ExistingWorkPolicy.KEEP
)
}
})
}
}

View file

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