Skip to content

Commit

Permalink
Fix: #344 endless pagination of size 30 (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
mariobodemann authored Jul 5, 2024
2 parents 4c7302d + c8d1c54 commit 7fc892d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import okhttp3.MediaType.Companion.toMediaType
import retrofit2.Retrofit
import retrofit2.converter.kotlinx.serialization.asConverterFactory
import retrofit2.http.GET
import retrofit2.http.Query
import javax.inject.Inject

class ZeContributorsService @Inject constructor() {
fun contributors(): Flow<List<Contributor>> = flow {
val contributors = githubApiService.getContributors()
fun contributors(page: Int): Flow<List<Contributor>> = flow {
val contributors = githubApiService.getContributors(page)

emit(
contributors.map { Contributor(it.login, it.url, it.imageUrl, it.contributions) },
Expand Down Expand Up @@ -53,5 +54,5 @@ private interface GithubApi {
)

@GET("contributors")
suspend fun getContributors(): List<Contributor>
suspend fun getContributors(@Query("page") page: Int): List<Contributor>
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
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.ui.Alignment
Expand Down Expand Up @@ -76,7 +77,10 @@ fun ZeAbout(
AsyncImage(
model = contributor.imageUrl,
contentDescription = "avatar",
modifier = Modifier.padding(8.dp).size(50.dp).clip(CircleShape),
modifier = Modifier
.padding(8.dp)
.size(50.dp)
.clip(CircleShape),
)
Text(
text = "${contributor.name}: ${contributor.contributions}",
Expand All @@ -87,6 +91,13 @@ fun ZeAbout(
)
}
}
item {
LaunchedEffect(true) {
if (contributors.isNotEmpty()) {
vm.loadNextPage()
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,34 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import de.berlindroid.zeapp.zeservices.ZeContributorsService
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class ZeAboutViewModel @Inject constructor(
contributorsService: ZeContributorsService,
private val contributorsService: ZeContributorsService,
) : ViewModel() {
private var page: Int = 1

val lines: StateFlow<List<Contributor>> = contributorsService.contributors()
.map { contributors -> contributors.sortedBy { -it.contributions } }
.stateIn(viewModelScope, SharingStarted.Lazily, initialValue = emptyList())
private val linesMutableState = MutableStateFlow(emptyList<Contributor>())
val lines = linesMutableState.asStateFlow()

init {
viewModelScope.launch {
contributorsService.contributors(page).collect {
linesMutableState.emit(it)
}
}
}

fun loadNextPage() {
page++
viewModelScope.launch {
contributorsService.contributors(page).collect {
linesMutableState.emit(lines.value + it)
}
}
}
}

0 comments on commit 7fc892d

Please sign in to comment.