diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/AlbumDao.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/AlbumDao.kt index 11eadc9..84b6742 100644 --- a/UMC_6th/app/src/main/java/com/example/umc_6th/AlbumDao.kt +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/AlbumDao.kt @@ -23,4 +23,16 @@ interface AlbumDao { @Query("SELECT * FROM AlbumTable WHERE id = :id") fun getAlbum(id : Int): Album + @Insert + fun likeAlbum(like : Like) + + @Query("select id from LikeTable where userId =:userId and albumId = :albumId") + fun isLikedAlbum(userId : Int, albumId : Int) : Int? + + @Query("delete from LikeTable where userId =:userId and albumId = :albumId") + fun dislikedAlbum(userId : Int, albumId : Int) + + @Query("select at.* from LikeTable as lt left join AlbumTable as at on lt.albumId = at.id where lt.userId = :userId") + fun getLikedAlbums(userId : Int) : List + } \ No newline at end of file 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 979ab75..48daa6a 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 @@ -5,6 +5,7 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Toast +import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.Fragment import androidx.fragment.app.setFragmentResultListener import com.example.umc_6th.databinding.FragmentAlbumBinding @@ -18,7 +19,9 @@ class AlbumFragment : Fragment() { lateinit var binding : FragmentAlbumBinding private var gson : Gson = Gson() + private var isLiked : Boolean = false private val information = arrayListOf("수록곡","상세정보","영상") + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -28,7 +31,9 @@ class AlbumFragment : Fragment() { val albumJson = arguments?.getString("album") val album = gson.fromJson(albumJson, Album::class.java) + isLiked = isLikedAlbum(album.id) setInit(album) + setOnClickListener(album) binding.albumBackIv.setOnClickListener{ (context as MainActivity).supportFragmentManager.beginTransaction(). @@ -36,6 +41,8 @@ class AlbumFragment : Fragment() { commitAllowingStateLoss() } + + val albumAdapter = AlbumVPAdapter(this) binding.albumContentVp.adapter = albumAdapter @@ -84,4 +91,46 @@ class AlbumFragment : Fragment() { binding.albumSingerNameTv.text = album.singer.toString() } + private fun getJwt() : Int { + val spf = requireActivity().getSharedPreferences("auth", AppCompatActivity.MODE_PRIVATE) + return spf.getInt("jwt", 0) + } + + private fun likeAlbum(userId : Int, albumId : Int) { + val songDB = SongDatabase.getInstance(requireActivity())!! + val like = Like(userId, albumId) + + songDB.albumDao().likeAlbum(like) + } + + private fun isLikedAlbum(albumId : Int) : Boolean { + val songDB = SongDatabase.getInstance(requireActivity())!! + val userId = getJwt() + + val likeId : Int? = songDB.albumDao().isLikedAlbum(userId, albumId) + return likeId != null + } + + private fun disLikeAlbum(albumId : Int) { + val songDB = SongDatabase.getInstance(requireActivity())!! + val userId = getJwt() + + songDB.albumDao().dislikedAlbum(userId, albumId) + } + + private fun setOnClickListener(album : Album) { + val userId = getJwt() + binding.albumLikeIv.setOnClickListener { + if(isLiked) { + binding.albumLikeIv.setImageResource(R.drawable.ic_my_like_off) + disLikeAlbum(album.id) + } + + else { + binding.albumLikeIv.setImageResource((R.drawable.ic_my_like_on)) + likeAlbum(userId, album.id) + } + } + + } } \ No newline at end of file diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/Like.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/Like.kt new file mode 100644 index 0000000..3d48b17 --- /dev/null +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/Like.kt @@ -0,0 +1,12 @@ +package com.example.umc_6th + +import androidx.room.Entity +import androidx.room.PrimaryKey + +@Entity(tableName = "LikeTable") +data class Like( + var userId : Int, + var albumId : Int +){ + @PrimaryKey(autoGenerate = true) var id : Int = 0 +} 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 4ca14a0..2edf4af 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,15 +1,19 @@ package com.example.umc_6th 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.databinding.FragmentLockerSavedalbumBinding class SavedAlbumFragment : Fragment() { lateinit var binding: FragmentLockerSavedalbumBinding + lateinit var albumDB: SongDatabase override fun onCreateView( inflater: LayoutInflater, @@ -17,7 +21,38 @@ class SavedAlbumFragment : Fragment() { savedInstanceState: Bundle? ): View? { binding = FragmentLockerSavedalbumBinding.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..f240878 --- /dev/null +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/SavedAlbumRVAdapter.kt @@ -0,0 +1,60 @@ +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/java/com/example/umc_6th/SongDatabase.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/SongDatabase.kt index 91d7e04..be55d68 100644 --- a/UMC_6th/app/src/main/java/com/example/umc_6th/SongDatabase.kt +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/SongDatabase.kt @@ -5,7 +5,7 @@ import androidx.room.Database import androidx.room.Room import androidx.room.RoomDatabase -@Database(entities = [Song::class, Album::class, User::class], version = 1) +@Database(entities = [Song::class, Album::class, User::class,Like::class], version = 1) abstract class SongDatabase: RoomDatabase() { abstract fun songDao(): SongDao abstract fun albumDao() : AlbumDao diff --git a/UMC_6th/app/src/main/res/layout/fragment_locker_savedalbum.xml b/UMC_6th/app/src/main/res/layout/fragment_locker_savedalbum.xml index 04bf526..2f7e2d0 100644 --- a/UMC_6th/app/src/main/res/layout/fragment_locker_savedalbum.xml +++ b/UMC_6th/app/src/main/res/layout/fragment_locker_savedalbum.xml @@ -1,17 +1,18 @@ - + android:layout_height="match_parent"> - + 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