Skip to content

Commit

Permalink
Merge pull request konifar#59 from DroidKaigi/add-paging-for-session-…
Browse files Browse the repository at this point in the history
…detail

Add paging for session detail
  • Loading branch information
konifar authored Jan 11, 2018
2 parents 59933f4 + a289d1e commit 0c39e48
Show file tree
Hide file tree
Showing 11 changed files with 451 additions and 294 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class SessionDataRepository @Inject constructor(
val firstDay = sessionEntities.first().session!!.stime.toLocalDate()
val speakerSessions = sessionEntities
.map { it.toSession(speakerEntities, favList, firstDay) }
.sortedWith(compareBy(
{ it.startTime.getTime().toInt() },
{ it.room.id }
))

speakerSessions + specialSessions
})
.subscribeOn(schedulerProvider.computation())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import android.content.Intent
import android.databinding.DataBindingUtil
import android.os.Bundle
import android.support.v4.app.Fragment
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentStatePagerAdapter
import android.support.v4.view.ViewPager
import dagger.android.AndroidInjector
import dagger.android.DispatchingAndroidInjector
import dagger.android.support.HasSupportFragmentInjector
Expand All @@ -18,11 +20,7 @@ import io.github.droidkaigi.confsched2018.presentation.NavigationController
import io.github.droidkaigi.confsched2018.presentation.Result
import io.github.droidkaigi.confsched2018.presentation.common.activity.BaseActivity
import io.github.droidkaigi.confsched2018.presentation.common.menu.DrawerMenu
import io.github.droidkaigi.confsched2018.util.CustomGlideApp
import io.github.droidkaigi.confsched2018.util.ext.observe
import io.github.droidkaigi.confsched2018.util.ext.toGone
import io.github.droidkaigi.confsched2018.util.ext.toVisible
import io.github.droidkaigi.confsched2018.util.lang
import timber.log.Timber
import javax.inject.Inject

Expand All @@ -44,67 +42,84 @@ class SessionDetailActivity : BaseActivity(), HasSupportFragmentInjector {
ViewModelProviders.of(this, viewModelFactory).get(SessionDetailViewModel::class.java)
}

private val pagerAdapter = SessionDetailFragmentPagerAdapter(supportFragmentManager)

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setSupportActionBar(binding.toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)

sessionDetailViewModel.sessionId =
intent!!.getStringExtra(SessionDetailFragment.EXTRA_SESSION_ID)

sessionDetailViewModel.session.observe(this) { result ->
supportActionBar?.let {
it.setDisplayHomeAsUpEnabled(true)
it.setDisplayShowTitleEnabled(false)
}
sessionDetailViewModel.sessions.observe(this) { result ->
when (result) {
is Result.Success -> {
val session = result.data
bindSessionData(session)
val sessions = result.data
bindSessions(sessions)
}
is Result.Failure -> {
Timber.e(result.e)
}
}
}

navigationController.navigateToDetail(intent.getStringExtra(EXTRA_SESSION_ID))
binding.detailSessionsPager.adapter = pagerAdapter
binding.detailSessionsPager.addOnPageChangeListener(
object : ViewPager.SimpleOnPageChangeListener() {

override fun onPageSelected(position: Int) {
updateSessionIndicator(position)
}
}
)
binding.detailSessionsPrevSession.setOnClickListener {
binding.detailSessionsPager.currentItem = binding.detailSessionsPager.currentItem - 1
}
binding.detailSessionsNextSession.setOnClickListener {
binding.detailSessionsPager.currentItem = binding.detailSessionsPager.currentItem + 1
}
drawerMenu.setup(binding.toolbar, binding.drawerLayout, binding.drawer)
}

fun bindSessionData(session: Session.SpeechSession) {
binding.session = session

binding.fab.setOnClickListener {
sessionDetailViewModel.onFavoriteClick(session)
private fun bindSessions(sessions: List<Session.SpeechSession>) {
val firstAssign = pagerAdapter.sessions.isEmpty() && sessions.isNotEmpty()
pagerAdapter.sessions = sessions
if (firstAssign) {
val sessionId = intent.getStringExtra(EXTRA_SESSION_ID)
val position = sessions.indexOfFirst { it.id == sessionId }
binding
.detailSessionsPager
.setCurrentItem(
position,
false
)
updateSessionIndicator(position)
}
}

binding.sessionTopic.text = session.topic.getNameByLang(lang())
val speakerImages = arrayOf(
binding.speakerImage1,
binding.speakerImage2,
binding.speakerImage3,
binding.speakerImage4,
binding.speakerImage5
)
speakerImages.forEachIndexed { index, imageView ->
if (index < session.speakers.size) {
imageView.toVisible()
val size = resources.getDimensionPixelSize(R.dimen.speaker_image)
CustomGlideApp
.with(this)
.load(session.speakers[index].imageUrl)
.placeholder(R.drawable.ic_person_black_24dp)
.override(size, size)
.dontAnimate()
.transform(CircleCrop())
.into(imageView)
} else {
imageView.toGone()
private fun updateSessionIndicator(position: Int) {
binding.prevSession = pagerAdapter.sessions.getOrNull(position - 1)
binding.nextSession = pagerAdapter.sessions.getOrNull(position + 1)
}

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

class SessionDetailFragmentPagerAdapter(
fragmentManager: FragmentManager
) : FragmentStatePagerAdapter(fragmentManager) {
var sessions: List<Session.SpeechSession> = listOf()
set(value) {
field = value
notifyDataSetChanged()
}

override fun getItem(position: Int): Fragment {
return SessionDetailFragment.newInstance(sessions[position].id)
}

binding.speakers.text = session.speakers.joinToString { it.name }
override fun getCount(): Int = sessions.size
}

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

companion object {
val EXTRA_SESSION_ID = "EXTRA_SESSION_ID"
fun start(context: Context, session: Session) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.load.resource.bitmap.CircleCrop
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.Session
import io.github.droidkaigi.confsched2018.presentation.Result
import io.github.droidkaigi.confsched2018.util.CustomGlideApp
import io.github.droidkaigi.confsched2018.util.ext.observe
import io.github.droidkaigi.confsched2018.util.ext.toGone
import io.github.droidkaigi.confsched2018.util.ext.toVisible
import io.github.droidkaigi.confsched2018.util.lang
import timber.log.Timber
import javax.inject.Inject

Expand All @@ -35,13 +42,13 @@ class SessionDetailFragment : Fragment(), Injectable {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
sessionDetailViewModel.sessionId = arguments!!.getString(EXTRA_SESSION_ID)

sessionDetailViewModel.session.observe(this) { result ->
val sessionId = arguments!!.getString(EXTRA_SESSION_ID)
sessionDetailViewModel.sessions.observe(this) { result ->
when (result) {
is Result.Success -> {
val session = result.data
binding.session = session
val sessions = result.data
bindSession(sessions.first { it.id == sessionId })
}
is Result.Failure -> {
Timber.e(result.e)
Expand All @@ -50,6 +57,38 @@ class SessionDetailFragment : Fragment(), Injectable {
}
}

private fun bindSession(session: Session.SpeechSession) {
binding.session = session
binding.fab.setOnClickListener {
sessionDetailViewModel.onFavoriteClick(session)
}
binding.sessionTopic.text = session.topic.getNameByLang(lang())
val speakerImages = arrayOf(
binding.speakerImage1,
binding.speakerImage2,
binding.speakerImage3,
binding.speakerImage4,
binding.speakerImage5
)
speakerImages.forEachIndexed { index, imageView ->
if (index < session.speakers.size) {
imageView.toVisible()
val size = resources.getDimensionPixelSize(R.dimen.speaker_image)
CustomGlideApp
.with(this)
.load(session.speakers[index].imageUrl)
.placeholder(R.drawable.ic_person_black_24dp)
.override(size, size)
.dontAnimate()
.transform(CircleCrop())
.into(imageView)
} else {
imageView.toGone()
}
}
binding.speakers.text = session.speakers.joinToString { it.name }
}

companion object {
val EXTRA_SESSION_ID = "EXTRA_SESSION_ID"
fun newInstance(sessionId: String): SessionDetailFragment = SessionDetailFragment().apply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ class SessionDetailViewModel @Inject constructor(
private val schedulerProvider: SchedulerProvider
) : ViewModel() {
private val compositeDisposable: CompositeDisposable = CompositeDisposable()
lateinit var sessionId: String

val session: LiveData<Result<Session.SpeechSession>> by lazy {
val sessions: LiveData<Result<List<Session.SpeechSession>>> by lazy {
repository.sessions
.map { sessions ->
sessions
.filterIsInstance<Session.SpeechSession>()
.first { it.id == sessionId }
}
.toResult(schedulerProvider)
.toLiveData()
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/drawable/ic_arrow_back_black_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0"
>
<path
android:fillColor="#FF000000"
android:pathData="M20,11H7.83l5.59,-5.59L12,4l-8,8 8,8 1.41,-1.41L7.83,13H20v-2z"
/>
</vector>
11 changes: 11 additions & 0 deletions app/src/main/res/drawable/ic_arrow_forward_black_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0"
>
<path
android:fillColor="#FF000000"
android:pathData="M12,4l-1.41,1.41L16.17,11H4v2h12.17l-5.58,5.59L12,20l8,-8z"
/>
</vector>
Loading

0 comments on commit 0c39e48

Please sign in to comment.