-
-
Notifications
You must be signed in to change notification settings - Fork 8
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
Showing
16 changed files
with
399 additions
and
12 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
60 changes: 60 additions & 0 deletions
60
app/src/main/java/movie/metropolis/app/model/adapter/MovieViewFromMovie.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,60 @@ | ||
package movie.metropolis.app.model.adapter | ||
|
||
import movie.core.model.Media | ||
import movie.core.model.Movie | ||
import movie.core.model.MovieFavorite | ||
import movie.metropolis.app.model.ImageView | ||
import movie.metropolis.app.model.MovieView | ||
import movie.metropolis.app.model.VideoView | ||
import movie.metropolis.app.util.toStringComponents | ||
import java.text.DateFormat | ||
import java.text.SimpleDateFormat | ||
import java.util.Locale | ||
|
||
data class MovieViewFromMovie( | ||
private val movie: MovieFavorite, | ||
private val media: Iterable<Media> | ||
) : MovieView { | ||
private val yearFormat = SimpleDateFormat("yyyy", Locale.getDefault()) | ||
private val dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM) | ||
private val images | ||
get() = media | ||
.asSequence() | ||
.filterIsInstance<Media.Image>() | ||
.sortedByDescending { it.height * it.width } | ||
|
||
override val id: String | ||
get() = movie.movie.id | ||
override val name: String | ||
get() = movie.movie.name | ||
override val releasedAt: String | ||
get() = yearFormat.format(movie.movie.releasedAt) | ||
override val duration: String | ||
get() = movie.movie.duration.toStringComponents() | ||
override val availableFrom: String | ||
get() = dateFormat.format(movie.movie.screeningFrom) | ||
override val directors: List<String> | ||
get() = emptyList() | ||
override val cast: List<String> | ||
get() = emptyList() | ||
override val countryOfOrigin: String | ||
get() = "" | ||
override val favorite: Boolean | ||
get() = true | ||
override val rating: String? | ||
get() = null | ||
override val poster: ImageView? | ||
get() = images.middleOrNull()?.let { ImageViewFromFeature(it) } | ||
override val posterLarge: ImageView? | ||
get() = images.firstOrNull()?.let { ImageViewFromFeature(it) } | ||
override val video: VideoView? | ||
get() = media | ||
.asSequence() | ||
.filterIsInstance<Media.Video>() | ||
.firstOrNull() | ||
?.let(::VideoViewFromFeature) | ||
|
||
override fun getBase(): Movie { | ||
return movie.movie | ||
} | ||
} |
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/presentation/favorite/FavoriteFacade.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.presentation.favorite | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import movie.metropolis.app.model.MovieView | ||
|
||
interface FavoriteFacade { | ||
|
||
fun get(): Flow<List<MovieView>> | ||
suspend fun remove(view: MovieView) | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
app/src/main/java/movie/metropolis/app/presentation/favorite/FavoriteFacadeFromFeature.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,39 @@ | ||
package movie.metropolis.app.presentation.favorite | ||
|
||
import kotlinx.coroutines.flow.channelFlow | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
import movie.core.EventDetailFeature | ||
import movie.core.FavoriteFeature | ||
import movie.core.model.MovieDetail | ||
import movie.metropolis.app.model.MovieView | ||
import movie.metropolis.app.model.adapter.MovieViewFromMovie | ||
|
||
class FavoriteFacadeFromFeature( | ||
private val favorite: FavoriteFeature, | ||
private val detail: EventDetailFeature | ||
) : FavoriteFacade { | ||
|
||
private val cache = mutableMapOf<String, MovieDetail>() | ||
|
||
override fun get() = channelFlow { | ||
val input = favorite.getAll() | ||
.getOrDefault(emptyList()) | ||
.map { MovieViewFromMovie(it, emptyList()) } | ||
.toMutableList() | ||
val mutex = Mutex() | ||
for ((index, it) in input.withIndex()) launch { | ||
val detail = cache.getOrPut(it.id) { detail.get(it.getBase()).getOrThrow() } | ||
val out = mutex.withLock { | ||
input[index] = it.copy(media = detail.media) | ||
input.toList() | ||
} | ||
send(out) | ||
} | ||
} | ||
|
||
override suspend fun remove(view: MovieView) { | ||
favorite.toggle(view.getBase()) | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
app/src/main/java/movie/metropolis/app/presentation/favorite/FavoriteFacadeRating.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,56 @@ | ||
package movie.metropolis.app.presentation.favorite | ||
|
||
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.core.EventDetailFeature | ||
import movie.core.model.Movie | ||
import movie.metropolis.app.model.MovieView | ||
import movie.metropolis.app.model.adapter.MovieViewWithRating | ||
import movie.rating.MetadataProvider | ||
import movie.rating.MovieDescriptor | ||
import movie.rating.MovieMetadata | ||
import java.util.Calendar | ||
|
||
class FavoriteFacadeRating( | ||
private val origin: FavoriteFacade, | ||
private val rating: MetadataProvider, | ||
private val detail: EventDetailFeature | ||
) : FavoriteFacade by origin { | ||
|
||
private val cache = mutableMapOf<String, MovieMetadata>() | ||
|
||
override fun get(): Flow<List<MovieView>> = origin.get().flatMapLatest { withRating(it) } | ||
|
||
private fun withRating(items: List<MovieView>) = channelFlow { | ||
if (cache.isEmpty()) send(items) | ||
val output = items.toMutableList() | ||
val lock = Mutex() | ||
for ((index, movie) in output.withIndex()) launch { | ||
val base = movie.getBase() | ||
val rating = getRating(base) ?: return@launch | ||
val out = lock.withLock { | ||
output[index] = MovieViewWithRating(movie, rating) | ||
output.toList() | ||
} | ||
send(out) | ||
} | ||
} | ||
|
||
private suspend fun getRating(movie: Movie): MovieMetadata? = cache.getOrPut(movie.id) { | ||
val descriptors = detail.get(movie).map { | ||
val year = Calendar.getInstance().apply { time = it.releasedAt }[Calendar.YEAR] | ||
arrayOf( | ||
MovieDescriptor.Original(it.originalName, year), | ||
MovieDescriptor.Local(it.name, year) | ||
) | ||
}.getOrNull() ?: return null | ||
descriptors.fold(null as MovieMetadata?) { acc, it -> | ||
acc ?: rating.get(it) | ||
} ?: return@getRating null | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
app/src/main/java/movie/metropolis/app/presentation/favorite/FavoriteFacadeReactive.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,23 @@ | ||
package movie.metropolis.app.presentation.favorite | ||
|
||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.flow.flatMapLatest | ||
import kotlinx.coroutines.flow.onStart | ||
import kotlinx.coroutines.flow.receiveAsFlow | ||
import movie.metropolis.app.model.MovieView | ||
|
||
class FavoriteFacadeReactive( | ||
private val origin: FavoriteFacade | ||
) : FavoriteFacade { | ||
|
||
private val trigger = Channel<Unit>() | ||
|
||
override fun get() = trigger.receiveAsFlow() | ||
.onStart { emit(Unit) } | ||
.flatMapLatest { origin.get() } | ||
|
||
override suspend fun remove(view: MovieView) { | ||
origin.remove(view) | ||
trigger.send(Unit) | ||
} | ||
} |
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.