generated from julioromano/skeleton-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shows page of trending movies using api pagination.
- Loading branch information
1 parent
4a6e64f
commit a6540a6
Showing
9 changed files
with
159 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 0 additions & 9 deletions
9
trending/src/main/kotlin/net/marcoromano/tmdb/trending/TrendingState.kt
This file was deleted.
Oops, something went wrong.
48 changes: 0 additions & 48 deletions
48
trending/src/main/kotlin/net/marcoromano/tmdb/trending/TrendingViewModel.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...otlin/net/marcoromano/tmdb/trending/widgets/trending/TrendingLazyVerticalGridViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package net.marcoromano.tmdb.trending.widgets.trending | ||
|
||
import androidx.lifecycle.ViewModel | ||
import androidx.paging.Pager | ||
import androidx.paging.PagingConfig | ||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import net.marcoromano.tmdb.httpapi.HttpApi | ||
import net.marcoromano.tmdb.httpapi.TrendingMovies | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
internal class TrendingLazyVerticalGridViewModel @Inject constructor( | ||
private val httpApi: HttpApi, | ||
) : ViewModel() { | ||
val pager = Pager(PagingConfig(pageSize = 20)) { // 20 comes from tmdb api docs | ||
NetworkPagingSource(httpApi) | ||
}.flow | ||
} | ||
|
||
private class NetworkPagingSource( | ||
private val httpApi: HttpApi, | ||
) : PagingSource<Int, TrendingMovies.Movie>() { | ||
override fun getRefreshKey(state: PagingState<Int, TrendingMovies.Movie>): Int? { | ||
return state.anchorPosition?.let { anchorPosition -> | ||
val anchorPage = state.closestPageToPosition(anchorPosition) | ||
anchorPage?.prevKey?.plus(1) ?: anchorPage?.nextKey?.minus(1) | ||
} | ||
} | ||
|
||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, TrendingMovies.Movie> { | ||
return runCatching { | ||
httpApi.trendingMovies(page = params.key ?: 1) | ||
}.fold( | ||
{ | ||
LoadResult.Page( | ||
data = it.results, | ||
prevKey = if (it.page > 1) it.page - 1 else null, | ||
nextKey = if (it.page < it.total_pages) it.page + 1 else null, | ||
) | ||
}, | ||
{ | ||
LoadResult.Error(it) | ||
}, | ||
) | ||
} | ||
} |
Oops, something went wrong.