Skip to content

Commit

Permalink
code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
T8RIN committed Feb 22, 2024
1 parent 3406b97 commit 70d3bab
Show file tree
Hide file tree
Showing 57 changed files with 325 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ class GlobalExceptionHandler<T : CrashHandler> private constructor(
private val activityToBeLaunched: Class<T>
) : Thread.UncaughtExceptionHandler {

override fun uncaughtException(p0: Thread, p1: Throwable) {
override fun uncaughtException(
p0: Thread,
p1: Throwable
) {
kotlin.runCatching {
Log.e(this.toString(), p1.stackTraceToString())
applicationContext.launchActivity(activityToBeLaunched, p1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ class ScreenshotService : Service() {
@Inject
lateinit var fileController: FileController

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
override fun onStartCommand(
intent: Intent?,
flags: Int,
startId: Int
): Int {
runCatching {
val mediaProjectionManager =
getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
Expand Down Expand Up @@ -110,7 +114,10 @@ class ScreenshotService : Service() {
return START_REDELIVER_INTENT
}

private fun startCapture(mediaProjection: MediaProjection, intent: Intent?) {
private fun startCapture(
mediaProjection: MediaProjection,
intent: Intent?
) {
Handler(
Looper.getMainLooper()
).postDelayed(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ class GlobalExceptionHandler<T : CrashHandler> private constructor(
private val activityToBeLaunched: Class<T>
) : Thread.UncaughtExceptionHandler {

override fun uncaughtException(p0: Thread, p1: Throwable) {
override fun uncaughtException(
p0: Thread,
p1: Throwable
) {
if (allowCollectCrashlytics) {
Firebase.crashlytics.recordException(p1)
Firebase.crashlytics.sendUnsentReports()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ internal class AndroidImageGetter @Inject constructor(
}
}

override suspend fun getImage(data: Any, originalSize: Boolean): Bitmap? {
override suspend fun getImage(
data: Any,
originalSize: Boolean
): Bitmap? {
return runCatching {
imageLoader.execute(
ImageRequest
Expand All @@ -97,7 +100,10 @@ internal class AndroidImageGetter @Inject constructor(
}.getOrNull()
}

override suspend fun getImage(data: Any, size: IntegerSize?): Bitmap? {
override suspend fun getImage(
data: Any,
size: IntegerSize?
): Bitmap? {
return runCatching {
imageLoader.execute(
ImageRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,20 @@ internal class AndroidImageTransformer @Inject constructor(
}
}

override suspend fun flip(image: Bitmap, isFlipped: Boolean): Bitmap {
override suspend fun flip(
image: Bitmap,
isFlipped: Boolean
): Bitmap {
return if (isFlipped) {
val matrix = Matrix().apply { postScale(-1f, 1f, image.width / 2f, image.height / 2f) }
return Bitmap.createBitmap(image, 0, 0, image.width, image.height, matrix, true)
} else image
}

override suspend fun rotate(image: Bitmap, degrees: Float): Bitmap {
override suspend fun rotate(
image: Bitmap,
degrees: Float
): Bitmap {
return if (degrees % 90 == 0f) {
val matrix = Matrix().apply { postRotate(degrees) }
Bitmap.createBitmap(image, 0, 0, image.width, image.height, matrix, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ internal class AndroidShareProvider @Inject constructor(
onComplete()
}

override suspend fun shareUri(uri: String, type: String?) {
override suspend fun shareUri(
uri: String,
type: String?
) {
val sendIntent = Intent(Intent.ACTION_SEND).apply {
putExtra(Intent.EXTRA_STREAM, uri.toUri())
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@ interface ImageGetter<I, M> {
originalSize: Boolean = true
): ImageData<I, M>?

suspend fun getImage(data: Any, originalSize: Boolean = true): I?
suspend fun getImage(
data: Any,
originalSize: Boolean = true
): I?

suspend fun getImage(data: Any, size: IntegerSize?): I?
suspend fun getImage(
data: Any,
size: IntegerSize?
): I?

fun getExtension(
uri: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,15 @@ interface ImageTransformer<I> {
size: IntegerSize
): I?

suspend fun rotate(image: I, degrees: Float): I
suspend fun rotate(
image: I,
degrees: Float
): I

suspend fun flip(image: I, isFlipped: Boolean): I
suspend fun flip(
image: I,
isFlipped: Boolean
): I

suspend fun applyPresetBy(
image: I?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ interface ShareProvider<I> {
onComplete: () -> Unit = {}
)

suspend fun shareUri(uri: String, type: String?)
suspend fun shareUri(
uri: String,
type: String?
)

suspend fun shareImageUris(uris: List<String>)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ interface Transformation<T> {
* @param size The size of the image request.
* @return The transformed [T].
*/
suspend fun transform(input: T, size: IntegerSize): T
suspend fun transform(
input: T,
size: IntegerSize
): T
}

interface ChainTransformation<T> : Transformation<T> {
Expand All @@ -61,5 +64,8 @@ class GenericTransformation<T>(
override val cacheKey: String
get() = (action to Random.nextInt()).hashCode().toString()

override suspend fun transform(input: T, size: IntegerSize): T = action(input)
override suspend fun transform(
input: T,
size: IntegerSize
): T = action(input)
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ import ru.tech.imageresizershrinker.core.ui.widget.text.HtmlText
import ru.tech.imageresizershrinker.core.ui.widget.text.TitleItem

@Composable
fun UpdateSheet(changelog: String, tag: String, visible: MutableState<Boolean>) {
fun UpdateSheet(
changelog: String,
tag: String,
visible: MutableState<Boolean>
) {
val context = LocalContext.current

SimpleSheet(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ class ImageInfoTransformation @AssistedInject constructor(
size: IntegerSize
): Bitmap = transform(input, size.asCoil())

override suspend fun transform(input: Bitmap, size: Size): Bitmap {
override suspend fun transform(
input: Bitmap,
size: Size
): Bitmap {
val transformedInput = imageScaler.scaleImage(
image = input,
width = imageInfo.width,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,10 @@ object ContextUtils {
}

/** Save a text into the clipboard. */
fun Context.copyToClipboard(label: String, value: String) {
fun Context.copyToClipboard(
label: String,
value: String
) {
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText(label, value)
clipboard.setPrimaryClip(clip)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ import ru.tech.imageresizershrinker.core.resources.R
import java.util.LinkedList


fun Uri?.toUiPath(context: Context, default: String): String = this?.let { uri ->
fun Uri?.toUiPath(
context: Context,
default: String
): String = this?.let { uri ->
DocumentFile
.fromTreeUri(context, uri)
?.uri?.path?.split(":")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ private val Context.dataStore by preferencesDataStore(

private class PermissionPreference(private val context: Context) {

private fun <T> get(key: Preferences.Key<T>, default: T): T {
private fun <T> get(
key: Preferences.Key<T>,
default: T
): T {
return runBlocking {
context.dataStore.edit {}[key] ?: default
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

sealed class EnhancedFloatingActionButtonType(val size: Dp, val shape: Shape) {
sealed class EnhancedFloatingActionButtonType(
val size: Dp,
val shape: Shape
) {
data object Small : EnhancedFloatingActionButtonType(
size = 40.dp,
shape = RoundedCornerShape(12.dp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ fun LoadingDialog(

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun LoadingDialog(done: Int, left: Int, canCancel: Boolean = true, onCancelLoading: () -> Unit) {
fun LoadingDialog(
done: Int,
left: Int,
canCancel: Boolean = true,
onCancelLoading: () -> Unit
) {
var showWantDismissDialog by remember(canCancel) { mutableStateOf(false) }
BasicAlertDialog(onDismissRequest = { showWantDismissDialog = canCancel }) {
Box(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ import ru.tech.imageresizershrinker.core.ui.widget.text.HtmlText
import ru.tech.imageresizershrinker.core.ui.widget.text.TitleItem

@Composable
fun UpdateSheet(changelog: String, tag: String, visible: MutableState<Boolean>) {
fun UpdateSheet(
changelog: String,
tag: String,
visible: MutableState<Boolean>
) {
val context = LocalContext.current
val scope = rememberCoroutineScope()
val toastHostState = LocalToastHostState.current
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ internal class CipherRepositoryImpl @Inject constructor() : CipherRepository {
return sb.toString()
}

override suspend fun decrypt(data: ByteArray, key: String): ByteArray {
override suspend fun decrypt(
data: ByteArray,
key: String
): ByteArray {
val keySpec = createKey(key)
val cipher = Cipher.getInstance(ENCRYPTION_STANDARD)
cipher.init(
Expand All @@ -65,7 +68,10 @@ internal class CipherRepositoryImpl @Inject constructor() : CipherRepository {
return cipher.doFinal(data)
}

override suspend fun encrypt(data: ByteArray, key: String): ByteArray {
override suspend fun encrypt(
data: ByteArray,
key: String
): ByteArray {
val keySpec = createKey(key)
val cipher = Cipher.getInstance(ENCRYPTION_STANDARD)
cipher.init(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ interface CipherRepository {

fun generateRandomString(len: Int): String

suspend fun decrypt(data: ByteArray, key: String): ByteArray
suspend fun decrypt(
data: ByteArray,
key: String
): ByteArray

suspend fun encrypt(data: ByteArray, key: String): ByteArray
suspend fun encrypt(
data: ByteArray,
key: String
): ByteArray

}
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,10 @@ fun FileCipherScreen(
}

private class CreateDocument : ActivityResultContracts.CreateDocument("*/*") {
override fun createIntent(context: Context, input: String): Intent {
override fun createIntent(
context: Context,
input: String
): Intent {
return super.createIntent(
context = context,
input = input.split("#")[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,11 @@ class FileCipherViewModel @Inject constructor(

fun generateRandomPassword(): String = generateRandomPasswordUseCase(18)

fun shareFile(it: ByteArray, filename: String, onComplete: () -> Unit) {
fun shareFile(
it: ByteArray,
filename: String,
onComplete: () -> Unit
) {
_isSaving.value = false
savingJob?.cancel()
savingJob = viewModelScope.launch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ class CompareViewModel @Inject constructor(
savingJob = it
}

private fun Bitmap.overlay(overlay: Bitmap, percent: Float): Bitmap {
private fun Bitmap.overlay(
overlay: Bitmap,
percent: Float
): Bitmap {
val finalBitmap = overlay.copy(overlay.config, true).apply { setHasAlpha(true) }
val canvas = android.graphics.Canvas(finalBitmap)
val image = createScaledBitmap(canvas.width, canvas.height)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ class CropViewModel @Inject constructor(
private val _isSaving: MutableState<Boolean> = mutableStateOf(false)
val isSaving: Boolean by _isSaving

fun updateBitmap(bitmap: Bitmap?, newBitmap: Boolean = false) {
fun updateBitmap(
bitmap: Bitmap?,
newBitmap: Boolean = false
) {
viewModelScope.launch {
_isImageLoading.value = true
val bmp = imageScaler.scaleUntilCanShow(bitmap)
Expand Down Expand Up @@ -187,7 +190,10 @@ class CropViewModel @Inject constructor(
_isImageLoading.value = false
}

fun setUri(uri: Uri, onError: (Throwable) -> Unit) {
fun setUri(
uri: Uri,
onError: (Throwable) -> Unit
) {
_uri.value = uri
imageGetter.getImageAsync(
uri = uri.toString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,11 @@ class DrawViewModel @Inject constructor(
_backgroundColor.value = Color.Transparent
}

fun startDrawOnBackground(reqWidth: Int, reqHeight: Int, color: Color) {
fun startDrawOnBackground(
reqWidth: Int,
reqHeight: Int,
color: Color
) {
val width = reqWidth.takeIf { it > 0 } ?: 1
val height = reqHeight.takeIf { it > 0 } ?: 1
val imageRatio = width / height.toFloat()
Expand Down
Loading

0 comments on commit 70d3bab

Please sign in to comment.