Skip to content

Commit

Permalink
Merge pull request #105 from boostcampwm2023/android/feature/upload-100
Browse files Browse the repository at this point in the history
[업로드] 파일 선택
  • Loading branch information
HamBP authored Nov 16, 2023
2 parents 1bc218b + dbe7cd9 commit 43decfc
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 26 deletions.
1 change: 1 addition & 0 deletions android/core/ui/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@
<string name="title">제목</string>
<string name="genre">장르</string>
<string name="description">설명</string>
<string name="select_thumbnail">썸네일 이미지 선택</string>
</resources>
2 changes: 1 addition & 1 deletion android/feature/upload/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ android {
}

dependencies {

implementation(libs.appcompat)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,44 @@ package com.ohdodok.catchytape.feature.upload

import android.os.Bundle
import android.view.View
import androidx.activity.result.PickVisualMediaRequest
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.result.contract.ActivityResultContracts.PickVisualMedia
import androidx.core.net.toFile
import androidx.fragment.app.viewModels
import com.ohdodok.catchytape.catchytape.upload.R
import com.ohdodok.catchytape.catchytape.upload.databinding.FragmentUploadBinding
import com.ohdodok.catchytape.core.ui.BaseFragment
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class UploadFragment : BaseFragment<FragmentUploadBinding>(R.layout.fragment_upload) {
private val viewModel: UploadViewModel by viewModels()

private val imagePickerLauncher = registerForActivityResult(PickVisualMedia()) { uri ->
if (uri == null) return@registerForActivityResult

viewModel.uploadImage(uri.toFile())
}

private val filePickerLauncher =
registerForActivityResult(ActivityResultContracts.GetContent()) { uri ->
if (uri == null) return@registerForActivityResult

viewModel.uploadAudio(uri.toFile())
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.viewModel = viewModel

setupBackStack(binding.tbUploadAppbar)
setupSelectThumbnailImage()
}

private fun setupSelectThumbnailImage() {
binding.cvUploadThumbnail.setOnClickListener {
imagePickerLauncher.launch(PickVisualMediaRequest(PickVisualMedia.ImageOnly))
}
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
package com.ohdodok.catchytape.feature.upload

import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import java.io.File
import javax.inject.Inject
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.stateIn

class UploadViewModel : ViewModel() {
@HiltViewModel
class UploadViewModel @Inject constructor(

val uploadedMusicTitle = MutableStateFlow<String>("")
) : ViewModel() {

private var uploadedImage: String? = null

val uploadedMusicTitle = MutableStateFlow("")

private val _uploadedMusicGenrePosition = MutableStateFlow(0)
val uploadedMusicGenrePosition = _uploadedMusicGenrePosition.asStateFlow()

private val _isUploadEnable = MutableStateFlow(false)
val isUploadEnable = _isUploadEnable.asStateFlow()

init {
observeMusicTitle()
observeGenrePosition()
}
val isUploadEnable: StateFlow<Boolean> =
combine(uploadedMusicTitle, uploadedMusicGenrePosition) { title, genrePosition ->
title.isNotBlank() && genrePosition != 0
}.stateIn(viewModelScope, SharingStarted.Eagerly, false)

val onChangePosition: (Int) -> Unit = { position: Int -> _uploadedMusicGenrePosition.value = position }


private fun observeMusicTitle() {
uploadedMusicTitle.onEach {
_isUploadEnable.value = uploadedMusicGenrePosition.value != 0 && it.isNotEmpty()
}.launchIn(viewModelScope)
fun uploadImage(imageFile: File) {
// todo : image 파일을 업로드 한다.
// todo : 반환 값을 uploadedImage에 저장한다.
}


private fun observeGenrePosition() {
uploadedMusicGenrePosition.onEach {
_isUploadEnable.value = it != 0 && uploadedMusicTitle.value.isNotEmpty()
}.launchIn(viewModelScope)
fun uploadAudio(audioFile: File) {
// todo : audio 파일을 업로드 한다.
}
}
20 changes: 15 additions & 5 deletions android/feature/upload/src/main/res/layout/fragment_upload.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,27 @@
android:layout_marginTop="@dimen/extra_large"
app:cardBackgroundColor="@color/on_surface_variant"
app:cardCornerRadius="20dp"
app:cardElevation="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tb_upload_appbar">
app:layout_constraintTop_toBottomOf="@id/tb_upload_appbar"
android:contentDescription="@string/select_thumbnail">

<ImageButton
android:id="@+id/btn_select_thumbnail"
<ImageView
android:id="@+id/iv_thumbnail_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@color/white"
android:importantForAccessibility="no" />

<ImageView
android:id="@+id/iv_camera_icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:background="@drawable/ic_camera"
android:backgroundTint="@color/surface" />
android:src="@drawable/ic_camera"
android:importantForAccessibility="no"/>

</androidx.cardview.widget.CardView>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<navigation 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"
android:id="@+id/upload_navigation"
android:id="@+id/upload_nav_graph"
app:startDestination="@id/upload_fragment">

<fragment
Expand Down

0 comments on commit 43decfc

Please sign in to comment.