Remove function 'handlePickedImage'

Put the code in the `onActivityResult` function so that things get a 
little easier.
This commit is contained in:
Finn M Glas 2020-10-20 16:23:43 +02:00
parent 179448007f
commit e8fa4f058d
No known key found for this signature in database
GPG key ID: 902A30146014DFBF

View file

@ -45,34 +45,13 @@ class SettingsFragmentLauncher : Fragment(), UIObject {
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
REQUEST_PERMISSION_STORAGE -> letUserPickImage()
REQUEST_PICK_IMAGE -> handlePickedImage(resultCode, data)
else -> super.onActivityResult(requestCode, resultCode, data)
}
}
REQUEST_PICK_IMAGE -> {
private fun letUserPickImage(crop: Boolean = false) {
val intent = Intent()
intent.type = "image/*"
intent.action = Intent.ACTION_PICK // other option: Intent.ACTION_GET_CONTENT
if (crop) { // crop to for the target devices screen
intent.putExtra("crop", "true")
val displayMetrics = DisplayMetrics()
activity!!.windowManager.defaultDisplay.getMetrics(displayMetrics)
intent.putExtra("aspectX", displayMetrics.widthPixels)
intent.putExtra("aspectY", displayMetrics.heightPixels)
}
intendedSettingsPause = true
startActivityForResult(intent, REQUEST_PICK_IMAGE)
}
private fun handlePickedImage(resultCode: Int, data: Intent?) {
if (resultCode == AppCompatActivity.RESULT_OK) {
if (data == null) return
if (resultCode == AppCompatActivity.RESULT_OK && data != null) {
val imageUri = data.data
background = MediaStore.Images.Media.getBitmap(context!!.contentResolver, imageUri)
@ -109,6 +88,25 @@ class SettingsFragmentLauncher : Fragment(), UIObject {
}
}
}
}
}
private fun letUserPickImage(crop: Boolean = false) {
val intent = Intent()
intent.type = "image/*"
intent.action = Intent.ACTION_PICK // other option: Intent.ACTION_GET_CONTENT
if (crop) { // crop to for the target devices screen
intent.putExtra("crop", "true")
val displayMetrics = DisplayMetrics()
activity!!.windowManager.defaultDisplay.getMetrics(displayMetrics)
intent.putExtra("aspectX", displayMetrics.widthPixels)
intent.putExtra("aspectY", displayMetrics.heightPixels)
}
intendedSettingsPause = true
startActivityForResult(intent, REQUEST_PICK_IMAGE)
}
override fun applyTheme() {