Skip to content

Commit

Permalink
Merge pull request #36 from SSUMC-6th/kara/#33
Browse files Browse the repository at this point in the history
[kara/#33] feat : 7주차 구현
  • Loading branch information
hyowon0204 authored Jun 2, 2024
2 parents bdc4ab3 + 526de30 commit a7c3f87
Show file tree
Hide file tree
Showing 26 changed files with 572 additions and 170 deletions.
11 changes: 10 additions & 1 deletion UMC_6th/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
alias(libs.plugins.androidApplication)
alias(libs.plugins.jetbrainsKotlinAndroid)
id("kotlin-kapt")
}

android {
Expand All @@ -11,7 +12,10 @@ android {
buildFeatures {
viewBinding = true
}

buildFeatures {
dataBinding = true
}

defaultConfig {
applicationId = "com.example.myfirstapp"
minSdk = 24
Expand Down Expand Up @@ -50,7 +54,12 @@ dependencies {
implementation ("androidx.fragment:fragment-ktx:1.6.2")
implementation ("me.relex:circleindicator:2.1.6")
implementation("com.squareup.retrofit2:converter-gson:2.9.0") //gson
//RoomDB
implementation("androidx.room:room-ktx:2.6.1")
implementation("androidx.room:room-runtime:2.6.1")
kapt("androidx.room:room-compiler:2.6.1")
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)

}
9 changes: 6 additions & 3 deletions UMC_6th/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication"
tools:targetApi="31">
<service
android:name=".ForegroundService"
android:enabled="true"
android:exported="true"></service>

<activity
android:name=".MainActivity"
android:exported="true"
Expand All @@ -24,9 +29,7 @@
</activity>
<activity
android:name=".SongActivity"
android:exported="true">
</activity>
android:exported="true"></activity>
</application>


</manifest>
14 changes: 9 additions & 5 deletions UMC_6th/app/src/main/java/com/example/myfirstapp/Album.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.example.myfirstapp

import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "AlbumTable")
data class Album(
var title : String? = "",
var singer : String? = "",
var coverImage : Int? = null,
var songs: ArrayList<Song>? = null // 앨범 수록곡
)
@PrimaryKey(autoGenerate = false) var id: Int = 0,
var title: String? = "",
var singer: String? = "",
var coverImg: Int? = null
)
21 changes: 21 additions & 0 deletions UMC_6th/app/src/main/java/com/example/myfirstapp/AlbumDao.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.example.myfirstapp

import androidx.room.*

@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 @@ -43,7 +43,7 @@ class AlbumFragment : Fragment() {
}

private fun setInit(album : Album) {
binding.albumAlbumIv.setImageResource(album.coverImage!!)
binding.albumAlbumIv.setImageResource(album.coverImg!!)
binding.albumTitleTv.text = album.title.toString()
binding.albumSingerTv.text = album.singer.toString()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class AlbumRVAdapter(private val albumlist: ArrayList<Album>): RecyclerView.Adap
fun bind(album: Album){
binding.itemAlbumTitleTv.text = album.title
binding.itemAlbumSingerTv.text = album.singer
binding.itemAlbumCoverImgIv.setImageResource(album.coverImage!!)
binding.itemAlbumCoverImgIv.setImageResource(album.coverImg!!)
}

}
Expand Down
6 changes: 6 additions & 0 deletions UMC_6th/app/src/main/java/com/example/myfirstapp/Constant.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.myfirstapp

object Constant {
const val CHANNEL_ID = "ch123"
const val MUSIC_NOTIFICATION_ID = 123
}
49 changes: 49 additions & 0 deletions UMC_6th/app/src/main/java/com/example/myfirstapp/CustomSnackbar.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.example.myfirstapp

import android.view.LayoutInflater
import android.view.View
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import com.example.myfirstapp.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)
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()
}
}
76 changes: 65 additions & 11 deletions UMC_6th/app/src/main/java/com/example/myfirstapp/HomeFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.example.myfirstapp
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -18,6 +19,7 @@ class HomeFragment : Fragment() , CommunicationInterface {

lateinit var binding: FragmentHomeBinding
private var albumDatas = ArrayList<Album>()
private lateinit var songDB: SongDatabase

private val timer = Timer()
private val handler = Handler(Looper.getMainLooper())
Expand All @@ -28,20 +30,16 @@ class HomeFragment : Fragment() , CommunicationInterface {
): View? {

binding = FragmentHomeBinding.inflate(inflater, container, false)
//

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())
Log.d("albumlist", albumDatas.toString())
inputDummyAlbums()


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

albumRVAdapter.setMyItemClickListener(object: AlbumRVAdapter.MyItemClickListener{
override fun onItemClick(album: Album) {
Expand Down Expand Up @@ -108,6 +106,62 @@ class HomeFragment : Fragment() , 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,
"Map of the Soul Persona",
"뮤직 보이 (Music Boy)",
R.drawable.img_album_exp4,
)
)


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

val songDBData = songDB.albumDao().getAlbums()
Log.d("DB data", songDBData.toString())
}

override fun sendData(album: Album) {
if (activity is MainActivity) {
val activity = activity as MainActivity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.myfirstapp.databinding.ItemLockerAlbumBinding

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

private val switchStatus = SparseBooleanArray()
private val songs = ArrayList<Song>()
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
Expand All @@ -20,13 +21,13 @@ class LockerAlbumRVAdapter (private val albumList: ArrayList<Album>) : RecyclerV
}

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.itemAlbumMoreIv.setOnClickListener {
itemClickListener.onRemoveAlbum(position)
itemClickListener.onRemoveAlbum(songs[position].id) // 좋아요 취소로 업데이트하는 메서드
removeSong(position) // 현재 화면에서 아이템을 제거
}

val switch = holder.binding.switchRV
switch.isChecked = switchStatus[position]
switch.setOnClickListener {
Expand All @@ -41,11 +42,10 @@ class LockerAlbumRVAdapter (private val albumList: ArrayList<Album>) : RecyclerV
}
}

override fun getItemCount(): Int = albumList.size
override fun getItemCount(): Int = songs.size

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

private lateinit var itemClickListener : OnItemClickListener
Expand All @@ -54,22 +54,24 @@ class LockerAlbumRVAdapter (private val albumList: ArrayList<Album>) : RecyclerV
this.itemClickListener = onItemClickListener
}

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

notifyDataSetChanged()
}

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

inner class ViewHolder(val binding: ItemLockerAlbumBinding): RecyclerView.ViewHolder(binding.root){

fun bind(album: Album){
binding.itemAlbumTitleTv.text = album.title
binding.itemAlbumSingerTv.text = album.singer
binding.itemAlbumCoverIv.setImageResource(album.coverImage!!)
fun bind(song : Song){
binding.itemAlbumTitleTv.text = song.title
binding.itemAlbumSingerTv.text = song.singer
binding.itemAlbumCoverIv.setImageResource(song.coverImg!!)
}
}
}
Loading

0 comments on commit a7c3f87

Please sign in to comment.