Skip to content

Commit

Permalink
Added Separate image converter option by #988
Browse files Browse the repository at this point in the history
  • Loading branch information
T8RIN committed Apr 26, 2024
1 parent 0a005a0 commit e35f001
Show file tree
Hide file tree
Showing 36 changed files with 1,023 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ internal class AndroidImageTransformer @Inject constructor(
)
}

is Preset.Numeric -> currentInfo.copy(
is Preset.Percentage -> currentInfo.copy(
quality = when (val quality = currentInfo.quality) {
is Quality.Base -> quality.copy(qualityValue = preset.value)
is Quality.Jxl -> quality.copy(qualityValue = preset.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,24 @@ sealed class Preset {

data object None : Preset()

class Numeric(val value: Int) : Preset()
data class Percentage(val value: Int) : Preset()

fun isTelegram(): Boolean = this is Telegram

fun value(): Int? = (this as? Numeric)?.value
fun value(): Int? = (this as? Percentage)?.value

fun isEmpty(): Boolean = this is None

companion object {
val Original by lazy {
Percentage(100)
}

fun createListFromInts(presets: String?): List<Preset> {
return ((presets?.split("*")?.map {
it.toInt()
} ?: List(6) { 100 - it * 10 })).toSortedSet().reversed().toList()
.map { Numeric(it) }
.map { Percentage(it) }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* ImageToolbox is an image editor for android
* Copyright (c) 2024 T8RIN (Malik Mukhametzyanov)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* You should have received a copy of the Apache License
* along with this program. If not, see <http://www.apache.org/licenses/LICENSE-2.0>.
*/

package ru.tech.imageresizershrinker.core.resources.icons

import androidx.compose.material.icons.Icons
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.PathFillType.Companion.NonZero
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.graphics.StrokeCap.Companion.Butt
import androidx.compose.ui.graphics.StrokeJoin.Companion.Miter
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.graphics.vector.path
import androidx.compose.ui.unit.dp

val Icons.Outlined.ImageConvert: ImageVector by lazy {
ImageVector.Builder(
name = "ImageConvert",
defaultWidth = 24.0.dp,
defaultHeight = 24.0.dp,
viewportWidth = 24.0f,
viewportHeight = 24.0f
).apply {
path(
fill = SolidColor(Color(0xFF000000)), stroke = null, strokeLineWidth = 0.0f,
strokeLineCap = Butt, strokeLineJoin = Miter, strokeLineMiter = 4.0f,
pathFillType = NonZero
) {
moveTo(9.0629f, 19.3864f)
curveTo(7.4781f, 18.6591f, 6.1905f, 17.5606f, 5.2f, 16.0909f)
curveToRelative(-0.9905f, -1.4697f, -1.4857f, -3.1288f, -1.4857f, -4.9773f)
curveToRelative(0.0f, -0.3939f, 0.019f, -0.7803f, 0.0571f, -1.1591f)
curveToRelative(0.0381f, -0.3788f, 0.1029f, -0.75f, 0.1943f, -1.1136f)
lineTo(2.9143f, 9.4545f)
lineTo(2.0f, 7.8864f)
lineToRelative(4.3657f, -2.5f)
lineToRelative(2.5143f, 4.3182f)
lineToRelative(-1.6f, 0.9091f)
lineTo(6.0457f, 8.4773f)
curveTo(5.8781f, 8.8864f, 5.7524f, 9.3106f, 5.6686f, 9.75f)
curveToRelative(-0.0838f, 0.4394f, -0.1257f, 0.8939f, -0.1257f, 1.3636f)
curveToRelative(0.0f, 1.4697f, 0.4038f, 2.8068f, 1.2114f, 4.0114f)
reflectiveCurveToRelative(1.8819f, 2.0947f, 3.2229f, 2.6705f)
lineTo(9.0629f, 19.3864f)
close()
moveTo(16.0571f, 8.3636f)
verticalLineTo(6.5455f)
horizontalLineToRelative(2.4914f)
curveToRelative(-0.701f, -0.8636f, -1.5467f, -1.5341f, -2.5371f, -2.0114f)
reflectiveCurveToRelative(-2.0419f, -0.7159f, -3.1543f, -0.7159f)
curveToRelative(-0.8381f, 0.0f, -1.6305f, 0.1288f, -2.3771f, 0.3864f)
curveToRelative(-0.7467f, 0.2576f, -1.4324f, 0.6212f, -2.0571f, 1.0909f)
lineToRelative(-0.9143f, -1.5909f)
curveTo(8.2705f, 3.1742f, 9.101f, 2.7576f, 10.0f, 2.4545f)
reflectiveCurveTo(11.8514f, 2.0f, 12.8571f, 2.0f)
curveToRelative(1.2038f, 0.0f, 2.3543f, 0.2235f, 3.4514f, 0.6705f)
reflectiveCurveToRelative(2.08f, 1.0947f, 2.9486f, 1.9432f)
verticalLineTo(3.3636f)
horizontalLineToRelative(1.8286f)
verticalLineToRelative(5.0f)
horizontalLineTo(16.0571f)
close()
moveTo(15.4629f, 22.0f)
lineToRelative(-4.3657f, -2.5f)
lineToRelative(2.5143f, -4.3182f)
lineTo(15.1886f, 16.0909f)
lineToRelative(-1.3029f, 2.2273f)
curveToRelative(1.7981f, -0.2576f, 3.2952f, -1.0682f, 4.4914f, -2.4318f)
curveToRelative(1.1962f, -1.3636f, 1.7943f, -2.9621f, 1.7943f, -4.7955f)
curveToRelative(0.0f, -0.1667f, -0.0038f, -0.322f, -0.0114f, -0.4659f)
reflectiveCurveToRelative(-0.0267f, -0.2917f, -0.0571f, -0.4432f)
horizontalLineToRelative(1.8514f)
curveToRelative(0.0152f, 0.1515f, 0.0267f, 0.2992f, 0.0343f, 0.4432f)
reflectiveCurveTo(22.0f, 10.9242f, 22.0f, 11.0909f)
curveToRelative(0.0f, 2.0455f, -0.6133f, 3.875f, -1.84f, 5.4886f)
reflectiveCurveToRelative(-2.8229f, 2.7008f, -4.7886f, 3.2614f)
lineToRelative(1.0057f, 0.5909f)
lineTo(15.4629f, 22.0f)
close()
}
path(
fill = SolidColor(Color(0xFF000000)), stroke = null, strokeLineWidth = 0.0f,
strokeLineCap = Butt, strokeLineJoin = Miter, strokeLineMiter = 4.0f,
pathFillType = NonZero
) {
moveTo(8.7447f, 13.0043f)
lineToRelative(8.0f, 0.0f)
lineToRelative(-2.625f, -3.5f)
lineToRelative(-1.875f, 2.5f)
lineToRelative(-1.375f, -1.825f)
close()
}
}.build()
}
2 changes: 2 additions & 0 deletions core/resources/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -912,4 +912,6 @@
<string name="legacy">Legacy</string>
<string name="lstm_network">LSTM network</string>
<string name="legacy_and_lstm"><![CDATA[Legacy & LSTM]]></string>
<string name="convert">Convert</string>
<string name="convert_sub">Convert image batches to given format</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ import coil.transform.Transformation as CoilTransformation

class ImageInfoTransformation @AssistedInject constructor(
@Assisted private val imageInfo: ImageInfo,
@Assisted private val preset: Preset = Preset.Numeric(100),
@Assisted private val transformations: List<Transformation<Bitmap>> = emptyList(),
@Assisted private val preset: Preset,
@Assisted private val transformations: List<Transformation<Bitmap>>,
private val imageTransformer: ImageTransformer<Bitmap>,
private val imageScaler: ImageScaler<Bitmap>,
private val imagePreviewCreator: ImagePreviewCreator<Bitmap>
Expand Down Expand Up @@ -111,7 +111,7 @@ class ImageInfoTransformation @AssistedInject constructor(
interface Factory {
operator fun invoke(
imageInfo: ImageInfo,
preset: Preset = Preset.Numeric(100),
preset: Preset = Preset.Original,
transformations: List<Transformation<Bitmap>> = emptyList(),
): ImageInfoTransformation
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ import android.net.Uri
import android.os.Build
import android.widget.Toast
import androidx.annotation.StringRes
import androidx.appcompat.app.AppCompatDelegate
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.ErrorOutline
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.core.app.ActivityCompat
import androidx.core.os.LocaleListCompat
import androidx.documentfile.provider.DocumentFile
import ru.tech.imageresizershrinker.core.resources.R
import ru.tech.imageresizershrinker.core.ui.utils.helper.IntentUtils.parcelable
Expand Down Expand Up @@ -296,8 +298,15 @@ object ContextUtils {
return !getSystemProperty("ro.miui.ui.version.name").isNullOrBlank()
}

fun isRedMagic(): Boolean {
val osName = System.getProperty("os.name")?.lowercase() ?: ""
return listOf("redmagic", "magic", "red").all {
it !in osName
}
}

private fun getSystemProperty(name: String): String? {
return kotlin.runCatching {
return runCatching {
val p = Runtime.getRuntime().exec("getprop $name")
BufferedReader(InputStreamReader(p.inputStream), 1024).use {
return@runCatching it.readLine()
Expand All @@ -318,4 +327,41 @@ object ContextUtils {
}
}

fun Context.getLanguages(): Map<String, String> {
val languages = mutableListOf("" to getString(R.string.system)).apply {
addAll(
LocaleConfigCompat(this@getLanguages)
.supportedLocales!!.toList()
.map {
it.toLanguageTag() to it.getDisplayName(it)
.replaceFirstChar(Char::uppercase)
}
)
}

return languages.let { tags ->
listOf(tags.first()) + tags.drop(1).sortedBy { it.second }
}.toMap()
}

fun Context.getCurrentLocaleString(): String {
val locales = AppCompatDelegate.getApplicationLocales()
if (locales == LocaleListCompat.getEmptyLocaleList()) {
return getString(R.string.system)
}
return getDisplayName(locales.toLanguageTags())
}

private fun getDisplayName(lang: String?): String {
if (lang == null) {
return ""
}

val locale = when (lang) {
"" -> LocaleListCompat.getAdjustedDefault()[0]
else -> Locale.forLanguageTag(lang)
}
return locale!!.getDisplayName(locale).replaceFirstChar { it.uppercase(locale) }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import ru.tech.imageresizershrinker.core.resources.icons.CropSmall
import ru.tech.imageresizershrinker.core.resources.icons.Encrypted
import ru.tech.imageresizershrinker.core.resources.icons.Exif
import ru.tech.imageresizershrinker.core.resources.icons.ImageCombine
import ru.tech.imageresizershrinker.core.resources.icons.ImageConvert
import ru.tech.imageresizershrinker.core.resources.icons.ImageDownload
import ru.tech.imageresizershrinker.core.resources.icons.ImageEdit
import ru.tech.imageresizershrinker.core.resources.icons.ImageLimit
Expand All @@ -70,6 +71,7 @@ sealed class Screen(
@StringRes val subtitle: Int
) : Parcelable {

@Suppress("unused")
val simpleName: String?
get() = when (this) {
is ApngTools -> "APNG_Tools"
Expand Down Expand Up @@ -100,10 +102,15 @@ sealed class Screen(
is Watermarking -> "Watermarking"
is Zip -> "Zip"
is Svg -> "Svg"
is Convert -> "Convert"
}

val icon: ImageVector?
get() = when (this) {
EasterEgg,
Main,
Settings -> null

is SingleEdit -> Icons.Outlined.ImageEdit
is ApngTools -> Icons.Rounded.ApngBox
is Cipher -> Icons.Outlined.Encrypted
Expand All @@ -129,9 +136,7 @@ sealed class Screen(
is Watermarking -> Icons.AutoMirrored.Outlined.BrandingWatermark
is Zip -> Icons.Outlined.FolderZip
is Svg -> Icons.Outlined.Svg
EasterEgg,
Main,
Settings -> null
is Convert -> Icons.Outlined.ImageConvert
}

data object Settings : Screen(
Expand Down Expand Up @@ -577,12 +582,21 @@ sealed class Screen(
subtitle = R.string.images_to_svg_sub
)

data class Convert(
val uris: List<Uri>? = null
) : Screen(
id = 25,
title = R.string.convert,
subtitle = R.string.convert_sub
)

companion object {
val typedEntries by lazy {
listOf(
listOf(
SingleEdit(),
ResizeAndConvert(),
Convert(),
Crop(),
ResizeByBytes(),
LimitResize(),
Expand Down Expand Up @@ -635,6 +649,6 @@ sealed class Screen(
typedEntries.flatMap { it.first }.sortedBy { it.id }
}

const val FEATURES_COUNT = 38
const val FEATURES_COUNT = 39
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package ru.tech.imageresizershrinker.core.ui.utils.state

import androidx.compose.runtime.MutableState

fun <T> MutableState<T>.update(
inline fun <T> MutableState<T>.update(
transform: (T) -> T
): MutableState<T> = apply {
this.value = transform(this.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ fun PresetSelector(
val selected = value.value() == it
EnhancedChip(
selected = selected,
onClick = { onValueChange(Preset.Numeric(it)) },
onClick = { onValueChange(Preset.Percentage(it)) },
selectedColor = MaterialTheme.colorScheme.primary,
shape = MaterialTheme.shapes.medium
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ import androidx.compose.ui.unit.dp
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun LoadingDialog(
canCancel: Boolean = true,
onCancelLoading: () -> Unit = {}
onCancelLoading: () -> Unit = {},
canCancel: Boolean = true
) {
var showWantDismissDialog by remember(canCancel) { mutableStateOf(false) }
BasicAlertDialog(onDismissRequest = { showWantDismissDialog = canCancel }) {
Expand Down Expand Up @@ -67,8 +67,8 @@ fun LoadingDialog(
fun LoadingDialog(
done: Int,
left: Int,
onCancelLoading: () -> Unit,
canCancel: Boolean = true,
onCancelLoading: () -> Unit
) {
var showWantDismissDialog by remember(canCancel) { mutableStateOf(false) }
BasicAlertDialog(onDismissRequest = { showWantDismissDialog = canCancel }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ internal fun List<Uri>.screenList(
listOf(
Screen.SingleEdit(uris.firstOrNull()),
Screen.ResizeAndConvert(uris),
Screen.Convert(uris),
Screen.ResizeByBytes(uris),
Screen.Crop(uris.firstOrNull()),
Screen.Filter(
Expand Down Expand Up @@ -150,6 +151,7 @@ internal fun List<Uri>.screenList(
mutableListOf(
Screen.ResizeAndConvert(uris),
Screen.ResizeByBytes(uris),
Screen.Convert(uris),
Screen.Filter(
type = Screen.Filter.Type.Basic(uris)
)
Expand Down Expand Up @@ -226,7 +228,7 @@ internal fun List<Uri>.screenList(
extraImageType == "pdf" -> pdfAvailableScreens
extraImageType == "gif" -> gifAvailableScreens
extraImageType == "file" -> filesAvailableScreens
uris.size <= 1 -> singleImageScreens
uris.size < 2 -> singleImageScreens
else -> multipleImagesScreens
}
}
Expand Down
Loading

0 comments on commit e35f001

Please sign in to comment.