changeset 16:3ed74dc0e34a

Fix error messages, make scaling menu more user-friendly, fix return from settings.
author David Barts <n5jrn@me.com>
date Sun, 21 Feb 2021 21:43:54 -0800
parents 20da616dcda0
children 86740f593b6c
files app/src/main/java/com/bartsent/simpleresizer/EditImage.kt app/src/main/res/values/strings.xml
diffstat 2 files changed, 29 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- 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<EditText>(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()
     }
--- 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 @@
     <string name="error_create_mediastore">Failed to create new MediaStore record.</string>
     <string name="error_get_output">Failed to get output stream.</string>
     <string name="error_io">I/O error.</string>
-    <string name="error_no_uri">No URI supplied!</string>
+    <string name="error_no_uri">No URI supplied, and no remembered image!</string>
     <string name="error_save_bitmap">Failed to save bitmap.</string>
     <string name="image_size_text">Width: %d, Height: %d.</string>
     <string name="ok_text">OK</string>