From b8c77a795c942e4089cad20d523cfd183a056be1 Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Wed, 1 Mar 2023 19:59:40 +0100 Subject: [PATCH] prevent adding multiple tabs of the same type (#3390) * prevent adding multiple tabs of the same type * use Objects.hash --- .../java/com/keylesspalace/tusky/TabData.kt | 89 +++++++++++-------- 1 file changed, 52 insertions(+), 37 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/TabData.kt b/app/src/main/java/com/keylesspalace/tusky/TabData.kt index 8d2b2a75..43e59500 100644 --- a/app/src/main/java/com/keylesspalace/tusky/TabData.kt +++ b/app/src/main/java/com/keylesspalace/tusky/TabData.kt @@ -24,6 +24,7 @@ import com.keylesspalace.tusky.components.timeline.TimelineFragment import com.keylesspalace.tusky.components.timeline.viewmodel.TimelineViewModel import com.keylesspalace.tusky.components.trending.TrendingFragment import com.keylesspalace.tusky.fragment.NotificationsFragment +import java.util.Objects /** this would be a good case for a sealed class, but that does not work nice with Room */ @@ -43,63 +44,77 @@ data class TabData( val fragment: (List) -> Fragment, val arguments: List = emptyList(), val title: (Context) -> String = { context -> context.getString(text) } -) +) { + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as TabData + + if (id != other.id) return false + if (arguments != other.arguments) return false + + return true + } + + override fun hashCode() = Objects.hash(id, arguments) +} fun List.hasTab(id: String): Boolean = this.find { it.id == id } != null fun createTabDataFromId(id: String, arguments: List = emptyList()): TabData { return when (id) { HOME -> TabData( - HOME, - R.string.title_home, - R.drawable.ic_home_24dp, - { TimelineFragment.newInstance(TimelineViewModel.Kind.HOME) } + id = HOME, + text = R.string.title_home, + icon = R.drawable.ic_home_24dp, + fragment = { TimelineFragment.newInstance(TimelineViewModel.Kind.HOME) } ) NOTIFICATIONS -> TabData( - NOTIFICATIONS, - R.string.title_notifications, - R.drawable.ic_notifications_24dp, - { NotificationsFragment.newInstance() } + id = NOTIFICATIONS, + text = R.string.title_notifications, + icon = R.drawable.ic_notifications_24dp, + fragment = { NotificationsFragment.newInstance() } ) LOCAL -> TabData( - LOCAL, - R.string.title_public_local, - R.drawable.ic_local_24dp, - { TimelineFragment.newInstance(TimelineViewModel.Kind.PUBLIC_LOCAL) } + id = LOCAL, + text = R.string.title_public_local, + icon = R.drawable.ic_local_24dp, + fragment = { TimelineFragment.newInstance(TimelineViewModel.Kind.PUBLIC_LOCAL) } ) FEDERATED -> TabData( - FEDERATED, - R.string.title_public_federated, - R.drawable.ic_public_24dp, - { TimelineFragment.newInstance(TimelineViewModel.Kind.PUBLIC_FEDERATED) } + id = FEDERATED, + text = R.string.title_public_federated, + icon = R.drawable.ic_public_24dp, + fragment = { TimelineFragment.newInstance(TimelineViewModel.Kind.PUBLIC_FEDERATED) } ) DIRECT -> TabData( - DIRECT, - R.string.title_direct_messages, - R.drawable.ic_reblog_direct_24dp, - { ConversationsFragment.newInstance() } + id = DIRECT, + text = R.string.title_direct_messages, + icon = R.drawable.ic_reblog_direct_24dp, + fragment = { ConversationsFragment.newInstance() } ) TRENDING -> TabData( - TRENDING, - R.string.title_public_trending_hashtags, - R.drawable.ic_trending_up_24px, - { TrendingFragment.newInstance() } + id = TRENDING, + text = R.string.title_public_trending_hashtags, + icon = R.drawable.ic_trending_up_24px, + fragment = { TrendingFragment.newInstance() } ) HASHTAG -> TabData( - HASHTAG, - R.string.hashtags, - R.drawable.ic_hashtag, - { args -> TimelineFragment.newHashtagInstance(args) }, - arguments, - { context -> arguments.joinToString(separator = " ") { context.getString(R.string.title_tag, it) } } + id = HASHTAG, + text = R.string.hashtags, + icon = R.drawable.ic_hashtag, + fragment = { args -> TimelineFragment.newHashtagInstance(args) }, + arguments = arguments, + title = { context -> arguments.joinToString(separator = " ") { context.getString(R.string.title_tag, it) } } ) LIST -> TabData( - LIST, - R.string.list, - R.drawable.ic_list, - { args -> TimelineFragment.newInstance(TimelineViewModel.Kind.LIST, args.getOrNull(0).orEmpty()) }, - arguments, - { arguments.getOrNull(1).orEmpty() } + id = LIST, + text = R.string.list, + icon = R.drawable.ic_list, + fragment = { args -> TimelineFragment.newInstance(TimelineViewModel.Kind.LIST, args.getOrNull(0).orEmpty()) }, + arguments = arguments, + title = { arguments.getOrNull(1).orEmpty() } ) else -> throw IllegalArgumentException("unknown tab type") }