Skip to content

Commit

Permalink
Started the migration of the image optimizer to compose
Browse files Browse the repository at this point in the history
  • Loading branch information
D4rK7355608 committed Jul 10, 2024
1 parent 6002ccd commit 20fdd91
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 79 deletions.
6 changes: 3 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,16 @@
android:parentActivityName=".MainActivity" />

<activity
android:name=".ui.imageoptimizer.ImageOptimizerActivity"
android:name=".ui.imageoptimizer.imageoptimizer.ImageOptimizerActivity"
android:exported="true"
android:label="@string/image_optimizer"
android:parentActivityName=".MainActivity" />

<activity
android:name=".ui.imageoptimizer.ImagePickerActivity"
android:name=".ui.imageoptimizer.imagepicker.ImagePickerActivity"
android:exported="true"
android:label="@string/image_optimizer"
android:parentActivityName=".ui.imageoptimizer.ImageOptimizerActivity" />
android:parentActivityName=".ui.imageoptimizer.imageoptimizer.ImageOptimizerActivity" />

<activity
android:name=".ui.settings.SettingsActivity"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/kotlin/com/d4rk/cleaner/MainComposable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import com.d4rk.cleaner.data.model.ui.navigation.NavigationDrawerItem
import com.d4rk.cleaner.ui.appmanager.AppManagerComposable
import com.d4rk.cleaner.ui.help.HelpActivity
import com.d4rk.cleaner.ui.home.HomeComposable
import com.d4rk.cleaner.ui.imageoptimizer.ImagePickerActivity
import com.d4rk.cleaner.ui.imageoptimizer.imagepicker.ImagePickerActivity
import com.d4rk.cleaner.ui.memory.MemoryManagerComposable
import com.d4rk.cleaner.ui.settings.SettingsActivity
import com.d4rk.cleaner.ui.support.SupportActivity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package com.d4rk.cleaner.adapters
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.d4rk.cleaner.ui.imageoptimizer.FileSizeFragment
import com.d4rk.cleaner.ui.imageoptimizer.ManualModeFragment
import com.d4rk.cleaner.ui.imageoptimizer.QuickCompressFragment
import com.d4rk.cleaner.ui.imageoptimizer.imageoptimizer.tabs.FileSizeFragment
import com.d4rk.cleaner.ui.imageoptimizer.imageoptimizer.tabs.ManualModeFragment
import com.d4rk.cleaner.ui.imageoptimizer.imageoptimizer.tabs.QuickCompressFragment

class ImageOptimizationPagerAdapter(fragmentActivity: FragmentActivity) :
FragmentStateAdapter(fragmentActivity) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.d4rk.cleaner.ui.imageoptimizer
package com.d4rk.cleaner.ui.imageoptimizer.imageoptimizer

import android.content.Context
import android.graphics.Bitmap
Expand All @@ -16,6 +16,9 @@ import com.bumptech.glide.Glide
import com.d4rk.cleaner.R
import com.d4rk.cleaner.adapters.ImageOptimizationPagerAdapter
import com.d4rk.cleaner.databinding.ActivityImageOptimizerBinding
import com.d4rk.cleaner.ui.imageoptimizer.imageoptimizer.tabs.FileSizeFragment
import com.d4rk.cleaner.ui.imageoptimizer.imageoptimizer.tabs.ManualModeFragment
import com.d4rk.cleaner.ui.imageoptimizer.imageoptimizer.tabs.QuickCompressFragment
import com.d4rk.cleaner.ui.viewmodel.ImageOptimizerViewModel
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.MobileAds
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.d4rk.cleaner.ui.imageoptimizer
package com.d4rk.cleaner.ui.imageoptimizer.imageoptimizer.tabs

import android.os.Bundle
import android.view.LayoutInflater
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.d4rk.cleaner.ui.imageoptimizer
package com.d4rk.cleaner.ui.imageoptimizer.imageoptimizer.tabs

import android.os.Bundle
import android.view.LayoutInflater
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.d4rk.cleaner.ui.imageoptimizer
package com.d4rk.cleaner.ui.imageoptimizer.imageoptimizer.tabs

import android.os.Bundle
import android.view.LayoutInflater
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.d4rk.cleaner.ui.imageoptimizer.imagepicker

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import androidx.lifecycle.ViewModelProvider
import com.d4rk.cleaner.R
import com.d4rk.cleaner.ui.settings.display.theme.style.AppTheme

class ImagePickerActivity : AppCompatActivity() {
private lateinit var viewModel: ImagePickerViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

viewModel = ViewModelProvider(this)[ImagePickerViewModel::class.java]

enableEdgeToEdge()
setContent {
AppTheme {
Surface(
modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background
) {
ImagePickerComposable(this, viewModel)
}
}
}
}

private val launcher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
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)
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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<Uri?>(null)
val selectedImageUri: Uri? get() = _selectedImageUri.value

fun setSelectedImageUri(uri: Uri?) {
_selectedImageUri.value = uri
}
}

0 comments on commit 20fdd91

Please sign in to comment.