Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix null context issue in CommonsDaggerSupportFragment #6218

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface ContributionsContract {

interface View {
fun showMessage(localizedMessage: String)
fun getContext(): Context
fun getContext(): Context?
}

interface UserActionListener : BasePresenter<View> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -307,9 +304,11 @@ class ContributionsFragment
}
}
notification.setOnClickListener { view: View? ->
startYourself(
context, "unread"
)
context?.let {
startYourself(
it, "unread"
)
}
}
}

Expand Down Expand Up @@ -889,14 +888,16 @@ class ContributionsFragment
* this function updates the number of contributions
*/
fun upDateUploadCount() {
WorkManager.getInstance(context)
.getWorkInfosForUniqueWorkLiveData(UploadWorker::class.java.simpleName).observe(
viewLifecycleOwner
) { workInfos: List<WorkInfo?> ->
if (workInfos.size > 0) {
setUploadCount()
context?.let {
WorkManager.getInstance(it)
.getWorkInfosForUniqueWorkLiveData(UploadWorker::class.java.simpleName).observe(
viewLifecycleOwner
) { workInfos: List<WorkInfo?> ->
if (workInfos.size > 0) {
setUploadCount()
}
}
}
}
}


Expand Down Expand Up @@ -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) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ class ContributionsPresenter @Inject internal constructor(
.save(contribution)
.subscribeOn(ioThreadScheduler)
.subscribe {
makeOneTimeWorkRequest(
view!!.getContext().applicationContext, ExistingWorkPolicy.KEEP
)
view!!.getContext()?.applicationContext?.let {
makeOneTimeWorkRequest(
it, ExistingWorkPolicy.KEEP
)
}
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}