Skip to content

Commit

Permalink
Merge pull request #561 from Team-WSS/feat/554
Browse files Browse the repository at this point in the history
feat: 보관함 읽기 상태에 따른 탭 이동 구현
  • Loading branch information
m6z1 authored Jan 25, 2025
2 parents 4454e16 + 784199a commit d4cbc33
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.into.websoso.data.model.NovelPreferenceEntity
import com.into.websoso.databinding.FragmentMyLibraryBinding
import com.into.websoso.ui.main.myPage.myLibrary.adapter.RestGenrePreferenceAdapter
import com.into.websoso.ui.userStorage.UserStorageActivity
import com.into.websoso.ui.userStorage.model.StorageTab
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
Expand Down Expand Up @@ -197,27 +198,32 @@ class MyLibraryFragment : BaseFragment<FragmentMyLibraryBinding>(R.layout.fragme
}

private fun onStorageButtonClick() {
binding.ivMyLibraryGoToStorage.setOnClickListener {
singleEventHandler.throttleFirst {
val intent = UserStorageActivity.getIntent(
context = requireContext(),
source = UserStorageActivity.SOURCE_MY_LIBRARY,
userId = myLibraryViewModel.userId,
)
userStorageResultLauncher.launch(intent)
val tabClickMappings = mapOf(
binding.clMyLibraryTopBar to StorageTab.INTEREST.readStatus,
binding.llMyLibraryStorageInteresting to StorageTab.INTEREST.readStatus,
binding.llMyLibraryStorageWatching to StorageTab.WATCHING.readStatus,
binding.llMyLibraryStorageWatched to StorageTab.WATCHED.readStatus,
binding.llMyLibraryStorageQuit to StorageTab.QUIT.readStatus,
)

tabClickMappings.forEach { (view, readStatus) ->
view.setOnClickListener {
singleEventHandler.throttleFirst {
navigateToStorageActivity(readStatus)
}
}
}
}

binding.llMyLibraryStorage.setOnClickListener {
singleEventHandler.throttleFirst {
val intent = UserStorageActivity.getIntent(
context = requireContext(),
source = UserStorageActivity.SOURCE_MY_LIBRARY,
userId = myLibraryViewModel.userId,
)
userStorageResultLauncher.launch(intent)
}
}
private fun navigateToStorageActivity(readStatus: String) {
userStorageResultLauncher.launch(
UserStorageActivity.getIntent(
context = requireContext(),
source = UserStorageActivity.SOURCE_MY_LIBRARY,
userId = myLibraryViewModel.userId,
readStatus = readStatus,
),
)
}

private fun updateDominantGenres(topGenres: List<GenrePreferenceEntity>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.into.websoso.data.model.NovelPreferenceEntity
import com.into.websoso.databinding.FragmentOtherUserLibraryBinding
import com.into.websoso.ui.otherUserPage.otherUserLibrary.adapter.RestGenrePreferenceAdapter
import com.into.websoso.ui.userStorage.UserStorageActivity
import com.into.websoso.ui.userStorage.model.StorageTab
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
Expand Down Expand Up @@ -73,6 +74,7 @@ class OtherUserLibraryFragment : BaseFragment<FragmentOtherUserLibraryBinding>(R
binding.clOtherUserLibraryKnownPreference.visibility = View.GONE
binding.clOtherUserLibraryUnknownPreference.visibility = View.VISIBLE
}

false -> {
binding.clOtherUserLibraryKnownPreference.visibility = View.VISIBLE
binding.clOtherUserLibraryUnknownPreference.visibility = View.GONE
Expand Down Expand Up @@ -198,29 +200,35 @@ class OtherUserLibraryFragment : BaseFragment<FragmentOtherUserLibraryBinding>(R
private fun onStorageButtonClick() {
val userId = requireNotNull(otherUserLibraryViewModel.userId.value)

binding.ivOtherUserLibraryGoToStorage.setOnClickListener {
singleEventHandler.throttleFirst {
startActivity(
UserStorageActivity.getIntent(
requireContext(),
UserStorageActivity.SOURCE_OTHER_USER_LIBRARY,
userId,
),
)
val clickMappings = mapOf(
binding.clOtherUserLibraryTopBar to StorageTab.INTEREST.readStatus,
binding.llOtherUserLibraryStorageInteresting to StorageTab.INTEREST.readStatus,
binding.llOtherUserLibraryStorageWatching to StorageTab.WATCHING.readStatus,
binding.llOtherUserLibraryStorageWatched to StorageTab.WATCHED.readStatus,
binding.llOtherUserLibraryStorageQuit to StorageTab.QUIT.readStatus,
)

clickMappings.forEach { (view, readStatus) ->
view.setOnClickListener {
singleEventHandler.throttleFirst {
navigateToUserStorageActivity(userId, readStatus)
}
}
}
}

binding.llOtherUserLibraryStorage.setOnClickListener {
singleEventHandler.throttleFirst {
startActivity(
UserStorageActivity.getIntent(
requireContext(),
UserStorageActivity.SOURCE_OTHER_USER_LIBRARY,
userId,
),
)
}
}
private fun navigateToUserStorageActivity(
userId: Long,
readStatus: String,
) {
startActivity(
UserStorageActivity.getIntent(
context = requireContext(),
source = UserStorageActivity.SOURCE_OTHER_USER_LIBRARY,
userId = userId,
readStatus = readStatus,
),
)
}

override fun onResume() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class UserStorageActivity : BaseActivity<ActivityStorageBinding>(R.layout.activi
setupUserId()
bindViewModel()
setupViewPagerAndTabLayout()
setupInitialReadStatusTab()
onBackButtonClick()
onSortTypeButtonClick()
onExploreButton()
Expand All @@ -52,8 +53,9 @@ class UserStorageActivity : BaseActivity<ActivityStorageBinding>(R.layout.activi
private fun setupUserId() {
val source = intent.getStringExtra(EXTRA_SOURCE) ?: SOURCE_MY_LIBRARY
val userId = intent.getLongExtra(USER_ID_KEY, UserStorageViewModel.DEFAULT_USER_ID)
val readStatus = intent.getStringExtra(READ_STATUS) ?: StorageTab.INTEREST.readStatus

userStorageViewModel.updateUserStorage(source, userId)
userStorageViewModel.updateUserStorage(source, userId, readStatus)
}

private fun bindViewModel() {
Expand All @@ -72,20 +74,31 @@ class UserStorageActivity : BaseActivity<ActivityStorageBinding>(R.layout.activi
binding.vpStorage.adapter = userStorageAdapter
}

private fun setupInitialReadStatusTab() {
val readStatus = intent.getStringExtra(READ_STATUS) ?: StorageTab.INTEREST.readStatus
userStorageViewModel.updateReadStatus(readStatus, forceLoad = true)

val initialTabIndex = StorageTab.entries.indexOfFirst { it.readStatus == readStatus }
binding.tlStorage.selectTab(binding.tlStorage.getTabAt(initialTabIndex.coerceAtLeast(0)))
}

private fun setupTabLayoutWithViewPager() {
TabLayoutMediator(binding.tlStorage, binding.vpStorage) { tab, position ->
tab.text = StorageTab.fromPosition(position).title
}.attach()

binding.tlStorage.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab?) {
val selectedTab = requireNotNull(tab) { "Tab must not be null" }
onReadingStatusTabSelected(selectedTab.position)
}
binding.tlStorage.addOnTabSelectedListener(
object : TabLayout.OnTabSelectedListener {
override fun onTabSelected(tab: TabLayout.Tab?) {
val selectedTab = requireNotNull(tab) { "Tab must not be null" }
onReadingStatusTabSelected(selectedTab.position)
}

override fun onTabUnselected(tab: TabLayout.Tab?) {}
override fun onTabReselected(tab: TabLayout.Tab?) {}
})
override fun onTabUnselected(tab: TabLayout.Tab?) {}

override fun onTabReselected(tab: TabLayout.Tab?) {}
},
)
}

private fun onReadingStatusTabSelected(position: Int) {
Expand Down Expand Up @@ -172,12 +185,20 @@ class UserStorageActivity : BaseActivity<ActivityStorageBinding>(R.layout.activi
const val USER_ID_KEY = "userId"
const val SOURCE_MY_LIBRARY = "myLibrary"
const val SOURCE_OTHER_USER_LIBRARY = "otherUserLibrary"

fun getIntent(context: Context, source: String, userId: Long): Intent {
return Intent(context, UserStorageActivity::class.java).apply {
putExtra(EXTRA_SOURCE, source)
putExtra(USER_ID_KEY, userId)
}
const val READ_STATUS = "read_status"

fun getIntent(
context: Context,
source: String,
userId: Long,
readStatus: String = StorageTab.INTEREST.readStatus,
) = Intent(
context,
UserStorageActivity::class.java,
).apply {
putExtra(EXTRA_SOURCE, source)
putExtra(USER_ID_KEY, userId)
putExtra(READ_STATUS, readStatus)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ class UserStorageViewModel @Inject constructor(
fun updateUserStorage(
source: String,
receivedUserId: Long,
readStatus: String = StorageTab.INTEREST.readStatus,
) {
this.source = source
this.userId = receivedUserId

updateReadStatus(StorageTab.INTEREST.readStatus, forceLoad = true)
updateReadStatus(readStatus, forceLoad = true)
}

fun updateRatingChanged() {
Expand Down Expand Up @@ -111,4 +112,4 @@ class UserStorageViewModel @Inject constructor(
const val STORAGE_NOVEL_SIZE = 100
const val DEFAULT_USER_ID = -1L
}
}
}
Loading

0 comments on commit d4cbc33

Please sign in to comment.