Skip to content

Commit

Permalink
PAINTROID-388 import images via overflow menu
Browse files Browse the repository at this point in the history
  • Loading branch information
Tscheppe committed Jun 8, 2022
1 parent 5a2c023 commit 73f2f10
Show file tree
Hide file tree
Showing 16 changed files with 87 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -337,17 +337,18 @@ class MainActivity : AppCompatActivity(), MainView, CommandListener {
R.id.pocketpaint_options_export -> presenterMain.saveCopyClicked(true)
R.id.pocketpaint_options_save_image -> presenterMain.saveImageClicked()
R.id.pocketpaint_options_save_duplicate -> presenterMain.saveCopyClicked(false)
R.id.pocketpaint_options_open_image -> presenterMain.loadImageClicked()
R.id.pocketpaint_replace_image -> presenterMain.replaceImageClicked()
R.id.pocketpaint_add_to_current_layer -> presenterMain.addImageToCurrentLayerClicked()
R.id.pocketpaint_options_new_image -> presenterMain.newImageClicked()
R.id.pocketpaint_options_discard_image -> presenterMain.discardImageClicked()
R.id.pocketpaint_options_fullscreen_mode -> presenterMain.enterFullscreenClicked()
R.id.pocketpaint_options_rate_us -> presenterMain.rateUsClicked()
R.id.pocketpaint_options_help -> presenterMain.showHelpClicked()
R.id.pocketpaint_options_about -> presenterMain.showAboutClicked()
android.R.id.home -> presenterMain.backToPocketCodeClicked()
R.id.pocketpaint_share_image_button -> presenterMain.shareImageClicked()
R.id.pocketpaint_options_feedback -> presenterMain.sendFeedback()
R.id.pocketpaint_advanced_settings -> presenterMain.showAdvancedSettingsClicked()
android.R.id.home -> presenterMain.backToPocketCodeClicked()
else -> return false
}
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ interface CommandFactory {

fun createResetCommand(): Command

fun createAddLayerCommand(): Command
fun createAddEmptyLayerCommand(): Command

fun createSelectLayerCommand(position: Int): Command

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import org.catrobat.paintroid.common.CommonFactory
import org.catrobat.paintroid.contract.LayerContracts
import org.catrobat.paintroid.model.Layer

class AddLayerCommand(private val commonFactory: CommonFactory) : Command {
class AddEmptyLayerCommand(private val commonFactory: CommonFactory) : Command {

override fun run(canvas: Canvas, layerModel: LayerContracts.Model) {
val layer = Layer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class DefaultCommandFactory : CommandFactory {
private val commonFactory = CommonFactory()
override fun createInitCommand(width: Int, height: Int): Command = CompositeCommand().apply {
addCommand(SetDimensionCommand(width, height))
addCommand(AddLayerCommand(commonFactory))
addCommand(AddEmptyLayerCommand(commonFactory))
}

override fun createInitCommand(bitmap: Bitmap): Command = CompositeCommand().apply {
Expand All @@ -57,10 +57,10 @@ class DefaultCommandFactory : CommandFactory {

override fun createResetCommand(): Command = CompositeCommand().apply {
addCommand(ResetCommand())
addCommand(AddLayerCommand(commonFactory))
addCommand(AddEmptyLayerCommand(commonFactory))
}

override fun createAddLayerCommand(): Command = AddLayerCommand(commonFactory)
override fun createAddEmptyLayerCommand(): Command = AddEmptyLayerCommand(commonFactory)

override fun createSelectLayerCommand(position: Int): Command = SelectLayerCommand(position)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ class DefaultCommandManager(
var success = true
var layerCount = layerModel.layerCount
val currentCommandName = command.javaClass.simpleName
val addLayerCommandRegex = AddLayerCommand::class.java.simpleName.toRegex()
val addEmptyLayerCommandRegex = AddEmptyLayerCommand::class.java.simpleName.toRegex()
val mergeLayerCommandRegex = MergeLayersCommand::class.java.simpleName.toRegex()

var backupLayer: LayerContracts.Layer? = null
if (currentCommandName.matches(addLayerCommandRegex)) {
if (currentCommandName.matches(addEmptyLayerCommandRegex)) {
layerCount--
backupLayer = layerModel.getLayerAt(0)
success = layerModel.removeLayerAt(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ package org.catrobat.paintroid.command.serialization
import com.esotericsoftware.kryo.Kryo
import com.esotericsoftware.kryo.io.Input
import com.esotericsoftware.kryo.io.Output
import org.catrobat.paintroid.command.implementation.AddLayerCommand
import org.catrobat.paintroid.command.implementation.AddEmptyLayerCommand
import org.catrobat.paintroid.common.CommonFactory

class AddLayerCommandSerializer(version: Int) : VersionSerializer<AddLayerCommand>(version) {
override fun write(kryo: Kryo, output: Output, command: AddLayerCommand) {
class AddLayerCommandSerializer(version: Int) : VersionSerializer<AddEmptyLayerCommand>(version) {
override fun write(kryo: Kryo, output: Output, command: AddEmptyLayerCommand) {
// Has no member variables to save
}

override fun read(kryo: Kryo, input: Input, type: Class<out AddLayerCommand>): AddLayerCommand =
override fun read(kryo: Kryo, input: Input, type: Class<out AddEmptyLayerCommand>): AddEmptyLayerCommand =
super.handleVersions(this, kryo, input, type)

override fun readCurrentVersion(kryo: Kryo, input: Input, type: Class<out AddLayerCommand>): AddLayerCommand =
AddLayerCommand(CommonFactory())
override fun readCurrentVersion(kryo: Kryo, input: Input, type: Class<out AddEmptyLayerCommand>): AddEmptyLayerCommand =
AddEmptyLayerCommand(CommonFactory())
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import com.esotericsoftware.kryo.io.Input
import com.esotericsoftware.kryo.io.Output
import org.catrobat.paintroid.command.Command
import org.catrobat.paintroid.command.CommandManager
import org.catrobat.paintroid.command.implementation.AddLayerCommand
import org.catrobat.paintroid.command.implementation.AddEmptyLayerCommand
import org.catrobat.paintroid.command.implementation.CompositeCommand
import org.catrobat.paintroid.command.implementation.CropCommand
import org.catrobat.paintroid.command.implementation.CutCommand
Expand Down Expand Up @@ -103,7 +103,7 @@ class CommandSerializationUtilities(private val activityContext: Context, privat
put(SetDimensionCommand::class.java, SetDimensionCommandSerializer(version))
put(SprayCommand::class.java, SprayCommandSerializer(version))
put(Paint::class.java, PaintSerializer(version, activityContext))
put(AddLayerCommand::class.java, AddLayerCommandSerializer(version))
put(AddEmptyLayerCommand::class.java, AddLayerCommandSerializer(version))
put(SelectLayerCommand::class.java, SelectLayerCommandSerializer(version))
put(LoadCommand::class.java, LoadCommandSerializer(version))
put(TextToolCommand::class.java, TextToolCommandSerializer(version, activityContext))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,54 @@ const val SAVE_IMAGE_DEFAULT = 1
const val SAVE_IMAGE_NEW_EMPTY = 2
const val SAVE_IMAGE_LOAD_NEW = 3
const val SAVE_IMAGE_FINISH = 4

const val LOAD_IMAGE_DEFAULT = 1
const val LOAD_IMAGE_IMPORT_PNG = 2
const val LOAD_IMAGE_CATROID = 3

const val CREATE_FILE_DEFAULT = 1

const val REQUEST_CODE_IMPORT_PNG = 1
const val REQUEST_CODE_LOAD_PICTURE = 2
const val REQUEST_CODE_INTRO = 3

const val PERMISSION_EXTERNAL_STORAGE_SAVE = 1
const val PERMISSION_EXTERNAL_STORAGE_SAVE_COPY = 2
const val PERMISSION_EXTERNAL_STORAGE_SAVE_CONFIRMED_LOAD_NEW = 3
const val PERMISSION_EXTERNAL_STORAGE_SAVE_CONFIRMED_NEW_EMPTY = 4
const val PERMISSION_EXTERNAL_STORAGE_SAVE_CONFIRMED_FINISH = 5
const val PERMISSION_REQUEST_CODE_LOAD_PICTURE = 6
const val PERMISSION_REQUEST_CODE_REPLACE_PICTURE = 6
const val PERMISSION_REQUEST_CODE_IMPORT_PICTURE = 7

const val RESULT_INTRO_MW_NOT_SUPPORTED = 10

class MainActivityConstants private constructor() {
@IntDef(SAVE_IMAGE_DEFAULT, SAVE_IMAGE_NEW_EMPTY, SAVE_IMAGE_LOAD_NEW, SAVE_IMAGE_FINISH)
@IntDef(
SAVE_IMAGE_DEFAULT,
SAVE_IMAGE_NEW_EMPTY,
SAVE_IMAGE_LOAD_NEW,
SAVE_IMAGE_FINISH
)
@Retention(AnnotationRetention.SOURCE)
annotation class SaveImageRequestCode

@IntDef(LOAD_IMAGE_DEFAULT, LOAD_IMAGE_IMPORT_PNG, LOAD_IMAGE_CATROID)
@IntDef(
LOAD_IMAGE_DEFAULT,
LOAD_IMAGE_IMPORT_PNG,
LOAD_IMAGE_CATROID
)
@Retention(AnnotationRetention.SOURCE)
annotation class LoadImageRequestCode

@IntDef(CREATE_FILE_DEFAULT)
@Retention(AnnotationRetention.SOURCE)
annotation class CreateFileRequestCode

@IntDef(REQUEST_CODE_IMPORT_PNG, REQUEST_CODE_LOAD_PICTURE, REQUEST_CODE_INTRO)
@IntDef(
REQUEST_CODE_IMPORT_PNG,
REQUEST_CODE_LOAD_PICTURE,
REQUEST_CODE_INTRO
)
@Retention(AnnotationRetention.SOURCE)
annotation class ActivityRequestCode

Expand All @@ -64,7 +82,7 @@ class MainActivityConstants private constructor() {
PERMISSION_EXTERNAL_STORAGE_SAVE_CONFIRMED_LOAD_NEW,
PERMISSION_EXTERNAL_STORAGE_SAVE_CONFIRMED_NEW_EMPTY,
PERMISSION_EXTERNAL_STORAGE_SAVE_CONFIRMED_FINISH,
PERMISSION_REQUEST_CODE_LOAD_PICTURE,
PERMISSION_REQUEST_CODE_REPLACE_PICTURE,
PERMISSION_REQUEST_CODE_IMPORT_PICTURE
)
@Retention(AnnotationRetention.SOURCE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ interface MainActivityContracts {

fun removeMoreOptionsItems(menu: Menu?)

fun loadImageClicked()
fun replaceImageClicked()

fun addImageToCurrentLayerClicked()

fun loadNewImage()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ class LayerPresenter(
override fun addLayer() {
if (layerCount < MAX_LAYERS) {
checkIfLineToolInUse()
commandManager.addCommand(commandFactory.createAddLayerCommand())
commandManager.addCommand(commandFactory.createAddEmptyLayerCommand())
} else {
navigator.showToast(R.string.layer_too_many_layers, Toast.LENGTH_SHORT)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import org.catrobat.paintroid.common.PERMISSION_EXTERNAL_STORAGE_SAVE_CONFIRMED_
import org.catrobat.paintroid.common.PERMISSION_EXTERNAL_STORAGE_SAVE_CONFIRMED_NEW_EMPTY
import org.catrobat.paintroid.common.PERMISSION_EXTERNAL_STORAGE_SAVE_COPY
import org.catrobat.paintroid.common.PERMISSION_REQUEST_CODE_IMPORT_PICTURE
import org.catrobat.paintroid.common.PERMISSION_REQUEST_CODE_LOAD_PICTURE
import org.catrobat.paintroid.common.PERMISSION_REQUEST_CODE_REPLACE_PICTURE
import org.catrobat.paintroid.common.REQUEST_CODE_IMPORT_PNG
import org.catrobat.paintroid.common.REQUEST_CODE_INTRO
import org.catrobat.paintroid.common.REQUEST_CODE_LOAD_PICTURE
Expand Down Expand Up @@ -131,11 +131,16 @@ open class MainActivityPresenter(
return sharedPreferences.preferenceImageNumber
}

override fun loadImageClicked() {
switchBetweenVersions(PERMISSION_REQUEST_CODE_LOAD_PICTURE, false)
override fun replaceImageClicked() {
switchBetweenVersions(PERMISSION_REQUEST_CODE_REPLACE_PICTURE, false)
setFirstCheckBoxInLayerMenu()
}

override fun addImageToCurrentLayerClicked() {
setTool(ToolType.IMPORTPNG)
switchBetweenVersions(PERMISSION_REQUEST_CODE_IMPORT_PICTURE)
}

private fun setFirstCheckBoxInLayerMenu() {
layerAdapter?.getViewHolderAt(0)?.apply { setLayerVisibilityCheckbox(true) }
}
Expand Down Expand Up @@ -295,8 +300,7 @@ open class MainActivityPresenter(
FileIO.fileType = FileIO.FileType.PNG
FileIO.isCatrobatImage = false
FileIO.deleteTempFile(internalMemoryPath)
val initCommand =
commandFactory.createInitCommand(metrics.widthPixels, metrics.heightPixels)
val initCommand = commandFactory.createInitCommand(metrics.widthPixels, metrics.heightPixels)
commandManager.setInitialStateCommand(initCommand)
commandManager.reset()
}
Expand All @@ -319,15 +323,16 @@ open class MainActivityPresenter(
if (navigator.isSdkAboveOrEqualM) {
askForReadAndWriteExternalStoragePermission(requestCode)
when (requestCode) {
PERMISSION_REQUEST_CODE_LOAD_PICTURE, PERMISSION_REQUEST_CODE_IMPORT_PICTURE -> Unit
PERMISSION_REQUEST_CODE_REPLACE_PICTURE,
PERMISSION_REQUEST_CODE_IMPORT_PICTURE -> Unit
PERMISSION_EXTERNAL_STORAGE_SAVE_CONFIRMED_LOAD_NEW,
PERMISSION_EXTERNAL_STORAGE_SAVE_CONFIRMED_NEW_EMPTY,
PERMISSION_EXTERNAL_STORAGE_SAVE_CONFIRMED_FINISH,
PERMISSION_EXTERNAL_STORAGE_SAVE_COPY,
PERMISSION_EXTERNAL_STORAGE_SAVE -> checkForDefaultFilename()
}
} else {
if (requestCode == PERMISSION_REQUEST_CODE_LOAD_PICTURE) {
if (requestCode == PERMISSION_REQUEST_CODE_REPLACE_PICTURE) {
if (isImageUnchanged || model.isSaved) {
navigator.startLoadImageActivity(REQUEST_CODE_LOAD_PICTURE)
setFirstCheckBoxInLayerMenu()
Expand Down Expand Up @@ -472,7 +477,7 @@ open class MainActivityPresenter(
saveCopyConfirmClicked(SAVE_IMAGE_DEFAULT)
checkForDefaultFilename()
}
PERMISSION_REQUEST_CODE_LOAD_PICTURE ->
PERMISSION_REQUEST_CODE_REPLACE_PICTURE ->
if (isImageUnchanged || model.isSaved) {
navigator.startLoadImageActivity(REQUEST_CODE_LOAD_PICTURE)
} else {
Expand Down
22 changes: 17 additions & 5 deletions Paintroid/src/main/res/menu/menu_pocketpaint_more_options.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,18 @@
android:title="@string/menu_save_copy" />
<item
android:id="@+id/pocketpaint_options_open_image"
android:title="@string/menu_load_image" />
android:title="@string/menu_load_image">

<menu>
<item
android:id="@+id/pocketpaint_replace_image"
android:title="@string/menu_replace_image"/>
<item
android:id="@+id/pocketpaint_add_to_current_layer"
android:title="@string/menu_add_to_current_layer"/>
</menu>
</item>

<item
android:id="@+id/pocketpaint_options_new_image"
android:title="@string/menu_new_image" />
Expand All @@ -20,17 +31,18 @@
<item
android:id="@+id/pocketpaint_options_fullscreen_mode"
android:title="@string/menu_hide_menu" />
<item android:id="@+id/pocketpaint_options_rate_us"
android:title="@string/menu_rate_us"/>
<item
android:id="@+id/pocketpaint_options_rate_us"
android:title="@string/menu_rate_us" />
<item
android:id="@+id/pocketpaint_options_help"
android:title="@string/help_title"/>
android:title="@string/help_title" />
<item
android:id="@+id/pocketpaint_options_about"
android:title="@string/pocketpaint_menu_about" />
<item
android:id="@+id/pocketpaint_share_image_button"
android:title="@string/share_image_menu"/>
android:title="@string/share_image_menu" />
<item
android:id="@+id/pocketpaint_options_feedback"
android:title="@string/menu_feedback" />
Expand Down
2 changes: 2 additions & 0 deletions Paintroid/src/main/res/values/string.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
<string name="no_longclick_on_hidden_layer">You are only able to merge or reorder if all layers are visible</string>
<string name="no_tools_on_hidden_layer">No tools are available on hidden layer</string>
<string name="menu_load_image">Load image</string>
<string name="menu_replace_image">Replace image</string>
<string name="menu_add_to_current_layer">Add to current layer</string>
<string name="menu_save_image">Save image</string>
<string name="menu_save_copy">Save copy</string>
<string name="menu_hide_menu">Fullscreen</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import android.graphics.Bitmap;
import android.graphics.Canvas;

import org.catrobat.paintroid.command.implementation.AddLayerCommand;
import org.catrobat.paintroid.command.implementation.AddEmptyLayerCommand;
import org.catrobat.paintroid.common.CommonFactory;
import org.catrobat.paintroid.contract.LayerContracts;
import org.catrobat.paintroid.model.LayerModel;
Expand All @@ -47,7 +47,7 @@ public class AddLayerCommandTest {
private Canvas canvas;

@InjectMocks
private AddLayerCommand command;
private AddEmptyLayerCommand command;

@Test
public void testSetUp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public void testAddLayer() {
layerModel.addLayerAt(0, mock(Layer.class));
layerModel.addLayerAt(1, mock(Layer.class));
Command command = mock(Command.class);
when(commandFactory.createAddLayerCommand()).thenReturn(command);
when(commandFactory.createAddEmptyLayerCommand()).thenReturn(command);

createPresenter();
layerPresenter.addLayer();
Expand Down
Loading

0 comments on commit 73f2f10

Please sign in to comment.