TabData.kt 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Copyright 2019 Conny Duck
  2. *
  3. * This file is a part of Tusky.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it under the terms of the
  6. * GNU General Public License as published by the Free Software Foundation; either version 3 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
  10. * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  11. * Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with Tusky; if not,
  14. * see <http://www.gnu.org/licenses>. */
  15. package com.keylesspalace.tusky
  16. import android.content.Context
  17. import androidx.annotation.DrawableRes
  18. import androidx.annotation.StringRes
  19. import androidx.fragment.app.Fragment
  20. import com.keylesspalace.tusky.components.conversation.ConversationsFragment
  21. import com.keylesspalace.tusky.components.notifications.NotificationsFragment
  22. import com.keylesspalace.tusky.components.timeline.TimelineFragment
  23. import com.keylesspalace.tusky.components.timeline.viewmodel.TimelineViewModel
  24. import com.keylesspalace.tusky.components.trending.TrendingFragment
  25. import java.util.Objects
  26. /** this would be a good case for a sealed class, but that does not work nice with Room */
  27. const val HOME = "Home"
  28. const val NOTIFICATIONS = "Notifications"
  29. const val LOCAL = "Local"
  30. const val FEDERATED = "Federated"
  31. const val DIRECT = "Direct"
  32. const val TRENDING = "Trending"
  33. const val HASHTAG = "Hashtag"
  34. const val LIST = "List"
  35. data class TabData(
  36. val id: String,
  37. @StringRes val text: Int,
  38. @DrawableRes val icon: Int,
  39. val fragment: (List<String>) -> Fragment,
  40. val arguments: List<String> = emptyList(),
  41. val title: (Context) -> String = { context -> context.getString(text) }
  42. ) {
  43. override fun equals(other: Any?): Boolean {
  44. if (this === other) return true
  45. if (javaClass != other?.javaClass) return false
  46. other as TabData
  47. if (id != other.id) return false
  48. if (arguments != other.arguments) return false
  49. return true
  50. }
  51. override fun hashCode() = Objects.hash(id, arguments)
  52. }
  53. fun List<TabData>.hasTab(id: String): Boolean = this.find { it.id == id } != null
  54. fun createTabDataFromId(id: String, arguments: List<String> = emptyList()): TabData {
  55. return when (id) {
  56. HOME -> TabData(
  57. id = HOME,
  58. text = R.string.title_home,
  59. icon = R.drawable.ic_home_24dp,
  60. fragment = { TimelineFragment.newInstance(TimelineViewModel.Kind.HOME) }
  61. )
  62. NOTIFICATIONS -> TabData(
  63. id = NOTIFICATIONS,
  64. text = R.string.title_notifications,
  65. icon = R.drawable.ic_notifications_24dp,
  66. fragment = { NotificationsFragment.newInstance() }
  67. )
  68. LOCAL -> TabData(
  69. id = LOCAL,
  70. text = R.string.title_public_local,
  71. icon = R.drawable.ic_local_24dp,
  72. fragment = { TimelineFragment.newInstance(TimelineViewModel.Kind.PUBLIC_LOCAL) }
  73. )
  74. FEDERATED -> TabData(
  75. id = FEDERATED,
  76. text = R.string.title_public_federated,
  77. icon = R.drawable.ic_public_24dp,
  78. fragment = { TimelineFragment.newInstance(TimelineViewModel.Kind.PUBLIC_FEDERATED) }
  79. )
  80. DIRECT -> TabData(
  81. id = DIRECT,
  82. text = R.string.title_direct_messages,
  83. icon = R.drawable.ic_reblog_direct_24dp,
  84. fragment = { ConversationsFragment.newInstance() }
  85. )
  86. TRENDING -> TabData(
  87. id = TRENDING,
  88. text = R.string.title_public_trending_hashtags,
  89. icon = R.drawable.ic_trending_up_24px,
  90. fragment = { TrendingFragment.newInstance() }
  91. )
  92. HASHTAG -> TabData(
  93. id = HASHTAG,
  94. text = R.string.hashtags,
  95. icon = R.drawable.ic_hashtag,
  96. fragment = { args -> TimelineFragment.newHashtagInstance(args) },
  97. arguments = arguments,
  98. title = { context -> arguments.joinToString(separator = " ") { context.getString(R.string.title_tag, it) } }
  99. )
  100. LIST -> TabData(
  101. id = LIST,
  102. text = R.string.list,
  103. icon = R.drawable.ic_list,
  104. fragment = { args -> TimelineFragment.newInstance(TimelineViewModel.Kind.LIST, args.getOrNull(0).orEmpty()) },
  105. arguments = arguments,
  106. title = { arguments.getOrNull(1).orEmpty() }
  107. )
  108. else -> throw IllegalArgumentException("unknown tab type")
  109. }
  110. }
  111. fun defaultTabs(): List<TabData> {
  112. return listOf(
  113. createTabDataFromId(HOME),
  114. createTabDataFromId(NOTIFICATIONS),
  115. createTabDataFromId(LOCAL),
  116. createTabDataFromId(DIRECT)
  117. )
  118. }