Skip to content

Commit

Permalink
Using ordinal of enum to render tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
mnaturel committed Jan 6, 2023
1 parent 9b5fda2 commit 85cfa43
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ class RoomPollsFragment : VectorBaseFragment<FragmentRoomPollsBinding>() {

tabLayoutMediator = TabLayoutMediator(views.roomPollsTabs, views.roomPollsViewPager) { tab, position ->
when (position) {
0 -> tab.text = getString(R.string.room_polls_active)
1 -> tab.text = getString(R.string.room_polls_ended)
RoomPollsType.ACTIVE.ordinal -> tab.text = getString(R.string.room_polls_active)
RoomPollsType.ENDED.ordinal -> tab.text = getString(R.string.room_polls_ended)
}
}.also { it.attach() }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ class RoomPollsPagerAdapter(
private val fragment: Fragment
) : FragmentStateAdapter(fragment) {

override fun getItemCount() = 2
override fun getItemCount() = RoomPollsType.values().size

override fun createFragment(position: Int): Fragment {
return when (position) {
0 -> instantiateFragment(RoomActivePollsFragment::class.java.name)
else -> instantiateFragment(RoomEndedPollsFragment::class.java.name)
RoomPollsType.ACTIVE.ordinal -> instantiateFragment(RoomActivePollsFragment::class.java.name)
RoomPollsType.ENDED.ordinal -> instantiateFragment(RoomEndedPollsFragment::class.java.name)
else -> throw IllegalArgumentException("position should be between 0 and ${itemCount - 1}, while it was $position")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package im.vector.app.features.roomprofile.polls

enum class RoomPollsFilterType {
enum class RoomPollsType {
ACTIVE,
ENDED,
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package im.vector.app.features.roomprofile.polls.active

import dagger.hilt.android.AndroidEntryPoint
import im.vector.app.R
import im.vector.app.features.roomprofile.polls.RoomPollsFilterType
import im.vector.app.features.roomprofile.polls.RoomPollsType
import im.vector.app.features.roomprofile.polls.list.RoomPollsListFragment

@AndroidEntryPoint
Expand All @@ -28,7 +28,7 @@ class RoomActivePollsFragment : RoomPollsListFragment() {
return getString(R.string.room_polls_active_no_item)
}

override fun getRoomPollsFilter(): RoomPollsFilterType {
return RoomPollsFilterType.ACTIVE
override fun getRoomPollsType(): RoomPollsType {
return RoomPollsType.ACTIVE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package im.vector.app.features.roomprofile.polls.ended

import dagger.hilt.android.AndroidEntryPoint
import im.vector.app.R
import im.vector.app.features.roomprofile.polls.RoomPollsFilterType
import im.vector.app.features.roomprofile.polls.RoomPollsType
import im.vector.app.features.roomprofile.polls.list.RoomPollsListFragment

@AndroidEntryPoint
Expand All @@ -28,7 +28,7 @@ class RoomEndedPollsFragment : RoomPollsListFragment() {
return getString(R.string.room_polls_ended_no_item)
}

override fun getRoomPollsFilter(): RoomPollsFilterType {
return RoomPollsFilterType.ENDED
override fun getRoomPollsType(): RoomPollsType {
return RoomPollsType.ENDED
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import im.vector.app.core.extensions.configureWith
import im.vector.app.core.platform.VectorBaseFragment
import im.vector.app.databinding.FragmentRoomPollsListBinding
import im.vector.app.features.roomprofile.polls.PollSummary
import im.vector.app.features.roomprofile.polls.RoomPollsFilterType
import im.vector.app.features.roomprofile.polls.RoomPollsType
import im.vector.app.features.roomprofile.polls.RoomPollsViewModel
import timber.log.Timber
import javax.inject.Inject
Expand All @@ -53,7 +53,7 @@ abstract class RoomPollsListFragment :

abstract fun getEmptyListTitle(): String

abstract fun getRoomPollsFilter(): RoomPollsFilterType
abstract fun getRoomPollsType(): RoomPollsType

private fun setupList() {
roomPollsController.listener = this
Expand All @@ -72,9 +72,9 @@ abstract class RoomPollsListFragment :
}

override fun invalidate() = withState(viewModel) { viewState ->
when (getRoomPollsFilter()) {
RoomPollsFilterType.ACTIVE -> renderList(viewState.polls.filterIsInstance(PollSummary.ActivePoll::class.java))
RoomPollsFilterType.ENDED -> renderList(viewState.polls.filterIsInstance(PollSummary.EndedPoll::class.java))
when (getRoomPollsType()) {
RoomPollsType.ACTIVE -> renderList(viewState.polls.filterIsInstance(PollSummary.ActivePoll::class.java))
RoomPollsType.ENDED -> renderList(viewState.polls.filterIsInstance(PollSummary.EndedPoll::class.java))
}
}

Expand Down

0 comments on commit 85cfa43

Please sign in to comment.