changeset 32:be08576794af

Replace static object with ViewModel.
author David Barts <n5jrn@me.com>
date Fri, 26 Feb 2021 14:26:50 -0800
parents 0023e6013dd9
children bead5d7e8c69
files app/src/main/java/com/bartsent/simpleresizer/EditImage.kt
diffstat 1 files changed, 19 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/app/src/main/java/com/bartsent/simpleresizer/EditImage.kt	Fri Feb 26 13:24:30 2021 -0800
+++ b/app/src/main/java/com/bartsent/simpleresizer/EditImage.kt	Fri Feb 26 14:26:50 2021 -0800
@@ -21,6 +21,8 @@
 import androidx.appcompat.app.AlertDialog
 import androidx.appcompat.app.AppCompatActivity
 import androidx.appcompat.widget.PopupMenu
+import androidx.lifecycle.ViewModel
+import androidx.lifecycle.ViewModelProvider
 import androidx.preference.PreferenceManager
 import com.bartsent.simpleresizer.databinding.ActivityEditImageBinding
 import com.bartsent.simpleresizer.lib.ThreadPools
@@ -29,10 +31,11 @@
 import java.io.IOException
 
 class EditImage : AppCompatActivity() {
-    private object State {
+    class State: ViewModel() {
         var uri: Uri? = null
         var bitmap: Bitmap? = null
     }
+    private lateinit var viewModel: State
 
     private val STDDIMS = arrayOf<Int>(1600, 1280, 1024, 800, 640, 512, 400, 320).apply {
         sort()
@@ -45,6 +48,7 @@
         binding = ActivityEditImageBinding.inflate(layoutInflater)
         setContentView(binding.root)
         PreferenceManager.setDefaultValues(applicationContext, R.xml.root_preferences, false)
+        viewModel = ViewModelProvider(this).get(State::class.java)
     }
 
     override fun onCreateOptionsMenu(menu: Menu?): Boolean {
@@ -97,16 +101,16 @@
         // Read the URI, die if we can't.
         val imageUri = intent?.data ?: intent?.extras?.get(Intent.EXTRA_STREAM) as? Uri
         if (imageUri == null) {
-            if (State.bitmap == null)
+            if (viewModel.bitmap == null)
                 showFatalError(getString(R.string.error_no_uri))
             else
-                setImage(State.bitmap!!)
+                setImage(viewModel.bitmap!!)
             return
         }
 
         // User has opened a new image.
-        if (imageUri != State.uri) {
-            State.uri = imageUri
+        if (imageUri != viewModel.uri) {
+            viewModel.uri = imageUri
             binding.progressBar.visibility = ProgressBar.VISIBLE
             ThreadPools.WORKERS.execute {
                 val newBitmap = contentResolver.openInputStream(imageUri).use {
@@ -124,7 +128,7 @@
         }
 
         // Rotation (of the phone).
-        val oldBitmap = State.bitmap
+        val oldBitmap = viewModel.bitmap
         if (oldBitmap != null)
             setImage(oldBitmap)
     }
@@ -132,20 +136,20 @@
     private fun setImage(image: Bitmap): Unit {
         binding.imageSize.text = getString(R.string.image_size_text, image.width, image.height)
         binding.image.setImageBitmap(image)
-        State.bitmap = image
+        viewModel.bitmap = image
         binding.root.invalidate()
     }
 
     private fun unsetImage(): Unit {
-        State.uri = null
-        State.bitmap = null
+        viewModel.uri = null
+        viewModel.bitmap = null
     }
 
     private val CUSTOM = 999998
     private val CANCEL = 999999
 
     fun scaleClicked(view: View): Unit {
-        val (maxSize, horizontal) = State.bitmap!!.run {
+        val (maxSize, horizontal) = viewModel.bitmap!!.run {
             if (width > height) Pair(width, true) else Pair(height, false)
         }
         PopupMenu(this, view).apply {
@@ -180,7 +184,7 @@
     }
 
     private fun doScale(newMax: Int): Unit {
-        val oldBitmap = State.bitmap!!
+        val oldBitmap = viewModel.bitmap!!
         val factor = newMax.toDouble() / maxOf(oldBitmap.width, oldBitmap.height).toDouble()
         if (factor >= 1.0) {
             throw IllegalArgumentException("can only scale down")
@@ -204,7 +208,7 @@
     }
 
     private fun showCustomScaleDialog(): Unit {
-        val image = State.bitmap!!
+        val image = viewModel.bitmap!!
         val curMaxDim = maxOf(image.width, image.height)
         val dialogView = layoutInflater.inflate(R.layout.dialog_custom_scale, null)
         AlertDialog.Builder(this).also {
@@ -251,7 +255,7 @@
     }
 
     private fun doRotate(deg: Int): Unit {
-        val oldBitmap = State.bitmap!!
+        val oldBitmap = viewModel.bitmap!!
         if (deg % 90 != 0) {
             throw IllegalArgumentException("$deg not a multiple of 90")
         }
@@ -280,11 +284,11 @@
     }
 
     fun doneClicked(view: View): Unit {
-        val image = State.bitmap!!
+        val image = viewModel.bitmap!!
         binding.progressBar.visibility = ProgressBar.VISIBLE
         ThreadPools.WORKERS.execute {
             val contentValues = ContentValues().apply {
-                var fileName = getFileName(State.uri!!)
+                var fileName = getFileName(viewModel.uri!!)
                 if (fileName == null) {
                     val d = java.util.Date()
                     fileName = "IMG_%tY%tm%td_%tH%tM%tS".format(d, d, d, d, d, d)