prevent adding multiple tabs of the same type (#3390)

* prevent adding multiple tabs of the same type

* use Objects.hash
This commit is contained in:
Konrad Pozniak 2023-03-01 19:59:40 +01:00 committed by GitHub
parent 1b6108ca94
commit b8c77a795c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -24,6 +24,7 @@ import com.keylesspalace.tusky.components.timeline.TimelineFragment
import com.keylesspalace.tusky.components.timeline.viewmodel.TimelineViewModel import com.keylesspalace.tusky.components.timeline.viewmodel.TimelineViewModel
import com.keylesspalace.tusky.components.trending.TrendingFragment import com.keylesspalace.tusky.components.trending.TrendingFragment
import com.keylesspalace.tusky.fragment.NotificationsFragment 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 */ /** 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<String>) -> Fragment, val fragment: (List<String>) -> Fragment,
val arguments: List<String> = emptyList(), val arguments: List<String> = emptyList(),
val title: (Context) -> String = { context -> context.getString(text) } 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<TabData>.hasTab(id: String): Boolean = this.find { it.id == id } != null fun List<TabData>.hasTab(id: String): Boolean = this.find { it.id == id } != null
fun createTabDataFromId(id: String, arguments: List<String> = emptyList()): TabData { fun createTabDataFromId(id: String, arguments: List<String> = emptyList()): TabData {
return when (id) { return when (id) {
HOME -> TabData( HOME -> TabData(
HOME, id = HOME,
R.string.title_home, text = R.string.title_home,
R.drawable.ic_home_24dp, icon = R.drawable.ic_home_24dp,
{ TimelineFragment.newInstance(TimelineViewModel.Kind.HOME) } fragment = { TimelineFragment.newInstance(TimelineViewModel.Kind.HOME) }
) )
NOTIFICATIONS -> TabData( NOTIFICATIONS -> TabData(
NOTIFICATIONS, id = NOTIFICATIONS,
R.string.title_notifications, text = R.string.title_notifications,
R.drawable.ic_notifications_24dp, icon = R.drawable.ic_notifications_24dp,
{ NotificationsFragment.newInstance() } fragment = { NotificationsFragment.newInstance() }
) )
LOCAL -> TabData( LOCAL -> TabData(
LOCAL, id = LOCAL,
R.string.title_public_local, text = R.string.title_public_local,
R.drawable.ic_local_24dp, icon = R.drawable.ic_local_24dp,
{ TimelineFragment.newInstance(TimelineViewModel.Kind.PUBLIC_LOCAL) } fragment = { TimelineFragment.newInstance(TimelineViewModel.Kind.PUBLIC_LOCAL) }
) )
FEDERATED -> TabData( FEDERATED -> TabData(
FEDERATED, id = FEDERATED,
R.string.title_public_federated, text = R.string.title_public_federated,
R.drawable.ic_public_24dp, icon = R.drawable.ic_public_24dp,
{ TimelineFragment.newInstance(TimelineViewModel.Kind.PUBLIC_FEDERATED) } fragment = { TimelineFragment.newInstance(TimelineViewModel.Kind.PUBLIC_FEDERATED) }
) )
DIRECT -> TabData( DIRECT -> TabData(
DIRECT, id = DIRECT,
R.string.title_direct_messages, text = R.string.title_direct_messages,
R.drawable.ic_reblog_direct_24dp, icon = R.drawable.ic_reblog_direct_24dp,
{ ConversationsFragment.newInstance() } fragment = { ConversationsFragment.newInstance() }
) )
TRENDING -> TabData( TRENDING -> TabData(
TRENDING, id = TRENDING,
R.string.title_public_trending_hashtags, text = R.string.title_public_trending_hashtags,
R.drawable.ic_trending_up_24px, icon = R.drawable.ic_trending_up_24px,
{ TrendingFragment.newInstance() } fragment = { TrendingFragment.newInstance() }
) )
HASHTAG -> TabData( HASHTAG -> TabData(
HASHTAG, id = HASHTAG,
R.string.hashtags, text = R.string.hashtags,
R.drawable.ic_hashtag, icon = R.drawable.ic_hashtag,
{ args -> TimelineFragment.newHashtagInstance(args) }, fragment = { args -> TimelineFragment.newHashtagInstance(args) },
arguments, arguments = arguments,
{ context -> arguments.joinToString(separator = " ") { context.getString(R.string.title_tag, it) } } title = { context -> arguments.joinToString(separator = " ") { context.getString(R.string.title_tag, it) } }
) )
LIST -> TabData( LIST -> TabData(
LIST, id = LIST,
R.string.list, text = R.string.list,
R.drawable.ic_list, icon = R.drawable.ic_list,
{ args -> TimelineFragment.newInstance(TimelineViewModel.Kind.LIST, args.getOrNull(0).orEmpty()) }, fragment = { args -> TimelineFragment.newInstance(TimelineViewModel.Kind.LIST, args.getOrNull(0).orEmpty()) },
arguments, arguments = arguments,
{ arguments.getOrNull(1).orEmpty() } title = { arguments.getOrNull(1).orEmpty() }
) )
else -> throw IllegalArgumentException("unknown tab type") else -> throw IllegalArgumentException("unknown tab type")
} }