# HG changeset patch # User David Barts # Date 1613972634 28800 # Node ID 3ed74dc0e34a084ff0a8167b095c9dd4406c4baf # Parent 20da616dcda08b62a47cc540e571099d9ca0c24f Fix error messages, make scaling menu more user-friendly, fix return from settings. diff -r 20da616dcda0 -r 3ed74dc0e34a app/src/main/java/com/bartsent/simpleresizer/EditImage.kt --- a/app/src/main/java/com/bartsent/simpleresizer/EditImage.kt Thu Feb 18 22:12:19 2021 -0800 +++ b/app/src/main/java/com/bartsent/simpleresizer/EditImage.kt Sun Feb 21 21:43:54 2021 -0800 @@ -18,6 +18,7 @@ import android.view.View import android.widget.EditText import android.widget.ProgressBar +import android.widget.Toast import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.PopupMenu @@ -78,7 +79,7 @@ return result } - private fun showError(message: String): Unit { + private fun showFatalError(message: String): Unit { AlertDialog.Builder(this).also { it.setMessage(message) it.setNeutralButton(R.string.ok_text) { dialog, _ -> @@ -97,7 +98,10 @@ // Read the URI, die if we can't. val imageUri = intent?.data if (imageUri == null) { - showError(getString(R.string.error_no_uri)) + if (State.bitmap == null) + showFatalError(getString(R.string.error_no_uri)) + else + setImage(State.bitmap!!) return } @@ -109,7 +113,7 @@ } } if (State.bitmap == null) { - showError(getString(R.string.error_bad_image)) + showFatalError(getString(R.string.error_bad_image)) return } setImage(State.bitmap!!) @@ -121,32 +125,35 @@ State.bitmap = image } + private val CUSTOM = 999998 + private val CANCEL = 999999 + fun scaleClicked(view: View): Unit { - val maxSize = State.bitmap!!.run { maxOf(width, height) } + val (maxSize, horizontal) = State.bitmap!!.run { + if (width > height) Pair(width, true) else Pair(height, false) + } PopupMenu(this, view).apply { menu.run { - STDDIMS.filter { it < maxSize }.forEach { add(it.toString()) } - add(getString(R.string.custom_text)) - add(getString(R.string.cancel_text)) + STDDIMS.filter { it < maxSize }.forEach { major -> + val minor = major * 3 / 4 + add(Menu.NONE, major, Menu.NONE, + if (horizontal) "$major ✕ $minor" else "$minor ✕ $major") + } + add(Menu.NONE, CUSTOM, Menu.NONE, R.string.custom_text) + add(Menu.NONE, CANCEL, Menu.NONE, R.string.cancel_text) } setOnMenuItemClickListener(::scaleMenuItemClicked) show() } } - fun scaleMenuItemClicked(item: MenuItem) : Boolean { - val itString = item.title.toString() - var itVal = itString.toIntOrNull() - if (itVal == null) { - return when (itString) { - getString(R.string.cancel_text) -> true - getString(R.string.custom_text) -> { showCustomScaleDialog(); true } - else -> false - } + fun scaleMenuItemClicked(item: MenuItem) : Boolean = + when (item.itemId) { + CUSTOM -> { showCustomScaleDialog(); true } + CANCEL -> true + in STDDIMS -> { doScale(item.itemId); true } + else -> false } - doScale(itVal) - return true - } private fun copyColorSpace(old: Bitmap, new: Bitmap): Unit { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { @@ -180,9 +187,6 @@ } } - // is there any way to remember the last scale value? - // if so: should we? - fun showCustomScaleDialog(): Unit { val image = State.bitmap!! val curMaxDim = maxOf(image.width, image.height) @@ -192,13 +196,7 @@ val maxDim = dialogView.findViewById(R.id.custom_scale)?.text.toString().toIntOrNull() dialog.dismiss() if (maxDim == null || maxDim < 8 || maxDim >= curMaxDim) { - AlertDialog.Builder(this).also { - it.setMessage(R.string.bad_scale) - it.setNeutralButton(R.string.ok_text) { dialog, _ -> - dialog.dismiss() - } - it.create() - }.show() + Toast.makeText(applicationContext, R.string.bad_scale, Toast.LENGTH_LONG).show() } else { doScale(maxDim) } @@ -308,7 +306,7 @@ } } } catch (ioe: IOException) { - showError(ioe.message ?: getString(R.string.error_io)) + Toast.makeText(applicationContext, ioe.message ?: getString(R.string.error_io), Toast.LENGTH_LONG).show() } finish() } diff -r 20da616dcda0 -r 3ed74dc0e34a app/src/main/res/values/strings.xml --- a/app/src/main/res/values/strings.xml Thu Feb 18 22:12:19 2021 -0800 +++ b/app/src/main/res/values/strings.xml Sun Feb 21 21:43:54 2021 -0800 @@ -9,7 +9,7 @@ Failed to create new MediaStore record. Failed to get output stream. I/O error. - No URI supplied! + No URI supplied, and no remembered image! Failed to save bitmap. Width: %d, Height: %d. OK