Skip to content
Permalink
Browse files

Merge pull request #356 from chrisbanes/cb/trending-grid-transition

Fix image transition with new trending item layout
  • Loading branch information...
chrisbanes committed Jul 7, 2019
2 parents 76383b7 + d148f2a commit ce0bfddadfc42a285f4443bccf93475c1506dd83
@@ -73,21 +73,35 @@ internal class DiscoverFragment : TiviMvRxFragment() {

controller.callbacks = object : DiscoverEpoxyController.Callbacks {
override fun onTrendingHeaderClicked(items: List<TrendingEntryWithShow>) {
findNavController().navigate(R.id.action_discover_to_trending, null, null,
listItemSharedElementHelper.createForItems(items).toFragmentNavigatorExtras())
val extras = listItemSharedElementHelper.createForItems(items)

findNavController().navigate(
R.id.action_discover_to_trending,
null,
null,
extras.toFragmentNavigatorExtras())
}

override fun onPopularHeaderClicked(items: List<PopularEntryWithShow>) {
findNavController().navigate(R.id.action_discover_to_popular, null, null,
listItemSharedElementHelper.createForItems(items).toFragmentNavigatorExtras())
val extras = listItemSharedElementHelper.createForItems(items)

findNavController().navigate(
R.id.action_discover_to_popular,
null,
null,
extras.toFragmentNavigatorExtras())
}

override fun onItemClicked(viewHolderId: Long, item: EntryWithShow<out Entry>) {
val direction = DiscoverFragmentDirections.actionDiscoverToActivityShowDetails(item.show.id)

val elements = listItemSharedElementHelper.createForId(viewHolderId, "poster") {
it.findViewById(R.id.show_poster)
}

findNavController().navigate(
direction,
listItemSharedElementHelper.createForId(viewHolderId, "poster")
.toActivityNavigatorExtras(requireActivity())
elements.toActivityNavigatorExtras(requireActivity())
)
}
}
@@ -42,7 +42,7 @@ class FollowedFragment : TiviMvRxFragment() {
@Inject lateinit var followedViewModelFactory: FollowedViewModel.Factory

private val listItemSharedElementHelper by lazy(LazyThreadSafetyMode.NONE) {
ListItemSharedElementHelper(binding.followedRv) { it.findViewById(R.id.show_poster) }
ListItemSharedElementHelper(binding.followedRv)
}

@Inject lateinit var controller: FollowedEpoxyController
@@ -60,10 +60,13 @@ class FollowedFragment : TiviMvRxFragment() {
controller.callbacks = object : FollowedEpoxyController.Callbacks {
override fun onItemClicked(item: FollowedShowEntryWithShow) {
val direction = FollowedFragmentDirections.actionFollowedToActivityShowDetails(item.show.id)

val extras = listItemSharedElementHelper.createForItem(item, "poster") {
it.findViewById(R.id.show_poster)
}

findNavController().navigate(
direction,
listItemSharedElementHelper.createForItem(item, "poster")
.toActivityNavigatorExtras(requireActivity())
direction, extras.toActivityNavigatorExtras(requireActivity())
)
}

@@ -56,9 +56,7 @@ internal class SearchFragment : TiviMvRxFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

listItemSharedElementHelper = ListItemSharedElementHelper(binding.recyclerview) {
it.findViewById(R.id.show_poster)
}
listItemSharedElementHelper = ListItemSharedElementHelper(binding.recyclerview)

binding.recyclerview.apply {
setController(controller)
@@ -68,8 +66,10 @@ internal class SearchFragment : TiviMvRxFragment() {
controller.callbacks = object : SearchEpoxyController.Callbacks {
override fun onSearchItemClicked(show: TiviShow) {
// We should really use AndroidX navigation here, but this fragment isn't in the tree
appNavigator.startShowDetails(show.id,
listItemSharedElementHelper.createForId(show.id, "poster"))
val extras = listItemSharedElementHelper.createForId(show.id, "poster") {
it.findViewById(R.id.show_poster)
}
appNavigator.startShowDetails(show.id, extras)
}
}
}
@@ -44,7 +44,7 @@ class WatchedFragment : TiviMvRxFragment() {
@Inject lateinit var controller: WatchedEpoxyController

private val listItemSharedElementHelper by lazy(LazyThreadSafetyMode.NONE) {
ListItemSharedElementHelper(binding.watchedRv) { it.findViewById(R.id.show_poster) }
ListItemSharedElementHelper(binding.watchedRv)
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
@@ -60,10 +60,14 @@ class WatchedFragment : TiviMvRxFragment() {
controller.callbacks = object : WatchedEpoxyController.Callbacks {
override fun onItemClicked(item: WatchedShowEntryWithShow) {
val direction = WatchedFragmentDirections.actionWatchedToActivityShowDetails(item.show.id)

val extras = listItemSharedElementHelper.createForItem(item, "poster") {
it.findViewById(R.id.show_poster)
}

findNavController().navigate(
direction,
listItemSharedElementHelper.createForItem(item, "poster")
.toActivityNavigatorExtras(requireActivity())
extras.toActivityNavigatorExtras(requireActivity())
)
}

@@ -23,33 +23,52 @@ import app.tivi.data.Entry
import app.tivi.data.resultentities.EntryWithShow

class ListItemSharedElementHelper(
private val recyclerView: RecyclerView,
private val viewFinder: (View) -> View = { it }
private val recyclerView: RecyclerView
) {
fun createForItem(item: EntryWithShow<out Entry>, transitionName: String): SharedElementHelper {
return createForId(item.generateStableId(), transitionName)
fun createForItem(
item: EntryWithShow<out Entry>,
transitionName: String,
viewFinder: (View) -> View = defaultViewFinder
): SharedElementHelper {
return createForId(item.generateStableId(), transitionName, viewFinder)
}

fun createForId(viewHolderId: Long, transitionName: String): SharedElementHelper {
fun createForId(
viewHolderId: Long,
transitionName: String,
viewFinder: (View) -> View = defaultViewFinder
): SharedElementHelper {
val sharedElementHelper = SharedElementHelper()
addSharedElement(sharedElementHelper, viewHolderId, transitionName)
addSharedElement(sharedElementHelper, viewHolderId, transitionName, viewFinder)
return sharedElementHelper
}

fun createForItems(items: List<EntryWithShow<out Entry>>?): SharedElementHelper {
fun createForItems(
items: List<EntryWithShow<out Entry>>?,
viewFinder: (View) -> View = defaultViewFinder
): SharedElementHelper {
val sharedElementHelper = SharedElementHelper()
items?.forEach {
val homepage = it.show.homepage
if (homepage != null) {
addSharedElement(sharedElementHelper, it.generateStableId(), homepage)
addSharedElement(sharedElementHelper, it.generateStableId(), homepage, viewFinder)
}
}
return sharedElementHelper
}

private fun addSharedElement(helper: SharedElementHelper, viewHolderId: Long, transitionName: String) {
private fun addSharedElement(
helper: SharedElementHelper,
viewHolderId: Long,
transitionName: String,
viewFinder: (View) -> View
) {
recyclerView.findViewHolderForItemId(viewHolderId)?.also {
helper.addSharedElement(viewFinder(it.itemView), transitionName)
}
}

companion object {
private val defaultViewFinder: (View) -> View = { it }
}
}

0 comments on commit ce0bfdd

Please sign in to comment.
You can’t perform that action at this time.