Skip to content

Commit

Permalink
Disable saving sketch on every change
Browse files Browse the repository at this point in the history
  • Loading branch information
udenr committed Aug 2, 2024
1 parent 79ed8c8 commit 94dae3c
Showing 1 changed file with 3 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,12 @@ import android.widget.Button
import android.widget.LinearLayout
import androidx.annotation.ColorInt
import androidx.core.content.FileProvider
import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager
import com.simplify.ink.InkView
import eltos.simpledialogfragment.SimpleDialog.OnDialogResultListener
import eltos.simpledialogfragment.color.SimpleColorDialog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import org.secuso.privacyfriendlynotes.R
import org.secuso.privacyfriendlynotes.room.DbContract
Expand All @@ -63,14 +59,12 @@ class SketchActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_SKETCH), OnDia
private lateinit var redoButton: MenuItem
private var mFileName = "finde_die_datei.mp4"
private var mFilePath: String? = null
private var mTempFilePath: String? = null
private var sketchLoaded = false
private val undoStates = mutableListOf<Bitmap>()
private var redoStates = mutableListOf<Bitmap>()
private var state: Bitmap? = null
private var oldSketch: BitmapDrawable? = null
private var initialSize: Pair<Int, Int>? = null
private val saveNoteMutex = Mutex()

private val undoRedoEnabled by lazy { PreferenceManager.getDefaultSharedPreferences(this).getBoolean("settings_sketch_undo_redo", true) }

Expand Down Expand Up @@ -119,9 +113,6 @@ class SketchActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_SKETCH), OnDia
state = emptyBitmap()
}
undoStates.add(state!!)
lifecycleScope.launch(Dispatchers.IO) {
saveBitmap(mTempFilePath!!)
}
redoStates.clear()
if (undoStates.size > 32) {
undoStates.removeFirst()
Expand All @@ -142,7 +133,6 @@ class SketchActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_SKETCH), OnDia
override fun onNoteLoadedFromDB(note: Note) {
mFileName = note.content
mFilePath = filesDir.path + "/sketches" + mFileName
mTempFilePath = cacheDir.path + "/sketches" + mFileName
File(cacheDir.path + "/sketches").mkdirs()
oldSketch = try {
loadSketchBitmap(this, note.content)
Expand All @@ -159,7 +149,6 @@ class SketchActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_SKETCH), OnDia
mFilePath = filesDir.path + "/sketches"
File(mFilePath!!).mkdirs() //ensure that the file exists
File(cacheDir.path + "/sketches").mkdirs()
mTempFilePath = cacheDir.path + "/sketches" + mFileName
mFilePath = filesDir.path + "/sketches" + mFileName
}

Expand All @@ -183,19 +172,13 @@ class SketchActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_SKETCH), OnDia
if (undoStates.isNotEmpty()) {
redoStates.add(state!!)
undoRedoState(undoStates.removeLast())
lifecycleScope.launch(Dispatchers.IO) {
saveBitmap(mTempFilePath!!)
}
}
}

R.id.action_sketch_redo -> {
if (redoStates.isNotEmpty()) {
undoStates.add(state!!)
undoRedoState(redoStates.removeLast())
lifecycleScope.launch(Dispatchers.IO) {
saveBitmap(mTempFilePath!!)
}
}
}

Expand Down Expand Up @@ -261,21 +244,8 @@ class SketchActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_SKETCH), OnDia
}

override fun onNoteSave(name: String, category: Int): ActionResult<Note, Int> {
if (undoRedoEnabled) {
runBlocking {
saveNoteMutex.withLock {
File(mTempFilePath!!).apply {
if (this.exists()) {
this.copyTo(File(mFilePath!!), overwrite = true)
this.delete()
}
}
}
}
} else {
runBlocking {
saveBitmap(mFilePath!!)
}
runBlocking {
saveBitmap(mFilePath!!)
}

if (name.isEmpty() && drawView.bitmap.sameAs(emptyBitmap())) {
Expand All @@ -284,7 +254,7 @@ class SketchActivity : BaseNoteActivity(DbContract.NoteEntry.TYPE_SKETCH), OnDia
return ActionResult(true, Note(name, mFileName, DbContract.NoteEntry.TYPE_SKETCH, category))
}

private suspend fun saveBitmap(path: String) = saveNoteMutex.withLock {
private suspend fun saveBitmap(path: String) {
val bitmap = oldSketch?.overlay(drawView.bitmap) ?: emptyBitmap().overlay(drawView.bitmap)
try {
val fo = withContext(Dispatchers.IO) {
Expand Down

0 comments on commit 94dae3c

Please sign in to comment.