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

음악을 재생목록에 추가할 때 발생하는 이슈 처리 #328

Merged
merged 4 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -102,7 +102,8 @@ class MainActivity : AppCompatActivity() {

navHostFragment.findNavController().addOnDestinationChangedListener { _, destination, _ ->
when (destination.id) {
com.ohdodok.catchytape.feature.player.R.id.player_fragment -> {
com.ohdodok.catchytape.feature.player.R.id.player_fragment,
com.ohdodok.catchytape.feature.player.R.id.playlist_bottom_sheet -> {
hideBottomNav()
hidePlayerController()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.annotation.StringRes
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.findNavController
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.ohdodok.catchytape.core.ui.cterror.toMessageId
import com.ohdodok.catchytape.core.ui.databinding.BottomSheetPlaylistBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
Expand All @@ -31,21 +34,34 @@ class PlaylistBottomSheet : BottomSheetDialogFragment() {
binding.viewModel = viewModel
binding.rvPlaylists.adapter = PlaylistAdapter()

observeEvent()
observeEvents()

return binding.root
}

private fun observeEvent() {
private fun observeEvents() {
viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.closeEvent.collect {
findNavController().popBackStack()
viewModel.events.collect { event ->
when (event) {
is PlaylistBottomSheetEvent.Success -> {
showMessage(R.string.add_to_playlist_success)
findNavController().popBackStack()
}

is PlaylistBottomSheetEvent.ShowMessage -> {
showMessage(event.error.toMessageId())
}
}
}
}
}
}

private fun showMessage(@StringRes messageId: Int) {
Toast.makeText(requireActivity(), messageId, Toast.LENGTH_SHORT).show()
}

Comment on lines +61 to +64
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

base fragment 에 showMessage 사용하지 않은 이유가 있을까용?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BaseFragment를 상속 받지 않았어요

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아항 제가 잘못봤네용

override fun onDestroyView() {
super.onDestroyView()
_binding = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package com.ohdodok.catchytape.core.ui
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ohdodok.catchytape.core.domain.model.CtErrorType
import com.ohdodok.catchytape.core.domain.model.CtException
import com.ohdodok.catchytape.core.domain.model.Playlist
import com.ohdodok.catchytape.core.domain.repository.PlaylistRepository
import com.ohdodok.catchytape.core.ui.model.PlaylistUiModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
Expand All @@ -17,6 +20,11 @@ import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import javax.inject.Inject

sealed interface PlaylistBottomSheetEvent {
data object Success : PlaylistBottomSheetEvent
data class ShowMessage(val error: CtErrorType) : PlaylistBottomSheetEvent
}

@HiltViewModel
class PlaylistBottomSheetViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
Expand All @@ -30,8 +38,16 @@ class PlaylistBottomSheetViewModel @Inject constructor(
private val _playlists = MutableStateFlow(emptyList<PlaylistUiModel>())
val playlists: StateFlow<List<PlaylistUiModel>> = _playlists.asStateFlow()

private val _closeEvent = MutableSharedFlow<Unit>()
val closeEvent: SharedFlow<Unit> = _closeEvent.asSharedFlow()
private val _events = MutableSharedFlow<PlaylistBottomSheetEvent>()
val events: SharedFlow<PlaylistBottomSheetEvent> = _events.asSharedFlow()

private val exceptionHandler = CoroutineExceptionHandler { _, throwable ->
val errorType =
if (throwable is CtException) throwable.ctError
else CtErrorType.UN_KNOWN

viewModelScope.launch { _events.emit(PlaylistBottomSheetEvent.ShowMessage(errorType)) }
}

init {
fetchPlaylists()
Expand All @@ -44,9 +60,9 @@ class PlaylistBottomSheetViewModel @Inject constructor(
}

private fun addMusicToPlaylist(playlistId: Int, musicId: String) {
viewModelScope.launch {
viewModelScope.launch(exceptionHandler) {
playlistRepository.addMusicToPlaylist(playlistId = playlistId, musicId = musicId)
_closeEvent.emit(Unit)
_events.emit(PlaylistBottomSheetEvent.Success)
}
}

Expand Down
3 changes: 2 additions & 1 deletion android/core/ui/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<string name="track_size">트랙 %d개</string>

<string name="add_to_playlist">재생 목록에 노래 추가</string>
<string name="add_to_playlist_success">노래를 추가했어요.</string>

<string name="error_message_connection">"error_message_connection"</string>
<string name="error_message_ssl_hand_shake">"error_message_ssl_hand_shake"</string>
Expand All @@ -48,7 +49,7 @@
<string name="error_message_server">서버가 작업 중이에요. 잠시만 기다려주세요.</string>
<string name="error_message_not_exist_playlist_on_user">"error_message_not_exist_playlist_on_user"</string>
<string name="error_message_not_exist_music">"error_message_not_exist_music"</string>
<string name="error_message_already_added">"error_message_already_added"</string>
<string name="error_message_already_added">이미 추가된 음악이에요.</string>
<string name="error_message_invalid_input_value">"error_message_invalid_input_value"</string>
<string name="error_message_not_exist_user">"error_message_not_exist_user"</string>
<string name="error_message_already_exist_email">이미 회원가입이 완료된 이메일이에요 !</string>
Expand Down