Skip to content

Commit

Permalink
[Simba/#33] :: feat 7주차 구현 (#37)
Browse files Browse the repository at this point in the history
* [simba/#33] feat:: Song 데이터 베이스 활용하기 (last song Toast 메시지 오류)

* [simba/#33] feat:: 좋아요 기능 구현

* [simba/#33] feat:: 보관함 recyclerview로 구현, 좋아요 누른 노래만 보관함으로 이동, 더보기 클릭 시 아이템 영구 삭제

* [simba/#33] feat:: MiniPlayer Seekbar 재생 시간과 동기화

* [simba/#33] feat:: Album 데이터베이스 구축하기

* [simba/#33] feat:: Bottom Sheet Dialog 구현

* [simba/#33] feat:: Bottom Sheet Dialog 구현
  • Loading branch information
BAEK0111 authored Jun 5, 2024
1 parent 518ade4 commit d3434ad
Show file tree
Hide file tree
Showing 32 changed files with 776 additions and 606 deletions.

This file was deleted.

This file was deleted.

7 changes: 7 additions & 0 deletions UMC_6th/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("kotlin-kapt")
}

android {
Expand Down Expand Up @@ -49,6 +50,12 @@ dependencies {
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("com.squareup.retrofit2:converter-gson:2.9.0")
testImplementation("junit:junit:4.13.2")

//roomDB
implementation("androidx.room:room-migration:2.6.0")
implementation("androidx.room:room-runtime:2.6.0")
kapt("androidx.room:room-compiler:2.6.0")

androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}
7 changes: 5 additions & 2 deletions UMC_6th/app/src/main/java/com/example/umc_6th/Album.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.example.umc_6th

import androidx.room.Entity
import androidx.room.PrimaryKey
import java.util.ArrayList

@Entity(tableName = "AlbumTable")
data class Album(
@PrimaryKey(autoGenerate = false) var id: Int = 0,
var title: String? = "",
var singer: String? = "",
var coverImg: Int? = null,
var songs: ArrayList<Song>? = null
var coverImg: Int? = null
)
26 changes: 26 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
@@ -0,0 +1,26 @@
package com.example.umc_6th

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update

@Dao
interface AlbumDao {
@Insert
fun insert(album: Album)

@Update
fun update(album: Album)

@Delete
fun delete(album: Album)

@Query("SELECT * FROM AlbumTable")
fun getAlbums(): List<Album>

@Query("SELECT * FROM AlbumTable WHERE id = :id")
fun getAlbum(id : Int): Album

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class AlbumRVAdapter(private val albumList:ArrayList<Album>) : RecyclerView.Adap

private lateinit var mItemClickListener : MyItemClickListener
fun setMyItemClickListener(itemClickListener: MyItemClickListener){
mItemClickListener = itemClickListener
this.mItemClickListener = itemClickListener
}

fun addItem(album: Album){
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.example.umc_6th

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.example.umc_6th.databinding.FragmentBottomSheetBinding
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

class BottomSheetFragment : BottomSheetDialogFragment() {

lateinit var binding: FragmentBottomSheetBinding

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentBottomSheetBinding.inflate(inflater,container,false)
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

binding.bottomSheetIv1.setOnClickListener {
Toast.makeText(requireActivity(),"듣기 버튼 클릭",Toast.LENGTH_SHORT).show()
}
binding.bottomSheetIv2.setOnClickListener {
Toast.makeText(requireActivity(),"재생목록 버튼 클릭",Toast.LENGTH_SHORT).show()
}
binding.bottomSheetIv3.setOnClickListener {
Toast.makeText(requireActivity(),"내 리스트 버튼 클릭",Toast.LENGTH_SHORT).show()
}
binding.bottomSheetIv4.setOnClickListener {
Toast.makeText(requireActivity(),"삭제 버튼 클릭",Toast.LENGTH_SHORT).show()
}
binding.bottomSheetTv4.setOnClickListener {

}
}

}
49 changes: 49 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/CustomSnackbar.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.example.umc_6th

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import com.example.umc_6th.databinding.CustomSnackbarBinding
import com.google.android.material.snackbar.Snackbar

class CustomSnackbar(view: View, private val message: String) {
companion object{
fun make(view: View, message: String) = CustomSnackbar(view, message)
}

private val context = view.context
private val snackbar = Snackbar.make(view,"",5000)
@SuppressLint("RestrictedApi")
private val snackbarLayout = snackbar.view as Snackbar.SnackbarLayout


private val inflater = LayoutInflater.from(context)
private val snackbarBinding: CustomSnackbarBinding = DataBindingUtil.inflate(inflater,R.layout.custom_snackbar, null, false)

init {
initView()
initData()
}

private fun initView() {
with(snackbarLayout) {
removeAllViews()
setPadding(0, 0, 0, 0)
setBackgroundColor(ContextCompat.getColor(context, android.R.color.transparent))
addView(snackbarBinding.root, 0)
}
}

private fun initData() {
snackbarBinding.customSnackbarTv.text = message
snackbarBinding.customSnackbarBtn.setOnClickListener {
// OK 버튼을 클릭했을 때 실행할 동작을 정의할 수 있다.
}
}

fun show() {
snackbar.show()
}
}
69 changes: 55 additions & 14 deletions UMC_6th/app/src/main/java/com/example/umc_6th/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.example.umc_6th
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
Expand All @@ -24,6 +25,7 @@ class HomeFragment : Fragment(), AlbumRVAdapter.CommunicationInterface {
private val timer = Timer()
private val handler = Handler(Looper.getMainLooper())
private var albumDatas = ArrayList<Album>()
private lateinit var songDB: SongDatabase

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -42,24 +44,18 @@ class HomeFragment : Fragment(), AlbumRVAdapter.CommunicationInterface {
): View? {
binding = FragmentHomeBinding.inflate(inflater,container,false)

// binding.homeAlbumImg01Iv1.setOnClickListener{
// setFragmentResult("TitleInfo", bundleOf("title" to binding.titleLilac.text.toString()))
// setFragmentResult("SingerInfo", bundleOf("singer" to binding.singerIu.text.toString()))
// (context as MainActivity).supportFragmentManager.beginTransaction().replace(R.id.main_frm,AlbumFragment()).commitAllowingStateLoss()
// }

albumDatas.apply{
add(Album("Butter","방탄소년단(BTS)",R.drawable.img_album_exp))
add(Album("Lilac","아이유(IU)",R.drawable.img_album_exp2))
add(Album("Next Level","에스파(AESPA)",R.drawable.img_album_exp3))
add(Album("Boy with Luv","방탄소년단(BTS)",R.drawable.img_album_exp4))
add(Album("BBoom BBoom","모모랜드(MOMOLAND)",R.drawable.img_album_exp5))
add(Album("Weekend","태연(Tae Yeon)",R.drawable.img_album_exp6))
songDB = SongDatabase.getInstance(requireContext())!!
albumDatas.addAll(songDB.albumDao().getAlbums())
inputDummyAlbums()

Log.d("HomeFragment", "Album Data Size: ${albumDatas.size}")
albumDatas.forEach { album ->
Log.d("HomeFragment", "Album: ${album.title}")
}

val albumRVAdapter = AlbumRVAdapter(albumDatas)
binding.homeTodayMusicAlbumRv.adapter = albumRVAdapter
binding.homeTodayMusicAlbumRv.layoutManager = LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false)
binding.homeTodayMusicAlbumRv.layoutManager = LinearLayoutManager(requireActivity(),LinearLayoutManager.HORIZONTAL,false)

albumRVAdapter.setMyItemClickListener(object : AlbumRVAdapter.MyItemClickListener{
override fun onItemClick(album: Album) {
Expand Down Expand Up @@ -123,4 +119,49 @@ class HomeFragment : Fragment(), AlbumRVAdapter.CommunicationInterface {
}, 3000, 3000)
}

private fun inputDummyAlbums(){
val songDB = SongDatabase.getInstance(requireActivity())!!
val songs = songDB.albumDao().getAlbums()

if (songs.isNotEmpty()) return

songDB.albumDao().insert(
Album(
1,
"IU 5th Album 'LILAC'",
"아이유 (IU)",
R.drawable.img_album_exp2
)
)

songDB.albumDao().insert(
Album(
2,
"Butter",
"방탄소년단 (BTS)",
R.drawable.img_album_exp
)
)

songDB.albumDao().insert(
Album(
3,
"iScreaM Vol.10: Next Level Remixes",
"에스파 (AESPA)",
R.drawable.img_album_exp3
)
)

songDB.albumDao().insert(
Album(
4,
"Great!",
"모모랜드 (MOMOLAND)",
R.drawable.img_album_exp5
)
)

val songDBData = songDB.albumDao().getAlbums()
Log.d("DB data", songDBData.toString())
}
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
package com.example.umc_6th

import android.annotation.SuppressLint
import android.util.SparseBooleanArray
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.Recycler
import com.example.umc_6th.databinding.ItemLockerAlbumBinding

class LockerAlbumRVAdapter (private var albumList: ArrayList<Album>) : RecyclerView.Adapter<LockerAlbumRVAdapter.ViewHolder>(){
class LockerAlbumRVAdapter () : RecyclerView.Adapter<LockerAlbumRVAdapter.ViewHolder>(){

private val switchStatus = SparseBooleanArray()
private lateinit var itemClickListener : OnItemClickListener
private val songs = ArrayList<Song>()

override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): LockerAlbumRVAdapter.ViewHolder {
val binding: ItemLockerAlbumBinding = ItemLockerAlbumBinding.inflate(LayoutInflater.from(parent.context),parent,false)
val binding: ItemLockerAlbumBinding = ItemLockerAlbumBinding
.inflate(LayoutInflater.from(parent.context),parent,false)

return ViewHolder(binding)
}

override fun onBindViewHolder(holder: LockerAlbumRVAdapter.ViewHolder, position: Int) {
holder.bind(albumList[position])
holder.itemView.setOnClickListener {
itemClickListener.onItemClick(albumList[position])
}
holder.bind(songs[position])

holder.binding.itemLockerAlbumMoreIv.setOnClickListener {
itemClickListener.onRemoveAlbum(position)
itemClickListener.onRemoveAlbum(songs[position].id)
removeSong(position) // 화면에서 아이템 제거
}

val switch = holder.binding.switchRV
Expand All @@ -43,34 +44,39 @@ class LockerAlbumRVAdapter (private var albumList: ArrayList<Album>) : RecyclerV
}
}

override fun getItemCount(): Int = songs.size

inner class ViewHolder(val binding:ItemLockerAlbumBinding) : RecyclerView.ViewHolder(binding.root){
fun bind(song: Song){
binding.itemLockerAlbumTitleTv.text = song.title
binding.itemLockerAlbumSingerTv.text = song.singer
binding.itemLockerAlbumCoverImgIv.setImageResource(song.coverImg!!)
}
}

interface OnItemClickListener {
fun onItemClick(album : Album)
fun onRemoveAlbum(position: Int) {
fun onRemoveAlbum(SongId: Int) {
}
}

private lateinit var itemClickListener: OnItemClickListener

fun setItemClickListener(onItemClickListener: OnItemClickListener) {
this.itemClickListener = onItemClickListener
}

fun addItem(album: Album){
albumList.add(album)
@SuppressLint("NotifyDataSetChanged")
fun addSongs(songs: ArrayList<Song>){
this.songs.clear()
this.songs.addAll(songs)

notifyDataSetChanged()
}

fun removeItem(position: Int){
albumList.removeAt(position)
@SuppressLint("NotifyDataSetChanged")
private fun removeSong(position: Int){
songs.removeAt(position)
notifyDataSetChanged()
}

override fun getItemCount(): Int = albumList.size

inner class ViewHolder(val binding:ItemLockerAlbumBinding) : RecyclerView.ViewHolder(binding.root){
fun bind(album: Album){
binding.itemLockerAlbumTitleTv.text = album.title
binding.itemLockerAlbumSingerTv.text = album.singer
binding.itemLockerAlbumCoverImgIv.setImageResource(album.coverImg!!)
}
}
}
Loading

0 comments on commit d3434ad

Please sign in to comment.