Skip to content

Commit 40b5239

Browse files
committed
fix imageLoader, remote images and logging
1 parent 1cf60c0 commit 40b5239

11 files changed

Lines changed: 136 additions & 10 deletions

File tree

core/core/src/main/java/st/slex/csplashscreen/core/core/Logger.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,17 @@ object Logger {
2828
val currentTag = "$DEFAULT_TAG:${tag.orEmpty()}"
2929
Log.d(currentTag, message)
3030
}
31-
}
31+
32+
fun i(
33+
message: String,
34+
tag: String? = null,
35+
throwable: Throwable? = null,
36+
) {
37+
if (BuildConfig.DEBUG.not()) return
38+
val currentTag = "$DEFAULT_TAG:${tag.orEmpty()}"
39+
Log.i(currentTag, message, throwable)
40+
}
41+
42+
fun tag(tag: String): LoggerTag = LoggerTag(tag)
43+
}
44+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package st.slex.csplashscreen.core.core
2+
3+
class LoggerTag(
4+
private val tag: String
5+
) {
6+
7+
fun e(throwable: Throwable, message: String? = null) {
8+
Logger.e(
9+
throwable = throwable,
10+
tag = tag,
11+
message = message
12+
)
13+
}
14+
15+
fun d(message: String) {
16+
Logger.d(
17+
message = message,
18+
tag = tag
19+
)
20+
}
21+
22+
fun i(
23+
message: String,
24+
throwable: Throwable? = null,
25+
) {
26+
Logger.i(
27+
message = message,
28+
tag = tag,
29+
throwable = throwable
30+
)
31+
}
32+
}

core/image/src/main/kotlin/st/slex/scplashscreen/core/image/AppImageRequest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import androidx.compose.runtime.remember
66
import androidx.compose.ui.platform.LocalContext
77
import coil3.request.CachePolicy
88
import coil3.request.ImageRequest
9+
import st.slex.csplashscreen.core.core.Logger
910

1011
object AppImageRequest {
1112

1213
@Composable
1314
fun createImageRequestBuilder(url: String): ImageRequest.Builder {
1415
val context = LocalContext.current
16+
Logger.tag("AppImageRequest").d("createImageRequestBuilder: $url")
1517
return remember {
1618
createImageRequestBuilder(context, url)
1719
.placeholderMemoryCacheKey(url)

core/network/src/main/java/st/slex/csplashscreen/core/network/model/remote/user/RemoteUserLinksModel.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ data class RemoteUserLinksModel(
1010
@SerialName("photos") val photos: String,
1111
@SerialName("likes") val likes: String,
1212
@SerialName("portfolio") val portfolio: String,
13-
@SerialName("following") val following: String,
14-
@SerialName("followers") val followers: String
13+
@SerialName("following") val following: String?,
14+
@SerialName("followers") val followers: String?
1515
)

core/ui/src/main/java/st/slex/csplashscreen/core/ui/components/ImageComponent.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package st.slex.csplashscreen.core.ui.components
33
import androidx.compose.runtime.Composable
44
import androidx.compose.ui.Modifier
55
import androidx.compose.ui.layout.ContentScale
6+
import androidx.compose.ui.platform.LocalContext
67
import coil3.compose.AsyncImage
8+
import coil3.request.CachePolicy
9+
import coil3.request.ImageRequest
710
import coil3.request.crossfade
8-
import st.slex.scplashscreen.core.image.AppImageRequest.createImageRequestBuilder
911

1012
@Composable
1113
fun ImageComponent(
@@ -15,7 +17,14 @@ fun ImageComponent(
1517
) {
1618
AsyncImage(
1719
modifier = modifier,
18-
model = createImageRequestBuilder(url)
20+
model = ImageRequest.Builder(LocalContext.current)
21+
.data(url)
22+
.placeholderMemoryCacheKey(url)
23+
.memoryCacheKey(url)
24+
.diskCacheKey(url)
25+
.networkCachePolicy(CachePolicy.ENABLED)
26+
.diskCachePolicy(CachePolicy.ENABLED)
27+
.memoryCachePolicy(CachePolicy.ENABLED)
1928
.crossfade(true)
2029
.build(),
2130
contentDescription = null,

core/ui/src/main/java/st/slex/csplashscreen/core/ui/mvi/Store.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,27 @@ abstract class Store<S : State, E : Event, A : Action, N : Navigation>(
3535
initialState: S
3636
) : ViewModel() {
3737

38+
private val logger = Logger.tag(this::class.simpleName ?: "Store")
39+
3840
private val _state: MutableStateFlow<S> = MutableStateFlow(initialState)
3941
val state: StateFlow<S>
4042
get() = _state.asStateFlow()
4143

4244
val event: MutableSharedFlow<E> = MutableSharedFlow()
4345

44-
abstract fun sendAction(action: A)
46+
open fun sendAction(action: A) {
47+
i("action: $action")
48+
}
4549

4650
fun sendEvent(event: E) {
51+
i("event: $event")
4752
viewModelScope.launch(appDispatcher.default) {
4853
this@Store.event.emit(event)
4954
}
5055
}
5156

5257
protected fun navigate(event: N) {
58+
i("navigate: $event")
5359
router(event)
5460
}
5561

@@ -106,4 +112,16 @@ abstract class Store<S : State, E : Event, A : Action, N : Navigation>(
106112
}
107113
.onEach(each)
108114
.launchIn(viewModelScope)
115+
116+
protected fun d(msg: String) {
117+
logger.d(msg)
118+
}
119+
120+
protected fun e(throwable: Throwable, msg: String? = null) {
121+
logger.e(throwable, msg)
122+
}
123+
124+
protected fun i(msg: String, throwable: Throwable? = null) {
125+
logger.i(msg, throwable)
126+
}
109127
}

feature/home/src/main/java/st/slex/csplashscreen/feature/home/domain/HomeInteractorImpl.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package st.slex.csplashscreen.feature.home.domain
22

33
import st.slex.csplashscreen.core.collection.data.CollectionsRepository
4+
import st.slex.csplashscreen.core.core.Logger
45
import st.slex.csplashscreen.core.network.model.mapToDomain
56
import st.slex.csplashscreen.core.network.model.toDomain
67
import st.slex.csplashscreen.core.network.model.ui.CollectionDomainModel
@@ -31,4 +32,7 @@ class HomeInteractorImpl(
3132
pageSize = pageSize
3233
)
3334
.toDomain()
35+
.also {
36+
Logger.tag("DEBUG_HOME").d("getAllPhotos: $it")
37+
}
3438
}

feature/home/src/main/java/st/slex/csplashscreen/feature/home/navigation/HomeGraph.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ fun NavGraphBuilder.homeGraph(
3434
// TODO NOT IMPLEMENTED YET
3535
}
3636

37+
LaunchedEffect(photos.loadState) {
38+
store.sendAction(Action.PhotosPagingState(photos.loadState))
39+
}
40+
41+
LaunchedEffect(photos.loadState) {
42+
store.sendAction(Action.CollectionsPagingState(photos.loadState))
43+
}
44+
3745
MainScreen(
3846
modifier = modifier,
3947
navToProfile = remember {

feature/home/src/main/java/st/slex/csplashscreen/feature/home/ui/presenter/HomeStore.kt

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package st.slex.csplashscreen.feature.home.ui.presenter
22

3+
import androidx.paging.CombinedLoadStates
4+
import androidx.paging.LoadState
35
import androidx.paging.Pager
46
import androidx.paging.PagingConfig
57
import androidx.paging.PagingData
@@ -28,20 +30,25 @@ class HomeStore(
2830
initialState = State.INIT
2931
) {
3032

31-
private val collections: StateFlow<PagingData<CollectionModel>> =
32-
Pager(config = config) { PagingSource(interactor::getAllCollections) }
33+
private val collections: StateFlow<PagingData<CollectionModel>>
34+
get() = Pager(config = config) { PagingSource(interactor::getAllCollections) }
3335
.state { collection -> collection.toPresentation() }
3436

35-
private val photos: StateFlow<PagingData<PhotoModel>> =
36-
Pager(config = config) { PagingSource(interactor::getAllPhotos) }
37+
private val photos: StateFlow<PagingData<PhotoModel>>
38+
get() = Pager(config = config) {
39+
PagingSource(interactor::getAllPhotos)
40+
}
3741
.state { image -> image.toPresentation() }
3842

3943
override fun sendAction(action: Action) {
44+
super.sendAction(action)
4045
when (action) {
4146
is Action.Init -> actionInit()
4247
is Action.OnCollectionClick -> actionCollectionClick(action)
4348
is Action.OnImageClick -> actionImageClick(action)
4449
is Action.OnUserClick -> actionUserClick(action)
50+
is Action.CollectionsPagingState -> actionCollectionPagingState(action)
51+
is Action.PhotosPagingState -> actionPhotosPagingState(action)
4552
}
4653
}
4754

@@ -70,6 +77,27 @@ class HomeStore(
7077
navigate(Navigation.User(action.username))
7178
}
7279

80+
private fun actionCollectionPagingState(action: Action.CollectionsPagingState) {
81+
action.combinedLoadStates.checkError()?.let { e(it) }
82+
}
83+
84+
private fun actionPhotosPagingState(action: Action.PhotosPagingState) {
85+
action.combinedLoadStates.checkError()?.let { e(it) }
86+
}
87+
88+
private fun CombinedLoadStates.checkError(): Throwable? {
89+
if (refresh is LoadState.Error) {
90+
return (refresh as LoadState.Error).error
91+
}
92+
if (append is LoadState.Error) {
93+
return (append as LoadState.Error).error
94+
}
95+
if (prepend is LoadState.Error) {
96+
return (prepend as LoadState.Error).error
97+
}
98+
return null
99+
}
100+
73101
companion object {
74102

75103
private val config = PagingConfig(

feature/home/src/main/java/st/slex/csplashscreen/feature/home/ui/presenter/HomeStoreComponent.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package st.slex.csplashscreen.feature.home.ui.presenter
22

33
import androidx.compose.runtime.Stable
4+
import androidx.paging.CombinedLoadStates
45
import androidx.paging.PagingData
56
import st.slex.csplashscreen.core.collection.ui.model.CollectionModel
67
import st.slex.csplashscreen.core.photos.ui.model.PhotoModel
@@ -63,6 +64,16 @@ interface HomeStoreComponent : StoreComponent {
6364
data class OnImageClick(
6465
val uuid: String
6566
) : Action
67+
68+
@Stable
69+
data class PhotosPagingState(
70+
val combinedLoadStates: CombinedLoadStates
71+
) : Action
72+
73+
@Stable
74+
data class CollectionsPagingState(
75+
val combinedLoadStates: CombinedLoadStates
76+
) : Action
6677
}
6778
}
6879

0 commit comments

Comments
 (0)