Skip to content

Commit

Permalink
Merge pull request #773 from DroidKaigi/feature/issue550_add_equalabl…
Browse files Browse the repository at this point in the history
…e_content_provider

[Based on #766] add equalable content provider
  • Loading branch information
takahirom authored Feb 3, 2019
2 parents 01b4b57 + d0a2d5d commit 47862c5
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import io.github.droidkaigi.confsched2019.di.PageScope
import io.github.droidkaigi.confsched2019.ext.android.changed
import io.github.droidkaigi.confsched2019.ext.android.requireValue
import io.github.droidkaigi.confsched2019.item.DividerItem
import io.github.droidkaigi.confsched2019.item.HeaderItem
import io.github.droidkaigi.confsched2019.item.SearchSectionHeaderItem
import io.github.droidkaigi.confsched2019.model.ServiceSession
import io.github.droidkaigi.confsched2019.model.Session
import io.github.droidkaigi.confsched2019.model.SpeechSession
Expand Down Expand Up @@ -98,7 +98,9 @@ class SearchFragment : DaggerFragment() {
result.query
)
}.sortedBy { it.speaker.name.toUpperCase() }
setHeader(HeaderItem(requireContext().getString(R.string.speaker_label)))
setHeader(
SearchSectionHeaderItem(requireContext().getString(R.string.speaker_label))
)
addAll(speakers)
setFooter(DividerItem())
}
Expand Down Expand Up @@ -136,7 +138,9 @@ class SearchFragment : DaggerFragment() {
else -> StickyHeaderItemDecoration.DEFAULT_TITLE
}
}
setHeader(HeaderItem(requireContext().getString(R.string.session_title)))
setHeader(
SearchSectionHeaderItem(requireContext().getString(R.string.session_title))
)
addAll(sessions)
}
groupAdapter.update(items)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.navigation.NavDirections
import com.squareup.inject.assisted.Assisted
import com.squareup.inject.assisted.AssistedInject
import com.xwray.groupie.databinding.BindableItem
import io.github.droidkaigi.confsched2019.item.EqualableContentsProvider
import io.github.droidkaigi.confsched2019.model.ServiceSession
import io.github.droidkaigi.confsched2019.model.defaultLang
import io.github.droidkaigi.confsched2019.session.R
Expand All @@ -19,7 +20,7 @@ class ServiceSessionItem @AssistedInject constructor(
val sessionContentsActionCreator: SessionContentsActionCreator
) : BindableItem<ItemServiceSessionBinding>(
session.id.hashCode().toLong()
), SessionItem {
), SessionItem, EqualableContentsProvider {
val serviceSession get() = session

@AssistedInject.Factory
Expand Down Expand Up @@ -63,18 +64,13 @@ class ServiceSessionItem @AssistedInject constructor(

override fun getLayout(): Int = R.layout.item_service_session

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as ServiceSessionItem
override fun providerEqualableContents(): Array<*> = arrayOf(session)

if (session != other.session) return false

return true
override fun equals(other: Any?): Boolean {
return isSameContents(other)
}

override fun hashCode(): Int {
return session.hashCode()
return contentsHash()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.squareup.inject.assisted.Assisted
import com.squareup.inject.assisted.AssistedInject
import com.squareup.picasso.Picasso
import com.xwray.groupie.databinding.BindableItem
import io.github.droidkaigi.confsched2019.item.EqualableContentsProvider
import io.github.droidkaigi.confsched2019.model.Speaker
import io.github.droidkaigi.confsched2019.session.R
import io.github.droidkaigi.confsched2019.session.databinding.ItemSpeakerBinding
Expand All @@ -18,7 +19,8 @@ class SpeakerItem @AssistedInject constructor(
@Assisted val clickNavDirection: NavDirections,
@Assisted val query: String?,
val navController: NavController
) : BindableItem<ItemSpeakerBinding>(speaker.id.hashCode().toLong()) {
) : BindableItem<ItemSpeakerBinding>(speaker.id.hashCode().toLong()),
EqualableContentsProvider {
@AssistedInject.Factory
interface Factory {
fun create(
Expand Down Expand Up @@ -64,19 +66,20 @@ class SpeakerItem @AssistedInject constructor(
}
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as SpeakerItem
override fun providerEqualableContents(): Array<*> = arrayOf(
speaker,
if (isContainsQuery()) query else null
)

if (speaker != other.speaker) return false
if (query != other.query) return false
private fun isContainsQuery() = query?.let {
speaker.name.toLowerCase().contains(it.toLowerCase())
} ?: false

return true
override fun equals(other: Any?): Boolean {
return isSameContents(other)
}

override fun hashCode(): Int {
return speaker.hashCode() + query.hashCode()
return contentsHash()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.squareup.inject.assisted.AssistedInject
import com.squareup.picasso.Picasso
import com.squareup.picasso.Target
import com.xwray.groupie.databinding.BindableItem
import io.github.droidkaigi.confsched2019.item.EqualableContentsProvider
import io.github.droidkaigi.confsched2019.model.Speaker
import io.github.droidkaigi.confsched2019.model.SpeechSession
import io.github.droidkaigi.confsched2019.model.defaultLang
Expand All @@ -27,7 +28,6 @@ import io.github.droidkaigi.confsched2019.session.ui.actioncreator.SessionConten
import io.github.droidkaigi.confsched2019.session.ui.bindingadapter.setHighlightText
import io.github.droidkaigi.confsched2019.util.lazyWithParam
import jp.wasabeef.picasso.transformations.CropCircleTransformation
import java.lang.IllegalArgumentException
import kotlin.math.max

class SpeechSessionItem @AssistedInject constructor(
Expand All @@ -40,7 +40,7 @@ class SpeechSessionItem @AssistedInject constructor(
val sessionContentsActionCreator: SessionContentsActionCreator
) : BindableItem<ItemSessionBinding>(
session.id.hashCode().toLong()
), SessionItem {
), SessionItem, EqualableContentsProvider {
val speechSession get() = session

@AssistedInject.Factory
Expand Down Expand Up @@ -199,19 +199,27 @@ class SpeechSessionItem @AssistedInject constructor(

override fun getLayout(): Int = R.layout.item_session

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as SpeechSessionItem

if (session != other.session) return false
if (query == null || other.query == null || query != other.query) return false
override fun providerEqualableContents(): Array<*> = arrayOf(
session,
if (isContainsQuery()) query else null
)

private fun isContainsQuery() = query?.let {
when {
session.title.getByLang(defaultLang()).toLowerCase()
.contains(query.toLowerCase()) -> true
session.speakers.find {
it.name.toLowerCase().contains(query.toLowerCase())
} != null -> true
else -> false
}
} ?: false

return true
override fun equals(other: Any?): Boolean {
return isSameContents(other)
}

override fun hashCode(): Int {
return session.hashCode() + query.hashCode()
return contentsHash()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.navigation.NavDirections
import com.squareup.inject.assisted.Assisted
import com.squareup.inject.assisted.AssistedInject
import com.xwray.groupie.databinding.BindableItem
import io.github.droidkaigi.confsched2019.item.EqualableContentsProvider
import io.github.droidkaigi.confsched2019.model.ServiceSession
import io.github.droidkaigi.confsched2019.model.defaultLang
import io.github.droidkaigi.confsched2019.session.R
Expand All @@ -17,7 +18,7 @@ class TabularServiceSessionItem @AssistedInject constructor(
private val navController: NavController
) : BindableItem<ItemTabularServiceSessionBinding>(
session.id.hashCode().toLong()
), SessionItem {
), SessionItem, EqualableContentsProvider {

@AssistedInject.Factory
interface Factory {
Expand Down Expand Up @@ -46,18 +47,13 @@ class TabularServiceSessionItem @AssistedInject constructor(

override fun getLayout() = R.layout.item_tabular_service_session

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as TabularServiceSessionItem
override fun providerEqualableContents(): Array<*> = arrayOf(session)

if (session != other.session) return false

return true
override fun equals(other: Any?): Boolean {
return isSameContents(other)
}

override fun hashCode(): Int {
return session.hashCode()
return contentsHash()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.squareup.picasso.Picasso
import com.xwray.groupie.GroupAdapter
import com.xwray.groupie.databinding.BindableItem
import com.xwray.groupie.databinding.ViewHolder
import io.github.droidkaigi.confsched2019.item.EqualableContentsProvider
import io.github.droidkaigi.confsched2019.model.Speaker
import io.github.droidkaigi.confsched2019.model.SpeechSession
import io.github.droidkaigi.confsched2019.model.defaultLang
Expand All @@ -26,7 +27,7 @@ class TabularSpeechSessionItem @AssistedInject constructor(
private val navController: NavController
) : BindableItem<ItemTabularSpeechSessionBinding>(
session.id.hashCode().toLong()
), SessionItem {
), SessionItem, EqualableContentsProvider {

@AssistedInject.Factory
interface Factory {
Expand Down Expand Up @@ -82,19 +83,14 @@ class TabularSpeechSessionItem @AssistedInject constructor(

override fun getLayout() = R.layout.item_tabular_speech_session

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as TabularSpeechSessionItem
override fun providerEqualableContents(): Array<*> = arrayOf(session)

if (session != other.session) return false

return true
override fun equals(other: Any?): Boolean {
return isSameContents(other)
}

override fun hashCode(): Int {
return session.hashCode()
return contentsHash()
}

private class SpeakerIconItem(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ class DividerItem : Item<ViewHolder>() {

override fun bind(viewHolder: ViewHolder, position: Int) {
}

override fun isSameAs(other: Item<*>?): Boolean {
return other is DividerItem
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.github.droidkaigi.confsched2019.item

import java.util.Arrays

interface EqualableContentsProvider {
fun providerEqualableContents(): Array<*>

override fun equals(other: Any?): Boolean

override fun hashCode(): Int

fun isSameContents(other: Any?): Boolean {
other ?: return false
if (other !is EqualableContentsProvider) return false
if (other::class != this::class) return false
return other.providerEqualableContents().contentDeepEquals(this.providerEqualableContents())
}

fun contentsHash(): Int {
return Arrays.deepHashCode(arrayOf(this::class, this.providerEqualableContents()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,21 @@ import com.xwray.groupie.Item
import com.xwray.groupie.ViewHolder
import io.github.droidkaigi.confsched2019.widget.component.R

class HeaderItem(private val title: CharSequence) : Item<ViewHolder>() {

class SearchSectionHeaderItem(
private val title: CharSequence
) : Item<ViewHolder>(title.hashCode().toLong()),
EqualableContentsProvider {
override fun getLayout(): Int = R.layout.view_section_header

override fun bind(viewHolder: ViewHolder, position: Int) {
viewHolder.itemView.findViewById<TextView>(R.id.section_title).apply {
text = title
}
}

override fun providerEqualableContents(): Array<*> = arrayOf(title)

override fun equals(other: Any?): Boolean = isSameContents(other)

override fun hashCode(): Int = contentsHash()
}

0 comments on commit 47862c5

Please sign in to comment.