Skip to content

Commit

Permalink
[simba/#33] feat:: Song 데이터 베이스 활용하기 (last song Toast 메시지 오류)
Browse files Browse the repository at this point in the history
  • Loading branch information
BAEK0111 committed May 26, 2024
1 parent 518ade4 commit beb7aec
Show file tree
Hide file tree
Showing 13 changed files with 305 additions and 380 deletions.

This file was deleted.

This file was deleted.

8 changes: 8 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,13 @@ 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")

}
150 changes: 129 additions & 21 deletions UMC_6th/app/src/main/java/com/example/umc_6th/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class MainActivity : AppCompatActivity() {
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

inputDummySongs()
initBottomNavigation()

activityResultLauncher =
Expand All @@ -38,16 +39,46 @@ class MainActivity : AppCompatActivity() {
}

binding.mainPlayerCl.setOnClickListener {
val intent = Intent(this, SongActivity::class.java)
intent.putExtra("title", song.title)
intent.putExtra("singer", song.singer)
intent.putExtra("second", song.second)
intent.putExtra("playTime", song.playTime)
intent.putExtra("isPlaying", song.isPlaying)
intent.putExtra("music",song.music)
// startActivity(intent)
activityResultLauncher.launch(intent)
// val intent = Intent(this, SongActivity::class.java)
// intent.putExtra("title", song.title)
// intent.putExtra("singer", song.singer)
// intent.putExtra("second", song.second)
// intent.putExtra("playTime", song.playTime)
// intent.putExtra("isPlaying", song.isPlaying)
// intent.putExtra("music",song.music)
// activityResultLauncher.launch(intent)
val editor = getSharedPreferences("song", MODE_PRIVATE).edit()
editor.putInt("songId", song.id)
editor.apply()

val intent = Intent(this,SongActivity::class.java)
startActivity(intent)
}
}

override fun onStart() {
super.onStart()
// val sharedPreferences = getSharedPreferences("song", MODE_PRIVATE)
// val jsonToSong = sharedPreferences.getString("songData", null)
//
// song = if(jsonToSong == null) { // 최초 실행 시
// Song("라일락", "아이유(IU)", 0, 60, false, "music_lilac")
// } else { // SongActivity에서 노래가 한번이라도 pause 된 경우
// gson.fromJson(jsonToSong, Song::class.java)
// }
val sharedPreferences = getSharedPreferences("song", MODE_PRIVATE)
val songId = sharedPreferences.getInt("songId",0)

val songDB = SongDatabase.getInstance(this)!!

song = if(songId == 0){
songDB.songDao().getSong(1)
} else {
songDB.songDao().getSong(songId)
}

Log.d("song ID", song.id.toString())
setMiniPlayer(song)
}

private fun initBottomNavigation() {
Expand Down Expand Up @@ -90,18 +121,7 @@ class MainActivity : AppCompatActivity() {
false
}
}
override fun onStart() {
super.onStart()
val sharedPreferences = getSharedPreferences("song", MODE_PRIVATE)
val jsonToSong = sharedPreferences.getString("songData", null)
Log.d("jsonToSong", jsonToSong.toString())
song = if(jsonToSong == null) { // 최초 실행 시
Song("라일락", "아이유(IU)", 0, 60, false, "music_lilac")
} else { // SongActivity에서 노래가 한번이라도 pause 된 경우
gson.fromJson(jsonToSong, Song::class.java)
}
setMiniPlayer(song)
}

private fun setMiniPlayer(song : Song) {
binding.mainMiniplayerTitleTv.text = song.title
binding.mainMiniplayerSingerTv.text = song.singer
Expand All @@ -113,4 +133,92 @@ class MainActivity : AppCompatActivity() {
binding.mainMiniplayerSingerTv.text = album.singer
binding.mainMiniplayerProgressSb.progress = 0
}

private fun inputDummySongs(){
val songDB = SongDatabase.getInstance(this)!!
val songs = songDB.songDao().getSongs()

if(songs.isNotEmpty()) return

songDB.songDao().insert(
Song(
"Lilac",
"아이유 (IU)",
0,
200,
false,
"music_lilac",
R.drawable.img_album_exp2,
false,
)
)

songDB.songDao().insert(
Song(
"Flu",
"아이유 (IU)",
0,
200,
false,
"music_flu",
R.drawable.img_album_exp2,
false,
)
)

songDB.songDao().insert(
Song(
"Butter",
"방탄소년단 (BTS)",
0,
190,
false,
"music_butter",
R.drawable.img_album_exp,
false,
)
)

songDB.songDao().insert(
Song(
"Next Level",
"에스파 (AESPA)",
0,
210,
false,
"music_next",
R.drawable.img_album_exp3,
false,
)
)

songDB.songDao().insert(
Song(
"Boy with Luv",
"방탄소년단 (BTS)",
0,
230,
false,
"music_boy",
R.drawable.img_album_exp,
false,
)
)

songDB.songDao().insert(
Song(
"BBoom BBoom",
"모모랜드 (MOMOLAND)",
0,
240,
false,
"music_bbom",
R.drawable.img_album_exp5,
false,
)
)

val _songs = songDB.songDao().getSongs()
Log.d("DB data",_songs.toString())
}
}
19 changes: 14 additions & 5 deletions UMC_6th/app/src/main/java/com/example/umc_6th/Song.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package com.example.umc_6th

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

@Entity(tableName = "SongTable")
data class Song(
val title : String = "",
val singer : String = "",
var title : String = "",
var singer : String = "",
var second : Int = 0,
var playTime : Int = 60,
var playTime : Int = 0,
var isPlaying : Boolean = false,
var music : String = ""
)
var music : String = "",
var coverImg: Int? = null,
var isLike: Boolean = false
){
@PrimaryKey(autoGenerate = true) var id: Int = 0
}

Loading

0 comments on commit beb7aec

Please sign in to comment.