Skip to content

Commit

Permalink
fix followers screen
Browse files Browse the repository at this point in the history
  • Loading branch information
stslex committed Feb 1, 2024
1 parent 10ae445 commit 360625e
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.stslex.core.ui.navigation.AppScreen
import com.stslex.feature.auth.ui.AuthScreen
import com.stslex.feature.film.ui.FilmScreen
import com.stslex.feature.follower.ui.FollowerScreen
import com.stslex.feature.follower.ui.FollowerScreenArgs
import com.stslex.feature.follower.navigation.FollowerScreenArgs
import com.stslex.feature.match_feed.ui.MatchFeedScreen
import main_screen.MainScreen

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ val featureFollowerModule = module {

factory<FollowerRepository> {
FollowerRepositoryImpl(
api = get()
client = get()
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.stslex.feature.follower.navigation

sealed class FollowerScreenArgs(
open val uuid: String
) {

data class Follower(
override val uuid: String
) : FollowerScreenArgs(uuid)

data class Following(
override val uuid: String
) : FollowerScreenArgs(uuid)
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package com.stslex.feature.follower.ui

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import cafe.adriel.voyager.core.screen.Screen
import cafe.adriel.voyager.koin.getScreenModel
import com.stslex.feature.follower.navigation.FollowerScreenArgs
import com.stslex.feature.follower.ui.store.FollowerScreenState
import com.stslex.feature.follower.ui.store.FollowerStore
import com.stslex.feature.follower.ui.store.FollowerStoreComponent.Action
import com.stslex.feature.follower.ui.store.FollowerStoreComponent.State
Expand Down Expand Up @@ -38,5 +45,45 @@ internal fun FollowerScreen(
state: State,
onAction: (Action) -> Unit
) {
Text("test")
when (state.screen) {
is FollowerScreenState.Content -> {
LazyColumn {
items(state.data.size) { index ->
Text(
"test: ${state.data[index].username}"
)
}
}
if (state.screen is FollowerScreenState.Content.Loading) {
Text("Loading")
}
}

is FollowerScreenState.Error -> {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Text("Error: ${state.screen.error.message}")
}
}

FollowerScreenState.Shimmer -> {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Text("Shimmer")
}
}

FollowerScreenState.Empty -> {
Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Text("Empty")
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import com.stslex.core.core.AppDispatcher
import com.stslex.core.ui.mvi.BaseStore
import com.stslex.feature.follower.domain.interactor.FollowerInteractor
import com.stslex.feature.follower.navigation.FollowerRouter
import com.stslex.feature.follower.navigation.FollowerScreenArgs
import com.stslex.feature.follower.ui.store.FollowerStoreComponent.Action
import com.stslex.feature.follower.ui.store.FollowerStoreComponent.Event
import com.stslex.feature.follower.ui.store.FollowerStoreComponent.Navigation
import com.stslex.feature.follower.ui.store.FollowerStoreComponent.State
import com.stslex.feature.follower.ui.store.FollowerStoreComponent.State.Companion.DEFAULT_PAGE
import kotlinx.collections.immutable.toImmutableList
import kotlinx.coroutines.Job

class FollowerStore(
private val interactor: FollowerInteractor,
Expand All @@ -19,13 +23,95 @@ class FollowerStore(
initialState = State.INITIAL,
) {

private var loadingJob: Job? = null

override fun process(action: Action) {
when (action) {
is Action.Init -> actionInit(action)
is Action.LoadMore -> actionLoadMore()
}
}

private fun actionLoadMore() {
loadNextItems()
}

private fun actionInit(action: Action.Init) {
TODO()
updateState { state ->
state.copy(
uuid = action.args.uuid,
type = action.args,
screen = FollowerScreenState.Shimmer
)
}
loadNextItems()
}

private fun loadNextItems() {
if (loadingJob?.isActive == true) return

val currentState = state.value

val loadingScreen = if (
currentState.screen is FollowerScreenState.Content &&
currentState.data.isNotEmpty()
) {
FollowerScreenState.Content.Loading
} else {
FollowerScreenState.Shimmer
}

updateState { state -> state.copy(screen = loadingScreen) }

val page = if (currentState.page == DEFAULT_PAGE) {
FIRST_PAGE
} else {
currentState.page.inc()
}

loadingJob = when (val type = currentState.type) {
is FollowerScreenArgs.Follower -> interactor.getFollowers(
uuid = type.uuid,
page = page,
pageSize = PAGE_SIZE
)

is FollowerScreenArgs.Following -> interactor.getFollowing(
uuid = type.uuid,
page = page,
pageSize = PAGE_SIZE
)
}.launchFlow(
each = { data ->
val screen = if (data.isEmpty()) {
FollowerScreenState.Empty
} else {
FollowerScreenState.Content.NotLoading
}
updateState { state ->
state.copy(
page = page,
data = currentState.data.plus(data).toImmutableList(),
screen = screen
)
}
},
onError = { error ->
if (currentState.data.isEmpty()) {
updateState { state ->
state.copy(
screen = FollowerScreenState.Error(error)
)
}
} else {
sendEvent(Event.ErrorSnackBar(error.message.orEmpty()))
}
}
)
}

companion object {
private const val PAGE_SIZE = 10
private const val FIRST_PAGE = 1
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.stslex.feature.follower.ui.store

import androidx.compose.runtime.Stable
import com.stslex.core.ui.mvi.Store
import com.stslex.feature.follower.ui.FollowerScreenArgs
import com.stslex.feature.follower.navigation.FollowerScreenArgs
import com.stslex.feature.follower.ui.model.FollowerModel
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList
Expand All @@ -12,14 +12,20 @@ interface FollowerStoreComponent : Store {
@Stable
data class State(
val uuid: String,
val page: Int,
val type: FollowerScreenArgs,
val data: ImmutableList<FollowerModel>,
val screen: FollowerScreenState
) : Store.State {

companion object {

const val DEFAULT_PAGE = -1

val INITIAL = State(
uuid = "",
page = DEFAULT_PAGE,
type = FollowerScreenArgs.Follower(""),
data = emptyList<FollowerModel>().toImmutableList(),
screen = FollowerScreenState.Shimmer
)
Expand All @@ -33,6 +39,9 @@ interface FollowerStoreComponent : Store {
data class Init(
val args: FollowerScreenArgs
) : Action

@Stable
data object LoadMore : Action
}

@Stable
Expand All @@ -51,14 +60,19 @@ sealed interface FollowerScreenState {
@Stable
sealed interface Content : FollowerScreenState {

@Stable
data object NotLoading : Content

@Stable
data object Loading : Content
}

@Stable
data object Shimmer : FollowerScreenState

@Stable
data object Empty : FollowerScreenState

@Stable
data class Error(val error: Throwable) : FollowerScreenState
}

0 comments on commit 360625e

Please sign in to comment.