Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 다른 유저 서재 ui 구현 및 api 연결 #270

Merged
merged 6 commits into from
Aug 29, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: 다른유저 서재 구현
  • Loading branch information
yeonjeen committed Aug 29, 2024
commit 4c42e9651c22245d90b49433e5745f693d409cfe
Original file line number Diff line number Diff line change
@@ -1,10 +1,134 @@
package com.teamwss.websoso.ui.otherUserPage.otherUserLibrary

import android.os.Bundle
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.View
import androidx.core.content.ContextCompat
import androidx.core.view.isVisible
import androidx.fragment.app.viewModels
import com.google.android.material.chip.Chip
import com.teamwss.websoso.R
import com.teamwss.websoso.common.ui.base.BaseFragment
import com.teamwss.websoso.common.ui.custom.WebsosoChip
import com.teamwss.websoso.common.util.setListViewHeightBasedOnChildren
import com.teamwss.websoso.data.model.NovelPreferenceEntity
import com.teamwss.websoso.databinding.FragmentOtherUserLibraryBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class OtherUserLibraryFragment :
BaseFragment<FragmentOtherUserLibraryBinding>(R.layout.fragment_other_user_library)
BaseFragment<FragmentOtherUserLibraryBinding>(R.layout.fragment_other_user_library) {
private val otherUserLibraryViewModel: OtherUserLibraryViewModel by viewModels()
private val restGenrePreferenceAdapter: com.teamwss.websoso.ui.otherUserPage.otherUserLibrary.adapter.RestGenrePreferenceAdapter by lazy {
com.teamwss.websoso.ui.otherUserPage.otherUserLibrary.adapter.RestGenrePreferenceAdapter()
yeonjeen marked this conversation as resolved.
Show resolved Hide resolved
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.lifecycleOwner = viewLifecycleOwner
binding.otherUserLibraryViewModel = otherUserLibraryViewModel

setupRestGenrePreferenceAdapter()
setUpObserve()
yeonjeen marked this conversation as resolved.
Show resolved Hide resolved
}

private fun setupRestGenrePreferenceAdapter() {
binding.lvOtherUserLibraryRestGenre.adapter = restGenrePreferenceAdapter
}

private fun setUpObserve() {
otherUserLibraryViewModel.restGenres.observe(viewLifecycleOwner) { genres ->
restGenrePreferenceAdapter.updateRestGenrePreferenceData(genres)
}

otherUserLibraryViewModel.isGenreListVisible.observe(viewLifecycleOwner) { isVisible ->
updateRestGenrePreferenceVisibility(isVisible)
}

otherUserLibraryViewModel.translatedAttractivePoints.observe(viewLifecycleOwner) { translatedPoints ->
val combinedText =
translatedPoints.joinToString(", ") + getString(R.string.my_library_attractive_point_fixed_text)
applyTextColors(combinedText)
}

otherUserLibraryViewModel.novelPreferences.observe(viewLifecycleOwner) { novelPreferences ->
updateNovelPreferencesKeywords(novelPreferences)
}
}

private fun updateRestGenrePreferenceVisibility(isVisible: Boolean) {
binding.lvOtherUserLibraryRestGenre.isVisible = isVisible
if (isVisible) {
binding.lvOtherUserLibraryRestGenre.setListViewHeightBasedOnChildren()
}
}

private fun applyTextColors(combinedText: String) {
val primary100 = requireContext().getColor(R.color.primary_100_6A5DFD)
val gray300 = requireContext().getColor(R.color.gray_300_52515F)

val spannableStringBuilder = SpannableStringBuilder()

val fixedText = getString(R.string.my_library_attractive_point_fixed_text)

val splitText = combinedText.split(fixedText)

if (splitText.isNotEmpty()) {
val attractivePoints =
SpannableString(splitText[0]).apply {
setSpan(
ForegroundColorSpan(primary100),
0,
length,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
spannableStringBuilder.append(attractivePoints)

val fixedSpannable =
SpannableString(fixedText).apply {
setSpan(
ForegroundColorSpan(gray300),
0,
length,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
spannableStringBuilder.append(fixedSpannable)
} else {
val spannable = SpannableString(combinedText).apply {
setSpan(
ForegroundColorSpan(primary100),
0,
length,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE,
)
}
spannableStringBuilder.append(spannable)
}

binding.tvOtherUserLibraryAttractivePoints.text = spannableStringBuilder
}

private fun updateNovelPreferencesKeywords(novelPreferences: NovelPreferenceEntity) {
novelPreferences.keywords.forEach { keyword ->
val chip = createKeywordChip(keyword)
binding.wcgOtherUserLibraryAttractivePoints.addView(chip)
}
}

private fun createKeywordChip(data: NovelPreferenceEntity.KeywordEntity): Chip {
return WebsosoChip(requireContext()).apply {
text = "${data.keywordName} ${data.keywordCount}"
isCheckable = true
yeonjeen marked this conversation as resolved.
Show resolved Hide resolved
isChecked = false

setChipBackgroundColorResource(R.color.primary_50_F1EFFF)
setTextColor(ContextCompat.getColor(requireContext(), R.color.primary_100_6A5DFD))
setTextAppearance(R.style.body2)
}
}
}