From 7f891856e2c2de7c203abbe96a8ce053c029b060 Mon Sep 17 00:00:00 2001 From: Himanshu Singh Date: Mon, 18 Jul 2022 23:27:53 +0530 Subject: [PATCH 1/5] Fix. Duplication of Image --- app/build.gradle.kts | 4 ++-- .../main/java/com/himanshoe/app/MainActivity.kt | 3 ++- buildSrc/src/main/kotlin/Versions.kt | 4 ++-- .../com/himanshoe/pluck/data/PluckDataSource.kt | 14 +++++++++----- .../com/himanshoe/pluck/data/PluckRepository.kt | 7 +++++-- .../java/com/himanshoe/pluck/theme/PluckDimens.kt | 2 ++ .../src/main/java/com/himanshoe/pluck/ui/Pluck.kt | 4 ++-- .../java/com/himanshoe/pluck/ui/PluckViewModel.kt | 2 +- .../com/himanshoe/pluck/util/PluckUriManager.kt | 5 ++--- 9 files changed, 27 insertions(+), 18 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index e24d067..251ae7a 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -38,8 +38,8 @@ android { } dependencies { -// implementation(project(":pluck")) - implementation("com.himanshoe:pluck:1.0.0-RC1") + implementation(project(":pluck")) +// implementation("com.himanshoe:pluck:1.0.0-RC1") implementation(Deps.Compose.ui) implementation(Deps.Compose.material) diff --git a/app/src/main/java/com/himanshoe/app/MainActivity.kt b/app/src/main/java/com/himanshoe/app/MainActivity.kt index 7eb3412..66382e2 100644 --- a/app/src/main/java/com/himanshoe/app/MainActivity.kt +++ b/app/src/main/java/com/himanshoe/app/MainActivity.kt @@ -44,7 +44,8 @@ class MainActivity : ComponentActivity() { ), goToAppSettings = { goToAppSettings() } ) { - Pluck(pluckConfiguration = PluckConfiguration(true), + Pluck( + pluckConfiguration = PluckConfiguration(true), onPhotoSelected = { } ) diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt index a6c29ed..9037766 100644 --- a/buildSrc/src/main/kotlin/Versions.kt +++ b/buildSrc/src/main/kotlin/Versions.kt @@ -1,8 +1,8 @@ object Versions { - const val compose = "1.0.5" + const val compose = "1.1.1" const val androidGradlePlugin = "7.0.3" const val vanniktechGradlePlugin = "0.18.0" - const val kotlin = "1.5.31" + const val kotlin = "1.6.10" const val activity = "1.3.1" const val material = "1.4.0" const val jUnit = "4.13.2" diff --git a/pluck/src/main/java/com/himanshoe/pluck/data/PluckDataSource.kt b/pluck/src/main/java/com/himanshoe/pluck/data/PluckDataSource.kt index 9758977..0dc4400 100644 --- a/pluck/src/main/java/com/himanshoe/pluck/data/PluckDataSource.kt +++ b/pluck/src/main/java/com/himanshoe/pluck/data/PluckDataSource.kt @@ -25,21 +25,25 @@ package com.himanshoe.pluck.data import androidx.paging.PagingSource import androidx.paging.PagingState +private const val Zero = 0 +private const val One = 1 + class PluckDataSource(private val onFetch: (limit: Int, offset: Int) -> List) : PagingSource() { + override fun getRefreshKey(state: PagingState): Int? { return state.anchorPosition?.let { - state.closestPageToPosition(it)?.prevKey?.plus(1) - ?: state.closestPageToPosition(it)?.nextKey?.minus(1) + state.closestPageToPosition(it)?.prevKey?.plus(One) + ?: state.closestPageToPosition(it)?.nextKey?.minus(One) } } override suspend fun load(params: LoadParams): LoadResult { - val pageNumber = params.key ?: 0 + val pageNumber = params.key ?: Zero val pageSize = params.loadSize val pictures = onFetch.invoke(pageSize, pageNumber * pageSize) - val prevKey = if (pageNumber > 0) pageNumber - 1 else null - val nextKey = if (pictures.isNotEmpty()) pageNumber + 1 else null + val prevKey = if (pageNumber > Zero) pageNumber.minus(One) else null + val nextKey = if (pictures.isNotEmpty()) pageNumber.plus(One) else null return LoadResult.Page( data = pictures, diff --git a/pluck/src/main/java/com/himanshoe/pluck/data/PluckRepository.kt b/pluck/src/main/java/com/himanshoe/pluck/data/PluckRepository.kt index fb7af79..21d4fbb 100644 --- a/pluck/src/main/java/com/himanshoe/pluck/data/PluckRepository.kt +++ b/pluck/src/main/java/com/himanshoe/pluck/data/PluckRepository.kt @@ -27,6 +27,9 @@ import androidx.paging.PagingSource import com.himanshoe.pluck.util.createCursor import com.himanshoe.pluck.util.fetchPagePicture +private const val Zero = 0 +private const val One = 1 + internal interface PluckRepository { suspend fun getCount(): Int suspend fun getByOffset(offset: Int): PluckImage? @@ -36,14 +39,14 @@ internal interface PluckRepository { internal class PluckRepositoryImpl(private val context: Context) : PluckRepository { override suspend fun getCount(): Int { - val cursor = context.createCursor(Int.MAX_VALUE, 0) ?: return 0 + val cursor = context.createCursor(Int.MAX_VALUE, Zero) ?: return Zero val count = cursor.count cursor.close() return count } override suspend fun getByOffset(offset: Int): PluckImage? { - return context.fetchPagePicture(1, offset).firstOrNull() + return context.fetchPagePicture(One, offset).firstOrNull() } override fun getPicturePagingSource(): PagingSource { diff --git a/pluck/src/main/java/com/himanshoe/pluck/theme/PluckDimens.kt b/pluck/src/main/java/com/himanshoe/pluck/theme/PluckDimens.kt index 9e490cf..09edbc0 100644 --- a/pluck/src/main/java/com/himanshoe/pluck/theme/PluckDimens.kt +++ b/pluck/src/main/java/com/himanshoe/pluck/theme/PluckDimens.kt @@ -26,6 +26,8 @@ import androidx.compose.ui.unit.dp object PluckDimens { val Zero = 0.dp + val Quarter = 2.dp + val Half = 4.dp val One = 8.dp val Three = 24.dp val Six = 48.dp diff --git a/pluck/src/main/java/com/himanshoe/pluck/ui/Pluck.kt b/pluck/src/main/java/com/himanshoe/pluck/ui/Pluck.kt index 7a5348f..847c0a0 100644 --- a/pluck/src/main/java/com/himanshoe/pluck/ui/Pluck.kt +++ b/pluck/src/main/java/com/himanshoe/pluck/ui/Pluck.kt @@ -50,7 +50,6 @@ import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.text.font.FontStyle import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextAlign -import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import androidx.lifecycle.viewmodel.compose.viewModel import androidx.paging.compose.LazyPagingItems @@ -61,6 +60,7 @@ import com.himanshoe.pluck.R import com.himanshoe.pluck.data.PluckImage import com.himanshoe.pluck.data.PluckRepositoryImpl import com.himanshoe.pluck.theme.PluckDimens +import com.himanshoe.pluck.theme.PluckDimens.Quarter import com.himanshoe.pluck.util.PluckUriManager import com.himanshoe.pluck.util.PluckViewModelFactory import kotlinx.coroutines.flow.StateFlow @@ -99,7 +99,7 @@ fun Pluck( icon = { Icon(Icons.Rounded.Check, "") } ) }) { - val modifier = Modifier.padding(2.dp) + val modifier = Modifier.padding(Quarter) val cameraLauncher = rememberLauncherForActivityResult(ActivityResultContracts.TakePicture()) { onPhotoSelected(listOf(pluckViewModel.getPluckImage()) as List) diff --git a/pluck/src/main/java/com/himanshoe/pluck/ui/PluckViewModel.kt b/pluck/src/main/java/com/himanshoe/pluck/ui/PluckViewModel.kt index 7de1d6d..bc2be57 100644 --- a/pluck/src/main/java/com/himanshoe/pluck/ui/PluckViewModel.kt +++ b/pluck/src/main/java/com/himanshoe/pluck/ui/PluckViewModel.kt @@ -74,7 +74,7 @@ internal class PluckViewModel( } fun getCameraImageUri(): Uri? { - uri = pluckUriManager.newUri + uri = pluckUriManager.getNewUri() return uri } } diff --git a/pluck/src/main/java/com/himanshoe/pluck/util/PluckUriManager.kt b/pluck/src/main/java/com/himanshoe/pluck/util/PluckUriManager.kt index 3db3709..2c37aac 100644 --- a/pluck/src/main/java/com/himanshoe/pluck/util/PluckUriManager.kt +++ b/pluck/src/main/java/com/himanshoe/pluck/util/PluckUriManager.kt @@ -39,9 +39,7 @@ internal class PluckUriManager(private val context: Context) { } } - private val resolver by lazy { context.contentResolver } - - val newUri = resolver.insert(photoCollection, setupPhotoDetails()) + fun getNewUri() = context.contentResolver.insert(photoCollection, setupPhotoDetails()) /** * generates a new pluck image @@ -53,6 +51,7 @@ internal class PluckUriManager(private val context: Context) { setupPhotoDetails().getAsString(MediaStore.Images.Media.DISPLAY_NAME), null, null ) } + /** * generates a photo detail */ From 3709511e2182f3dfe15d963ea4f58b1587934184 Mon Sep 17 00:00:00 2001 From: Himanshu Singh Date: Tue, 19 Jul 2022 00:19:09 +0530 Subject: [PATCH 2/5] Changed position for Indicator --- .../com/himanshoe/pluck/theme/PluckTheme.kt | 33 ----------- .../main/java/com/himanshoe/pluck/ui/Pluck.kt | 55 +++++++++++++------ 2 files changed, 37 insertions(+), 51 deletions(-) delete mode 100644 pluck/src/main/java/com/himanshoe/pluck/theme/PluckTheme.kt diff --git a/pluck/src/main/java/com/himanshoe/pluck/theme/PluckTheme.kt b/pluck/src/main/java/com/himanshoe/pluck/theme/PluckTheme.kt deleted file mode 100644 index 3e5bd05..0000000 --- a/pluck/src/main/java/com/himanshoe/pluck/theme/PluckTheme.kt +++ /dev/null @@ -1,33 +0,0 @@ -package com.himanshoe.pluck.theme -/* -* MIT License -* -* Copyright (c) 2022 Himanshu Singh -* -* Permission is hereby granted, free of charge, to any person obtaining a copy -* of this software and associated documentation files (the "Software"), to deal -* in the Software without restriction, including without limitation the rights -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -* copies of the Software, and to permit persons to whom the Software is -* furnished to do so, subject to the following conditions: -* -* The above copyright notice and this permission notice shall be included in all -* copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -* SOFTWARE. -*/ -import androidx.compose.material.MaterialTheme -import androidx.compose.runtime.Composable - -@Composable -fun PluckTheme(content: @Composable () -> Unit) { - MaterialTheme( - content = content - ) -} diff --git a/pluck/src/main/java/com/himanshoe/pluck/ui/Pluck.kt b/pluck/src/main/java/com/himanshoe/pluck/ui/Pluck.kt index 847c0a0..13e2525 100644 --- a/pluck/src/main/java/com/himanshoe/pluck/ui/Pluck.kt +++ b/pluck/src/main/java/com/himanshoe/pluck/ui/Pluck.kt @@ -36,7 +36,9 @@ import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.lazy.GridCells +import androidx.compose.foundation.lazy.LazyGridState import androidx.compose.foundation.lazy.LazyVerticalGrid +import androidx.compose.foundation.lazy.rememberLazyGridState import androidx.compose.material.* import androidx.compose.material.icons.Icons import androidx.compose.material.icons.rounded.Check @@ -76,6 +78,8 @@ fun Pluck( onPhotoSelected: (List) -> Unit, ) { val context = LocalContext.current + val gridState: LazyGridState = rememberLazyGridState() + val pluckViewModel: PluckViewModel = viewModel( factory = PluckViewModelFactory( PluckRepositoryImpl( @@ -104,27 +108,17 @@ fun Pluck( rememberLauncherForActivityResult(ActivityResultContracts.TakePicture()) { onPhotoSelected(listOf(pluckViewModel.getPluckImage()) as List) } + LazyVerticalGrid( + state = gridState, modifier = Modifier.background(MaterialTheme.colors.surface), cells = GridCells.Fixed(THREE) ) { item { - Box( - contentAlignment = Alignment.Center, - modifier = modifier - .size(PluckDimens.Sixteen) - .clickable { handleCamera(pluckViewModel, cameraLauncher) } - .then(Modifier.background(MaterialTheme.colors.background)) - ) { - Image( - painter = rememberImagePainter(R.drawable.ic_camera), - contentDescription = null, - contentScale = ContentScale.Crop, - modifier = Modifier - .size(PluckDimens.Six) - .alpha(0.2F) - ) - } + CameraIcon( + modifier = modifier, + cameraLauncher = cameraLauncher, + pluckViewModel = pluckViewModel) } items(lazyPluckImages.itemCount) { index -> lazyPluckImages[index]?.let { pluckImage -> @@ -146,6 +140,30 @@ fun Pluck( } } +@Composable +internal fun CameraIcon( + modifier: Modifier, + cameraLauncher: ManagedActivityResultLauncher, + pluckViewModel: PluckViewModel, +) { + Box( + contentAlignment = Alignment.Center, + modifier = modifier + .size(PluckDimens.Sixteen) + .clickable { handleCamera(pluckViewModel, cameraLauncher) } + .then(Modifier.background(MaterialTheme.colors.background)) + ) { + Image( + painter = rememberImagePainter(R.drawable.ic_camera), + contentDescription = null, + contentScale = ContentScale.Crop, + modifier = Modifier + .size(PluckDimens.Six) + .alpha(0.2F) + ) + } +} + private fun handleCamera( pluckViewModel: PluckViewModel, onPhotoClicked: ManagedActivityResultLauncher, @@ -180,11 +198,12 @@ internal fun PluckImage( contentScale = ContentScale.Crop, modifier = Modifier.padding(animatedPadding) ) + Box( modifier = Modifier .clickable { if (!pluckConfiguration.multipleImagesAllowed) { - if (images.count() < 1) { + if (images.isEmpty()) { selected.value = !selected.value onSelectedPhoto(pluckImage, selected.value) } else { @@ -197,7 +216,7 @@ internal fun PluckImage( } } .size(PluckDimens.Sixteen), - contentAlignment = Alignment.TopEnd, + contentAlignment = Alignment.BottomEnd, ) { PluckImageIndicator(text = images.indexOf(pluckImage).plus(ONE).toString()) } From d191d29fedf35af282180ea1f8646346d79dde03 Mon Sep 17 00:00:00 2001 From: Himanshu Singh Date: Tue, 19 Jul 2022 00:37:19 +0530 Subject: [PATCH 3/5] Removed animation --- .../com/himanshoe/pluck/theme/PluckDimens.kt | 1 + .../main/java/com/himanshoe/pluck/ui/Pluck.kt | 43 +++++++++---------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/pluck/src/main/java/com/himanshoe/pluck/theme/PluckDimens.kt b/pluck/src/main/java/com/himanshoe/pluck/theme/PluckDimens.kt index 09edbc0..5dcd3c7 100644 --- a/pluck/src/main/java/com/himanshoe/pluck/theme/PluckDimens.kt +++ b/pluck/src/main/java/com/himanshoe/pluck/theme/PluckDimens.kt @@ -26,6 +26,7 @@ import androidx.compose.ui.unit.dp object PluckDimens { val Zero = 0.dp + val HalfQuarter = 1.dp val Quarter = 2.dp val Half = 4.dp val One = 8.dp diff --git a/pluck/src/main/java/com/himanshoe/pluck/ui/Pluck.kt b/pluck/src/main/java/com/himanshoe/pluck/ui/Pluck.kt index 13e2525..ba4d006 100644 --- a/pluck/src/main/java/com/himanshoe/pluck/ui/Pluck.kt +++ b/pluck/src/main/java/com/himanshoe/pluck/ui/Pluck.kt @@ -26,13 +26,12 @@ import android.net.Uri import androidx.activity.compose.ManagedActivityResultLauncher import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.result.contract.ActivityResultContracts -import androidx.compose.animation.core.animateDp -import androidx.compose.animation.core.updateTransition import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.Image import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.lazy.GridCells @@ -46,7 +45,7 @@ import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.alpha -import androidx.compose.ui.draw.drawBehind +import androidx.compose.ui.graphics.Color import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.text.font.FontStyle @@ -62,7 +61,7 @@ import com.himanshoe.pluck.R import com.himanshoe.pluck.data.PluckImage import com.himanshoe.pluck.data.PluckRepositoryImpl import com.himanshoe.pluck.theme.PluckDimens -import com.himanshoe.pluck.theme.PluckDimens.Quarter +import com.himanshoe.pluck.theme.PluckDimens.HalfQuarter import com.himanshoe.pluck.util.PluckUriManager import com.himanshoe.pluck.util.PluckViewModelFactory import kotlinx.coroutines.flow.StateFlow @@ -103,7 +102,7 @@ fun Pluck( icon = { Icon(Icons.Rounded.Check, "") } ) }) { - val modifier = Modifier.padding(Quarter) + val modifier = Modifier.padding(HalfQuarter) val cameraLauncher = rememberLauncherForActivityResult(ActivityResultContracts.TakePicture()) { onPhotoSelected(listOf(pluckViewModel.getPluckImage()) as List) @@ -181,11 +180,7 @@ internal fun PluckImage( ) { val selected = remember { mutableStateOf(false) } val images by selectedImages.collectAsState(initial = emptyList()) - val transition = updateTransition(selected.value, label = "change-padding") - - val animatedPadding by transition.animateDp(label = "change-padding") { isSelected -> - if (isSelected) PluckDimens.One else PluckDimens.Zero - } + val backgroundColor = if (selected.value) Color.Black else Color.Transparent Box( modifier = modifier @@ -196,11 +191,11 @@ internal fun PluckImage( painter = rememberImagePainter(pluckImage.uri), contentDescription = null, contentScale = ContentScale.Crop, - modifier = Modifier.padding(animatedPadding) + modifier = modifier ) Box( - modifier = Modifier + modifier = modifier .clickable { if (!pluckConfiguration.multipleImagesAllowed) { if (images.isEmpty()) { @@ -215,32 +210,34 @@ internal fun PluckImage( onSelectedPhoto(pluckImage, selected.value) } } - .size(PluckDimens.Sixteen), - contentAlignment = Alignment.BottomEnd, + .fillMaxSize() + .alpha(0.5F) + .background(color = backgroundColor), ) { - PluckImageIndicator(text = images.indexOf(pluckImage).plus(ONE).toString()) + PluckImageIndicator( + modifier = modifier, + text = images.indexOf(pluckImage).plus(ONE).toString()) } } } @Composable -internal fun PluckImageIndicator(text: String) { +internal fun PluckImageIndicator(modifier: Modifier = Modifier, text: String) { if (text.toInt() > 0) { - val backgroundColor = MaterialTheme.colors.primary val textColor = MaterialTheme.colors.onPrimary Text( text = text, - textAlign = TextAlign.Center, + textAlign = TextAlign.End, color = textColor, fontStyle = FontStyle.Normal, fontWeight = FontWeight.SemiBold, - fontSize = 12.sp, - modifier = Modifier - .drawBehind { - drawCircle(backgroundColor) - } + fontSize = 16.sp, + modifier = modifier + .fillMaxSize() .padding(PluckDimens.One) + + ) } } From 4fc4f8ae6886b3df8d74079c525cfb7adec33814 Mon Sep 17 00:00:00 2001 From: Himanshu Singh Date: Tue, 19 Jul 2022 11:40:40 +0530 Subject: [PATCH 4/5] Upgraded to Material you --- app/build.gradle.kts | 2 +- buildSrc/src/main/kotlin/Dependencies.kt | 2 + buildSrc/src/main/kotlin/ModuleExtension.kt | 4 +- buildSrc/src/main/kotlin/Versions.kt | 2 +- pluck/build.gradle.kts | 2 +- .../main/java/com/himanshoe/pluck/ui/Pluck.kt | 71 +++++++++++-------- .../pluck/ui/permission/Permission.kt | 29 ++++---- 7 files changed, 65 insertions(+), 47 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 251ae7a..6a1b515 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -42,7 +42,7 @@ dependencies { // implementation("com.himanshoe:pluck:1.0.0-RC1") implementation(Deps.Compose.ui) - implementation(Deps.Compose.material) + implementation(Deps.Compose.materialYou) implementation(Deps.Compose.uiToolingPreview) implementation(Deps.Compose.activity) diff --git a/buildSrc/src/main/kotlin/Dependencies.kt b/buildSrc/src/main/kotlin/Dependencies.kt index 255766f..898ecc3 100644 --- a/buildSrc/src/main/kotlin/Dependencies.kt +++ b/buildSrc/src/main/kotlin/Dependencies.kt @@ -17,6 +17,8 @@ object Deps { val activity by lazy { "androidx.activity:activity-compose:${Versions.activity}" } val viewmodel by lazy { "androidx.lifecycle:lifecycle-viewmodel-compose:1.0.0-alpha07" } val paging by lazy { "androidx.paging:paging-compose:1.0.0-alpha14" } + val materialYou by lazy { "androidx.compose.material3:material3:1.0.0-alpha14" } + } object Accompanist { diff --git a/buildSrc/src/main/kotlin/ModuleExtension.kt b/buildSrc/src/main/kotlin/ModuleExtension.kt index d64243f..6fedc33 100644 --- a/buildSrc/src/main/kotlin/ModuleExtension.kt +++ b/buildSrc/src/main/kotlin/ModuleExtension.kt @@ -1,10 +1,10 @@ object ModuleExtension { - const val compileSdkVersion = 31 + const val compileSdkVersion = 32 const val jvmTarget = "1.8" object DefaultConfigs { const val minSdkVersion = 21 - const val targetSdkVersion = 31 + const val targetSdkVersion = 32 const val testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" const val defaultConsumerProguardFiles = "consumer-rules.pro" const val proGuardRules = "proguard-rules.pro" diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt index 9037766..1eff3d2 100644 --- a/buildSrc/src/main/kotlin/Versions.kt +++ b/buildSrc/src/main/kotlin/Versions.kt @@ -4,7 +4,7 @@ object Versions { const val vanniktechGradlePlugin = "0.18.0" const val kotlin = "1.6.10" const val activity = "1.3.1" - const val material = "1.4.0" + const val material = "1.0.0-alpha14" const val jUnit = "4.13.2" const val jUnitExtensions = "1.1.3" const val espresso = "3.4.0" diff --git a/pluck/build.gradle.kts b/pluck/build.gradle.kts index 9a97fb4..1937fc0 100644 --- a/pluck/build.gradle.kts +++ b/pluck/build.gradle.kts @@ -40,7 +40,7 @@ android { dependencies { implementation(Deps.Compose.ui) - implementation(Deps.Compose.material) + implementation(Deps.Compose.materialYou) implementation(Deps.Compose.uiToolingPreview) implementation(Deps.Compose.activity) implementation(Deps.Compose.paging) diff --git a/pluck/src/main/java/com/himanshoe/pluck/ui/Pluck.kt b/pluck/src/main/java/com/himanshoe/pluck/ui/Pluck.kt index ba4d006..236e159 100644 --- a/pluck/src/main/java/com/himanshoe/pluck/ui/Pluck.kt +++ b/pluck/src/main/java/com/himanshoe/pluck/ui/Pluck.kt @@ -22,11 +22,11 @@ package com.himanshoe.pluck.ui * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ +import android.annotation.SuppressLint import android.net.Uri import androidx.activity.compose.ManagedActivityResultLauncher import androidx.activity.compose.rememberLauncherForActivityResult import androidx.activity.result.contract.ActivityResultContracts -import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.Image import androidx.compose.foundation.background import androidx.compose.foundation.clickable @@ -34,14 +34,23 @@ import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size -import androidx.compose.foundation.lazy.GridCells -import androidx.compose.foundation.lazy.LazyGridState -import androidx.compose.foundation.lazy.LazyVerticalGrid -import androidx.compose.foundation.lazy.rememberLazyGridState -import androidx.compose.material.* +import androidx.compose.foundation.lazy.grid.GridCells +import androidx.compose.foundation.lazy.grid.LazyGridState +import androidx.compose.foundation.lazy.grid.LazyVerticalGrid +import androidx.compose.foundation.lazy.grid.rememberLazyGridState import androidx.compose.material.icons.Icons import androidx.compose.material.icons.rounded.Check -import androidx.compose.runtime.* +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.ExtendedFloatingActionButton +import androidx.compose.material3.Icon +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Scaffold +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.alpha @@ -66,13 +75,15 @@ import com.himanshoe.pluck.util.PluckUriManager import com.himanshoe.pluck.util.PluckViewModelFactory import kotlinx.coroutines.flow.StateFlow -private const val SELECT = "SELECT" -private const val ONE = 1 -private const val THREE = 3 +private const val Select = "SELECT" +private const val One = 1 +private const val Three = 3 @Composable -@OptIn(ExperimentalFoundationApi::class) +@OptIn(ExperimentalMaterial3Api::class) +@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter") fun Pluck( + modifier: Modifier = Modifier, pluckConfiguration: PluckConfiguration = PluckConfiguration(), onPhotoSelected: (List) -> Unit, ) { @@ -93,36 +104,38 @@ fun Pluck( pluckViewModel.getImages().collectAsLazyPagingItems() Scaffold(floatingActionButton = { + ExtendedFloatingActionButton( modifier = Modifier, - backgroundColor = MaterialTheme.colors.primary, - contentColor = MaterialTheme.colors.onPrimary, - text = { Text(text = SELECT) }, + containerColor = MaterialTheme.colorScheme.primary, + contentColor = MaterialTheme.colorScheme.onPrimary, + text = { Text(text = Select) }, onClick = { onPhotoSelected(pluckViewModel.selectedImage.value) }, - icon = { Icon(Icons.Rounded.Check, "") } + icon = { Icon(Icons.Rounded.Check, "fab-icon") } ) - }) { - val modifier = Modifier.padding(HalfQuarter) + }, content = { + val newModifier = modifier.padding(HalfQuarter) val cameraLauncher = - rememberLauncherForActivityResult(ActivityResultContracts.TakePicture()) { + rememberLauncherForActivityResult(ActivityResultContracts.TakePicture()) { it: Boolean -> onPhotoSelected(listOf(pluckViewModel.getPluckImage()) as List) } LazyVerticalGrid( state = gridState, - modifier = Modifier.background(MaterialTheme.colors.surface), - cells = GridCells.Fixed(THREE) + modifier = Modifier.background(MaterialTheme.colorScheme.surface), + columns = GridCells.Fixed(Three) ) { item { CameraIcon( - modifier = modifier, + modifier = newModifier, cameraLauncher = cameraLauncher, - pluckViewModel = pluckViewModel) + pluckViewModel = pluckViewModel + ) } items(lazyPluckImages.itemCount) { index -> lazyPluckImages[index]?.let { pluckImage -> PluckImage( - modifier = modifier, + modifier = newModifier, pluckImage = pluckImage, pluckConfiguration = pluckConfiguration, selectedImages = pluckViewModel.selectedImage, @@ -136,7 +149,7 @@ fun Pluck( } } } - } + }) } @Composable @@ -150,7 +163,7 @@ internal fun CameraIcon( modifier = modifier .size(PluckDimens.Sixteen) .clickable { handleCamera(pluckViewModel, cameraLauncher) } - .then(Modifier.background(MaterialTheme.colors.background)) + .then(Modifier.background(MaterialTheme.colorScheme.background)) ) { Image( painter = rememberImagePainter(R.drawable.ic_camera), @@ -178,6 +191,7 @@ internal fun PluckImage( pluckConfiguration: PluckConfiguration, onSelectedPhoto: (PluckImage, isSelected: Boolean) -> Unit, ) { + val selected = remember { mutableStateOf(false) } val images by selectedImages.collectAsState(initial = emptyList()) val backgroundColor = if (selected.value) Color.Black else Color.Transparent @@ -216,7 +230,8 @@ internal fun PluckImage( ) { PluckImageIndicator( modifier = modifier, - text = images.indexOf(pluckImage).plus(ONE).toString()) + text = images.indexOf(pluckImage).plus(One).toString() + ) } } } @@ -224,7 +239,7 @@ internal fun PluckImage( @Composable internal fun PluckImageIndicator(modifier: Modifier = Modifier, text: String) { if (text.toInt() > 0) { - val textColor = MaterialTheme.colors.onPrimary + val textColor = MaterialTheme.colorScheme.onPrimary Text( text = text, @@ -236,8 +251,6 @@ internal fun PluckImageIndicator(modifier: Modifier = Modifier, text: String) { modifier = modifier .fillMaxSize() .padding(PluckDimens.One) - - ) } } diff --git a/pluck/src/main/java/com/himanshoe/pluck/ui/permission/Permission.kt b/pluck/src/main/java/com/himanshoe/pluck/ui/permission/Permission.kt index d8af1cc..c28a738 100644 --- a/pluck/src/main/java/com/himanshoe/pluck/ui/permission/Permission.kt +++ b/pluck/src/main/java/com/himanshoe/pluck/ui/permission/Permission.kt @@ -22,6 +22,7 @@ package com.himanshoe.pluck.ui.permission * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ +import android.annotation.SuppressLint import androidx.compose.foundation.Image import androidx.compose.foundation.background import androidx.compose.foundation.layout.Column @@ -32,10 +33,11 @@ import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size -import androidx.compose.material.Button -import androidx.compose.material.MaterialTheme -import androidx.compose.material.Scaffold -import androidx.compose.material.Text +import androidx.compose.material3.Button +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Scaffold +import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember @@ -55,8 +57,9 @@ import com.google.accompanist.permissions.rememberMultiplePermissionsState import com.himanshoe.pluck.R import com.himanshoe.pluck.theme.PluckDimens +@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter") @Composable -@OptIn(ExperimentalPermissionsApi::class) +@OptIn(ExperimentalPermissionsApi::class, ExperimentalMaterial3Api::class) fun Permission( permissions: List, goToAppSettings: () -> Unit, @@ -71,7 +74,7 @@ fun Permission( permissionsNotGrantedContent = { Scaffold( Modifier - .background(MaterialTheme.colors.surface) + .background(MaterialTheme.colorScheme.surface) .fillMaxSize() .padding(PluckDimens.One), ) { @@ -94,7 +97,7 @@ fun Permission( Text( stringResource(R.string.permission_prompt), textAlign = TextAlign.Center, - color = MaterialTheme.colors.onSurface, + color = MaterialTheme.colorScheme.onSurface, fontStyle = FontStyle.Normal, fontWeight = FontWeight.SemiBold, fontSize = 24.sp, @@ -105,7 +108,7 @@ fun Permission( modifier = Modifier.alpha(0.3F), text = "Allowing access to your camera/storage will let you pick your memories asap!", textAlign = TextAlign.Center, - color = MaterialTheme.colors.onSurface, + color = MaterialTheme.colorScheme.onSurface, fontStyle = FontStyle.Normal, fontWeight = FontWeight.Normal, fontSize = 16.sp, @@ -118,11 +121,11 @@ fun Permission( }, modifier = Modifier .fillMaxWidth() - .background(MaterialTheme.colors.primary), + .background(MaterialTheme.colorScheme.primary), ) { Text( text = "Enable permissions", - color = MaterialTheme.colors.onPrimary + color = MaterialTheme.colorScheme.onPrimary ) } } @@ -148,7 +151,7 @@ fun Permission( Text( stringResource(R.string.permissions_rationale), textAlign = TextAlign.Center, - color = MaterialTheme.colors.onSurface, + color = MaterialTheme.colorScheme.onSurface, fontStyle = FontStyle.Normal, fontWeight = FontWeight.SemiBold, fontSize = 24.sp, @@ -159,11 +162,11 @@ fun Permission( onClick = { goToAppSettings() }, modifier = Modifier .fillMaxWidth() - .background(MaterialTheme.colors.primary), + .background(MaterialTheme.colorScheme.primary), ) { Text( text = "Go to Settings!", - color = MaterialTheme.colors.onPrimary + color = MaterialTheme.colorScheme.onPrimary ) } } From 887a40a0903e6d023af7a2b08be1e4619253447e Mon Sep 17 00:00:00 2001 From: Himanshu Singh Date: Tue, 19 Jul 2022 12:00:41 +0530 Subject: [PATCH 5/5] Released RC2 --- README.md | 4 ++-- app/build.gradle.kts | 4 ++-- pluck/gradle.properties | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 4027f5f..917cdd4 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ![Pluck](art/pluck.jpg) -This is an image-picker for your jetpack compose project. You can select from Gallery/Camera. +This is an image-picker for your jetpack compose project. You can select from Gallery/Camera. This uses Material you and will be getting support for it in future as well. _Made with ❤️ for Android Developers by Himanshu_ @@ -30,7 +30,7 @@ In `build.gradle` of app module, include the following dependency ```gradle dependencies { - implementation("com.himanshoe:pluck:1.0.0-RC1") + implementation("com.himanshoe:pluck:1.0.0-RC2") } ``` diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 6a1b515..ced94a7 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -38,8 +38,8 @@ android { } dependencies { - implementation(project(":pluck")) -// implementation("com.himanshoe:pluck:1.0.0-RC1") +// implementation(project(":pluck")) + implementation("com.himanshoe:pluck:1.0.0-RC2") implementation(Deps.Compose.ui) implementation(Deps.Compose.materialYou) diff --git a/pluck/gradle.properties b/pluck/gradle.properties index da358bf..4ccbebf 100644 --- a/pluck/gradle.properties +++ b/pluck/gradle.properties @@ -5,7 +5,7 @@ POM_DESCRIPTION=A Compose Image Picker. POM_PACKAGING=aar POM_INCEPTION_YEAR=2022 GROUP=com.himanshoe -VERSION_NAME=1.0.0-RC1 +VERSION_NAME=1.0.0-RC2 VERSION_CODE=1 POM_URL=https://github.com/hi-manshu POM_LICENCE_NAME=The Apache Software License, Version 2.0