Skip to content

Commit def8847

Browse files
committed
Fix NullPointerException in UploadCategoriesFragment (commons-app#6246)
1 parent e2b8a1c commit def8847

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

app/src/main/java/fr/free/nrw/commons/upload/categories/UploadCategoriesFragment.kt

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ class UploadCategoriesFragment : UploadBaseFragment(), CategoriesContract.View {
9797
}
9898
if (media == null) {
9999
if (callback != null) {
100-
binding!!.tvTitle.text = getString(R.string.step_count,
101-
callback!!.getIndexInViewFlipper(this) + 1,
102-
callback!!.totalNumberOfSteps,
100+
binding?.tvTitle?.text = getString(R.string.step_count,
101+
callback?.getIndexInViewFlipper(this)?.plus(1) ?: 1,
102+
callback?.totalNumberOfSteps ?: 1,
103103
getString(R.string.categories_activity_title))
104104
}
105105
} else {
@@ -110,7 +110,7 @@ class UploadCategoriesFragment : UploadBaseFragment(), CategoriesContract.View {
110110
}
111111

112112
setTvSubTitle()
113-
binding!!.tooltip.setOnClickListener {
113+
binding?.let { it.tooltip.setOnClickListener {
114114
showAlertDialog(
115115
requireActivity(),
116116
getString(R.string.categories_activity_title),
@@ -119,10 +119,11 @@ class UploadCategoriesFragment : UploadBaseFragment(), CategoriesContract.View {
119119
null
120120
)
121121
}
122+
}
122123
if (media == null) {
123-
presenter!!.onAttachView(this)
124+
presenter?.onAttachView(this)
124125
} else {
125-
presenter!!.onAttachViewWithMedia(this, media!!)
126+
presenter?.onAttachViewWithMedia(this, media!!)
126127
}
127128
binding!!.btnNext.setOnClickListener { v: View? -> onNextButtonClicked() }
128129
binding!!.btnPrevious.setOnClickListener { v: View? -> onPreviousButtonClicked() }
@@ -137,7 +138,7 @@ class UploadCategoriesFragment : UploadBaseFragment(), CategoriesContract.View {
137138
}
138139
subscribe = RxTextView.textChanges(binding!!.etSearch)
139140
.doOnEach { v: Notification<CharSequence?>? ->
140-
binding!!.tilContainerSearch.error =
141+
binding?.tilContainerSearch?.error =
141142
null
142143
}
143144
.takeUntil(RxView.detaches(binding!!.etSearch))
@@ -163,25 +164,25 @@ class UploadCategoriesFragment : UploadBaseFragment(), CategoriesContract.View {
163164
}
164165

165166
private fun searchForCategory(query: String) {
166-
presenter!!.searchForCategories(query)
167+
presenter?.searchForCategories(query)
167168
}
168169

169170
private fun initRecyclerView() {
170-
adapter = UploadCategoryAdapter({ categoryItem: CategoryItem? ->
171-
presenter!!.onCategoryItemClicked(categoryItem!!)
171+
if (adapter == null) { adapter = UploadCategoryAdapter({ categoryItem: CategoryItem? ->
172+
presenter?.onCategoryItemClicked(categoryItem!!)
172173
Unit
173174
}, nearbyPlaceCategory)
174-
175-
if (binding != null) {
176-
binding!!.rvCategories.layoutManager = LinearLayoutManager(context)
177-
binding!!.rvCategories.adapter = adapter
175+
}
176+
binding?.rvCategories?.apply {
177+
layoutManager = LinearLayoutManager(context)
178+
adapter = this@UploadCategoriesFragment.adapter
178179
}
179180
}
180181

181182
override fun onDestroyView() {
182183
super.onDestroyView()
183-
presenter!!.onDetachView()
184-
subscribe!!.dispose()
184+
presenter?.onDetachView()
185+
subscribe?.dispose()
185186
}
186187

187188
override fun showProgress(shouldShow: Boolean) {
@@ -197,26 +198,28 @@ class UploadCategoriesFragment : UploadBaseFragment(), CategoriesContract.View {
197198
}
198199

199200
override fun setCategories(categories: List<CategoryItem>?) {
201+
if (adapter == null) {
202+
Timber.e("Adapter is null in setCategories")
203+
return
204+
}
205+
200206
if (categories == null) {
201207
adapter!!.clear()
202208
} else {
203209
adapter!!.items = categories
204210
}
205211
adapter!!.notifyDataSetChanged()
206212

207-
if (binding == null) {
208-
return
209-
}
210-
// Nested waiting for search result data to load into the category
211-
// list and smoothly scroll to the top of the search result list.
212-
binding!!.rvCategories.post {
213-
binding!!.rvCategories.smoothScrollToPosition(0)
214-
binding!!.rvCategories.post {
215-
binding!!.rvCategories.smoothScrollToPosition(
213+
binding?.let {
214+
it.rvCategories.post {
215+
it.rvCategories.smoothScrollToPosition(0)
216+
it.rvCategories.post {
217+
it.rvCategories.smoothScrollToPosition(
216218
0
217219
)
220+
}
218221
}
219-
}
222+
} ?: Timber.e("Binding is null in setCategories")
220223
}
221224

222225
override fun goToNextScreen() {
@@ -308,7 +311,7 @@ class UploadCategoriesFragment : UploadBaseFragment(), CategoriesContract.View {
308311

309312
fun onNextButtonClicked() {
310313
if (media != null) {
311-
presenter!!.updateCategories(media!!, wikiText!!)
314+
presenter?.updateCategories(media!!, wikiText!!)
312315
} else {
313316
presenter!!.verifyCategories()
314317
}
@@ -318,7 +321,7 @@ class UploadCategoriesFragment : UploadBaseFragment(), CategoriesContract.View {
318321
if (media != null) {
319322
presenter!!.clearPreviousSelection()
320323
adapter!!.items = null
321-
val mediaDetailFragment = checkNotNull(parentFragment as MediaDetailFragment?)
324+
val mediaDetailFragment = parentFragment as? MediaDetailFragment?: return
322325
mediaDetailFragment.onResume()
323326
goBackToPreviousScreen()
324327
} else {
@@ -345,7 +348,7 @@ class UploadCategoriesFragment : UploadBaseFragment(), CategoriesContract.View {
345348
super.onResume()
346349

347350
if (media != null) {
348-
binding!!.etSearch.setOnKeyListener { v: View?, keyCode: Int, event: KeyEvent? ->
351+
binding?.etSearch?.setOnKeyListener { v: View?, keyCode: Int, event: KeyEvent? ->
349352
if (keyCode == KeyEvent.KEYCODE_BACK) {
350353
binding!!.etSearch.clearFocus()
351354
presenter!!.clearPreviousSelection()

0 commit comments

Comments
 (0)