Skip to content

Commit

Permalink
Merge branch 'develop' into PAINTROID-396
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenkomotive committed Sep 20, 2023
2 parents afb561a + f57707d commit d572eb6
Show file tree
Hide file tree
Showing 18 changed files with 408 additions and 396 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ interface CommandManager {

fun getUndoCommandCount(): Int

fun getColorCommandCount(): Int

fun isLastColorCommandOnTop(): Boolean

interface CommandListener {
fun commandPostExecute()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,12 @@ open class AsyncCommandManager(
override fun getUndoCommandCount(): Int {
synchronized(layerModel) { return commandManager.getUndoCommandCount() }
}

override fun getColorCommandCount(): Int {
synchronized(layerModel) { return commandManager.getColorCommandCount() }
}

override fun isLastColorCommandOnTop(): Boolean {
synchronized(layerModel) { return commandManager.isLastColorCommandOnTop() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,25 @@ class ColorChangedCommand(toolReference: ToolReference, context: Context, color:
}
}

fun runInUndoMode() {
if (toolReference.tool !is LineTool) {
(context as MainActivity).runOnUiThread {
toolReference.tool?.changePaintColor(color, false)
}
} else {
if (toolReference.tool is LineTool && !firstTime) {
(context as MainActivity).runOnUiThread {
(toolReference.tool as LineTool).undoColorChangedCommand(color, false)
}
} else {
(context as MainActivity).runOnUiThread {
toolReference.tool?.changePaintColor(color, false)
}
firstTime = false
}
}
}

override fun freeResources() {
// No resources to free
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,22 +199,58 @@ class DefaultCommandManager(

initialStateCommand?.run(canvas, layerModel)

val colorCommandCount = getColorCommandCount()

var currentColorCommandCount = 0
val iterator = undoCommandList.descendingIterator()

while (iterator.hasNext()) {
val nextCommand = iterator.next()
if (nextCommand is ColorChangedCommand && ignoreColorCommand) {
continue
}
val currentLayer = layerModel.currentLayer
canvas.setBitmap(currentLayer?.bitmap)
nextCommand.run(canvas, layerModel)
if (nextCommand is ColorChangedCommand && ++currentColorCommandCount < colorCommandCount) {
nextCommand.runInUndoMode()
} else {
nextCommand.run(canvas, layerModel)
}
}

if (!currentCommandName.matches(mergeLayerCommandRegex)) {
fetchBackCheckBoxes(layerCount, checkBoxes)
}
}

override fun getColorCommandCount(): Int {
val commandIterator = undoCommandList.iterator()
var counter = 0
while (commandIterator.hasNext()) {
val nextCommand = commandIterator.next()
if (nextCommand is ColorChangedCommand) {
counter++
}
}
return counter
}

override fun isLastColorCommandOnTop(): Boolean {
var retVal = false
if (undoCommandList.first is ColorChangedCommand) {
val commandIterator = undoCommandList.iterator()
var counter = 0
while (commandIterator.hasNext()) {
val nextCommand = commandIterator.next()
if (nextCommand is ColorChangedCommand) {
counter++
}
}
retVal = counter == 1
}
return retVal
}

override fun executeAllCommands() {
val layerCount = layerModel.layerCount
val checkBoxes: MutableList<Boolean> = ArrayList(Collections.nCopies(layerCount, true))
Expand Down Expand Up @@ -324,11 +360,10 @@ class DefaultCommandManager(
override fun undoInConnectedLinesMode() {
val colorCommandList = removeColorCommands()
if (undoCommandList.isNotEmpty()) {
val commandForUndo: Command
if (colorCommandList.isNotEmpty()) {
commandForUndo = colorCommandList.pop()
val commandForUndo: Command = if (colorCommandList.isNotEmpty()) {
colorCommandList.pop()
} else {
commandForUndo = undoCommandList.pop()
undoCommandList.pop()
}
redoCommandList.addFirst(commandForUndo)
handleUndo(commandForUndo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ open class CommandSerializer(private val activityContext: Context, private val c
returnUri = Uri.fromFile(imageFile)
}

val downloadManager = OpenRasterFileFormatConversion.mainActivity.baseContext.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val downloadManager = OpenRasterFileFormatConversion.mainActivity?.baseContext?.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val id = downloadManager.addCompletedDownload(fileName, fileName, true, "application/zip", imageFile.absolutePath, imageFile.length(), true)
val sharedPreferences = OpenRasterFileFormatConversion.mainActivity.getSharedPreferences(SPECIFIC_FILETYPE_SHARED_PREFERENCES_NAME, 0)
sharedPreferences.edit().putLong(imageFile.absolutePath, id).apply()
val sharedPreferences = OpenRasterFileFormatConversion.mainActivity?.getSharedPreferences(SPECIFIC_FILETYPE_SHARED_PREFERENCES_NAME, 0)
sharedPreferences?.edit()?.putLong(imageFile.absolutePath, id)?.apply()
}

return returnUri
Expand All @@ -209,10 +209,10 @@ open class CommandSerializer(private val activityContext: Context, private val c
} else {
val file = File(uri.path.toString())
val isDeleted = file.delete()
val sharedPreferences = OpenRasterFileFormatConversion.mainActivity.getSharedPreferences(SPECIFIC_FILETYPE_SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE)
val id = sharedPreferences.getLong(uri.path, -1)
if (id > -1) {
val downloadManager = OpenRasterFileFormatConversion.mainActivity.baseContext.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val sharedPreferences = OpenRasterFileFormatConversion.mainActivity?.getSharedPreferences(SPECIFIC_FILETYPE_SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE)
val id = sharedPreferences?.getLong(uri.path, -1)
if (id != null && id > -1) {
val downloadManager = OpenRasterFileFormatConversion.mainActivity?.baseContext?.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
downloadManager.remove(id)
}
if (!isDeleted) {
Expand Down
Loading

0 comments on commit d572eb6

Please sign in to comment.