From 5b5844c35213ac9d5730233fdac50ab340e388eb Mon Sep 17 00:00:00 2001 From: leesumin0526 Date: Wed, 5 Jun 2024 18:21:13 +0900 Subject: [PATCH] =?UTF-8?q?[maro/#38]=20feat=20::=20=EC=A2=8B=EC=95=84?= =?UTF-8?q?=EC=9A=94=20=ED=91=9C=EC=8B=9C=20=EC=9C=A0=EC=A0=80=20=EA=B5=AC?= =?UTF-8?q?=EB=B6=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/workspace.xml | 27 +++++---- .../main/java/com/example/umc_6th/Album.kt | 3 +- .../java/com/example/umc_6th/AlbumFragment.kt | 28 +-------- .../com/example/umc_6th/LockerFragment.kt | 2 +- .../com/example/umc_6th/SavedAlbumFragment.kt | 45 +++++++++++++- .../example/umc_6th/SavedAlbumRVAdapter.kt | 59 +++++++++++++++++++ .../src/main/res/layout/fragment_locker.xml | 2 +- .../main/res/layout/fragment_saved_album.xml | 18 +++--- 8 files changed, 135 insertions(+), 49 deletions(-) create mode 100644 UMC_6th/app/src/main/java/com/example/umc_6th/SavedAlbumRVAdapter.kt diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 1b36bb6..3ffc0c0 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -126,15 +126,14 @@ @@ -867,7 +866,15 @@ - @@ -917,6 +923,7 @@ - \ No newline at end of file diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/Album.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/Album.kt index cbf3ccd..9c492df 100644 --- a/UMC_6th/app/src/main/java/com/example/umc_6th/Album.kt +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/Album.kt @@ -10,5 +10,6 @@ data class Album( var id: Int = 0, var title: String? = "", var singer: String? = "", - var coverImg: Int? = null + var coverImg: Int? = null, + var isLike : Boolean = true, ) diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/AlbumFragment.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/AlbumFragment.kt index 5143f49..15e63bd 100644 --- a/UMC_6th/app/src/main/java/com/example/umc_6th/AlbumFragment.kt +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/AlbumFragment.kt @@ -26,8 +26,8 @@ class AlbumFragment : Fragment() { ): View? { binding = inflate(inflater,container,false) - val albumJson = arguments?.getString("album") - val album = gson.fromJson(albumJson, Album::class.java) + val albumToJson = arguments?.getString("album") + val album = gson.fromJson(albumToJson, Album::class.java) isLiked = isLikedAlbum(album.id) setInit(album) setOnClickListener(album) @@ -54,30 +54,6 @@ class AlbumFragment : Fragment() { tab.text = information[position] }.attach() - -// binding.songLalacLayout.setOnClickListener{ -// Toast.makeText(activity,"LILAC",Toast.LENGTH_SHORT).show() -// } -// -// binding.songFluLayout.setOnClickListener { -// Toast.makeText(activity,"FLU", Toast.LENGTH_SHORT).show() -// } -// -// binding.songCoinLayout.setOnClickListener { -// Toast.makeText(activity,"Coin", Toast.LENGTH_SHORT).show() -// } -// -// binding.songSpringLayout.setOnClickListener { -// Toast.makeText(activity,"봄 안녕 봄", Toast.LENGTH_SHORT).show() -// } -// -// binding.songCelebrityLayout.setOnClickListener { -// Toast.makeText(activity,"Celebrity", Toast.LENGTH_SHORT).show() -// } -// -// binding.songSingLayout.setOnClickListener { -// Toast.makeText(activity,"돌림노래 (Feat. DEAN)", Toast.LENGTH_SHORT).show() -// } return binding.root } private fun setInit(album: Album){ diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/LockerFragment.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/LockerFragment.kt index ea40aa4..e77715d 100644 --- a/UMC_6th/app/src/main/java/com/example/umc_6th/LockerFragment.kt +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/LockerFragment.kt @@ -13,7 +13,7 @@ import com.google.android.material.tabs.TabLayoutMediator class LockerFragment : Fragment() { lateinit var binding: FragmentLockerBinding - private var information = arrayListOf("저장한곡", "음악파일", "저장앨범") + private val information = arrayListOf("저장한곡", "음악파일", "저장앨범") val bottomSheetFragment = BottomSheetFragment() diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/SavedAlbumFragment.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/SavedAlbumFragment.kt index 375464b..b9c8886 100644 --- a/UMC_6th/app/src/main/java/com/example/umc_6th/SavedAlbumFragment.kt +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/SavedAlbumFragment.kt @@ -1,22 +1,61 @@ package com.example.myfirstapp import android.os.Bundle +import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.Fragment +import androidx.recyclerview.widget.LinearLayoutManager +import com.example.umc_6th.SavedAlbumRVAdapter +import com.example.umc_6th.SongDatabase import com.example.umc_6th.databinding.FragmentSavedAlbumBinding -class SavedAlbumFragment : Fragment(){ - +class SavedAlbumFragment : Fragment() { lateinit var binding: FragmentSavedAlbumBinding + lateinit var albumDB: SongDatabase + private lateinit var albumRVAdapter: SavedAlbumRVAdapter override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { - binding = FragmentSavedAlbumBinding.inflate(inflater,container,false) + binding = FragmentSavedAlbumBinding.inflate(inflater, container, false) + + albumDB = SongDatabase.getInstance(requireContext())!! + return binding.root } + + override fun onStart() { + super.onStart() + initRecyclerview() + } + + private fun initRecyclerview(){ + binding.lockerSavedSongRecyclerView.layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false) + + val albumRVAdapter = SavedAlbumRVAdapter() + //리스너 객체 생성 및 전달 + + albumRVAdapter.setMyItemClickListener(object : SavedAlbumRVAdapter.MyItemClickListener{ + override fun onRemoveSong(songId: Int) { + albumDB.albumDao().getLikedAlbums(getJwt()) + } + }) + + binding.lockerSavedSongRecyclerView.adapter = albumRVAdapter + + albumRVAdapter.addAlbums(albumDB.albumDao().getLikedAlbums(getJwt()) as ArrayList) + } + + private fun getJwt() : Int { + val spf = activity?.getSharedPreferences("auth" , AppCompatActivity.MODE_PRIVATE) + val jwt = spf!!.getInt("jwt", 0) + Log.d("MAIN_ACT/GET_JWT", "jwt_token: $jwt") + + return jwt + } } \ No newline at end of file diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/SavedAlbumRVAdapter.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/SavedAlbumRVAdapter.kt new file mode 100644 index 0000000..bae1640 --- /dev/null +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/SavedAlbumRVAdapter.kt @@ -0,0 +1,59 @@ +package com.example.umc_6th + +import android.annotation.SuppressLint +import android.view.LayoutInflater +import android.view.ViewGroup +import androidx.recyclerview.widget.RecyclerView +import com.example.umc_6th.databinding.ItemLockerAlbumBinding + +class SavedAlbumRVAdapter (): RecyclerView.Adapter() { + private val albums = ArrayList() + + interface MyItemClickListener{ + fun onRemoveSong(songId: Int) + } + + private lateinit var mItemClickListener: MyItemClickListener + + fun setMyItemClickListener(itemClickListener: MyItemClickListener){ + mItemClickListener = itemClickListener + } + + override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): SavedAlbumRVAdapter.ViewHolder { + val binding: ItemLockerAlbumBinding = ItemLockerAlbumBinding.inflate(LayoutInflater.from(viewGroup.context), viewGroup, false) + + return ViewHolder(binding) + } + + override fun onBindViewHolder(holder: SavedAlbumRVAdapter.ViewHolder, position: Int) { + holder.bind(albums[position]) + holder.binding.itemLockerAlbumMoreIv.setOnClickListener { + mItemClickListener.onRemoveSong(albums[position].id) + removeSong(position) + } + } + + override fun getItemCount(): Int = albums.size + + @SuppressLint("NotifyDataSetChanged") + fun addAlbums(albums: ArrayList) { + this.albums.clear() + this.albums.addAll(albums) + + notifyDataSetChanged() + } + + fun removeSong(position: Int){ + albums.removeAt(position) + notifyDataSetChanged() + } + + inner class ViewHolder(val binding: ItemLockerAlbumBinding) : RecyclerView.ViewHolder(binding.root){ + fun bind(album: Album){ + binding.itemLockerAlbumCoverImgIv.setImageResource(album.coverImg!!) + binding.itemLockerAlbumTitleTv.text = album.title + binding.itemLockerAlbumSingerTv.text = album.singer + } + } + +} \ No newline at end of file diff --git a/UMC_6th/app/src/main/res/layout/fragment_locker.xml b/UMC_6th/app/src/main/res/layout/fragment_locker.xml index 5aa6fed..f8acfb1 100644 --- a/UMC_6th/app/src/main/res/layout/fragment_locker.xml +++ b/UMC_6th/app/src/main/res/layout/fragment_locker.xml @@ -31,7 +31,7 @@ - + android:layout_height="match_parent"> - - + tools:listitem="@layout/item_locker_album" + android:overScrollMode="never" + android:orientation="vertical" + app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/> - \ No newline at end of file + \ No newline at end of file