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

내가 업로드한 노래 3개 가져오기 #214

Merged
merged 4 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -20,4 +20,6 @@ interface MusicApi {
@GET("musics/recent-uploads")
suspend fun getRecentUploads(): List<MusicResponse>

@GET("musics/my-uploads")
suspend fun getMyUploads(): List<MusicResponse>
Copy link
Collaborator

@2taezeat 2taezeat Nov 28, 2023

Choose a reason for hiding this comment

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

getMyUploads 는 함수명 치고, 너무 추상적인 거 같은데 getMyUploadedMusics 가 어떠신가요?

Copy link
Collaborator

Choose a reason for hiding this comment

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

getRecentUploads 도 마찬가지 맥락입니다~

Copy link
Member Author

Choose a reason for hiding this comment

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

데이터 레이어에 대한 추상화는 repository에서 이루어져서, 여기서는 서버를 그대로 반영하려고 했어!

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ohdodok.catchytape.core.data.model

import com.ohdodok.catchytape.core.domain.model.Music
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

Expand All @@ -13,4 +14,15 @@ data class MusicResponse (
val musicFile : String,
val genre: String,
val user: NicknameResponse
)
) {
fun toDomain(): Music {
return Music(
id = musicId,
title = title,
artist = user.nickname,
imageUrl = cover
)
}
}
Comment on lines +18 to +26
Copy link
Member

Choose a reason for hiding this comment

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

요 부분 슬랙에도 말했는데 domain model 에서 data model로 변환할 때는 domain 이 data를 몰라서 클래스안에 멤버 함수로 둘 수가 없게 돼!! 그래서 두가지 방식을 일관성 있게 처리할라면 이 멤버 함수도 internal 확장함수로 두어야할 것 같아용

Copy link
Member Author

Choose a reason for hiding this comment

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

나는 그래도 멤버함수파이긴 한데, 이거는 일단 머지해 놓고 다시 고민해볼게


internal fun List<MusicResponse>.toDomains(): List<Music> = this.map { it.toDomain() }
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.ohdodok.catchytape.core.data.repository
import com.ohdodok.catchytape.core.data.api.MusicApi
import com.ohdodok.catchytape.core.data.model.MusicRequest
import com.ohdodok.catchytape.core.data.model.MusicResponse
import com.ohdodok.catchytape.core.data.model.toDomains
import com.ohdodok.catchytape.core.domain.model.Music
import com.ohdodok.catchytape.core.domain.repository.MusicRepository
import kotlinx.coroutines.flow.Flow
Expand All @@ -20,7 +21,7 @@ class MusicRepositoryImpl @Inject constructor(

override fun getRecentUploadedMusic(): Flow<List<Music>> = flow {
val musicResponses = musicApi.getRecentUploads()
emit(musicResponses.map { it.toDomain() })
emit(musicResponses.toDomains())
}

override fun postMusic(
Expand All @@ -42,14 +43,9 @@ class MusicRepositoryImpl @Inject constructor(

emit(response)
}
}

fun MusicResponse.toDomain(): Music {
return Music(
id = musicId,
title = title,
artist = user.nickname,
imageUrl = cover
)
override fun getMyMusics(): Flow<List<Music>> = flow {
val myMusics = musicApi.getMyUploads()
emit(myMusics.toDomains())
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ interface MusicRepository {

fun postMusic(musicId: String, title: String, imageUrl: String, audioUrl: String, genre: String): Flow<Unit>

fun getMyMusics(): Flow<List<Music>>

}
2 changes: 2 additions & 0 deletions android/core/ui/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@
<dimen name="btn_radius">4dp</dimen>

<dimen name="et_height">50dp</dimen>

<dimen name="appbar_height">48dp</dimen>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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.toMessageId
import com.ohdodok.catchytape.feature.mypage.databinding.FragmentMyPageBinding
import dagger.hilt.android.AndroidEntryPoint

Expand All @@ -15,7 +16,19 @@ class MyPageFragment : BaseFragment<FragmentMyPageBinding>(R.layout.fragment_my_
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.viewModel = viewModel
}

observeEvents()
}

private fun observeEvents() {
repeatOnStarted {
viewModel.events.collect { event ->
when (event) {
is MyPageEvent.ShowMessage -> {
showMessage(event.error.toMessageId())
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,72 @@
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 MyPageUiState(
val myMusics: List<Music> = emptyList()
)

@HiltViewModel
class MyPageViewModel @Inject constructor() : ViewModel() {
class MyPageViewModel @Inject constructor(
private val musicRepository: MusicRepository,
) : ViewModel() {

private val _uiState = MutableStateFlow(MyPageUiState())
val uiState: StateFlow<MyPageUiState> = _uiState.asStateFlow()

private val _events = MutableSharedFlow<MyPageEvent>()
val events: SharedFlow<MyPageEvent> = _events.asSharedFlow()
}

private val exceptionHandler = CoroutineExceptionHandler { _, throwable ->
viewModelScope.launch {
if (throwable is CtException) {
_events.emit(MyPageEvent.ShowMessage(throwable.ctError))
} else {
_events.emit(MyPageEvent.ShowMessage(CtErrorType.UN_KNOWN))
}
}
}

sealed interface MyPageEvent {
private val viewModelScopeWithExceptionHandler = viewModelScope + exceptionHandler

init {
fetchMyMusics()
}

private fun fetchMyMusics() {
musicRepository.getMyMusics()
.onEach { response ->
Copy link
Member

Choose a reason for hiding this comment

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

response 보다는 musics 로 하는게 어떨까??? response 는 데이터 레이어와 관련된 것 같은 느낌이 들어서!!!

_uiState.update {
it.copy(
myMusics = response.take(3)
)
Copy link
Collaborator

@2taezeat 2taezeat Nov 28, 2023

Choose a reason for hiding this comment

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

마이너 한데,
it.copy( myMusics = response.take(3) )
로 idenet 줄일 수 있으면 좋을 거 같아요 (가능하다면)

}
}
.launchIn(viewModelScopeWithExceptionHandler)
}
}

sealed interface MyPageEvent {
data class ShowMessage(
val error: CtErrorType
) : MyPageEvent
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<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.MyPageViewModel" />
Expand All @@ -11,6 +14,81 @@
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/mt_my_page"
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:title="@string/my_page">

</com.google.android.material.appbar.MaterialToolbar>

<TextView
android:id="@+id/tv_upload"
style="@style/BodyLarge"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/margin_horizontal"
android:text="@string/share_music"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/mt_my_page" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_upload"
style="@style/BodyLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/margin_horizontal"
android:background="@android:color/transparent"
android:minWidth="0dp"
android:minHeight="0dp"
android:text="@string/upload"
android:textColor="@color/primary"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/tv_upload" />

<TextView
android:id="@+id/tv_my_music"
style="@style/BodyLarge"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_horizontal"
android:layout_marginTop="@dimen/extra_large"
android:text="@string/my_musics"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_upload" />

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btn_more"
style="@style/BodySmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/margin_horizontal"
android:background="@android:color/transparent"
android:minWidth="0dp"
android:minHeight="0dp"
android:text="@string/more"
app:layout_constraintBottom_toBottomOf="@id/tv_my_music"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/tv_my_music" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_musics"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="@dimen/large"
android:paddingHorizontal="@dimen/margin_horizontal"
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/tv_my_music"
app:list="@{viewModel.uiState.myMusics}"
tools:itemCount="3"
tools:listitem="@layout/item_music_vertical" />

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
5 changes: 5 additions & 0 deletions android/feature/mypage/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="share_music">노래를 공유해 보세요.</string>
<string name="my_musics">내가 업로드한 노래</string>
</resources>