Skip to content

Commit

Permalink
refactoring code to clean arch 3
Browse files Browse the repository at this point in the history
  • Loading branch information
T8RIN committed Jul 30, 2023
1 parent 712e6a8 commit 8f99cb9
Show file tree
Hide file tree
Showing 26 changed files with 495 additions and 520 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ class AndroidImageManager @Inject constructor(
}
}

override suspend fun getImage(data: Any, originalSize: Boolean): Bitmap? {
return loader().execute(
ImageRequest
.Builder(context)
.data(data)
.apply {
if (originalSize) size(Size.ORIGINAL)
}
.build()
).drawable?.toBitmap()
}

override fun getImageAsync(
uri: String,
originalSize: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class FileControllerImpl @Inject constructor(
if (it.isFailure) {
return SaveResult.Error.Exception(it.exceptionOrNull() ?: Throwable())
} else {
return SaveResult.Success(savingPath, filename)
return SaveResult.Success(filename = filename, savingPath = savingPath)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,6 @@ interface ImageManager<I, M> {
onComplete: () -> Unit
)

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -177,23 +177,20 @@ fun BatchResizeScreen(
pickImageLauncher.pickImage()
}

var showSaveLoading by rememberSaveable { mutableStateOf(false) }
val saveBitmaps: () -> Unit = {
showSaveLoading = true
viewModel.saveBitamps { savingPath ->
if (savingPath.isNotEmpty()) {
scope.launch {
toastHostState.showToast(
context.getString(
R.string.saved_to,
R.string.saved_to_without_filename,
savingPath
),
Icons.Rounded.Save
)
}
showConfetti()
}
showSaveLoading = false
}
}

Expand Down Expand Up @@ -240,7 +237,7 @@ fun BatchResizeScreen(
showOriginal = showOriginal,
previewBitmap = viewModel.previewBitmap,
originalBitmap = viewModel.bitmap,
isLoading = viewModel.isLoading,
isLoading = viewModel.isImageLoading,
shouldShowPreview = viewModel.shouldShowPreview
)
}
Expand All @@ -254,11 +251,7 @@ fun BatchResizeScreen(

IconButton(
onClick = {
showSaveLoading = true
viewModel.shareBitmaps {
showConfetti()
showSaveLoading = false
}
viewModel.shareBitmaps { showConfetti() }
},
enabled = viewModel.previewBitmap != null
) {
Expand Down Expand Up @@ -388,7 +381,7 @@ fun BatchResizeScreen(
TopAppBarTitle(
title = stringResource(R.string.batch_resize),
bitmap = viewModel.bitmap,
isLoading = viewModel.isLoading,
isLoading = viewModel.isImageLoading,
size = viewModel.imageInfo.sizeInBytes.toLong()
)
},
Expand Down Expand Up @@ -463,7 +456,7 @@ fun BatchResizeScreen(
) {
if (viewModel.bitmap != null) {
ImageCounter(
imageCount = viewModel.uris?.size?.takeIf { it > 1 && !viewModel.isLoading },
imageCount = viewModel.uris?.size?.takeIf { it > 1 && !viewModel.isImageLoading },
onRepick = {
showPickImageFromUrisDialog = true
}
Expand Down Expand Up @@ -514,7 +507,7 @@ fun BatchResizeScreen(
resizeType = bitmapInfo.resizeType,
onResizeChange = viewModel::setResizeType
)
} else if (!viewModel.isLoading) {
} else if (!viewModel.isImageLoading) {
ImageNotPickedWidget(onPickImage = pickImage)
}
}
Expand Down Expand Up @@ -582,7 +575,7 @@ fun BatchResizeScreen(
visible = showExitDialog
)

if (showSaveLoading) {
if (viewModel.isSaving) {
LoadingDialog(viewModel.done, viewModel.uris?.size ?: 1)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import ru.tech.imageresizershrinker.domain.model.ImageFormat
import ru.tech.imageresizershrinker.domain.model.ImageInfo
import ru.tech.imageresizershrinker.domain.model.ResizeType
import ru.tech.imageresizershrinker.domain.saving.FileController
import ru.tech.imageresizershrinker.domain.saving.SaveResult
import ru.tech.imageresizershrinker.domain.saving.model.ImageSaveTarget
import javax.inject.Inject

Expand All @@ -42,8 +43,11 @@ class BatchResizeViewModel @Inject constructor(
private val _imageInfo: MutableState<ImageInfo> = mutableStateOf(ImageInfo())
val imageInfo: ImageInfo by _imageInfo

private val _isLoading: MutableState<Boolean> = mutableStateOf(false)
val isLoading: Boolean by _isLoading
private val _isImageLoading: MutableState<Boolean> = mutableStateOf(false)
val isImageLoading: Boolean by _isImageLoading

private val _isSaving: MutableState<Boolean> = mutableStateOf(false)
val isSaving: Boolean by _isSaving

private val _shouldShowPreview: MutableState<Boolean> = mutableStateOf(true)
val shouldShowPreview by _shouldShowPreview
Expand Down Expand Up @@ -118,9 +122,9 @@ class BatchResizeViewModel @Inject constructor(
_isTelegramSpecs.value = false
}
job?.cancel()
_isLoading.value = false
_isImageLoading.value = false
job = viewModelScope.launch {
_isLoading.value = true
_isImageLoading.value = true
delay(600)
_bitmap.value?.let { bmp ->
val preview = updatePreview(bmp)
Expand All @@ -135,7 +139,7 @@ class BatchResizeViewModel @Inject constructor(
) else this
}
}
_isLoading.value = false
_isImageLoading.value = false
}
}

Expand Down Expand Up @@ -279,43 +283,43 @@ class BatchResizeViewModel @Inject constructor(
fun saveBitamps(
onComplete: (path: String) -> Unit
) = viewModelScope.launch {
_isSaving.value = true
withContext(Dispatchers.IO) {
if (!fileController.isExternalStorageWritable()) {
onComplete("")
fileController.requestReadWritePermissions()
} else {
_done.value = 0
uris?.forEach { uri ->
runCatching {
imageManager.getImage(uri.toString())?.image
}.getOrNull()?.let { bitmap ->
imageInfo.let {
imageManager.applyPresetBy(
image = bitmap,
preset = _presetSelected.value,
currentInfo = it
)
}.apply {
fileController.save(
ImageSaveTarget<ExifInterface>(
imageInfo = this,
originalUri = uri.toString(),
sequenceNumber = _done.value + 1,
data = imageManager.compress(
ImageData(
bitmap,
imageInfo
)
_done.value = 0
uris?.forEach { uri ->
runCatching {
imageManager.getImage(uri.toString())?.image
}.getOrNull()?.let { bitmap ->
imageInfo.let {
imageManager.applyPresetBy(
image = bitmap,
preset = _presetSelected.value,
currentInfo = it
)
}.apply {
val result = fileController.save(
ImageSaveTarget<ExifInterface>(
imageInfo = this,
originalUri = uri.toString(),
sequenceNumber = _done.value + 1,
data = imageManager.compress(
ImageData(
bitmap,
imageInfo
)
), keepExif
)
)
), keepExif
)
if (result is SaveResult.Error.MissingPermissions) {
return@withContext onComplete("")
}
}
_done.value += 1
}
onComplete(fileController.savingPath)
_done.value += 1
}
onComplete(fileController.savingPath)
}
_isSaving.value = false
}

fun setBitmap(uri: Uri) {
Expand Down Expand Up @@ -358,6 +362,7 @@ class BatchResizeViewModel @Inject constructor(

fun shareBitmaps(onComplete: () -> Unit) {
viewModelScope.launch {
_isSaving.value = true
imageManager.shareImages(
uris = uris?.map { it.toString() } ?: emptyList(),
imageLoader = { uri ->
Expand All @@ -366,6 +371,7 @@ class BatchResizeViewModel @Inject constructor(
onProgressChange = {
if (it == -1) {
onComplete()
_isSaving.value = false
_done.value = 0
} else {
_done.value = it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ fun BytesResizeScreen(
pickImageLauncher.pickImage()
}

var showSaveLoading by rememberSaveable { mutableStateOf(false) }
var showExitDialog by rememberSaveable { mutableStateOf(false) }

val onBack = {
Expand All @@ -197,7 +196,6 @@ fun BytesResizeScreen(


val saveBitmaps: () -> Unit = {
showSaveLoading = true
viewModel.saveBitmaps { failed, savingPath ->
context.failedToSaveImages(
scope = scope,
Expand All @@ -207,7 +205,6 @@ fun BytesResizeScreen(
savingPathString = savingPath,
showConfetti = showConfetti
)
showSaveLoading = false
}
}

Expand Down Expand Up @@ -266,7 +263,7 @@ fun BytesResizeScreen(
showOriginal = false,
previewBitmap = viewModel.previewBitmap,
originalBitmap = viewModel.bitmap,
isLoading = viewModel.isLoading,
isLoading = viewModel.isImageLoading,
shouldShowPreview = true
)
}
Expand Down Expand Up @@ -326,7 +323,7 @@ fun BytesResizeScreen(
TopAppBarTitle(
title = stringResource(R.string.by_bytes_resize),
bitmap = viewModel.bitmap,
isLoading = viewModel.isLoading,
isLoading = viewModel.isImageLoading,
size = viewModel.selectedUri?.fileSize(LocalContext.current) ?: 0L
)
},
Expand All @@ -350,11 +347,7 @@ fun BytesResizeScreen(
if (viewModel.previewBitmap != null) {
IconButton(
onClick = {
showSaveLoading = true
viewModel.shareBitmaps {
showConfetti()
showSaveLoading = false
}
viewModel.shareBitmaps { showConfetti() }
},
enabled = viewModel.canSave
) {
Expand Down Expand Up @@ -418,7 +411,7 @@ fun BytesResizeScreen(
if (imageInside && viewModel.bitmap == null) imageBlock()
if (viewModel.bitmap != null) {
ImageCounter(
imageCount = viewModel.uris?.size?.takeIf { it > 1 && !viewModel.isLoading },
imageCount = viewModel.uris?.size?.takeIf { it > 1 && !viewModel.isImageLoading },
onRepick = {
showPickImageFromUrisDialog = true
}
Expand Down Expand Up @@ -472,7 +465,7 @@ fun BytesResizeScreen(
imageFormat = viewModel.imageFormat,
onMimeChange = viewModel::setMime
)
} else if (!viewModel.isLoading) {
} else if (!viewModel.isImageLoading) {
ImageNotPickedWidget(onPickImage = pickImage)
Spacer(Modifier.size(8.dp))
}
Expand Down Expand Up @@ -502,7 +495,7 @@ fun BytesResizeScreen(
}
}

if (showSaveLoading) {
if (viewModel.isSaving) {
LoadingDialog(viewModel.done, viewModel.uris?.size ?: 1)
}

Expand Down
Loading

0 comments on commit 8f99cb9

Please sign in to comment.