Skip to content

Commit

Permalink
[simba/#38] feat:: 회원가입, 로그인 구현2
Browse files Browse the repository at this point in the history
  • Loading branch information
BAEK0111 committed Jun 5, 2024
1 parent 3bbad12 commit 6d9bc85
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 12 deletions.
12 changes: 12 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/AlbumDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Album>

}
49 changes: 49 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/AlbumFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?,
Expand All @@ -28,14 +31,18 @@ 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().
replace(R.id.main_frm,HomeFragment()).
commitAllowingStateLoss()
}



val albumAdapter = AlbumVPAdapter(this)
binding.albumContentVp.adapter = albumAdapter

Expand Down Expand Up @@ -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)
}
}

}
}
12 changes: 12 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/Like.kt
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,58 @@
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,
container: ViewGroup?,
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
}
}
Original file line number Diff line number Diff line change
@@ -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<SavedAlbumRVAdapter.ViewHolder>() {
private val albums = ArrayList<Album>()

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<Album>) {
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
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 12 additions & 11 deletions UMC_6th/app/src/main/res/layout/fragment_locker_savedalbum.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
android:layout_height="match_parent">

<TextView
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/locker_savedSong_recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="저장한 앨범이 없습니다."
android:textSize="30sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
android:layout_height="match_parent"
tools:listitem="@layout/item_locker_album"
android:overScrollMode="never"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 6d9bc85

Please sign in to comment.