-
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.
- Loading branch information
1 parent
7ad0a9d
commit 1611ebf
Showing
13 changed files
with
157 additions
and
94 deletions.
There are no files selected for viewing
3 changes: 2 additions & 1 deletion
3
app/src/main/java/com/commandiron/toprated10films/data/mapper/ActorMapper.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
40 changes: 40 additions & 0 deletions
40
app/src/main/java/com/commandiron/toprated10films/data/paging/ActorPagingSource.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,40 @@ | ||
package com.commandiron.toprated10films.data.paging | ||
|
||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import com.commandiron.toprated10films.data.mapper.toActor | ||
import com.commandiron.toprated10films.data.remote.MovieApi | ||
import com.commandiron.toprated10films.domain.model.Actor | ||
|
||
class ActorPagingSource( | ||
private val api: MovieApi | ||
) : PagingSource<Int, Actor>() { | ||
|
||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Actor> { | ||
val currentPage = params.key ?: 1 | ||
return try { | ||
val response = api.getActors(currentPage) | ||
val endOfPaginationReached = response.page == response.total_pages | ||
if (!endOfPaginationReached) { | ||
LoadResult.Page( | ||
data = response.movieDbActors.map { it.toActor() }, | ||
prevKey = if (currentPage == 1) null else currentPage - 1, | ||
nextKey = currentPage + 1 | ||
) | ||
} else { | ||
LoadResult.Page( | ||
data = emptyList(), | ||
prevKey = null, | ||
nextKey = null | ||
) | ||
} | ||
} catch (e: Exception) { | ||
LoadResult.Error(e) | ||
} | ||
} | ||
|
||
override fun getRefreshKey(state: PagingState<Int, Actor>): Int? { | ||
return state.anchorPosition | ||
} | ||
|
||
} |
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
7 changes: 7 additions & 0 deletions
7
app/src/main/java/com/commandiron/toprated10films/domain/model/Actor.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,7 @@ | ||
package com.commandiron.toprated10films.domain.model | ||
|
||
data class Actor( | ||
val id: Int, | ||
val name: String, | ||
val imageUrl: String | ||
) |
5 changes: 3 additions & 2 deletions
5
app/src/main/java/com/commandiron/toprated10films/domain/repository/AppRepository.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 |
---|---|---|
@@ -1,12 +1,13 @@ | ||
package com.commandiron.toprated10films.domain.repository | ||
|
||
import androidx.paging.PagingData | ||
import com.commandiron.toprated10films.domain.model.Genre | ||
import com.commandiron.toprated10films.ui.model.Actor | ||
import com.commandiron.toprated10films.domain.model.Actor | ||
import com.commandiron.toprated10films.util.Response | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface AppRepository { | ||
suspend fun getGenres(): Flow<Response<List<Genre>>> | ||
suspend fun saveGenres(genres: List<Genre>) | ||
suspend fun getActors(page: Int): Flow<Response<List<Actor>>> | ||
suspend fun getActors(): Flow<PagingData<Actor>> | ||
} |
6 changes: 3 additions & 3 deletions
6
app/src/main/java/com/commandiron/toprated10films/domain/use_cases/GetActors.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 |
---|---|---|
@@ -1,12 +1,12 @@ | ||
package com.commandiron.toprated10films.domain.use_cases | ||
|
||
import androidx.paging.PagingData | ||
import com.commandiron.toprated10films.domain.repository.AppRepository | ||
import com.commandiron.toprated10films.ui.model.Actor | ||
import com.commandiron.toprated10films.util.Response | ||
import com.commandiron.toprated10films.domain.model.Actor | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
class GetActors( | ||
private val repository: AppRepository | ||
) { | ||
suspend operator fun invoke(page: Int): Flow<Response<List<Actor>>> = repository.getActors(page) | ||
suspend operator fun invoke(): Flow<PagingData<Actor>> = repository.getActors() | ||
} |
35 changes: 0 additions & 35 deletions
35
app/src/main/java/com/commandiron/toprated10films/ui/model/Actor.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
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
52 changes: 52 additions & 0 deletions
52
...java/com/commandiron/toprated10films/ui/presentation/actor/components/LazyGridScopeExt.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,52 @@ | ||
package com.commandiron.toprated10films.ui.presentation.actor.components | ||
|
||
import android.annotation.SuppressLint | ||
import android.os.Parcel | ||
import android.os.Parcelable | ||
import androidx.compose.foundation.lazy.grid.LazyGridItemScope | ||
import androidx.compose.foundation.lazy.grid.LazyGridScope | ||
import androidx.compose.runtime.Composable | ||
import androidx.paging.compose.LazyPagingItems | ||
|
||
fun <T : Any> LazyGridScope.items( | ||
items: LazyPagingItems<T>, | ||
key: ((item: T) -> Any)? = null, | ||
itemContent: @Composable LazyGridItemScope.(item: T?) -> Unit | ||
) { | ||
items( | ||
count = items.itemCount, | ||
key = if (key == null) null else { index -> | ||
val item = items.peek(index) | ||
if (item == null) { | ||
PagingPlaceholderKey(index) | ||
} else { | ||
key(item) | ||
} | ||
} | ||
) { index -> | ||
itemContent(items[index]) | ||
} | ||
} | ||
|
||
@SuppressLint("BanParcelableUsage") | ||
private data class PagingPlaceholderKey(private val index: Int) : Parcelable { | ||
override fun writeToParcel(parcel: Parcel, flags: Int) { | ||
parcel.writeInt(index) | ||
} | ||
|
||
override fun describeContents(): Int { | ||
return 0 | ||
} | ||
|
||
companion object { | ||
@Suppress("unused") | ||
@JvmField | ||
val CREATOR: Parcelable.Creator<PagingPlaceholderKey> = | ||
object : Parcelable.Creator<PagingPlaceholderKey> { | ||
override fun createFromParcel(parcel: Parcel) = | ||
PagingPlaceholderKey(parcel.readInt()) | ||
|
||
override fun newArray(size: Int) = arrayOfNulls<PagingPlaceholderKey?>(size) | ||
} | ||
} | ||
} |
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