-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #225 from boostcampwm2023/android/feature/61
내가 업로드한 노래 전체 보기
- Loading branch information
Showing
7 changed files
with
193 additions
and
14 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
...id/feature/mypage/src/main/java/com/ohdodok/catchytape/feature/mypage/MyMusicsFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.ohdodok.catchytape.feature.mypage | ||
|
||
import android.os.Bundle | ||
import android.view.View | ||
import androidx.fragment.app.viewModels | ||
import com.ohdodok.catchytape.core.ui.BaseFragment | ||
import com.ohdodok.catchytape.core.ui.MusicAdapter | ||
import com.ohdodok.catchytape.core.ui.Orientation | ||
import com.ohdodok.catchytape.core.ui.toMessageId | ||
import com.ohdodok.catchytape.feature.mypage.databinding.FragmentMyMusicsBinding | ||
import dagger.hilt.android.AndroidEntryPoint | ||
|
||
@AndroidEntryPoint | ||
class MyMusicsFragment : BaseFragment<FragmentMyMusicsBinding>(R.layout.fragment_my_musics) { | ||
private val viewModel: MyMusicsViewModel by viewModels() | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
binding.viewModel = viewModel | ||
|
||
setupRecyclerView() | ||
observeEvents() | ||
setupBackStack(binding.tbMyMusics) | ||
} | ||
|
||
private fun setupRecyclerView() { | ||
binding.rvMyMusics.adapter = MusicAdapter(Orientation.VERTICAL) | ||
} | ||
|
||
private fun observeEvents() { | ||
repeatOnStarted { | ||
viewModel.events.collect { event -> | ||
when (event) { | ||
is MyMusicsEvent.ShowMessage -> { | ||
showMessage(event.error.toMessageId()) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
...d/feature/mypage/src/main/java/com/ohdodok/catchytape/feature/mypage/MyMusicsViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.ohdodok.catchytape.feature.mypage | ||
|
||
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.Music | ||
import com.ohdodok.catchytape.core.domain.repository.MusicRepository | ||
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 | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.onEach | ||
import kotlinx.coroutines.flow.update | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.plus | ||
import javax.inject.Inject | ||
|
||
data class MyMusicsUiState( | ||
val myMusics: List<Music> = emptyList() | ||
) | ||
|
||
sealed interface MyMusicsEvent { | ||
data class ShowMessage( | ||
val error: CtErrorType | ||
) : MyMusicsEvent | ||
} | ||
|
||
@HiltViewModel | ||
class MyMusicsViewModel @Inject constructor( | ||
private val musicRepository: MusicRepository, | ||
) : ViewModel() { | ||
|
||
private val _uiState = MutableStateFlow(MyMusicsUiState()) | ||
val uiState: StateFlow<MyMusicsUiState> = _uiState.asStateFlow() | ||
|
||
private val _events = MutableSharedFlow<MyMusicsEvent>() | ||
val events: SharedFlow<MyMusicsEvent> = _events.asSharedFlow() | ||
|
||
private val exceptionHandler = CoroutineExceptionHandler { _, throwable -> | ||
viewModelScope.launch { | ||
val errorType = | ||
if (throwable is CtException) throwable.ctError | ||
else CtErrorType.UN_KNOWN | ||
|
||
_events.emit(MyMusicsEvent.ShowMessage(errorType)) | ||
} | ||
} | ||
|
||
private val viewModelScopeWithExceptionHandler = viewModelScope + exceptionHandler | ||
|
||
init { | ||
fetchMyMusics() | ||
} | ||
|
||
private fun fetchMyMusics() { | ||
musicRepository.getMyMusics() | ||
.onEach { musics -> | ||
_uiState.update { | ||
it.copy(myMusics = musics) | ||
} | ||
} | ||
.launchIn(viewModelScopeWithExceptionHandler) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
android/feature/mypage/src/main/res/layout/fragment_my_musics.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<layout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools"> | ||
|
||
<data> | ||
|
||
<variable | ||
name="viewModel" | ||
type="com.ohdodok.catchytape.feature.mypage.MyMusicsViewModel" /> | ||
</data> | ||
|
||
<androidx.constraintlayout.widget.ConstraintLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<com.google.android.material.appbar.MaterialToolbar | ||
android:id="@+id/tb_my_musics" | ||
android:layout_width="0dp" | ||
android:layout_height="@dimen/appbar_height" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" | ||
app:navigationIcon="@drawable/ic_arrow_back" | ||
app:title="@string/my_musics" /> | ||
|
||
<androidx.recyclerview.widget.RecyclerView | ||
android:id="@+id/rv_my_musics" | ||
android:layout_width="0dp" | ||
android:layout_height="0dp" | ||
android:clipToPadding="false" | ||
android:paddingHorizontal="@dimen/margin_horizontal" | ||
android:paddingTop="@dimen/large" | ||
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toBottomOf="@id/tb_my_musics" | ||
app:list="@{viewModel.uiState.myMusics}" | ||
tools:listitem="@layout/item_music_vertical" /> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> | ||
</layout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters