Skip to content

Commit

Permalink
Merge pull request #21 from manununhez/feature/20-optimize-image-loading
Browse files Browse the repository at this point in the history
feature/20-optimize-image-loading
  • Loading branch information
manununhez authored Apr 12, 2024
2 parents 9a578e0 + 27b5054 commit 0cf8426
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 2 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ dependencies {

implementation(libs.androidx.compose.material3)

implementation(libs.coil.kt)

// Compose
val composeBom = platform(libs.androidx.compose.bom)
implementation(composeBom)
Expand Down
29 changes: 28 additions & 1 deletion app/src/main/kotlin/com/manuelnunez/apps/MainApplication.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
package com.manuelnunez.apps

import android.app.Application
import coil.ImageLoader
import coil.ImageLoaderFactory
import coil.disk.DiskCache
import coil.memory.MemoryCache
import coil.request.CachePolicy
import coil.util.DebugLogger
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp class MainApplication : Application()
@HiltAndroidApp
class MainApplication : Application(), ImageLoaderFactory {
override fun newImageLoader() =
ImageLoader(this)
.newBuilder()
.memoryCachePolicy(CachePolicy.ENABLED)
.memoryCache {
MemoryCache.Builder(this)
.maxSizePercent(0.1) // up to 10% of remaining memory
.strongReferencesEnabled(true)
.build()
}
.diskCachePolicy(CachePolicy.ENABLED)
.diskCache {
DiskCache.Builder()
.maxSizePercent(0.03) // up to 3% of remaining memory
.directory(cacheDir)
.build()
}
.logger(DebugLogger())
.build()
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ constructor(
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), HomeUiState.Empty)

init {
getItems()
}

fun getItems() {
getPopularItems()
getFeaturedItems()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListScope
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.CircularProgressIndicator
Expand Down Expand Up @@ -48,7 +50,30 @@ fun HomeView(
) {
val items by viewModel.state.collectAsStateWithLifecycle()

HomeScreen(items, navigateToDetails, navigateToSeeMore)
if (items.popularItemsState is PopularItemsState.Error &&
items.featuredItemsState is FeaturedItemsState.Error) {
HomeErrorScreen { viewModel.getItems() }
} else {
HomeScreen(items, navigateToDetails, navigateToSeeMore)
}
}

@Composable
private fun HomeErrorScreen(retry: () -> Unit) {
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
Column {
Button(
onClick = { retry.invoke() },
colors =
ButtonDefaults.buttonColors(
containerColor = MaterialTheme.colorScheme.onBackground,
)) {
Text(text = stringResource(id = R.string.button_retry))
}

Text(text = stringResource(id = R.string.alert_error_try_again))
}
}
}

@Composable
Expand Down Expand Up @@ -76,10 +101,19 @@ private fun LazyListScope.featuredSection(
item {
LoadingIndicator(loaderContentDescription = stringResource(id = R.string.section_feature))
}
FeaturedItemsState.Error -> item { FeatureError() }
else -> {}
}
}

@Composable
private fun FeatureError() {
Column {
HeaderTitle(title = stringResource(id = R.string.section_feature))
Text(text = "An error has occurred")
}
}

@Composable
private fun FeaturedItem(items: List<Item>, navigateToDetails: () -> Unit) {
Column {
Expand Down Expand Up @@ -117,10 +151,21 @@ private fun LazyListScope.popularSection(
item {
LoadingIndicator(loaderContentDescription = stringResource(id = R.string.section_popular))
}
PopularItemsState.Error -> {
item { PopularError() }
}
else -> {}
}
}

@Composable
private fun PopularError() {
Column {
HeaderTitle(title = stringResource(id = R.string.section_popular))
Text(text = "An error has occurred")
}
}

@Composable
@OptIn(ExperimentalLayoutApi::class)
private fun PopularItem(
Expand Down
2 changes: 2 additions & 0 deletions features/home/ui/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
<string name="section_feature">Featured</string>
<string name="section_popular">Popular</string>
<string name="section_gallery">See more</string>
<string name="alert_error_try_again">An Error has occurred. Please pull and refresh.</string>
<string name="button_retry">Retry</string>
</resources>

0 comments on commit 0cf8426

Please sign in to comment.