Skip to content

Commit 32d485c

Browse files
authored
fix: null context issue by removing context getter override (#6218)
Refactored related code to resolve build issues
1 parent d11439f commit 32d485c

File tree

4 files changed

+23
-25
lines changed

4 files changed

+23
-25
lines changed

app/src/main/java/fr/free/nrw/commons/contributions/ContributionsContract.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ interface ContributionsContract {
1010

1111
interface View {
1212
fun showMessage(localizedMessage: String)
13-
fun getContext(): Context
13+
fun getContext(): Context?
1414
}
1515

1616
interface UserActionListener : BasePresenter<View> {

app/src/main/java/fr/free/nrw/commons/contributions/ContributionsFragment.kt

+17-16
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,9 @@ import java.util.Date
7474
import javax.inject.Inject
7575
import javax.inject.Named
7676

77-
class ContributionsFragment
78-
79-
: CommonsDaggerSupportFragment(), FragmentManager.OnBackStackChangedListener,
77+
class ContributionsFragment : CommonsDaggerSupportFragment(), FragmentManager.OnBackStackChangedListener,
8078
LocationUpdateListener, MediaDetailProvider, SensorEventListener, ICampaignsView,
81-
ContributionsContract.View,
82-
ContributionsListFragment.Callback {
79+
ContributionsContract.View, ContributionsListFragment.Callback {
8380
@JvmField
8481
@Inject
8582
@Named("default_preferences")
@@ -307,9 +304,11 @@ class ContributionsFragment
307304
}
308305
}
309306
notification.setOnClickListener { view: View? ->
310-
startYourself(
311-
context, "unread"
312-
)
307+
context?.let {
308+
startYourself(
309+
it, "unread"
310+
)
311+
}
313312
}
314313
}
315314

@@ -889,14 +888,16 @@ class ContributionsFragment
889888
* this function updates the number of contributions
890889
*/
891890
fun upDateUploadCount() {
892-
WorkManager.getInstance(context)
893-
.getWorkInfosForUniqueWorkLiveData(UploadWorker::class.java.simpleName).observe(
894-
viewLifecycleOwner
895-
) { workInfos: List<WorkInfo?> ->
896-
if (workInfos.size > 0) {
897-
setUploadCount()
891+
context?.let {
892+
WorkManager.getInstance(it)
893+
.getWorkInfosForUniqueWorkLiveData(UploadWorker::class.java.simpleName).observe(
894+
viewLifecycleOwner
895+
) { workInfos: List<WorkInfo?> ->
896+
if (workInfos.size > 0) {
897+
setUploadCount()
898+
}
898899
}
899-
}
900+
}
900901
}
901902

902903

@@ -953,7 +954,7 @@ class ContributionsFragment
953954
Timber.d("Skipping re-upload for non-failed %s", contribution.toString())
954955
}
955956
} else {
956-
showLongToast(context, R.string.this_function_needs_network_connection)
957+
context?.let { showLongToast(it, R.string.this_function_needs_network_connection) }
957958
}
958959
}
959960

app/src/main/java/fr/free/nrw/commons/contributions/ContributionsPresenter.kt

+5-3
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,11 @@ class ContributionsPresenter @Inject internal constructor(
8080
.save(contribution)
8181
.subscribeOn(ioThreadScheduler)
8282
.subscribe {
83-
makeOneTimeWorkRequest(
84-
view!!.getContext().applicationContext, ExistingWorkPolicy.KEEP
85-
)
83+
view!!.getContext()?.applicationContext?.let {
84+
makeOneTimeWorkRequest(
85+
it, ExistingWorkPolicy.KEEP
86+
)
87+
}
8688
})
8789
}
8890
}

app/src/main/java/fr/free/nrw/commons/di/CommonsDaggerSupportFragment.kt

-5
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,4 @@ abstract class CommonsDaggerSupportFragment : Fragment(), HasSupportFragmentInje
6363

6464
return getInstance(activity.applicationContext)
6565
}
66-
67-
// Ensure getContext() returns a non-null Context
68-
override fun getContext(): Context {
69-
return super.getContext() ?: throw IllegalStateException("Context is null")
70-
}
7166
}

0 commit comments

Comments
 (0)