From 20fdd911a5e167a3faac10791104928f10930f55 Mon Sep 17 00:00:00 2001 From: D4rK7355608 Date: Thu, 11 Jul 2024 00:35:21 +0300 Subject: [PATCH] Started the migration of the image optimizer to compose --- app/src/main/AndroidManifest.xml | 6 +- .../kotlin/com/d4rk/cleaner/MainComposable.kt | 2 +- .../adapters/ImageOptimizationPagerAdapter.kt | 6 +- .../ui/imageoptimizer/ImagePickerActivity.kt | 68 ------------- .../ImageOptimizerActivity.kt | 5 +- .../tabs}/FileSizeFragment.kt | 2 +- .../tabs}/ManualModeFragment.kt | 2 +- .../tabs}/QuickCompressFragment.kt | 2 +- .../imagepicker/ImagePickerActivity.kt | 51 ++++++++++ .../imagepicker/ImagePickerComposable.kt | 97 +++++++++++++++++++ .../imagepicker/ImagePickerViewModel.kt | 14 +++ 11 files changed, 176 insertions(+), 79 deletions(-) delete mode 100644 app/src/main/kotlin/com/d4rk/cleaner/ui/imageoptimizer/ImagePickerActivity.kt rename app/src/main/kotlin/com/d4rk/cleaner/ui/imageoptimizer/{ => imageoptimizer}/ImageOptimizerActivity.kt (97%) rename app/src/main/kotlin/com/d4rk/cleaner/ui/imageoptimizer/{ => imageoptimizer/tabs}/FileSizeFragment.kt (94%) rename app/src/main/kotlin/com/d4rk/cleaner/ui/imageoptimizer/{ => imageoptimizer/tabs}/ManualModeFragment.kt (96%) rename app/src/main/kotlin/com/d4rk/cleaner/ui/imageoptimizer/{ => imageoptimizer/tabs}/QuickCompressFragment.kt (97%) create mode 100644 app/src/main/kotlin/com/d4rk/cleaner/ui/imageoptimizer/imagepicker/ImagePickerActivity.kt create mode 100644 app/src/main/kotlin/com/d4rk/cleaner/ui/imageoptimizer/imagepicker/ImagePickerComposable.kt create mode 100644 app/src/main/kotlin/com/d4rk/cleaner/ui/imageoptimizer/imagepicker/ImagePickerViewModel.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 5f9ab34..b2a83b2 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -74,16 +74,16 @@ android:parentActivityName=".MainActivity" /> + android:parentActivityName=".ui.imageoptimizer.imageoptimizer.ImageOptimizerActivity" /> + if (result.resultCode == Activity.RESULT_OK && result.data != null) { + val selectedImageUri = result.data?.data + viewModel.setSelectedImageUri(selectedImageUri) + } + } + + fun selectImage() { + val intent = Intent(Intent.ACTION_PICK) + intent.type = "image/*" + val chooser = Intent.createChooser(intent, getString(R.string.select_image)) + launcher.launch(chooser) + } +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/d4rk/cleaner/ui/imageoptimizer/imagepicker/ImagePickerComposable.kt b/app/src/main/kotlin/com/d4rk/cleaner/ui/imageoptimizer/imagepicker/ImagePickerComposable.kt new file mode 100644 index 0000000..5f04dca --- /dev/null +++ b/app/src/main/kotlin/com/d4rk/cleaner/ui/imageoptimizer/imagepicker/ImagePickerComposable.kt @@ -0,0 +1,97 @@ +package com.d4rk.cleaner.ui.imageoptimizer.imagepicker + +import android.content.Intent +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.padding +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.automirrored.filled.ArrowBack +import androidx.compose.material.icons.outlined.ImageSearch +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.ExtendedFloatingActionButton +import androidx.compose.material3.Icon +import androidx.compose.material3.IconButton +import androidx.compose.material3.LargeTopAppBar +import androidx.compose.material3.Scaffold +import androidx.compose.material3.Text +import androidx.compose.material3.TopAppBarDefaults +import androidx.compose.material3.rememberTopAppBarState +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.input.nestedscroll.nestedScroll +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp +import androidx.constraintlayout.compose.ConstraintLayout +import com.d4rk.cleaner.R +import com.d4rk.cleaner.ads.BannerAdsComposable +import com.d4rk.cleaner.data.datastore.DataStore +import com.d4rk.cleaner.ui.imageoptimizer.imageoptimizer.ImageOptimizerActivity +import com.d4rk.cleaner.utils.compose.bounceClick + +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun ImagePickerComposable( + activity: ImagePickerActivity, + viewModel: ImagePickerViewModel +) { + val context = LocalContext.current + val dataStore = DataStore.getInstance(context) + val scrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior(rememberTopAppBarState()) + Scaffold(modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection), topBar = { + LargeTopAppBar( + title = { Text(stringResource(R.string.image_optimizer)) }, + navigationIcon = { + IconButton(onClick = { + activity.finish() + }) { + Icon(Icons.AutoMirrored.Filled.ArrowBack, contentDescription = null) + } + }, + scrollBehavior = scrollBehavior + ) + }) { paddingValues -> + ConstraintLayout( + modifier = Modifier + .fillMaxSize() + .padding(paddingValues) + ) { + val (fab, adView) = createRefs() + + ExtendedFloatingActionButton( + modifier = Modifier + .padding(16.dp) + .bounceClick() + .constrainAs(fab) { + end.linkTo(parent.end) + bottom.linkTo(adView.top) + }, + text = { Text(stringResource(R.string.choose_image)) }, + onClick = { + activity.selectImage() + }, + icon = { + Icon( + Icons.Outlined.ImageSearch, contentDescription = null + ) + } + ) + + BannerAdsComposable( + modifier = Modifier + .constrainAs(adView) { + bottom.linkTo(parent.bottom) + start.linkTo(parent.start) + end.linkTo(parent.end) + }, + dataStore = dataStore + ) + } + } + + viewModel.selectedImageUri?.let { uri -> + val intent = Intent(context, ImageOptimizerActivity::class.java) + intent.putExtra("imageUri", uri.toString()) + activity.startActivity(intent) + viewModel.setSelectedImageUri(null) + } +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/d4rk/cleaner/ui/imageoptimizer/imagepicker/ImagePickerViewModel.kt b/app/src/main/kotlin/com/d4rk/cleaner/ui/imageoptimizer/imagepicker/ImagePickerViewModel.kt new file mode 100644 index 0000000..b6bfb5f --- /dev/null +++ b/app/src/main/kotlin/com/d4rk/cleaner/ui/imageoptimizer/imagepicker/ImagePickerViewModel.kt @@ -0,0 +1,14 @@ +package com.d4rk.cleaner.ui.imageoptimizer.imagepicker + +import android.net.Uri +import androidx.compose.runtime.mutableStateOf +import androidx.lifecycle.ViewModel + +class ImagePickerViewModel : ViewModel() { + private val _selectedImageUri = mutableStateOf(null) + val selectedImageUri: Uri? get() = _selectedImageUri.value + + fun setSelectedImageUri(uri: Uri?) { + _selectedImageUri.value = uri + } +} \ No newline at end of file