Skip to content

Commit c49c85e

Browse files
Fix : UninitializedPropertyAccessException (commons-app#6248)
* Fix crash when uploading a duplicate file * Fix: app crash * added Kdoc * remove line b/w kdoc and function --------- Co-authored-by: Nicolas Raoul <nicolas.raoul@gmail.com>
1 parent 91ca2e6 commit c49c85e

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
173173
override fun onCreate(savedInstanceState: Bundle?) {
174174
super.onCreate(savedInstanceState)
175175

176+
// Ensure basicKvStoreFactory is always initialized before use
177+
presenter?.setupBasicKvStoreFactory { BasicKvStore(this@UploadActivity, it) }
178+
176179
_binding = ActivityUploadBinding.inflate(layoutInflater)
177180
setContentView(binding.root)
178181

@@ -903,7 +906,6 @@ class UploadActivity : BaseActivity(), UploadContract.View, UploadBaseFragment.C
903906
// Save the user's choice to not show the dialog again
904907
defaultKvStore.putBoolean("hasAlreadyLaunchedCategoriesDialog", true)
905908
}
906-
presenter!!.setupBasicKvStoreFactory { BasicKvStore(this@UploadActivity, it) }
907909
presenter!!.checkImageQuality(0)
908910
UploadMediaPresenter.isCategoriesDialogShowing = false
909911
}

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ class UploadPresenter @Inject internal constructor(
3434

3535
private val compositeDisposable = CompositeDisposable()
3636

37-
lateinit var basicKvStoreFactory: (String) -> BasicKvStore
38-
37+
private var basicKvStoreFactory: ((String) -> BasicKvStore)? = null
3938
/**
4039
* Called by the submit button in [UploadActivity]
4140
*/
@@ -132,14 +131,38 @@ class UploadPresenter @Inject internal constructor(
132131
basicKvStoreFactory = factory
133132
}
134133

134+
/**
135+
* Returns the current BasicKvStore factory or throws if not initialized.
136+
*
137+
* @throws IllegalStateException if basicKvStoreFactory has not been initialized.
138+
*/
139+
private fun getBasicKvStoreFactory(): (String) -> BasicKvStore {
140+
return basicKvStoreFactory ?: throw IllegalStateException("basicKvStoreFactory has not been initialized")
141+
}
142+
143+
/**
144+
* Ensures that the BasicKvStore factory has been initialized before use.
145+
*
146+
* @throws IllegalStateException if the factory is null.
147+
*/
148+
private fun requireFactoryInitialized() {
149+
val field = this::class.java.getDeclaredField("basicKvStoreFactory")
150+
field.isAccessible = true
151+
val value = field.get(this)
152+
if (value == null) {
153+
throw IllegalStateException("basicKvStoreFactory must be initialized before use. Please call setupBasicKvStoreFactory() before using presenter methods that require it.")
154+
}
155+
}
156+
135157
/**
136158
* Calls checkImageQuality of UploadMediaPresenter to check image quality of next image
137159
*
138160
* @param uploadItemIndex Index of next image, whose quality is to be checked
139161
*/
140162
override fun checkImageQuality(uploadItemIndex: Int) {
163+
requireFactoryInitialized()
141164
repository.getUploadItem(uploadItemIndex)?.let {
142-
presenter.setupBasicKvStoreFactory(basicKvStoreFactory)
165+
presenter.setupBasicKvStoreFactory(getBasicKvStoreFactory())
143166
presenter.checkImageQuality(it, uploadItemIndex)
144167
}
145168
}

0 commit comments

Comments
 (0)