Skip to content

Commit

Permalink
Remove unnecessary properties / move SharedElementCallback to anonymo…
Browse files Browse the repository at this point in the history
…us object
  • Loading branch information
e10dokup committed Feb 4, 2018
1 parent a87e33d commit ceddbbe
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.github.droidkaigi.confsched2018.presentation.detail

import android.annotation.TargetApi
import android.app.Activity
import android.app.SharedElementCallback
import android.arch.lifecycle.ViewModelProvider
Expand Down Expand Up @@ -40,8 +39,6 @@ class SessionDetailActivity :
@Inject lateinit var viewModelFactory: ViewModelProvider.Factory
@Inject lateinit var drawerMenu: DrawerMenu

private var backPressed = false

private val binding: ActivitySessionDetailBinding by lazy {
DataBindingUtil
.setContentView<ActivitySessionDetailBinding>(
Expand All @@ -62,23 +59,6 @@ class SessionDetailActivity :
intent.getStringExtra(EXTRA_SESSION_ID)
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private val sharedElementCallback = object : SharedElementCallback() {
override fun onMapSharedElements(
names: MutableList<String>?,
sharedElements: MutableMap<String, View>?) {
super.onMapSharedElements(names, sharedElements)

if (backPressed) {
val currentFragment = pagerAdapter.findFragmentByPosition(
binding.detailSessionsPager,
binding.detailSessionsPager.currentItem)
sharedElements?.clear()
currentFragment.hideButton()
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportPostponeEnterTransition()
Expand All @@ -93,9 +73,20 @@ class SessionDetailActivity :
sessions = result.data
bindSessions(sessions)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setEnterSharedElementCallback(sharedElementCallback)
setEnterSharedElementCallback(object : SharedElementCallback() {
override fun onMapSharedElements(
names: MutableList<String>?,
sharedElements: MutableMap<String, View>?) {
super.onMapSharedElements(names, sharedElements)

val currentFragment = pagerAdapter.findFragmentByPosition(
binding.detailSessionsPager,
binding.detailSessionsPager.currentItem)
sharedElements?.clear()
currentFragment.hideButton()
}
})
}

}
is Result.Failure -> {
Timber.e(result.e)
Expand All @@ -121,11 +112,9 @@ class SessionDetailActivity :
}
}


override fun supportFragmentInjector(): AndroidInjector<Fragment> = dispatchingAndroidInjector

override fun onBackPressed() {
backPressed = true
if (drawerMenu.closeDrawerIfNeeded()) {
super.onBackPressed()
}
Expand Down Expand Up @@ -158,7 +147,6 @@ class SessionDetailActivity :
fun findFragmentByPosition(viewPager: ViewPager, position: Int): SessionDetailFragment {
return instantiateItem(viewPager, position) as SessionDetailFragment
}

}

companion object {
Expand All @@ -170,9 +158,11 @@ class SessionDetailActivity :
}

fun start(activity: Activity, session: Session, sharedElement: Pair<View, String>) {
val options = ActivityOptionsCompat.makeSceneTransitionAnimation(activity, sharedElement)
activity.startActivity(createIntent(activity, session.id, sharedElement),
options.toBundle())
val options = ActivityOptionsCompat.makeSceneTransitionAnimation(
activity,
sharedElement)
val intent = createIntent(activity, session.id, sharedElement)
activity.startActivity(intent, options.toBundle())
}

fun createIntent(context: Context, sessionId: String): Intent {
Expand All @@ -189,6 +179,5 @@ class SessionDetailActivity :
putExtra(EXTRA_TRANSITION_NAME, sharedElement.second)
}
}

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.droidkaigi.confsched2018.presentation.detail

import android.annotation.TargetApi
import android.arch.lifecycle.ViewModelProvider
import android.arch.lifecycle.ViewModelProviders
import android.graphics.drawable.Animatable
Expand All @@ -11,16 +12,15 @@ import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
import io.github.droidkaigi.confsched2018.R
import io.github.droidkaigi.confsched2018.databinding.FragmentSessionDetailBinding
import io.github.droidkaigi.confsched2018.di.Injectable
import io.github.droidkaigi.confsched2018.model.Level
import io.github.droidkaigi.confsched2018.model.Session
import io.github.droidkaigi.confsched2018.presentation.NavigationController
import io.github.droidkaigi.confsched2018.presentation.Result
import io.github.droidkaigi.confsched2018.presentation.common.view.SpeakersSummaryLayout
import io.github.droidkaigi.confsched2018.util.SessionAlarm
import io.github.droidkaigi.confsched2018.util.ext.addOnetimeOnPreDrawListener
import io.github.droidkaigi.confsched2018.util.ext.context
import io.github.droidkaigi.confsched2018.util.ext.drawable
import io.github.droidkaigi.confsched2018.util.ext.observe
Expand All @@ -39,9 +39,6 @@ class SessionDetailFragment : Fragment(), Injectable {
ViewModelProviders.of(activity!!, viewModelFactory).get(SessionDetailViewModel::class.java)
}

val speakerSummary: SpeakersSummaryLayout
get() = binding.speakerSummary

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand Down Expand Up @@ -84,25 +81,16 @@ class SessionDetailFragment : Fragment(), Injectable {

binding.toolbar.setNavigationOnClickListener { activity?.finish() }

binding.speakerSummary.viewTreeObserver.addOnPreDrawListener(
object : ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean {
binding.speakerSummary.viewTreeObserver.removeOnPreDrawListener(this)

val firstSessionId = (activity as? SessionDetailActivity)?.firstSessionId
?: return true
val transitionName = arguments!!.getString(EXTRA_TRANSITION_NAME)
if (!TextUtils.isEmpty(arguments!!.getString(EXTRA_TRANSITION_NAME))
&& firstSessionId == transitionName
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ViewCompat.setTransitionName(
view.findViewById<View>(R.id.speaker_summary),
transitionName)
activity?.supportStartPostponedEnterTransition()
}
return true
}
})
val firstSessionId = (activity as? SessionDetailActivity)?.firstSessionId ?: return
val transitionName = arguments!!.getString(EXTRA_TRANSITION_NAME)

if (!TextUtils.isEmpty(transitionName)
&& firstSessionId == transitionName
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
initViewTransitions(view)
}


}

private fun bindSession(session: Session.SpeechSession) {
Expand Down Expand Up @@ -142,6 +130,16 @@ class SessionDetailFragment : Fragment(), Injectable {
binding.fab.visibility = View.INVISIBLE
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private fun initViewTransitions(view: View) {
binding.speakerSummary.addOnetimeOnPreDrawListener {
ViewCompat.setTransitionName(
view.findViewById<View>(R.id.speaker_summary),
arguments!!.getString(EXTRA_TRANSITION_NAME))
activity?.supportStartPostponedEnterTransition()
}
}

interface OnClickBottomAreaListener {
fun onClickPrevSession()
fun onClickNextSession()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ class FavoriteSessionsFragment : Fragment(), Injectable {
val sharedElement = Pair(
v.findViewById<View>(R.id.speaker_summary),
sessionItem.session.id)
navigationController.navigateToSessionDetailActivity(sessionItem.session, sharedElement)
navigationController.navigateToSessionDetailActivity(
sessionItem.session,
sharedElement)
})
}
binding.sessionsRecycler.apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ class SearchSessionsFragment : Fragment(), Injectable {
val sharedElement = Pair(
v.findViewById<View>(R.id.speaker_summary),
sessionItem.session.id)
navigationController.navigateToSessionDetailActivity(sessionItem.session, sharedElement)
navigationController.navigateToSessionDetailActivity(
sessionItem.session,
sharedElement)
})
}
binding.searchSessionRecycler.apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ class AllSessionsFragment : Fragment(), Injectable, CurrentSessionScroller {
val sharedElement = Pair(
v.findViewById<View>(R.id.speaker_summary),
sessionItem.session.id)
navigationController.navigateToSessionDetailActivity(sessionItem.session, sharedElement)
navigationController.navigateToSessionDetailActivity(
sessionItem.session,
sharedElement)
})
}
binding.sessionsRecycler.apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ class RoomSessionsFragment : Fragment(), Injectable, CurrentSessionScroller {
val sharedElement = Pair(
v.findViewById<View>(R.id.speaker_summary),
sessionItem.session.id)
navigationController.navigateToSessionDetailActivity(sessionItem.session, sharedElement)
navigationController.navigateToSessionDetailActivity(
sessionItem.session,
sharedElement)
})
}
binding.sessionsRecycler.apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,9 @@ class SpeakerDetailFragment : Fragment(), Injectable {
val sharedElement = Pair(
v.findViewById<View>(R.id.speaker_summary),
sessionItem.session.id)
navigationController.navigateToSessionDetailActivity(sessionItem.session, sharedElement)
navigationController.navigateToSessionDetailActivity(
sessionItem.session,
sharedElement)
})
}
val linearLayoutManager = LinearLayoutManager(context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ class TopicDetailFragment : Fragment(), Injectable {
val sharedElement = Pair(
v.findViewById<View>(R.id.speaker_summary),
sessionItem.session.id)
navigationController.navigateToSessionDetailActivity(sessionItem.session, sharedElement)
navigationController.navigateToSessionDetailActivity(
sessionItem.session,
sharedElement)
}
}
val linearLayoutManager = LinearLayoutManager(context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.github.droidkaigi.confsched2018.util.ext

import android.os.Build
import android.view.View
import android.view.ViewTreeObserver

fun View.setVisible(visible: Boolean) {
if (visible) {
Expand All @@ -23,6 +24,17 @@ fun View.isGone() = visibility == View.GONE

fun View.isVisible() = visibility == View.VISIBLE

fun View.addOnetimeOnPreDrawListener(listener: () -> Unit) {
viewTreeObserver.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean {
viewTreeObserver.removeOnPreDrawListener(this)
listener.invoke()

return true
}
})
}

var View.elevationForPostLollipop: Float
get() = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
elevation
Expand Down

0 comments on commit ceddbbe

Please sign in to comment.