Skip to content

Commit

Permalink
Merge pull request #378 from boostcampwm2023/android/feature/377
Browse files Browse the repository at this point in the history
동일한 음악에 대해 동일한 uuid로 업로드
  • Loading branch information
HamBP authored Jan 22, 2024
2 parents 6d6475b + 158fea1 commit e60a05f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ object NetworkModule {
@Singleton
@Provides
fun provideLoggingInterceptor(): HttpLoggingInterceptor {
val logger = HttpLoggingInterceptor.Logger { message -> Timber.tag("okHttp").d(message) }
val logger = HttpLoggingInterceptor.Logger { message ->
if (message.contains("")) return@Logger

Timber.tag("okHttp").d(message)
}

return HttpLoggingInterceptor(logger)
.setLevel(HttpLoggingInterceptor.Level.BODY)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
package com.ohdodok.catchytape.core.domain.usecase.upload

import com.ohdodok.catchytape.core.domain.repository.UploadRepository
import com.ohdodok.catchytape.core.domain.repository.UuidRepository
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import java.io.File
import javax.inject.Inject

class UploadFileUseCase @Inject constructor(
private val uuidRepository: UuidRepository,
private val uploadRepository: UploadRepository,
) {

fun uploadAudio(audioFile: File): Flow<String> = uuidRepository.getUuid().map { uuid ->
uploadRepository.uploadAudio(uuid, audioFile).first()
fun uploadAudio(audioFile: File, uuid: String): Flow<String> {
return uploadRepository.uploadAudio(uuid, audioFile)
}

fun uploadMusicCover(imageFile: File): Flow<String> = uuidRepository.getUuid().map { uuid ->
uploadRepository.uploadImage(uuid, imageFile).first()
fun uploadMusicCover(imageFile: File, uuid: String): Flow<String> {
return uploadRepository.uploadImage(uuid, imageFile)
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ 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.repository.MusicRepository
import com.ohdodok.catchytape.core.domain.repository.UuidRepository
import com.ohdodok.catchytape.core.domain.usecase.upload.UploadFileUseCase
import com.ohdodok.catchytape.core.domain.usecase.upload.UploadMusicUseCase
import com.ohdodok.catchytape.core.domain.usecase.upload.ValidateMusicTitleUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineExceptionHandler
Expand All @@ -19,6 +19,7 @@ import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.retry
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.plus
Expand Down Expand Up @@ -62,8 +63,8 @@ sealed interface UploadEvent {
class UploadViewModel @Inject constructor(
private val musicRepository: MusicRepository,
private val uploadFileUseCase: UploadFileUseCase,
private val uploadMusicUseCase: UploadMusicUseCase,
private val validateMusicTitleUseCase: ValidateMusicTitleUseCase
private val validateMusicTitleUseCase: ValidateMusicTitleUseCase,
private val uuidRepository: UuidRepository,
) : ViewModel() {

private val _events = MutableSharedFlow<UploadEvent>()
Expand All @@ -79,13 +80,16 @@ class UploadViewModel @Inject constructor(
}
}

private lateinit var uuid: String

private val viewModelScopeWithExceptionHandler = viewModelScope + exceptionHandler

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

init {
fetchGenres()
generateUuid()
}

private fun fetchGenres() {
Expand All @@ -94,6 +98,13 @@ class UploadViewModel @Inject constructor(
}.launchIn(viewModelScope)
}

private fun generateUuid() {
uuidRepository.getUuid().onEach { generatedUuid ->
uuid = generatedUuid
}.retry(3)
.launchIn(viewModelScopeWithExceptionHandler)
}

fun updateMusicTitle(title: CharSequence) {
_uiState.update {
it.copy(
Expand All @@ -110,7 +121,7 @@ class UploadViewModel @Inject constructor(
}

fun uploadImage(imageFile: File) {
uploadFileUseCase.uploadMusicCover(imageFile).onStart {
uploadFileUseCase.uploadMusicCover(imageFile, uuid).onStart {
_uiState.update { it.copy(imageState = it.imageState.copy(isLoading = true)) }
}.onEach { url ->
_uiState.update { it.copy(imageState = it.imageState.copy(url = url)) }
Expand All @@ -120,7 +131,7 @@ class UploadViewModel @Inject constructor(
}

fun uploadAudio(audioFile: File) {
uploadFileUseCase.uploadAudio(audioFile).onStart {
uploadFileUseCase.uploadAudio(audioFile, uuid).onStart {
_uiState.update { it.copy(audioState = it.audioState.copy(isLoading = true)) }
}.onEach { url ->
_uiState.update { it.copy(audioState = it.audioState.copy(url = url)) }
Expand All @@ -132,7 +143,8 @@ class UploadViewModel @Inject constructor(
fun uploadMusic() {
if (!uiState.value.isUploadEnable) return

uploadMusicUseCase(
musicRepository.postMusic(
musicId = uuid,
imageUrl = uiState.value.imageState.url,
audioUrl = uiState.value.audioState.url,
title = uiState.value.musicTitleState.title,
Expand All @@ -142,4 +154,3 @@ class UploadViewModel @Inject constructor(
}.launchIn(viewModelScopeWithExceptionHandler)
}
}

0 comments on commit e60a05f

Please sign in to comment.