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

최근 업로드된 노래 #160

Merged
merged 3 commits into from
Nov 23, 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 @@ -2,6 +2,7 @@ package com.ohdodok.catchytape.core.data.api

import com.ohdodok.catchytape.core.data.model.MusicGenresResponse
import com.ohdodok.catchytape.core.data.model.MusicRequest
import com.ohdodok.catchytape.core.data.model.MusicResponse
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.GET
Expand All @@ -17,4 +18,7 @@ interface MusicApi {
@Body music: MusicRequest
): Response<Unit>

@GET("musics/recent-uploads")
suspend fun getRecentUploads(): Response<List<MusicResponse>>

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ object NetworkModule {
@Singleton
@Provides
fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit {
val jsonConfig = Json { ignoreUnknownKeys = true }
return Retrofit.Builder()
.addConverterFactory(Json.asConverterFactory("application/json".toMediaType()))
.addConverterFactory(jsonConfig.asConverterFactory("application/json".toMediaType()))
.client(okHttpClient)
.baseUrl(BuildConfig.BASE_URL)
.build()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.ohdodok.catchytape.core.data.model

import kotlinx.serialization.Serializable

@Serializable
data class MusicResponse (
val musicId: Int,
val title: String,
val cover: String,
val musicFile : String,
val genre: String,
val user: NicknameResponse
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import kotlinx.serialization.Serializable

@Serializable
data class NicknameResponse(
val nickname: String
)
val nickname: String,
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.ohdodok.catchytape.core.data.repository

import com.ohdodok.catchytape.core.data.api.MusicApi
import com.ohdodok.catchytape.core.data.model.MusicResponse
import com.ohdodok.catchytape.core.domain.model.Music
import com.ohdodok.catchytape.core.data.model.MusicRequest
import com.ohdodok.catchytape.core.domain.repository.MusicRepository
import kotlinx.coroutines.flow.Flow
Expand All @@ -20,6 +22,15 @@ class MusicRepositoryImpl @Inject constructor(
}
}

override fun getRecentUploadedMusic(): Flow<List<Music>> = flow {
val response = musicApi.getRecentUploads()
when (response.code()) {
// TODO : 네트워크 에러 로직 처리
in 200..299 -> emit(response.body()?.map { it.toDomain() } ?: emptyList())
else -> throw RuntimeException("네트워크 에러")
}
}

override fun postMusic(
title: String,
imageUrl: String,
Expand All @@ -40,4 +51,14 @@ class MusicRepositoryImpl @Inject constructor(
else -> throw RuntimeException("네트워크 에러")
}
}
}
}

fun MusicResponse.toDomain(): Music {
return Music(
id = musicId,
title = title,
artist = user.nickname,
imageUrl = cover
)
}

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.ohdodok.catchytape.core.domain.model

data class Music(
val id: String,
val id: Int,
val title: String,
val artist: String,
val imageUrl: String
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.ohdodok.catchytape.core.domain.repository

import com.ohdodok.catchytape.core.domain.model.Music
import kotlinx.coroutines.flow.Flow

interface MusicRepository {

fun getGenres(): Flow<List<String>>

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

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.ohdodok.catchytape.core.domain.usecase

import com.ohdodok.catchytape.core.domain.model.Music
import com.ohdodok.catchytape.core.domain.repository.MusicRepository
import kotlinx.coroutines.flow.Flow
import javax.inject.Inject

class GetRecentUploadedMusic @Inject constructor(
private val musicRepository: MusicRepository
) {
operator fun invoke(): Flow<List<Music>> = musicRepository.getRecentUploadedMusic()
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
android:id="@+id/iv_thumbnail"
android:layout_width="@dimen/music_horizontal_img"
android:layout_height="@dimen/music_horizontal_img"
app:imgUrl="@{music.imageUrl}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
android:id="@+id/iv_thumbnail"
android:layout_width="@dimen/music_vertical_img"
android:layout_height="@dimen/music_vertical_img"
app:imgUrl="@{music.imageUrl}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:background="@color/on_surface" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,23 @@ package com.ohdodok.catchytape.feature.home
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.ohdodok.catchytape.core.domain.model.Music
import kotlinx.coroutines.flow.Flow
import com.ohdodok.catchytape.core.domain.usecase.GetRecentUploadedMusic
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.update
import javax.inject.Inject

data class HomeUiState(
val recentlyUploadedMusics: List<Music> = emptyList()
)

class HomeViewModel constructor(
// todo : DI로 주입하는 코드로 변경
private val getMusicUseCase: GetMusicUseCase = GetMusicUseCase {
flow {
emit(
listOf(
Music("1", "title1", "artist1", ""),
Music("2", "title2", "artist2", ""),
Music("3", "title3", "artist3", "")
) // 화면 확인용 Dummy data 입니다.
)
}
},
@HiltViewModel
class HomeViewModel @Inject constructor(
private val getRecentUploadedMusicUseCase: GetRecentUploadedMusic
) : ViewModel() {

private val _uiState = MutableStateFlow(HomeUiState())
Expand All @@ -39,7 +30,7 @@ class HomeViewModel constructor(
}

private fun fetchUploadedMusics() {
getMusicUseCase()
getRecentUploadedMusicUseCase()
.onEach { musics ->
_uiState.update {
it.copy(
Expand All @@ -49,9 +40,4 @@ class HomeViewModel constructor(
}
.launchIn(viewModelScope)
}
}

// todo : domain layer로 이동
fun interface GetMusicUseCase {
operator fun invoke(): Flow<List<Music>>
}
}
138 changes: 73 additions & 65 deletions android/feature/home/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,79 +10,87 @@
type="com.ohdodok.catchytape.feature.home.HomeViewModel" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/tb_home"
style="@style/ToolBar"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:title="@string/home" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageButton
android:id="@+id/ib_upload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/margin_horizontal"
android:background="@android:color/transparent"
android:src="@drawable/ic_upload"
app:layout_constraintBottom_toBottomOf="@id/tb_home"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/tb_home" />
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/tb_home"
style="@style/ToolBar"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:title="@string/home" />

<TextView
android:id="@+id/tv_recently_played_song"
style="@style/TitleMedium"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/margin_horizontal"
android:layout_marginTop="@dimen/extra_large"
android:text="@string/recently_played_song"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tb_home" />
<ImageButton
android:id="@+id/ib_upload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/margin_horizontal"
android:background="@android:color/transparent"
android:src="@drawable/ic_upload"
app:layout_constraintBottom_toBottomOf="@id/tb_home"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@id/tb_home" />

<ImageView
android:id="@+id/iv_recently_played_song"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="@dimen/small"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="@id/tv_recently_played_song"
app:layout_constraintStart_toStartOf="@id/tv_recently_played_song"
app:layout_constraintTop_toBottomOf="@id/tv_recently_played_song"
tools:background="@color/on_surface" />
<TextView
android:id="@+id/tv_recently_played_song"
style="@style/TitleMedium"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/margin_horizontal"
android:layout_marginTop="@dimen/extra_large"
android:text="@string/recently_played_song"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tb_home" />

<TextView
android:id="@+id/tv_recently_added_song"
style="@style/TitleMedium"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/margin_horizontal"
android:layout_marginTop="@dimen/extra_large"
android:text="@string/recently_added_song"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/iv_recently_played_song" />
<ImageView
android:id="@+id/iv_recently_played_song"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="@dimen/small"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="@id/tv_recently_played_song"
app:layout_constraintStart_toStartOf="@id/tv_recently_played_song"
app:layout_constraintTop_toBottomOf="@id/tv_recently_played_song"
tools:background="@color/on_surface" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_recently_added_song"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/small"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintEnd_toEndOf="@id/tv_recently_added_song"
app:layout_constraintStart_toStartOf="@id/tv_recently_added_song"
app:layout_constraintTop_toBottomOf="@id/tv_recently_added_song"
app:submitList="@{viewModel.uiState.recentlyUploadedMusics}"
tools:listitem="@layout/item_music_horizontal" />
<TextView
android:id="@+id/tv_recently_added_song"
style="@style/TitleMedium"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/margin_horizontal"
android:layout_marginTop="@dimen/extra_large"
android:text="@string/recently_added_song"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/iv_recently_played_song" />

</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_recently_added_song"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/small"
android:layout_marginBottom="@dimen/medium"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@id/tv_recently_added_song"
app:layout_constraintStart_toStartOf="@id/tv_recently_added_song"
app:layout_constraintTop_toBottomOf="@id/tv_recently_added_song"
app:submitList="@{viewModel.uiState.recentlyUploadedMusics}"
Copy link
Member

Choose a reason for hiding this comment

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

태경이 형 커밋에서 submitList -> list로 변경되어서
둘이 원만하게 합의하길...

Copy link
Member Author

Choose a reason for hiding this comment

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

충돌 1초전이네...

Copy link
Collaborator

Choose a reason for hiding this comment

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

😂

tools:listitem="@layout/item_music_horizontal" />

</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.core.widget.NestedScrollView>

</layout>