Skip to content

Commit

Permalink
[nunu/#18] feat: SongActivity Time Seekbar 구현
Browse files Browse the repository at this point in the history
- 타이머 중지 및 재실행 구현
- 파란바 타이머 초에 맞춰 회색바 기준으로 길이 조절

문제
- 색 적용 제대로 안됨
- 초기 시작시 파란바 길이 엄청 길어졌다가 시작됨
  • Loading branch information
Ssamssamukja committed May 7, 2024
1 parent f6276d8 commit 0445aad
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 24 deletions.
84 changes: 62 additions & 22 deletions UMC_6th/app/src/main/java/com/example/umc_6th/SongActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ import android.content.Intent
import android.graphics.PorterDuff
import android.os.Bundle
import android.os.PersistableBundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.ui.graphics.Color
import androidx.core.content.ContextCompat
import com.example.umc_6th.databinding.ActivitySongBinding
import kotlin.math.log

class SongActivity : AppCompatActivity() {
private lateinit var binding : ActivitySongBinding
private var maxBarWidth = 0
private var timerThread: Thread? = null
@Volatile private var isTimerRunning: Boolean = false
private var elapsedSeconds = 0
private val totalSeconds = 60

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -23,46 +31,78 @@ class SongActivity : AppCompatActivity() {
binding.txSongTitle.text = songTitle
binding.txSongArtist.text = songArtist

binding.imgSongRandom.setOnClickListener(){
setupColorFilters()
setupButtonListeners()

binding.imgSongDown.setOnClickListener {
val returnIntent = Intent().apply {
putExtra("albumTitle", "LILAC")
}
setResult(Activity.RESULT_OK, returnIntent)
finish()
}

binding.viewSongBar.post{
maxBarWidth = binding.viewSongBar.width
binding.viewSongBarBlue.layoutParams.width=1
}

}

private fun setupColorFilters() {
binding.imgSongRandom.setOnClickListener {
if (binding.imgSongRandom.colorFilter != null) {
binding.imgSongRandom.clearColorFilter()
} else {
binding.imgSongRandom.setColorFilter(R.color.flo, PorterDuff.Mode.SRC_IN)
binding.imgSongRandom.setColorFilter(ContextCompat.getColor(this, R.color.flo), PorterDuff.Mode.SRC_IN)
}
}
binding.imgSongRepeat.setOnClickListener(){

binding.imgSongRepeat.setOnClickListener {
if (binding.imgSongRepeat.colorFilter != null) {
binding.imgSongRepeat.clearColorFilter()
} else {
binding.imgSongRepeat.setColorFilter(R.color.flo, PorterDuff.Mode.SRC_IN)
binding.imgSongRepeat.setColorFilter(ContextCompat.getColor(this, R.color.flo), PorterDuff.Mode.SRC_IN)
}
}
}

binding.imgSongDown.setOnClickListener {
val returnIntent = Intent().apply {
putExtra("albumTitle", "LILAC")
}
setResult(Activity.RESULT_OK, returnIntent)
finish()
private fun setupButtonListeners() {
binding.imgSongPlayBtn.setOnClickListener {
isTimerRunning = true
binding.imgSongPlayPauseBtn.visibility = View.VISIBLE
binding.imgSongPlayBtn.visibility = View.GONE
startOrResumeTimer()
}

binding.imgSongPlayPauseBtn.setOnClickListener {
isTimerRunning = false
binding.imgSongPlayBtn.visibility = View.VISIBLE
binding.imgSongPlayPauseBtn.visibility = View.GONE
}
}

private fun startOrResumeTimer() {
if (timerThread == null || !timerThread!!.isAlive) {
startTimer(totalSeconds)
}
startTimer(60) // 미리듣기 1분
}

private fun startTimer(totalSeconds: Int) {
var elapsedSeconds = 0
Thread {
while (elapsedSeconds <= totalSeconds) {
timerThread = Thread {
while (elapsedSeconds <= totalSeconds && isTimerRunning) {
val minutes = elapsedSeconds / 60
val seconds = elapsedSeconds % 6

runOnUiThread { // 메인 스레드에서 UI 업데이트
val seconds = elapsedSeconds % 60
val newWidth = maxBarWidth * elapsedSeconds / totalSeconds
runOnUiThread {
val layoutParams = binding.viewSongBarBlue.layoutParams
layoutParams.width = newWidth
binding.viewSongBarBlue.layoutParams = layoutParams
binding.txSongBarStartTime.text = String.format("%02d:%02d", minutes, seconds)
}
Thread.sleep(1000) // 1초 대기
Thread.sleep(1000)
elapsedSeconds++
}
runOnUiThread {
binding.txSongBarStartTime.text = "미리듣기는 1분입니다"
}
}.start()
}.apply { start() }
}
}
4 changes: 2 additions & 2 deletions UMC_6th/app/src/main/res/layout/activity_song.xml
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@

<View
android:id="@+id/viewSongBarBlue"
android:layout_width="35dp"
android:layout_width="1dp"
android:layout_height="2dp"
android:layout_marginStart="20dp"
android:background="@color/flo"
Expand All @@ -181,7 +181,7 @@
android:id="@+id/txSongBarStartTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:08"
android:text="00:00"
android:textColor="@color/flo"
app:layout_constraintStart_toStartOf="@+id/viewSongBar"
app:layout_constraintTop_toBottomOf="@+id/viewSongBar" />
Expand Down

0 comments on commit 0445aad

Please sign in to comment.