From 1ac25358b84482661c449eb0cc532023516339ce Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 2 Sep 2019 20:58:07 +0200 Subject: [PATCH] add an initial check related to exiting the app with unsaved changes --- .../simplemobiletools/draw/pro/activities/MainActivity.kt | 7 +++++++ .../com/simplemobiletools/draw/pro/views/MyCanvas.kt | 2 ++ 2 files changed, 9 insertions(+) diff --git a/app/src/main/kotlin/com/simplemobiletools/draw/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/draw/pro/activities/MainActivity.kt index b410a2c..3f6a667 100644 --- a/app/src/main/kotlin/com/simplemobiletools/draw/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/draw/pro/activities/MainActivity.kt @@ -51,6 +51,7 @@ class MainActivity : SimpleActivity(), CanvasListener { private var uriToLoad: Uri? = null private var color = 0 private var brushSize = 0f + private var savedPathsHash = 0L private var isEraserOn = false private var isImageCaptureIntent = false private var isEditIntent = false @@ -140,6 +141,11 @@ class MainActivity : SimpleActivity(), CanvasListener { return true } + override fun onBackPressed() { + val hasUnsavedChanges = savedPathsHash != my_canvas.getDrawingHashCode() + super.onBackPressed() + } + private fun launchSettings() { startActivity(Intent(applicationContext, SettingsActivity::class.java)) } @@ -344,6 +350,7 @@ class MainActivity : SimpleActivity(), CanvasListener { private fun saveImage() { SaveImageDialog(this, defaultExtension, defaultPath, defaultFilename) { + savedPathsHash = my_canvas.getDrawingHashCode() saveFile(it) defaultPath = it.getParentPath() defaultFilename = it.getFilenameFromPath() diff --git a/app/src/main/kotlin/com/simplemobiletools/draw/pro/views/MyCanvas.kt b/app/src/main/kotlin/com/simplemobiletools/draw/pro/views/MyCanvas.kt index 7be43a7..9ba9e25 100644 --- a/app/src/main/kotlin/com/simplemobiletools/draw/pro/views/MyCanvas.kt +++ b/app/src/main/kotlin/com/simplemobiletools/draw/pro/views/MyCanvas.kt @@ -248,6 +248,8 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) { mListener?.toggleUndoVisibility(mPaths.isNotEmpty() || mLastPaths.isNotEmpty()) } + fun getDrawingHashCode() = mPaths.hashCode().toLong() + (mBackgroundBitmap?.hashCode()?.toLong() ?: 0L) + override fun onTouchEvent(event: MotionEvent): Boolean { if (mAllowZooming) { mScaleDetector!!.onTouchEvent(event)