Skip to content

Commit

Permalink
Moving forward with the image optimizer
Browse files Browse the repository at this point in the history
  • Loading branch information
D4rK7355608 committed Jul 12, 2024
1 parent ee7eecc commit d9ef199
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 304 deletions.
8 changes: 1 addition & 7 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ android {
applicationId = "com.d4rk.cleaner"
minSdk = 26
targetSdk = 34
versionCode = 93
versionCode = 94
versionName = "2.0.0"
archivesName = "${applicationId}-v${versionName}"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
Expand Down Expand Up @@ -151,10 +151,4 @@ dependencies {
androidTestImplementation(libs.androidx.ui.test.junit4)
debugImplementation(libs.androidx.ui.tooling)
debugImplementation(libs.androidx.ui.test.manifest)



// TODO: Clean-up
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("androidx.gridlayout:gridlayout:1.0.0")
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ data class ImageOptimizerState(
val manualWidth: Int = 0,
val manualHeight: Int = 0,
val manualQuality: Int = 50,
val currentTab: Int = 0
val currentTab: Int = 0,
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,30 @@ import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.LargeTopAppBar
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Tab
import androidx.compose.material3.TabRow
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.rememberTopAppBarState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.constraintlayout.compose.ConstraintLayout
Expand Down Expand Up @@ -140,8 +141,10 @@ fun ImageOptimizerComposable(activity: ImageOptimizerActivity, viewModel: ImageO
}
}

Button(
onClick = { /* Handle Compress button click */ },
OutlinedButton(
onClick = {

},
modifier = Modifier
.constrainAs(compressButton) {
start.linkTo(parent.start)
Expand Down Expand Up @@ -173,25 +176,33 @@ fun ImageOptimizerComposable(activity: ImageOptimizerActivity, viewModel: ImageO

@Composable
fun ImageDisplay(viewModel: ImageOptimizerViewModel) {
val state by viewModel.uiState.collectAsState()
val state = viewModel.uiState.collectAsState()
val showCompressedImage = remember { mutableStateOf(false) }

LaunchedEffect(key1 = state.value.compressedImageUri) {
if (state.value.compressedImageUri != null) {
showCompressedImage.value = true
}
}

Box(
modifier = Modifier
.fillMaxWidth()
.aspectRatio(1f),
contentAlignment = Alignment.Center
) {
if (state.isLoading) {
if (state.value.isLoading) {
CircularProgressIndicator()
} else {
state.compressedImageUri?.let { imageUri ->
AsyncImage(
model = imageUri,
contentDescription = "Selected Image",
modifier = Modifier.fillMaxSize(),
contentScale = ContentScale.Crop,
placeholder = painterResource(id = R.drawable.ic_image),
error = painterResource(id = R.drawable.ic_image)
)
if (showCompressedImage.value) {
state.value.compressedImageUri?.let { imageUri ->
AsyncImage(
model = imageUri,
contentDescription = "Selected Image",
modifier = Modifier.fillMaxSize(),
contentScale = ContentScale.Crop,
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import android.graphics.Bitmap
import android.net.Uri
import android.provider.OpenableColumns
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.d4rk.cleaner.data.model.ui.imageoptimizer.ImageOptimizerState
import id.zelory.compressor.Compressor
Expand All @@ -26,17 +25,11 @@ import java.io.FileOutputStream

class ImageOptimizerViewModel(application: Application) : AndroidViewModel(application) {

val _uiState = MutableStateFlow(ImageOptimizerState())
private val _uiState = MutableStateFlow(ImageOptimizerState())
val uiState = _uiState.asStateFlow()
private val compressionLevelLiveData = MutableLiveData<Int>()

private fun getAppContext(): Context = getApplication<Application>().applicationContext


fun setCompressionLevel(compressionLevel: Int) {
compressionLevelLiveData.value = compressionLevel
}

suspend fun setQuickCompressValue(value: Int) {
_uiState.emit(_uiState.value.copy(quickCompressValue = value))
compressImage()
Expand Down Expand Up @@ -64,19 +57,16 @@ class ImageOptimizerViewModel(application: Application) : AndroidViewModel(appli

}


private fun compressImage() = viewModelScope.launch {
_uiState.emit(_uiState.value.copy(isLoading = true))
val context = getAppContext()
val originalFile = _uiState.value.selectedImageUri?.let { getRealFileFromUri(context, it) }

// Fetch the current tab index BEFORE the suspension point
val currentTab = _uiState.value.currentTab // Access _uiState.value here
val currentTab = _uiState.value.currentTab
val compressedFile = originalFile?.let { file ->
withContext(Dispatchers.IO) {
try {
Compressor.compress(context, file) {
when (currentTab) { // Use the fetched currentTab value
when (currentTab) {
0 -> {
quality(_uiState.value.quickCompressValue)
format(Bitmap.CompressFormat.JPEG)
Expand Down Expand Up @@ -106,12 +96,11 @@ class ImageOptimizerViewModel(application: Application) : AndroidViewModel(appli
_uiState.value.copy(
isLoading = false,
compressedImageUri = compressedFile?.let { Uri.fromFile(it) }
?: _uiState.value.selectedImageUri
?: _uiState.value.selectedImageUri,
)
)
}

// Now a member function of the ViewModel
private fun getRealFileFromUri(context: Context, uri: Uri): File? {
if (uri.scheme == "content") {
val cursor: Cursor? = context.contentResolver.query(uri, null, null, null, null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,16 @@ fun ManualModeScreen(viewModel: ImageOptimizerViewModel) {

Spacer(modifier = Modifier.height(4.dp))

Row(modifier = Modifier.fillMaxWidth(),
Row(
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically
) {
Text(text = stringResource(R.string.image_compressor_percentage_format, qualityValue.toInt()))
Text(
text = stringResource(
R.string.image_compressor_percentage_format,
qualityValue.toInt()
)
)
Slider(
value = qualityValue,
onValueChange = { newValue ->
Expand All @@ -101,7 +107,6 @@ fun ManualModeScreen(viewModel: ImageOptimizerViewModel) {
)
}
},
// ... rest of the Slider parameters ...
)
}
}
Expand Down
76 changes: 0 additions & 76 deletions app/src/main/res/layout/activity_image_optimizer.xml

This file was deleted.

29 changes: 0 additions & 29 deletions app/src/main/res/layout/fragment_file_size.xml

This file was deleted.

Loading

0 comments on commit d9ef199

Please sign in to comment.