Skip to content
This repository has been archived by the owner on Aug 2, 2024. It is now read-only.

Commit

Permalink
Merge branch 'main' into chris/remove_adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
bentrengrove authored Jan 1, 2024
2 parents eb1834a + 9a00783 commit 3339aca
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,34 @@

package com.google.samples.apps.sunflower.compose.gallery

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.pulltorefresh.PullToRefreshContainer
import androidx.compose.material3.pulltorefresh.rememberPullToRefreshState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.paging.LoadState
import androidx.paging.PagingData
import androidx.paging.compose.LazyPagingItems
import androidx.paging.compose.collectAsLazyPagingItems
Expand All @@ -60,35 +67,65 @@ fun GalleryScreen(
plantPictures = viewModel.plantPictures,
onPhotoClick = onPhotoClick,
onUpClick = onUpClick,
onPullToRefresh = viewModel::refreshData,
)
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun GalleryScreen(
plantPictures: Flow<PagingData<UnsplashPhoto>>,
onPhotoClick: (UnsplashPhoto) -> Unit = {},
onUpClick: () -> Unit = {},
onPullToRefresh: () -> Unit,
) {
Scaffold(
topBar = {
GalleryTopBar(onUpClick = onUpClick)
},
) { padding ->
val pagingItems: LazyPagingItems<UnsplashPhoto> = plantPictures.collectAsLazyPagingItems()
LazyVerticalGrid(
columns = GridCells.Fixed(2),
modifier = Modifier.padding(padding),
contentPadding = PaddingValues(all = dimensionResource(id = R.dimen.card_side_margin))

val pullToRefreshState = rememberPullToRefreshState()

if (pullToRefreshState.isRefreshing) {
onPullToRefresh()
}

val pagingItems: LazyPagingItems<UnsplashPhoto> =
plantPictures.collectAsLazyPagingItems()

LaunchedEffect(pagingItems.loadState) {
when (pagingItems.loadState.refresh) {
is LoadState.Loading -> Unit
is LoadState.Error,is LoadState.NotLoading -> {
pullToRefreshState.endRefresh()
}
}
}

Box(
modifier = Modifier
.padding(padding)
.nestedScroll(pullToRefreshState.nestedScrollConnection)
) {
items(
count = pagingItems.itemCount,
key = pagingItems.itemKey { it }
) { index ->
val photo = pagingItems[index] ?: return@items
PhotoListItem(photo = photo) {
onPhotoClick(photo)
LazyVerticalGrid(
columns = GridCells.Fixed(2),
contentPadding = PaddingValues(all = dimensionResource(id = R.dimen.card_side_margin))
) {
items(
count = pagingItems.itemCount,
key = pagingItems.itemKey { it.id }
) { index ->
val photo = pagingItems[index] ?: return@items
PhotoListItem(photo = photo) {
onPhotoClick(photo)
}
}
}

PullToRefreshContainer(
modifier = Modifier.align(Alignment.TopCenter),
state = pullToRefreshState
)
}
}
}
Expand All @@ -107,7 +144,7 @@ private fun GalleryTopBar(
navigationIcon = {
IconButton(onClick = onUpClick) {
Icon(
Icons.Filled.ArrowBack,
Icons.AutoMirrored.Filled.ArrowBack,
contentDescription = null
)
}
Expand All @@ -120,7 +157,7 @@ private fun GalleryTopBar(
private fun GalleryScreenPreview(
@PreviewParameter(GalleryScreenPreviewParamProvider::class) plantPictures: Flow<PagingData<UnsplashPhoto>>
) {
GalleryScreen(plantPictures = plantPictures)
GalleryScreen(plantPictures = plantPictures, onPullToRefresh = {})
}

private class GalleryScreenPreviewParamProvider :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.google.samples.apps.sunflower.compose.home

import android.util.Log
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.compose.foundation.ExperimentalFoundationApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,18 +326,18 @@ private fun PlantImage(
override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: Target<Drawable>?,
target: Target<Drawable>,
isFirstResource: Boolean
): Boolean {
isLoading = false
return false
}

override fun onResourceReady(
resource: Drawable?,
model: Any?,
resource: Drawable,
model: Any,
target: Target<Drawable>?,
dataSource: DataSource?,
dataSource: DataSource,
isFirstResource: Boolean
): Boolean {
isLoading = false
Expand Down Expand Up @@ -596,4 +596,4 @@ private fun PlantDetailContentPreview() {
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.size
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.DefaultAlpha
import androidx.compose.ui.graphics.painter.ColorPainter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalInspectionMode
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -65,10 +67,6 @@ fun SunflowerImage(
alpha = alpha,
colorFilter = colorFilter,
requestBuilderTransform = requestBuilderTransform,
loading = placeholder {
Box(modifier.fillMaxWidth(), contentAlignment = Alignment.Center) {
CircularProgressIndicator(Modifier.size(40.dp))
}
}
loading = placeholder(ColorPainter(MaterialTheme.colorScheme.secondary))
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,43 @@ package com.google.samples.apps.sunflower.viewmodels
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.google.samples.apps.sunflower.data.UnsplashPhoto
import com.google.samples.apps.sunflower.data.UnsplashRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class GalleryViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
repository: UnsplashRepository
private val repository: UnsplashRepository
) : ViewModel() {

private var queryString: String? = savedStateHandle["plantName"]

val plantPictures =
repository.getSearchResultStream(queryString ?: "").cachedIn(viewModelScope)

private val _plantPictures = MutableStateFlow<PagingData<UnsplashPhoto>?>(null)
val plantPictures: Flow<PagingData<UnsplashPhoto>> get() = _plantPictures.filterNotNull()

init {
refreshData()
}


fun refreshData() {

viewModelScope.launch {
try {
_plantPictures.value = repository.getSearchResultStream(queryString ?: "").cachedIn(viewModelScope).first()
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}
51 changes: 51 additions & 0 deletions app/src/main/res/values-vi/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="utf-8"?><!--
~ Copyright 2021 Google LLC
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ https://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<resources>
<string name="menu_filter_by_grow_zone">Lọc theo vùng phát triển</string>
<string name="my_garden_title">Vườn của tôi</string>
<string name="plant_list_title">Danh sách cây</string>
<string name="available">Cây Hiện Có</string>
<string name="plant_details_title">Chi tiết cây</string>
<string name="add_plant">Thêm cây</string>
<string name="added_plant_to_garden">Đã thêm cây vào vườn</string>
<string name="garden_empty">Vườn của bạn đang trống</string>
<string name="plant_date_header">Đã trồng</string>
<string name="watered_date_header">Lần cuối tưới</string>
<string name="menu_item_share_plant">Chia sẻ</string>
<string name="share_text_plant">Xem cây %s tại ứng dụng Android Sunflower</string>
<string name="gallery_title">Hình ảnh từ Unsplash</string>

<!-- String plurals and their related prefix / suffix strings -->
<string name="watering_needs_prefix">Cần tưới</string>
<plurals name="watering_needs_suffix">
<item quantity="one">hàng ngày</item>
<item quantity="other">mỗi %d ngày</item>
</plurals>

<plurals name="watering_next">
<item quantity="one">tưới vào ngày mai.</item>
<item quantity="other">tưới trong %d ngày.</item>
</plurals>

<!-- Accessibility -->
<string name="a11y_plant_item_image">Hình ảnh của cây</string>
<string name="a11y_back">Trở lại</string>
<string name="gallery_content_description">Đi đến thư viện</string>
<string name="plant_detail_image_content_description">Hình của cây</string>


</resources>
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ constraintLayoutCompose = "1.0.1"
coreTesting = "2.2.0"
coroutines = "1.6.4"
espresso = "3.4.0"
glide = "1.0.0-alpha.3"
glide = "1.0.0-beta01"
gradle = "7.2.0"
gson = "2.9.0"
guava = "31.1-android"
Expand All @@ -39,7 +39,7 @@ ktlint = "0.40.0"
ktx = "1.7.0"
lifecycle = "2.6.0-alpha04"
material = "1.8.0-rc01"
material3 = "1.0.1"
material3 = "1.2.0-alpha11"
# @keep
minSdk = "23"
monitor = "1.6.0"
Expand Down

0 comments on commit 3339aca

Please sign in to comment.