Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Extracting most UI state to EditingState #126

Merged
merged 53 commits into from
Nov 13, 2024
Merged
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
68a146e
First iteration of refactoring
isakovch Oct 29, 2023
71b2147
- Revert gradle.properties, since CI/CD is not support cache. CI/CD w…
isakovch Oct 29, 2023
0b56a78
NO-TICKET - Ktlint fixes
isakovch Oct 29, 2023
7e9b537
Fix NoBeanDefFoundException in EditModule + add ktlint gradle plugin …
shubertm Jan 21, 2024
b8df169
Update workflow to JDK 17
shubertm Jan 21, 2024
fa814c4
Remove ktlint gradle plugin and add ktlintCheck to ktlint.gradle
shubertm Jan 22, 2024
8244fce
Run ktlintCheck after ktlintFormat
shubertm Jan 22, 2024
b2b9f31
Remove context from shareImage
shubertm Jan 22, 2024
7178dbb
Delete getCachedImageUri
shubertm Jan 22, 2024
6eba793
Extract image loading from EditViewModel file
shubertm Jan 22, 2024
efe609b
#97: Complete refactoring with best practices (#105)
shubertm Feb 1, 2024
2f607b2
Merge remote-tracking branch 'origin/develop' into develop
shubertm Apr 3, 2024
ff0292e
Refactor packages structure
shubertm Apr 4, 2024
1429d2c
Merge branch 'main' into develop
shubertm Apr 6, 2024
85f3a47
Resolve conflicts
shubertm Apr 6, 2024
6e65ad2
Refactor: Extract crop mode to EditingState (#125)
shubertm Apr 10, 2024
ec87906
Extract isRotateMode to editingState
shubertm Apr 19, 2024
cb936fe
Extract isRotateMode to editingState
shubertm Apr 19, 2024
4d3089a
Extract isRotateMode to editingState
shubertm Apr 22, 2024
7e34760
Extract isResizeMode to editingState
shubertm Apr 30, 2024
531e856
Extract isResizeMode to editingState
shubertm Apr 30, 2024
8d33c15
Extract isEraseMode to EditingState
shubertm May 7, 2024
e69f739
Extract isEraseMode to EditingState
shubertm May 8, 2024
1d22478
Extract isZoomMode + isPanMode to EditingState
shubertm May 9, 2024
89768f1
Extract isBlurMode to EditingState
shubertm Jun 2, 2024
76eef62
Extract isBlurMode to EditingState
shubertm Jun 2, 2024
659edee
Extract isBlurMode to EditingState d
shubertm Jun 2, 2024
c8913dc
Fix resize operation
shubertm Jun 4, 2024
b79c800
Extract isEyeDropperMode to EditingState
shubertm Jun 4, 2024
6a8e5cd
Extract canUndo and canRedo to EditingState
shubertm Jun 5, 2024
f1d4c61
Clean up
shubertm Jun 6, 2024
cab01b9
Clean up
shubertm Jun 6, 2024
9f3eee4
Remove commented out code
shubertm Jun 17, 2024
7e2d5bf
Move all toggleCrop logic from EditScreen to EditViewModel
shubertm Jul 10, 2024
ad36a3c
Move operations logic from EditScreen to EditViewModel
shubertm Jul 12, 2024
6fdb288
Fix lint + remove redundant permission requests
shubertm Jul 22, 2024
1d4c825
Fix lint + remove redundant permission requests
shubertm Jul 22, 2024
633fba1
Check lint failure cause
shubertm Jul 22, 2024
2682f02
Check lint failure cause
shubertm Jul 22, 2024
4216f58
Fix lint
shubertm Jul 22, 2024
99e0dc2
Fix lint
shubertm Jul 22, 2024
4b2a12c
Update compose to 1.6.8
shubertm Jul 22, 2024
91ee12c
Update compose to 1.6.8
shubertm Jul 22, 2024
3d873a6
Fix lint
shubertm Jul 22, 2024
36aa214
Fix lint
shubertm Jul 25, 2024
861b1c9
Fix lint
shubertm Jul 25, 2024
ffb97bd
Fix lint
shubertm Jul 25, 2024
570808e
Update workflow
shubertm Jul 25, 2024
3813f97
Update workflows
shubertm Jul 25, 2024
2a93baf
Update gradle
shubertm Jul 26, 2024
68a1a73
Fix SavePathDialog
shubertm Jul 26, 2024
7e24ab3
Fix sharing image
shubertm Jul 26, 2024
9962dc1
Minor refactors
shubertm Aug 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Extract isEraseMode to EditingState
shubertm committed May 7, 2024
commit 8d33c15c7e8cedaf6b0fa7b91152b9b0dd476a8f
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ plugins {
apply from: "$project.rootDir/.scripts/ktlint.gradle"

android {
compileSdk 33
compileSdk 34

namespace 'dev.arkbuilders.arkretouch'

@@ -87,6 +87,7 @@ dependencies {
implementation 'androidx.activity:activity-compose:1.3.1'
implementation "androidx.appcompat:appcompat:1.5.1"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:1.4.0"
implementation 'androidx.compose.ui:ui-graphics-android:1.6.7'

def composeVersion = "1.3.0"
implementation "androidx.compose.ui:ui:$composeVersion"
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dev.arkbuilders.arkretouch.data.model

import androidx.compose.ui.graphics.BlendMode
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.graphics.PaintingStyle
import dev.arkbuilders.arkretouch.utils.defaultPaint

data class DrawingState(
val currentPaint: Paint = defaultPaint(),
val drawPaint: Paint = defaultPaint(),
val backgroundPaint: Paint = Paint().also { it.color = Color.Transparent },
val erasePaint: Paint = Paint().apply {
shader = null
color = backgroundPaint.color
style = PaintingStyle.Stroke
blendMode = BlendMode.SrcOut
}
)
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package dev.arkbuilders.arkretouch.editing.draw

import androidx.compose.ui.graphics.Path
import dev.arkbuilders.arkretouch.editing.Operation
import dev.arkbuilders.arkretouch.editing.manager.EditManager

class DrawOperation(private val editManager: EditManager) : Operation {
private var path = Path()

override fun apply() {
editManager.addDrawPath(path)
}
override fun apply() {}

override fun undo() {
editManager.apply {
@@ -30,8 +25,4 @@ class DrawOperation(private val editManager: EditManager) : Operation {
}
}
}

fun draw(path: Path) {
this.path = path
}
}
Original file line number Diff line number Diff line change
@@ -4,12 +4,8 @@ import androidx.compose.runtime.MutableState
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.BlendMode
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.graphics.PaintingStyle
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.unit.IntSize
import android.graphics.Matrix
import dev.arkbuilders.arkretouch.data.model.DrawPath
@@ -25,8 +21,6 @@ import dev.arkbuilders.arkretouch.editing.resize.ResizeOperation
import dev.arkbuilders.arkretouch.editing.rotate.RotateOperation
import dev.arkbuilders.arkretouch.presentation.viewmodels.fitBackground
import dev.arkbuilders.arkretouch.presentation.viewmodels.fitImage
import dev.arkbuilders.arkretouch.utils.copy
import dev.arkbuilders.arkretouch.utils.defaultPaint
import timber.log.Timber
import java.util.Stack

@@ -35,30 +29,7 @@ class EditManager {

private var imageSize: IntSize = IntSize.Zero

private val drawPaint: MutableState<Paint> = mutableStateOf(defaultPaint())

private val _paintColor: MutableState<Color> =
mutableStateOf(drawPaint.value.color)
val paintColor: State<Color> = _paintColor

private val _backgroundColor = mutableStateOf(Color.Transparent)
val backgroundColor: State<Color> = _backgroundColor

private val erasePaint: Paint = Paint().apply {
shader = null
color = backgroundColor.value
style = PaintingStyle.Stroke
blendMode = BlendMode.SrcOut
}

val backgroundPaint: Paint
get() {
return Paint().apply {
color = backgroundImage.value?.let {
Color.Transparent
} ?: backgroundColor.value
}
}

val blurIntensity = mutableStateOf(12f)

@@ -70,12 +41,6 @@ class EditManager {
val cropOperation = CropOperation(this, {})
val blurOperation = BlurOperation(this, imageSize)

private val currentPaint: Paint
get() = when (true) {
isEraseMode.value -> erasePaint
else -> drawPaint.value
}

val drawPaths = Stack<DrawPath>()

val redoPaths = Stack<DrawPath>()
@@ -115,10 +80,6 @@ class EditManager {

var invalidatorTick = mutableStateOf(0)

// TODO: Consider using [EditionMode] instead
private val _isEraseMode: MutableState<Boolean> = mutableStateOf(false)
val isEraseMode: State<Boolean> = _isEraseMode

// TODO: Consider using [EditionMode] instead
private val _canUndo: MutableState<Boolean> = mutableStateOf(false)
val canUndo: State<Boolean> = _canUndo
@@ -239,10 +200,6 @@ class EditManager {
}
}

fun setBackgroundColor(color: Color) {
_backgroundColor.value = color
}

fun setImageResolution(value: Resolution) {
_resolution.value = value
}
@@ -420,24 +377,12 @@ class EditManager {
}
}

fun addDrawPath(path: Path) {
drawPaths.add(
DrawPath(
path,
currentPaint.copy().apply {
strokeWidth = drawPaint.value.strokeWidth
}
)
)
fun addDrawPath(path: DrawPath) {
drawPaths.add(path)
if (canRedo.value) clearRedo()
undoStack.add(DRAW)
}

fun setPaintColor(color: Color) {
drawPaint.value.color = color
_paintColor.value = color
}

private fun clearPaths() {
drawPaths.clear()
redoPaths.clear()
@@ -507,10 +452,6 @@ class EditManager {
updateAvailableDrawArea()
}

fun toggleEraseMode() {
_isEraseMode.value = !isEraseMode.value
}

fun toggleZoomMode() {
_isZoomMode.value = !isZoomMode.value
}
@@ -538,10 +479,6 @@ class EditManager {
_isBlurMode.value = !isBlurMode.value
}

fun setPaintStrokeWidth(strokeWidth: Float) {
drawPaint.value.strokeWidth = strokeWidth
}

fun calcImageOffset(): Offset {
val drawArea = drawAreaSize.value
val allowedArea = availableDrawAreaSize.value
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.graphics.ClipOp
import androidx.compose.ui.graphics.Paint
import androidx.compose.ui.graphics.asAndroidBitmap
import androidx.compose.ui.graphics.drawscope.drawIntoCanvas
import androidx.compose.ui.graphics.nativeCanvas
@@ -20,6 +21,7 @@ fun BackgroundCanvas(
isRotating: Boolean,
isResizing: Boolean,
imageSize: IntSize,
backgroundPaint: Paint,
editManager: EditManager
) {
Canvas(modifier) {
Original file line number Diff line number Diff line change
@@ -40,10 +40,7 @@ fun DrawCanvas(modifier: Modifier, viewModel: EditViewModel) {
path.moveTo(eventX, eventY)
currentPoint.x = eventX
currentPoint.y = eventY
editManager.apply {
drawOperation.draw(path)
viewModel.applyOperation()
}
viewModel.onDrawPath(path)
}
MotionEvent.ACTION_MOVE -> {
path.quadraticBezierTo(
Original file line number Diff line number Diff line change
@@ -63,7 +63,13 @@ fun EditCanvasScreen(viewModel: EditViewModel) {
}
TransparencyChessBoardCanvas(modifier, viewModel.imageSize, editManager)
BackgroundCanvas(
modifier, viewModel.isCropping(), viewModel.isRotating(), viewModel.isResizing(), viewModel.imageSize, editManager
modifier,
viewModel.isCropping(),
viewModel.isRotating(),
viewModel.isResizing(),
viewModel.imageSize,
viewModel.drawingState.backgroundPaint,
editManager
)
DrawCanvas(modifier, viewModel)
}
Original file line number Diff line number Diff line change
@@ -38,6 +38,7 @@ import dev.arkbuilders.arkretouch.R
import dev.arkbuilders.arkretouch.data.model.Resolution
import dev.arkbuilders.arkretouch.editing.manager.EditManager
import dev.arkbuilders.arkretouch.presentation.theme.Gray
import dev.arkbuilders.arkretouch.presentation.viewmodels.EditViewModel
import dev.arkbuilders.arkretouch.presentation.views.Hint
import dev.arkbuilders.arkretouch.presentation.views.delayHidingHint

@@ -47,10 +48,11 @@ fun NewImageOptionsDialog(
maxResolution: Resolution,
_backgroundColor: Color,
navigateBack: () -> Unit,
editManager: EditManager,
viewModel: EditViewModel,
persistDefaults: (Color, Resolution) -> Unit,
onConfirm: () -> Unit
) {
val editManager: EditManager = viewModel.editManager
var isVisible by remember { mutableStateOf(true) }
var backgroundColor by remember {
mutableStateOf(_backgroundColor)
@@ -301,7 +303,7 @@ fun NewImageOptionsDialog(
height.toInt()
)
editManager.setImageResolution(resolution)
editManager.setBackgroundColor(backgroundColor)
viewModel.onSetBackgroundColor(backgroundColor)
if (rememberDefaults)
persistDefaults(backgroundColor, resolution)
onConfirm()
Loading