-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added displaying comprehensive actor data on movie detail
- Loading branch information
Showing
14 changed files
with
151 additions
and
45 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
11 changes: 11 additions & 0 deletions
11
app/src/main/java/movie/metropolis/app/model/PersonView.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,11 @@ | ||
package movie.metropolis.app.model | ||
|
||
import androidx.compose.runtime.* | ||
|
||
@Immutable | ||
interface PersonView { | ||
val name: String | ||
val popularity: Int | ||
val image: String | ||
val starredInMovies: Int | ||
} |
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
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
81 changes: 81 additions & 0 deletions
81
app/src/main/java/movie/metropolis/app/presentation/detail/MovieFacadeWithActors.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,81 @@ | ||
package movie.metropolis.app.presentation.detail | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.channelFlow | ||
import kotlinx.coroutines.flow.flatMapLatest | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
import movie.metropolis.app.model.MovieDetailView | ||
import movie.metropolis.app.model.PersonView | ||
import movie.rating.Actor | ||
import movie.rating.ActorProvider | ||
|
||
class MovieFacadeWithActors( | ||
private val origin: MovieFacade, | ||
provider: ActorProvider | ||
) : MovieFacade by origin { | ||
|
||
private val actors = ActorProviderCaching(provider) | ||
|
||
override val movie: Flow<MovieDetailView> = origin.movie.flatMapLatest { | ||
channelFlow { | ||
val it = MovieDetailReplacing(it) | ||
send(it) | ||
val directors = it.directors.toMutableList() | ||
val cast = it.cast.toMutableList() | ||
val directorsMutex = Mutex() | ||
val castMutex = Mutex() | ||
for ((index, d) in directors.withIndex()) launch { | ||
val actor = actors.runCatching { get(d.name) }.getOrNull() ?: return@launch | ||
val out = directorsMutex.withLock { | ||
directors[index] = PersonViewFromActor(actor) | ||
directors.toList() | ||
} | ||
send(it.copy(directors = out)) | ||
} | ||
for ((index, c) in cast.withIndex()) launch { | ||
val actor = actors.runCatching { get(c.name) }.getOrNull() ?: return@launch | ||
val out = castMutex.withLock { | ||
cast[index] = PersonViewFromActor(actor) | ||
cast.toList() | ||
} | ||
send(it.copy(cast = out)) | ||
} | ||
} | ||
} | ||
|
||
class ActorProviderCaching( | ||
private val origin: ActorProvider | ||
) : ActorProvider { | ||
private val cache = mutableMapOf<String, Actor>() | ||
private val mutex = Mutex() | ||
override suspend fun get(query: String): Actor { | ||
return cache[query] ?: mutex.withLock { cache[query] } ?: origin.get(query).also { | ||
mutex.withLock { | ||
cache[query] = it | ||
} | ||
} | ||
} | ||
} | ||
|
||
data class MovieDetailReplacing( | ||
private val origin: MovieDetailView, | ||
override val directors: List<PersonView> = origin.directors, | ||
override val cast: List<PersonView> = origin.cast | ||
) : MovieDetailView by origin | ||
|
||
data class PersonViewFromActor( | ||
private val actor: Actor | ||
) : PersonView { | ||
override val name: String | ||
get() = actor.name | ||
override val popularity: Int | ||
get() = actor.popularity | ||
override val image: String | ||
get() = actor.image | ||
override val starredInMovies: Int | ||
get() = actor.movies.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
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
Oops, something went wrong.