Skip to content

Commit

Permalink
Merge fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dranoer committed Aug 24, 2023
2 parents f6b0ebd + 55e7f0b commit 26bc260
Show file tree
Hide file tree
Showing 23 changed files with 268 additions and 154 deletions.
12 changes: 8 additions & 4 deletions app/src/main/java/com/dranoer/photoalbum/di/AppModule.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.dranoer.photoalbum.di

import com.dranoer.photoalbum.data.remote.WebService
import com.dranoer.photoalbum.domain.PhotoMapper
import com.dranoer.photoalbum.domain.DomainModelMapper
import com.dranoer.photoalbum.domain.PhotoRepository
import com.dranoer.photoalbum.util.Constant.BASE_URL
import com.dranoer.photoalbum.util.Constant.NETWORK_REQUEST_TIMEOUT
import com.dranoer.photoalbum.util.UiModelMapper
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
Expand Down Expand Up @@ -45,9 +46,12 @@ class AppModule {
retrofit.create(WebService::class.java)

@Provides
fun provideMapper(): PhotoMapper = PhotoMapper()
fun provideDomainMapper(): DomainModelMapper = DomainModelMapper()

@Provides
fun provideRepository(webService: WebService, mapper: PhotoMapper): PhotoRepository =
PhotoRepository(webService = webService, mapper = mapper)
fun provideUiMapper(): UiModelMapper = UiModelMapper()

@Provides
fun provideRepository(webService: WebService, mapper: DomainModelMapper): PhotoRepository =
PhotoRepository(webService = webService, domainMapper = mapper)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ import com.dranoer.photoalbum.domain.model.AlbumItem
import com.dranoer.photoalbum.domain.model.PhotoItem
import javax.inject.Inject

class PhotoMapper @Inject constructor() {
class DomainModelMapper @Inject constructor() {
fun mapAlbums(albumList: List<AlbumModel>): List<AlbumItem> {
return albumList.map { album ->
AlbumItem(userId = album.userId, id = album.id, title = album.title)
AlbumItem(
userId = album.userId,
id = album.id,
title = album.title,
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import javax.inject.Inject

class PhotoRepository @Inject constructor(
private val webService: WebService,
private val mapper: PhotoMapper,
private val domainMapper: DomainModelMapper,
) {

suspend fun fetchAlbums(): List<AlbumItem> {
try {
val response = webService.fetchAlbums()
return mapper.mapAlbums(albumList = response)
return domainMapper.mapAlbums(albumList = response)
} catch (e: Exception) {
throw e.toAppException()
}
Expand All @@ -23,7 +23,7 @@ class PhotoRepository @Inject constructor(
suspend fun fetchPhotos(id: Int): List<PhotoItem> {
try {
val response = webService.fetchPhotos(id = id)
return mapper.mapPhotos(photoList = response)
return domainMapper.mapPhotos(photoList = response)
} catch (e: Exception) {
throw e.toAppException()
}
Expand Down
15 changes: 8 additions & 7 deletions app/src/main/java/com/dranoer/photoalbum/ui/album/AlbumScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.hilt.navigation.compose.hiltViewModel
import com.dranoer.photoalbum.R
import com.dranoer.photoalbum.domain.model.AlbumItem
import com.dranoer.photoalbum.ui.model.AlbumUiModel
import com.dranoer.photoalbum.ui.model.AlbumUiState
import com.dranoer.photoalbum.ui.theme.PhotoAlbumTheme
import com.dranoer.photoalbum.util.getRandomColor
import com.dranoer.rijksmuseum.ui.component.ErrorView
Expand Down Expand Up @@ -112,7 +113,7 @@ fun AlbumScreen(

@Composable
private fun AlbumList(
data: List<AlbumItem>,
data: List<AlbumUiModel>,
navigateToPhoto: (String) -> Unit,
) {
Box(modifier = Modifier.fillMaxSize()) {
Expand All @@ -125,12 +126,12 @@ private fun AlbumList(
),
) {
items(data) { album ->
AlbumCard(
AlbumView(
title = album.title,
id = album.id.toString(),
color = getRandomColor(),
onAlbumClicked = { navigateToPhoto(album.id.toString()) },
modifier = Modifier.wrapContentWidth()
modifier = Modifier.wrapContentWidth(),
)
}
}
Expand All @@ -152,17 +153,17 @@ private fun AlbumListPreview_Normal() {
PhotoAlbumTheme {
AlbumList(
data = listOf(
AlbumItem(
AlbumUiModel(
userId = 1,
id = 2,
title = "AlbumItem title"
),
AlbumItem(
AlbumUiModel(
userId = 1,
id = 2,
title = "AlbumItem title"
),
AlbumItem(
AlbumUiModel(
userId = 1,
id = 2,
title = "AlbumItem title"
Expand Down
10 changes: 0 additions & 10 deletions app/src/main/java/com/dranoer/photoalbum/ui/album/AlbumUiState.kt

This file was deleted.

57 changes: 52 additions & 5 deletions app/src/main/java/com/dranoer/photoalbum/ui/album/AlbumView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import com.dranoer.photoalbum.ui.theme.PhotoAlbumTheme
import com.dranoer.photoalbum.util.getRandomColor

@Composable
fun AlbumCard(
fun AlbumView(
modifier: Modifier,
id: String,
title: String,
Expand All @@ -44,9 +44,9 @@ fun AlbumCard(
//region Preview
@Preview
@Composable
private fun AlbumCardPreview_Normal() {
private fun AlbumViewPreview_Normal() {
PhotoAlbumTheme {
AlbumCard(
AlbumView(
id = "2",
title = "AlbumItem title",
color = getRandomColor(),
Expand All @@ -58,9 +58,9 @@ private fun AlbumCardPreview_Normal() {

@Preview
@Composable
private fun AlbumCardPreview_LongTitle() {
private fun AlbumViewPreview_LongTitle() {
PhotoAlbumTheme {
AlbumCard(
AlbumView(
id = "2",
title = "This is a very long long long long long long long long long long long long long long long AlbumItem title.",
color = getRandomColor(),
Expand All @@ -69,4 +69,51 @@ private fun AlbumCardPreview_LongTitle() {
)
}
}

@Preview
@Composable
private fun AlbumViewPreview_NoTitle() {
PhotoAlbumTheme {
AlbumView(
id = "2",
title = "",
color = getRandomColor(),
onAlbumClicked = {},
modifier = Modifier,
)
}
}

@Preview
@Composable
private fun AlbumViewPreview_DarkTheme() {
PhotoAlbumTheme(darkTheme = true) {
AlbumView(
id = "2",
title = "AlbumItem title.",
color = getRandomColor(),
onAlbumClicked = {},
modifier = Modifier,
)
}
}

/**
* This function depicts the `AlbumCard` composable in a state where it's clickable.
* This is to visualize the UI state in the Compose Preview,
* although actual click events are not interactive within the Preview.
*/
@Preview
@Composable
private fun AlbumViewPreview_Clickable() {
PhotoAlbumTheme {
AlbumView(
id = "2",
title = "AlbumItem title.",
color = getRandomColor(),
onAlbumClicked = { clickedId -> println("Clicked on album with id: $clickedId") },
modifier = Modifier,
)
}
}
//endregion
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package com.dranoer.photoalbum.ui.album
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.dranoer.photoalbum.domain.PhotoRepository
import com.dranoer.photoalbum.ui.model.AlbumUiState
import com.dranoer.photoalbum.util.UiModelMapper
import com.dranoer.photoalbum.util.exception.AppException
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -14,6 +16,7 @@ import javax.inject.Inject
@HiltViewModel
class AlbumViewModel @Inject constructor(
private val repository: PhotoRepository,
private val mapper: UiModelMapper,
) : ViewModel() {

private val _albumState = MutableStateFlow<AlbumUiState>(AlbumUiState.Empty)
Expand All @@ -24,7 +27,8 @@ class AlbumViewModel @Inject constructor(
viewModelScope.launch {
try {
val response = repository.fetchAlbums()
_albumState.value = AlbumUiState.Loaded(data = response)
val uiModel = mapper.mapAlbums(albumList = response)
_albumState.value = AlbumUiState.Loaded(data = uiModel)
} catch (e: AppException) {
when (e) {
is AppException.NetworkException -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ fun ErrorView(
refresh: () -> Unit
) {
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.fillMaxSize()
) {
Text(message)
Spacer(Modifier.height(dimensionResource(id = R.dimen.size_16)))
Expand All @@ -33,7 +33,7 @@ fun ErrorView(
}

//region Preview
@Preview("Normal Error view")
@Preview
@Composable
private fun PreviewErrorView_Normal() {
PhotoAlbumTheme() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import com.dranoer.photoalbum.R
import com.dranoer.photoalbum.domain.model.PhotoItem
import com.dranoer.photoalbum.ui.model.PhotoUiModel
import com.dranoer.photoalbum.ui.theme.PhotoAlbumTheme

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DetailScreen(
photo: PhotoItem,
photo: PhotoUiModel,
backPress: () -> Unit,
) {
PhotoAlbumTheme {
Expand Down Expand Up @@ -75,7 +75,7 @@ private fun DetailScreenPreview_Normal() {
PhotoAlbumTheme {
DetailScreen(
backPress = {},
photo = PhotoItem(
photo = PhotoUiModel(
albumId = 1,
id = 2,
title = "PhotoItem title.",
Expand Down
41 changes: 41 additions & 0 deletions app/src/main/java/com/dranoer/photoalbum/ui/detail/DetailView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.dranoer.photoalbum.ui.detail
import android.content.res.Configuration
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.aspectRatio
Expand Down Expand Up @@ -119,4 +120,44 @@ private fun DetailViewPreview_LongTitle() {
)
}
}

@Preview
@Composable
private fun DetailViewPreview_DarkTheme() {
PhotoAlbumTheme(darkTheme = true) {
DetailView(
title = "DetailView title.",
url = "url",
modifier = Modifier,
)
}
}

@Preview
@Composable
private fun DetailViewPreview_Portrait() {
PhotoAlbumTheme {
Box(modifier = Modifier.aspectRatio(0.6f)) {
DetailView(
title = "DetailView title.",
url = "url",
modifier = Modifier.fillMaxSize(),
)
}
}
}

@Preview
@Composable
private fun DetailViewPreview_Landscape() {
PhotoAlbumTheme {
Box(modifier = Modifier.aspectRatio(2f)) {
DetailView(
title = "DetailView title.",
url = "url",
modifier = Modifier.fillMaxSize(),
)
}
}
}
//endregion
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.dranoer.photoalbum.ui.model

data class AlbumUiModel(
val userId: Int,
val id: Int,
val title: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.dranoer.photoalbum.ui.model

sealed class AlbumUiState {
object Empty : AlbumUiState()
object Loading : AlbumUiState()
data class Loaded(val data: List<AlbumUiModel>, val isRefreshing: Boolean = false) : AlbumUiState()
data class Error(val message: String) : AlbumUiState()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.dranoer.photoalbum.ui.model

data class PhotoUiModel(
val albumId: Int,
val id: Int,
val title: String,
val url: String,
val thumbnailUrl: String,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.dranoer.photoalbum.ui.model

sealed class PhotoUiState {
object Empty : PhotoUiState()
object Loading : PhotoUiState()
class Loaded(val data: List<PhotoUiModel>, val isRefreshing: Boolean = false) : PhotoUiState()
class Error(val message: String) : PhotoUiState()
}
Loading

0 comments on commit 26bc260

Please sign in to comment.