Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UI/#61] 리듬뷰 / UI 수정사항 대응 #63

Merged
merged 17 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,34 @@ import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
import androidx.fragment.app.replace
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import com.kkkk.core.base.BaseActivity
import com.kkkk.presentation.main.profile.ProfileFragment
import com.kkkk.presentation.main.record.RecordFragment
import com.kkkk.presentation.main.rhythm.RhythmFragment
import com.kkkk.presentation.main.rhythm.RhythmViewModel
import com.kkkk.presentation.main.rhythm.StretchFragment
import com.kkkk.presentation.main.study.StudyFragment
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kr.genti.presentation.R
import kr.genti.presentation.databinding.ActivityMainBinding

@AndroidEntryPoint
class MainActivity : BaseActivity<ActivityMainBinding>(R.layout.activity_main) {

private lateinit var rhythmViewModel: RhythmViewModel

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

initBnvItemIconTintList()
initBnvItemSelectedListener()
initViewModelProvider()
observeStretchViewNavigate()
}

private fun initBnvItemIconTintList() {
Expand Down Expand Up @@ -56,4 +68,18 @@ class MainActivity : BaseActivity<ActivityMainBinding>(R.layout.activity_main) {
replace<T>(R.id.fcv_main, T::class.java.canonicalName)
}
}

private fun initViewModelProvider() {
rhythmViewModel = ViewModelProvider(this)[RhythmViewModel::class.java]
}

private fun observeStretchViewNavigate() {
rhythmViewModel.isStretchView.flowWithLifecycle(lifecycle).onEach { isStretch ->
if (isStretch) {
navigateTo<StretchFragment>()
} else {
navigateTo<RhythmFragment>()
}
}.launchIn(lifecycleScope)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import com.google.android.gms.wearable.DataEventBuffer
import com.google.android.gms.wearable.DataMapItem
import com.google.android.gms.wearable.Wearable
import com.kkkk.core.base.BaseFragment
import com.kkkk.core.extension.colorOf
import com.kkkk.core.extension.drawableOf
import com.kkkk.core.extension.setOnSingleClickListener
import com.kkkk.core.extension.setStatusBarColor
import com.kkkk.core.extension.stringOf
Expand Down Expand Up @@ -59,10 +61,12 @@ class RhythmFragment : BaseFragment<FragmentRhythmBinding>(R.layout.fragment_rhy
) {
super.onViewCreated(view, savedInstanceState)

initChangeLevelBtnListener()
initChangeRhythmBtnListener()
initStretchNavigateBtnListener()
initPlayBtnListener()
initStopBtnListener()
initWearableSyncBtnListener()
initExistingRhythm()
observeStepCount()
observeRhythmChanged()
observeRhythmUrlState()
Expand All @@ -71,13 +75,19 @@ class RhythmFragment : BaseFragment<FragmentRhythmBinding>(R.layout.fragment_rhy
setStatusBarColor(R.color.white)
}

private fun initChangeLevelBtnListener() {
private fun initChangeRhythmBtnListener() {
binding.btnChangeLevel.setOnSingleClickListener {
rhythmBottomSheet = RhythmBottomSheet()
rhythmBottomSheet?.show(parentFragmentManager, BOTTOM_SHEET_CHANGE_LEVEL)
}
}

private fun initStretchNavigateBtnListener() {
binding.btnStretchMode.setOnSingleClickListener {
viewModel.navigateToStretchView(true)
}
}

private fun initPlayBtnListener() {
binding.btnRhythmPlay.setOnSingleClickListener {
if (::mediaPlayer.isInitialized) {
Expand Down Expand Up @@ -116,55 +126,61 @@ class RhythmFragment : BaseFragment<FragmentRhythmBinding>(R.layout.fragment_rhy
}
}

private fun initExistingRhythm() {
setUiWithCurrentRhythm()
viewModel.postToGetRhythmUrlFromServer()
}

private fun observeStepCount() {
viewModel.stepCount.flowWithLifecycle(lifecycle).distinctUntilChanged().onEach { level ->
binding.tvRhythmStep.text = viewModel.stepCount.value.toString()
viewModel.stepCount.flowWithLifecycle(lifecycle).distinctUntilChanged().onEach {
binding.tvRhythmStep.text =
getString(R.string.rhythm_tv_step, viewModel.stepCount.value)
}.launchIn(lifecycleScope)
}

private fun observeRhythmChanged() {
viewModel.isRhythmChanged.flowWithLifecycle(lifecycle).distinctUntilChanged()
viewModel.isRhythmChanged.flowWithLifecycle(lifecycle)
.onEach { isChanged ->
if (isChanged) {
if (::mediaPlayer.isInitialized) {
mediaPlayer.pause()
switchPlayingState(false)
}
setUiWithCurrentLevel()
setUiWithCurrentRhythm()
viewModel.resetRhythmChangedState()
viewModel.postToGetRhythmUrlFromServer()
}
}.launchIn(lifecycleScope)
}

private fun setUiWithCurrentLevel() {
val color = when (viewModel.bit.rem(3)) {
private fun setUiWithCurrentRhythm() {
val color = when (viewModel.bit) {
2 -> COLOR_PURPLE
3 -> COLOR_SKY
4 -> COLOR_GREEN
6 -> COLOR_PURPLE
8 -> COLOR_SKY
else -> return
}
// with(binding) {
// tvRhythmLevel.apply {
// text = getString(R.string.rhythm_tv_level, viewModel.rhythmLevel.value)
// setTextColor(colorOf(getResource("${color}_50", COLOR)))
// background =
// drawableOf(getResource("shape_white_fill_${color}50_line_17_rect", DRAWABLE))
// }
// tvRhythmStep.apply {
// setTextColor(colorOf(getResource("${color}_50", COLOR)))
// background =
// drawableOf(getResource("shape_white_fill_${color}50_line_17_rect", DRAWABLE))
// }
// ivRhythmBg.setImageResource(getResource("img_rhythm_bg_$color", DRAWABLE))
// lottieRhythmBg.apply {
// setAnimation(getResource("stempo_rhythm_$color", RAW))
// speed = viewModel.bpm / FLOAT_120
// playAnimation()
// }
// }
with(binding) {
tvRhythmBpm.apply {
text = getString(R.string.rhythm_tv_bpm, viewModel.bpm)
setTextColor(colorOf(getResource("${color}_50", COLOR)))
background =
drawableOf(getResource("shape_white_fill_${color}50_line_17_rect", DRAWABLE))
}
tvRhythmBit.apply {
text = getString(R.string.rhythm_tv_bit, viewModel.bit)
background =
drawableOf(getResource("shape_${color}50_fill_17_rect", DRAWABLE))
}
ivRhythmBg.setImageResource(getResource("img_rhythm_bg_$color", DRAWABLE))
lottieRhythmBg.apply {
setAnimation(getResource("stempo_rhythm_$color", RAW))
speed = viewModel.bpm / FLOAT_120
playAnimation()
}
}
}

private fun getResource(name: String, defType: String) =
Expand Down Expand Up @@ -225,10 +241,7 @@ class RhythmFragment : BaseFragment<FragmentRhythmBinding>(R.layout.fragment_rhy
if (::mediaPlayer.isInitialized) mediaPlayer.release()
mediaPlayer = MediaPlayer().apply {
setDataSource(
File(
requireContext().filesDir,
viewModel.filename
).absolutePath
File(requireContext().filesDir, viewModel.filename).absolutePath
)
prepare()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ constructor(
var isBpmMinusAvailable = MutableLiveData<Boolean>(false)
var isBpmPlusAvailable = MutableLiveData<Boolean>(true)

private val _isStretchView = MutableSharedFlow<Boolean>()
val isStretchView: SharedFlow<Boolean> = _isStretchView

private val _isRhythmChanged = MutableSharedFlow<Boolean>()
val isRhythmChanged: SharedFlow<Boolean> = _isRhythmChanged

Expand All @@ -57,6 +60,7 @@ constructor(

init {
initRhythmLevelFromDataStore()

}

private fun initRhythmLevelFromDataStore() {
Expand Down Expand Up @@ -106,7 +110,16 @@ constructor(
}

fun resetRhythmChangedState() {
_isRhythmChanged.resetReplayCache()
viewModelScope.launch {
_isRhythmChanged.emit(false)
}
}

fun navigateToStretchView(isStretch: Boolean) {
viewModelScope.launch {
_isStretchView.emit(isStretch)
_isStretchView.resetReplayCache()
}
}

fun postToGetRhythmUrlFromServer() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.kkkk.presentation.main.rhythm

import android.media.MediaPlayer
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels
import com.kkkk.core.base.BaseFragment
import com.kkkk.core.extension.setOnSingleClickListener
import com.kkkk.core.extension.stringOf
import com.kkkk.core.extension.toast
import dagger.hilt.android.AndroidEntryPoint
import kr.genti.presentation.R
import kr.genti.presentation.databinding.FragmentStretchBinding
import java.io.File

@AndroidEntryPoint
class StretchFragment : BaseFragment<FragmentStretchBinding>(R.layout.fragment_stretch) {

private val viewModel by activityViewModels<RhythmViewModel>()
private lateinit var mediaPlayer: MediaPlayer

override fun onViewCreated(
view: View,
savedInstanceState: Bundle?,
) {
super.onViewCreated(view, savedInstanceState)

initRhythmNavigateBtnListener()
initPlayBtnListener()
initStopBtnListener()
setMediaPlayer()
}

private fun initRhythmNavigateBtnListener() {
binding.btnRhythmMode.setOnSingleClickListener {
viewModel.navigateToStretchView(false)
}
}

private fun initPlayBtnListener() {
binding.btnStretchPlay.setOnSingleClickListener {
if (::mediaPlayer.isInitialized) {
mediaPlayer.start()
switchPlayingState(true)
requireActivity().window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
} else {
toast(stringOf(R.string.error_msg))
}
}
}

private fun initStopBtnListener() {
binding.btnStretchStop.setOnSingleClickListener {
if (::mediaPlayer.isInitialized) {
mediaPlayer.pause()
switchPlayingState(false)
requireActivity().window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
}
}

private fun switchPlayingState(start: Boolean) {
with(binding) {
btnStretchPlay.isVisible = !start
btnStretchStop.isVisible = start
lottieStretchBg.isVisible = start
}
}

private fun setMediaPlayer() {
if (File(requireContext().filesDir, STRETCH_WAV_FILE).exists()) {
if (::mediaPlayer.isInitialized) mediaPlayer.release()
mediaPlayer = MediaPlayer().apply {
setDataSource(
File(requireContext().filesDir, STRETCH_WAV_FILE).absolutePath
)
prepare()
}
} else {
toast(stringOf(R.string.error_msg))
}
}

companion object {
const val STRETCH_WAV_FILE = "stempo_bpm_65_bit_2"
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/gray_200" />
<corners android:radius="16dp" />
<corners android:radius="17dp" />
</shape>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/gray_200" />
<corners android:radius="26dp" />
</shape>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/green_50" />
<corners android:radius="17dp" />
</shape>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/purple_50" />
<corners android:radius="16dp" />
<corners android:radius="17dp" />
</shape>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/sky_50" />
<corners android:radius="17dp" />
</shape>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white" />
<corners android:radius="26dp" />
</shape>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/white" />
<stroke android:width="2dp" />
<stroke android:color="@color/gray_600" />
<corners android:radius="17dp"/>
</shape>
Loading
Loading