Skip to content

Commit

Permalink
[nunu/#33] 7주차 구현7
Browse files Browse the repository at this point in the history
- MainActivity -> SongActivity 간 songId 데이터 공유( 근데, id가 서로 다름.. 그래서 -1 해줘서 보내줌.)
  • Loading branch information
Ssamssamukja committed Jun 2, 2024
1 parent accaf89 commit 26d0394
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
29 changes: 20 additions & 9 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 @@ -3,6 +3,7 @@ package com.example.umc_6th
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import android.util.Log
import android.widget.Toast
Expand All @@ -19,7 +20,6 @@ class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var resultLauncher: ActivityResultLauncher<Intent>
private lateinit var viewModel: HomeFragment.SharedViewModel
private lateinit var prefs: SharedPreferencesHelper

private var song:Song = Song()
private val songs = arrayListOf<Song>()
Expand All @@ -32,6 +32,7 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val prefs = getSharedPreferences("song", MODE_PRIVATE)

setContentView(binding.root)

Expand Down Expand Up @@ -77,21 +78,20 @@ class MainActivity : AppCompatActivity() {

binding.layoutPlayContainer.setOnClickListener{
val editor = getSharedPreferences("song", MODE_PRIVATE).edit()
editor.putInt("songId",song.id)
editor.putInt("songId",songs[nowPos].id-1)
editor.apply()

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

prefs = SharedPreferencesHelper(this)

}
//문제없음..
override fun onStart() {
super.onStart()
val spf = getSharedPreferences("song", MODE_PRIVATE)
val songId = spf.getInt("songId",0)
val prefs = getSharedPreferences("song", MODE_PRIVATE)
val songId = prefs.getInt("songId",0)

val songDB = SongDatabase.getInstance(this)!!

Expand All @@ -114,6 +114,7 @@ class MainActivity : AppCompatActivity() {
override fun onResume() {
super.onResume()
updateFromSharedPrefs() // 추가적으로 UI 업데이트
Log.d("MainActivityNextSongId", nowPos.toString())
}


Expand Down Expand Up @@ -162,22 +163,32 @@ class MainActivity : AppCompatActivity() {
}

private fun playNextSong() {
val prefs = getSharedPreferences("song", MODE_PRIVATE)
val songDB = SongDatabase.getInstance(this)!!
val currentSongId = prefs.getSongId()
val currentSongId = prefs.getInt("songId",0)
val nextSong = songDB.songDao().getNextSong(currentSongId) // 다음 곡을 조회하는 로직 필요
if (nextSong != null) {
setMiniPlayer(nextSong)
prefs.saveSongId(nextSong.id) // Shared Preferences에 다음 곡 ID 저장
val editor = prefs.edit()
editor.putInt("songId", nextSong.id)
editor.apply()
Log.d("isRight?", nextSong.id.toString())
// 일단 재생중으로 선택. Shared Preferences에 다음 곡 ID 저장
}
}

private fun playPreviousSong() {
val prefs = getSharedPreferences("song", MODE_PRIVATE)
val songDB = SongDatabase.getInstance(this)!!
val currentSongId = prefs.getSongId()
val currentSongId = prefs.getInt("songId",0)
val previousSong = songDB.songDao().getPreviousSong(currentSongId) // 이전 곡을 조회하는 로직 필요
if (previousSong != null) {
setMiniPlayer(previousSong)
prefs.saveSongId(previousSong.id) // Shared Preferences에 이전 곡 ID 저장
val editor = prefs.edit()
editor.putInt("songId", previousSong.id)
editor.apply()
Log.d("isRight?", previousSong.id.toString())
// 일단 재생중으로 선택. Shared Preferences에 이전 곡 ID 저장
}
}

Expand Down
12 changes: 11 additions & 1 deletion UMC_6th/app/src/main/java/com/example/umc_6th/SongActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class SongActivity : AppCompatActivity() {
initPlayList() // 노래 목록 초기화
val sharedPref = getSharedPreferences("song", MODE_PRIVATE)
nowPos = sharedPref.getInt("songId", 0)
Log.d("songId", nowPos.toString())
if (songs.isNotEmpty()) {
updateSongUI(songs[nowPos])
}
Expand Down Expand Up @@ -194,6 +193,16 @@ class SongActivity : AppCompatActivity() {
}
}


override fun onStart() {
super.onStart()
val sharedPref = getSharedPreferences("song", MODE_PRIVATE)
nowPos = sharedPref.getInt("songId", 0)
Log.d("SongActivitysongId", nowPos.toString())
if (songs.isNotEmpty()) {
updateSongUI(songs[nowPos])
}
}
override fun onPause() {
super.onPause()
saveCurrentSongInfo()
Expand Down Expand Up @@ -283,6 +292,7 @@ class SongActivity : AppCompatActivity() {
nowPos = 0 // 목록의 첫 번째 곡으로 돌아가기
}
updateSongUI(songs[nowPos])
Log.d("SongActivityNextSongId", nowPos.toString())
startOrResumeTimer() // 타이머를 다시 시작
}

Expand Down

0 comments on commit 26d0394

Please # to comment.