Skip to content

Commit

Permalink
Added Helper grid for better drawing by #1396
Browse files Browse the repository at this point in the history
  • Loading branch information
T8RIN committed Oct 10, 2024
1 parent 8a9caf3 commit 31eb35c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,9 @@ fun SingleEditContent(
drawPathMode = viewModel.drawPathMode,
onUpdateDrawPathMode = viewModel::updateDrawPathMode,
drawLineStyle = viewModel.drawLineStyle,
onUpdateDrawLineStyle = viewModel::updateDrawLineStyle
onUpdateDrawLineStyle = viewModel::updateDrawLineStyle,
helperGridParams = viewModel.helperGridParams,
onUpdateHelperGridParams = viewModel::updateHelperGridParams
)

EraseBackgroundEditOption(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ import ru.tech.imageresizershrinker.core.ui.widget.buttons.EnhancedIconButton
import ru.tech.imageresizershrinker.core.ui.widget.buttons.EraseModeButton
import ru.tech.imageresizershrinker.core.ui.widget.buttons.PanModeButton
import ru.tech.imageresizershrinker.core.ui.widget.controls.selection.AlphaSelector
import ru.tech.imageresizershrinker.core.ui.widget.controls.selection.HelperGridParamsSelector
import ru.tech.imageresizershrinker.core.ui.widget.modifier.HelperGridParams
import ru.tech.imageresizershrinker.core.ui.widget.modifier.container
import ru.tech.imageresizershrinker.core.ui.widget.other.DrawLockScreenOrientation
import ru.tech.imageresizershrinker.core.ui.widget.other.EnhancedTopAppBar
Expand Down Expand Up @@ -125,6 +127,8 @@ fun DrawEditOption(
lastPaths: List<UiPathPaint>,
undonePaths: List<UiPathPaint>,
addPath: (UiPathPaint) -> Unit,
helperGridParams: HelperGridParams,
onUpdateHelperGridParams: (HelperGridParams) -> Unit,
) {
bitmap?.let {
var panEnabled by rememberSaveable { mutableStateOf(false) }
Expand Down Expand Up @@ -355,6 +359,11 @@ fun DrawEditOption(
value = drawLineStyle,
onValueChange = onUpdateDrawLineStyle
)
HelperGridParamsSelector(
value = helperGridParams,
onValueChange = onUpdateHelperGridParams,
modifier = Modifier.padding(horizontal = 16.dp)
)
val settingsInteractor = LocalSimpleSettingsInteractor.current
val scope = rememberCoroutineScope()
PreferenceRowSwitch(
Expand Down Expand Up @@ -448,7 +457,8 @@ fun DrawEditOption(
},
drawPathMode = drawPathMode,
backgroundColor = Color.Transparent,
drawLineStyle = drawLineStyle
drawLineStyle = drawLineStyle,
helperGridParams = helperGridParams
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import ru.tech.imageresizershrinker.core.settings.domain.SettingsProvider
import ru.tech.imageresizershrinker.core.ui.utils.BaseViewModel
import ru.tech.imageresizershrinker.core.ui.utils.helper.ImageUtils.safeAspectRatio
import ru.tech.imageresizershrinker.core.ui.utils.state.update
import ru.tech.imageresizershrinker.core.ui.widget.modifier.HelperGridParams
import ru.tech.imageresizershrinker.feature.draw.domain.DrawLineStyle
import ru.tech.imageresizershrinker.feature.draw.domain.DrawMode
import ru.tech.imageresizershrinker.feature.draw.domain.DrawPathMode
Expand All @@ -83,7 +84,7 @@ class SingleEditViewModel @Inject constructor(
private val shareProvider: ShareProvider<Bitmap>,
private val filterProvider: FilterProvider<Bitmap>,
private val settingsProvider: SettingsProvider,
dispatchersHolder: DispatchersHolder
dispatchersHolder: DispatchersHolder,
) : BaseViewModel(dispatchersHolder) {

private val _originalSize: MutableState<IntegerSize?> = mutableStateOf(null)
Expand Down Expand Up @@ -172,6 +173,10 @@ class SingleEditViewModel @Inject constructor(
private val _drawLineStyle: MutableState<DrawLineStyle> = mutableStateOf(DrawLineStyle.None)
val drawLineStyle: DrawLineStyle by _drawLineStyle

private val _helperGridParams: MutableState<HelperGridParams> =
mutableStateOf(HelperGridParams())
val helperGridParams: HelperGridParams by _helperGridParams

init {
viewModelScope.launch {
val settingsState = settingsProvider.getSettingsState()
Expand All @@ -180,6 +185,13 @@ class SingleEditViewModel @Inject constructor(
it.copy(resizeType = settingsState.defaultResizeType)
}
}
viewModelScope.launch {
val params = fileController.restoreObject(
"helperGridParams",
HelperGridParams::class
) ?: HelperGridParams()
_helperGridParams.update { params }
}
}

private var job: Job? by smartJob {
Expand Down Expand Up @@ -233,7 +245,7 @@ class SingleEditViewModel @Inject constructor(
}

private suspend fun updatePreview(
bitmap: Bitmap
bitmap: Bitmap,
): Bitmap? = withContext(defaultDispatcher) {
return@withContext imageInfo.run {
_showWarning.update {
Expand Down Expand Up @@ -281,7 +293,7 @@ class SingleEditViewModel @Inject constructor(

fun updateBitmapAfterEditing(
bitmap: Bitmap?,
saveOriginalSize: Boolean = false
saveOriginalSize: Boolean = false,
) {
viewModelScope.launch {
if (!saveOriginalSize) {
Expand Down Expand Up @@ -424,7 +436,7 @@ class SingleEditViewModel @Inject constructor(

fun decodeBitmapByUri(
uri: Uri,
onError: (Throwable) -> Unit
onError: (Throwable) -> Unit,
) {
_isImageLoading.update { true }
_imageInfo.update {
Expand Down Expand Up @@ -541,7 +553,7 @@ class SingleEditViewModel @Inject constructor(

fun updateExifByTag(
tag: MetadataTag,
value: String
value: String,
) {
val exifInterface = _exif.value
exifInterface?.setAttribute(tag.key, value)
Expand All @@ -550,7 +562,7 @@ class SingleEditViewModel @Inject constructor(

fun setCropAspectRatio(
domainAspectRatio: DomainAspectRatio,
aspectRatio: AspectRatio
aspectRatio: AspectRatio,
) {
_cropProperties.update { properties ->
properties.copy(
Expand All @@ -577,7 +589,7 @@ class SingleEditViewModel @Inject constructor(
fun <T : Any> updateFilter(
value: T,
index: Int,
showError: (Throwable) -> Unit
showError: (Throwable) -> Unit,
) {
val list = _filterList.value.toMutableList()
runCatching {
Expand Down Expand Up @@ -705,14 +717,14 @@ class SingleEditViewModel @Inject constructor(

suspend fun filter(
bitmap: Bitmap,
filters: List<Filter<*>>
filters: List<Filter<*>>,
): Bitmap? = imageTransformer.transform(
image = bitmap,
transformations = mapFilters(filters)
)

fun mapFilters(
filters: List<Filter<*>>
filters: List<Filter<*>>,
): List<Transformation<Bitmap>> = filters.map { filterProvider.filterToTransformation(it) }

fun updateDrawMode(drawMode: DrawMode) {
Expand All @@ -733,4 +745,18 @@ class SingleEditViewModel @Inject constructor(
_drawLineStyle.update { style }
}

private var smartSavingJob: Job? by smartJob()

fun updateHelperGridParams(params: HelperGridParams) {
_helperGridParams.update { params }

smartSavingJob = viewModelScope.launch {
delay(200)
fileController.saveObject(
key = "helperGridParams",
value = params
)
}
}

}

0 comments on commit 31eb35c

Please sign in to comment.