Skip to content

Commit

Permalink
[feat/store]: feat 스와이프시 앨범 갯수 반영
Browse files Browse the repository at this point in the history
  • Loading branch information
librarywon committed Jul 5, 2023
1 parent 378588d commit 11a6971
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ import androidx.core.text.buildSpannedString
import androidx.core.text.color
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.viewpager2.widget.ViewPager2
import com.teampophory.pophory.R
import com.teampophory.pophory.common.fragment.colorOf
import com.teampophory.pophory.common.primitive.textAppearance
import com.teampophory.pophory.common.view.ItemDiffCallback
import com.teampophory.pophory.common.view.viewBinding
import com.teampophory.pophory.databinding.FragmentStoreBinding
import com.teampophory.pophory.feature.album.AlbumListActivity
import com.teampophory.pophory.feature.home.mypage.MyPageInfoState
import com.teampophory.pophory.feature.home.store.apdater.OnPageChangedListener
import com.teampophory.pophory.feature.home.store.apdater.StoreAdapter
import com.teampophory.pophory.feature.home.store.model.AlbumItem
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class StoreFragment : Fragment() {
class StoreFragment : Fragment(), OnPageChangedListener {
private val binding by viewBinding(FragmentStoreBinding::bind)

private var storeAdapter: StoreAdapter? = null
Expand Down Expand Up @@ -53,8 +54,11 @@ class StoreFragment : Fragment() {
is StoreState.Loading -> {}

is StoreState.SuccessAlbums -> {
with(binding){
with(binding) {
storeAdapter?.submitList(storeState.data)

//최초 데이터 세팅
binding.tvStoreAlbumPhotoCount.text = storeState.data[0].photoCount.toString() + "/30"
}
}

Expand All @@ -65,17 +69,26 @@ class StoreFragment : Fragment() {
}

private fun setupViewPager() {
storeAdapter = StoreAdapter { albumItem ->
storeAdapter = StoreAdapter({ albumItem ->
val intent = Intent(context, AlbumListActivity::class.java).apply {
putExtra("albumId", albumItem.id)
}
startActivity(intent)
}
}, this)

binding.viewpagerStore.adapter = storeAdapter

//1차 스프린트용 입력 방지
binding.viewpagerStore.isUserInputEnabled = false

// 페이지가 변경될 때마다 콜백 등록
binding.viewpagerStore.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
super.onPageSelected(position)
val currentItem = storeAdapter?.currentList?.get(position)
currentItem?.let { onPageChanged(it) }
}
})
}

private fun setSpannableString() {
Expand All @@ -94,4 +107,8 @@ class StoreFragment : Fragment() {

binding.tvStoreWelcome.text = text
}
}

override fun onPageChanged(albumItem: AlbumItem) {
binding.tvStoreAlbumPhotoCount.text = albumItem.photoCount.toString() + "/30"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.teampophory.pophory.feature.home.store.apdater

import android.content.Intent
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.ListAdapter
Expand All @@ -10,7 +9,8 @@ import com.teampophory.pophory.databinding.ItemStorePagerBinding
import com.teampophory.pophory.feature.home.store.model.AlbumItem

class StoreAdapter(
private val onItemClicked: (AlbumItem) -> Unit
private val onItemClicked: (AlbumItem) -> Unit,
private val onPageChangedListener: OnPageChangedListener
) : ListAdapter<AlbumItem, StoreAdapter.StoreViewHolder>(
ItemDiffCallback<AlbumItem>(
onItemsTheSame = { old, new -> old.hashCode() == new.hashCode() },
Expand All @@ -20,16 +20,18 @@ class StoreAdapter(
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StoreViewHolder {
val binding =
ItemStorePagerBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return StoreViewHolder(binding, onItemClicked)
return StoreViewHolder(binding, onItemClicked, onPageChangedListener)
}

class StoreViewHolder(
private val binding: ItemStorePagerBinding,
private val onItemClicked: (AlbumItem) -> Unit
private val onItemClicked: (AlbumItem) -> Unit,
private val onPageChangedListener: OnPageChangedListener
) : RecyclerView.ViewHolder(binding.root) {
fun bind(albumItem: AlbumItem) {
itemView.setOnClickListener {
onItemClicked(albumItem)
onPageChangedListener.onPageChanged(albumItem)
}
}
}
Expand All @@ -38,3 +40,7 @@ class StoreAdapter(
holder.bind(getItem(position))
}
}

interface OnPageChangedListener {
fun onPageChanged(albumItem: AlbumItem)
}
Binary file not shown.
Binary file not shown.
Binary file removed app/src/main/res/drawable/ic_album_cover_love.png
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions app/src/main/res/layout/item_store_pager.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginHorizontal="45dp"
android:src="@drawable/img_album_cover"
android:contentDescription="pager item image"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
Expand Down

0 comments on commit 11a6971

Please sign in to comment.