Skip to content

Commit

Permalink
[kara/#33] feat : 7주차 구현1
Browse files Browse the repository at this point in the history
  • Loading branch information
hyowon0204 committed May 28, 2024
1 parent bdc4ab3 commit 7668960
Show file tree
Hide file tree
Showing 20 changed files with 479 additions and 179 deletions.
37 changes: 21 additions & 16 deletions UMC_6th/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
plugins {
alias(libs.plugins.androidApplication)
alias(libs.plugins.jetbrainsKotlinAndroid)
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("kotlin-kapt")
}

android {
namespace = "com.example.myfirstapp"
compileSdk = 34


buildFeatures {
viewBinding = true
}

defaultConfig {
applicationId = "com.example.myfirstapp"
minSdk = 24
Expand Down Expand Up @@ -41,16 +41,21 @@ android {
}

dependencies {
implementation("androidx.core:core-ktx:1.10.1")
implementation("androidx.appcompat:appcompat:1.6.1")
implementation("com.google.android.material:material:1.8.0")
implementation("androidx.activity:activity-ktx:1.7.1")
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
implementation("androidx.fragment:fragment-ktx:1.6.2")
implementation("me.relex:circleindicator:2.1.6")
implementation("com.squareup.retrofit2:converter-gson:2.9.0")

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.material)
implementation(libs.androidx.activity)
implementation(libs.androidx.constraintlayout)
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
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
}
// 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("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}
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
73 changes: 63 additions & 10 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,16 +30,11 @@ 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())


val albumRVAdapter = AlbumRVAdapter(albumDatas)
binding.homeTodayAlbumRv.adapter = albumRVAdapter
Expand Down Expand Up @@ -108,6 +105,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 7668960

Please sign in to comment.