-
-
Notifications
You must be signed in to change notification settings - Fork 402
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Collections: add movie collection screen
- Loading branch information
1 parent
3cb4593
commit 18d5bff
Showing
7 changed files
with
313 additions
and
2 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
62 changes: 62 additions & 0 deletions
62
app/src/main/java/com/battlelancer/seriesguide/movies/collection/MovieCollectionActivity.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,62 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright 2024 Uwe Trottmann | ||
|
||
package com.battlelancer.seriesguide.movies.collection | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import androidx.fragment.app.replace | ||
import com.battlelancer.seriesguide.R | ||
import com.battlelancer.seriesguide.ui.BaseActivity | ||
import com.battlelancer.seriesguide.ui.SinglePaneActivity | ||
import com.battlelancer.seriesguide.util.commitReorderingAllowed | ||
|
||
/** | ||
* Hosts a [MovieCollectionFragment], contains a large top app bar that lifts on scroll, | ||
* displays close navigation indicator. | ||
*/ | ||
class MovieCollectionActivity : BaseActivity() { | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
val binding = SinglePaneActivity.onCreateFor(this) | ||
binding.sgAppBarLayout.sgAppBarLayout.liftOnScrollTargetViewId = | ||
MovieCollectionFragment.liftOnScrollTargetViewId | ||
setupActionBar() | ||
|
||
val title = intent.getStringExtra(EXTRA_TITLE) | ||
?: getString(R.string.title_similar_movies) | ||
setTitle(title) | ||
|
||
val collectionId = intent.getIntExtra(EXTRA_COLLECTION_ID, 0) | ||
if (collectionId <= 0) { | ||
throw IllegalArgumentException("EXTRA_COLLECTION_ID must be positive, but was $collectionId") | ||
} | ||
|
||
if (savedInstanceState == null) { | ||
supportFragmentManager.commitReorderingAllowed { | ||
replace<MovieCollectionFragment>( | ||
R.id.content_frame, | ||
args = MovieCollectionFragment.buildArgs(collectionId) | ||
) | ||
} | ||
} | ||
} | ||
|
||
override fun setupActionBar() { | ||
super.setupActionBar() | ||
supportActionBar?.setHomeAsUpIndicator(R.drawable.ic_clear_24dp) | ||
supportActionBar?.setDisplayHomeAsUpEnabled(true) | ||
} | ||
|
||
companion object { | ||
private const val EXTRA_COLLECTION_ID = "COLLECTION_ID" | ||
private const val EXTRA_TITLE = "ARG_TITLE" | ||
|
||
fun intent(context: Context, collectionId: Int, title: String): Intent = | ||
Intent(context, MovieCollectionActivity::class.java) | ||
.putExtra(EXTRA_COLLECTION_ID, collectionId) | ||
.putExtra(EXTRA_TITLE, title) | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
app/src/main/java/com/battlelancer/seriesguide/movies/collection/MovieCollectionFragment.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,124 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright 2024 Uwe Trottmann | ||
|
||
package com.battlelancer.seriesguide.movies.collection | ||
|
||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.core.os.bundleOf | ||
import androidx.core.view.isGone | ||
import androidx.core.view.isVisible | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.viewModels | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.lifecycleScope | ||
import com.battlelancer.seriesguide.R | ||
import com.battlelancer.seriesguide.databinding.FragmentShowsSimilarBinding | ||
import com.battlelancer.seriesguide.movies.base.BaseMovieListAdapter | ||
import com.battlelancer.seriesguide.movies.base.SearchMenuProvider | ||
import com.battlelancer.seriesguide.movies.collection.MovieCollectionViewModel.MoviesListUiState | ||
import com.battlelancer.seriesguide.ui.AutoGridLayoutManager | ||
import com.battlelancer.seriesguide.util.ThemeUtils | ||
import com.battlelancer.seriesguide.util.ViewTools | ||
import kotlinx.coroutines.flow.collectLatest | ||
import kotlinx.coroutines.launch | ||
|
||
/** | ||
* Displays movies of a TMDB movie collection. | ||
* | ||
* Re-uses [FragmentShowsSimilarBinding] layout. | ||
*/ | ||
class MovieCollectionFragment : Fragment() { | ||
|
||
private val viewModel: MovieCollectionViewModel by viewModels( | ||
extrasProducer = { | ||
MovieCollectionViewModel.creationExtras( | ||
defaultViewModelCreationExtras, | ||
requireArguments().getInt(ARG_COLLECTION_ID) | ||
) | ||
}, | ||
factoryProducer = { MovieCollectionViewModel.Factory } | ||
) | ||
private var binding: FragmentShowsSimilarBinding? = null | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View { | ||
return FragmentShowsSimilarBinding.inflate(layoutInflater, container, false) | ||
.also { binding = it } | ||
.root | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
val binding = binding!! | ||
ThemeUtils.applyBottomPaddingForNavigationBar(binding.recyclerViewShowsSimilar) | ||
ThemeUtils.applyBottomMarginForNavigationBar(binding.textViewPoweredByShowsSimilar) | ||
|
||
binding.swipeRefreshLayoutShowsSimilar.apply { | ||
ViewTools.setSwipeRefreshLayoutColors(requireActivity().theme, this) | ||
setOnRefreshListener { viewModel.refresh() } | ||
} | ||
binding.emptyViewShowsSimilar.setButtonClickListener { | ||
binding.swipeRefreshLayoutShowsSimilar.isRefreshing = true | ||
viewModel.refresh() | ||
} | ||
|
||
binding.swipeRefreshLayoutShowsSimilar.isRefreshing = true | ||
binding.emptyViewShowsSimilar.isGone = true | ||
|
||
val adapter = BaseMovieListAdapter(requireContext()) | ||
binding.recyclerViewShowsSimilar.also { | ||
it.setHasFixedSize(true) | ||
it.layoutManager = | ||
AutoGridLayoutManager(it.context, R.dimen.movie_grid_columnWidth, 1, 1) | ||
it.adapter = adapter | ||
} | ||
|
||
viewLifecycleOwner.lifecycleScope.launch { | ||
viewModel.movies.collectLatest { | ||
binding.swipeRefreshLayoutShowsSimilar.isRefreshing = false | ||
when (it) { | ||
is MoviesListUiState.Error -> { | ||
binding.recyclerViewShowsSimilar.isGone = true | ||
binding.emptyViewShowsSimilar.apply { | ||
isVisible = true | ||
setMessage(it.message) | ||
} | ||
} | ||
|
||
is MoviesListUiState.Success -> { | ||
// Note: no need to handle empty state, collection should never be empty | ||
adapter.submitList(it.movies) | ||
binding.recyclerViewShowsSimilar.isVisible = true | ||
binding.emptyViewShowsSimilar.isGone = true | ||
} | ||
} | ||
} | ||
} | ||
|
||
requireActivity().addMenuProvider( | ||
SearchMenuProvider(requireContext()), | ||
viewLifecycleOwner, | ||
Lifecycle.State.RESUMED | ||
) | ||
} | ||
|
||
override fun onDestroyView() { | ||
super.onDestroyView() | ||
binding = null | ||
} | ||
|
||
companion object { | ||
val liftOnScrollTargetViewId = R.id.recyclerViewShowsSimilar | ||
|
||
private const val ARG_COLLECTION_ID = "COLLECTION_ID" | ||
|
||
fun buildArgs(collectionId: Int): Bundle = bundleOf( | ||
ARG_COLLECTION_ID to collectionId | ||
) | ||
} | ||
|
||
} |
100 changes: 100 additions & 0 deletions
100
app/src/main/java/com/battlelancer/seriesguide/movies/collection/MovieCollectionViewModel.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,100 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright 2024 Uwe Trottmann | ||
|
||
package com.battlelancer.seriesguide.movies.collection | ||
|
||
import android.app.Application | ||
import androidx.lifecycle.AndroidViewModel | ||
import androidx.lifecycle.ViewModelProvider | ||
import androidx.lifecycle.viewModelScope | ||
import androidx.lifecycle.viewmodel.CreationExtras | ||
import androidx.lifecycle.viewmodel.MutableCreationExtras | ||
import androidx.lifecycle.viewmodel.initializer | ||
import androidx.lifecycle.viewmodel.viewModelFactory | ||
import com.battlelancer.seriesguide.R | ||
import com.battlelancer.seriesguide.SgApp | ||
import com.battlelancer.seriesguide.movies.MoviesSettings | ||
import com.battlelancer.seriesguide.tmdbapi.TmdbTools2 | ||
import com.uwetrottmann.androidutils.AndroidUtils | ||
import com.uwetrottmann.tmdb2.entities.BaseMovie | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.SharingStarted | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.flow.flowOn | ||
import kotlinx.coroutines.flow.shareIn | ||
|
||
/** | ||
* Provides a movie collection [Flow] that can be [refresh]ed. | ||
*/ | ||
class MovieCollectionViewModel( | ||
application: Application, | ||
collectionId: Int | ||
) : AndroidViewModel(application) { | ||
|
||
sealed class MoviesListUiState { | ||
data class Success(val movies: List<BaseMovie>) : MoviesListUiState() | ||
data class Error(val message: String) : MoviesListUiState() | ||
} | ||
|
||
private val tmdb = SgApp.getServicesComponent(application).tmdb() | ||
|
||
private val moviesRefreshTrigger = Channel<Unit>(capacity = Channel.CONFLATED) | ||
val movies: Flow<MoviesListUiState> = flow { | ||
for (trigger in moviesRefreshTrigger) { | ||
val collection = TmdbTools2().getMovieCollection( | ||
tmdb, | ||
collectionId, | ||
MoviesSettings.getMoviesLanguage(application) | ||
) | ||
if (collection == null) { | ||
val message = if (AndroidUtils.isNetworkConnected(application)) { | ||
application.getString( | ||
R.string.api_error_generic, | ||
application.getString(R.string.tmdb) | ||
) | ||
} else { | ||
application.getString(R.string.offline) | ||
} | ||
emit(MoviesListUiState.Error(message)) | ||
} else { | ||
val parts = collection.parts | ||
parts?.sortBy { it.release_date } | ||
emit(MoviesListUiState.Success(parts ?: emptyList())) | ||
} | ||
} | ||
}.flowOn(Dispatchers.IO) | ||
.shareIn( | ||
scope = viewModelScope, | ||
started = SharingStarted.Lazily, | ||
replay = 1 | ||
) | ||
|
||
init { | ||
refresh() | ||
} | ||
|
||
fun refresh() { | ||
moviesRefreshTrigger.trySend(Unit) | ||
} | ||
|
||
companion object { | ||
private val KEY_COLLECTION_ID = object : CreationExtras.Key<Int> {} | ||
|
||
val Factory = viewModelFactory { | ||
initializer { | ||
MovieCollectionViewModel( | ||
this[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY]!!, | ||
this[KEY_COLLECTION_ID]!! | ||
) | ||
} | ||
} | ||
|
||
fun creationExtras(defaultExtras: CreationExtras, collectionId: Int) = | ||
MutableCreationExtras(defaultExtras).apply { | ||
set(KEY_COLLECTION_ID, collectionId) | ||
} | ||
} | ||
|
||
} |
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