Skip to content

Commit

Permalink
#34 feat: 일반일기 사진 등록 로직 추가
Browse files Browse the repository at this point in the history
#34 feat: 일반일기 사진 등록 로직 추가
  • Loading branch information
ChunBaee authored May 24, 2023
2 parents 92b926e + 510efb1 commit a7f349d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ data class RequestNormalDiaryData(
val locationName: String?,
val locationAddr: String?,
val locationX: Double?,
val locationY: Double?
val locationY: Double?,
val imagePath : MutableList<String>?
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import com.chunbae.narchive.data.data.LocationData

interface NormalDiaryUseCase {

suspend fun invoke(content : String, locationData: LocationData?) : Result<String>
suspend fun invoke(content : String, locationData: LocationData?, images : MutableList<String>?) : Result<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@ import javax.inject.Inject

class NormalDiaryUseCaseImpl @Inject constructor(private val repo : NormalDiaryRepository) : NormalDiaryUseCase{

override suspend fun invoke(content: String, locationData: LocationData?): Result<String> {
return repo.postNormalDiary(content.mapToRequest(locationData))
override suspend fun invoke(content: String, locationData: LocationData?, images : MutableList<String>?): Result<String> {
return repo.postNormalDiary(content.mapToRequest(locationData, images))
}

private fun String.mapToRequest(locationData: LocationData?) : RequestNormalDiaryData {
private fun String.mapToRequest(locationData: LocationData?, images : MutableList<String>?) : RequestNormalDiaryData {
return RequestNormalDiaryData(
this,
locationData?.place_name,
locationData?.road_address_name,
locationData?.x?.toDouble(),
locationData?.y?.toDouble()
locationData?.y?.toDouble(),
images
)
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
package com.chunbae.narchive.presentation.ui.write.diary.normal.viewmodel

import android.net.Uri
import android.util.Log
import androidx.core.net.toUri
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.chunbae.narchive.data.data.LocationData
import com.chunbae.narchive.domain.repository.FirebaseRepository
import com.chunbae.narchive.domain.repository.KakaoAiDiaryRepository
import com.chunbae.narchive.domain.usecase.NormalDiaryUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class WriteNormalDiaryViewModel @Inject constructor(private val aiRepo : KakaoAiDiaryRepository, private val normalUseCase : NormalDiaryUseCase): ViewModel() {
class WriteNormalDiaryViewModel @Inject constructor(
private val aiRepo: KakaoAiDiaryRepository,
private val normalUseCase: NormalDiaryUseCase,
private val firebaseRepo: FirebaseRepository
) : ViewModel() {

private val _selectedLocation = MutableLiveData<LocationData>()
val selectedLocation : LiveData<LocationData> = _selectedLocation
val selectedLocation: LiveData<LocationData> = _selectedLocation

private val _selectedImages = MutableLiveData<MutableList<String>>()
val selectedImages : LiveData<MutableList<String>> = _selectedImages
val selectedImages: LiveData<MutableList<String>> = _selectedImages

private val imageDownloadPath = mutableListOf<String>()

var userInputContent = MutableLiveData<String>().apply { value = "" }

Expand All @@ -29,13 +36,13 @@ class WriteNormalDiaryViewModel @Inject constructor(private val aiRepo : KakaoAi
var isDialogOpened = MutableLiveData<Boolean>(false)

private val _diaryState = MutableLiveData<String>()
val diaryState : LiveData<String> = _diaryState
val diaryState: LiveData<String> = _diaryState

fun setLocation(data : LocationData) {
fun setLocation(data: LocationData) {
_selectedLocation.value = data
}

fun setImages(data : MutableList<String>) {
fun setImages(data: MutableList<String>) {
_selectedImages.value = data
}

Expand All @@ -49,6 +56,7 @@ class WriteNormalDiaryViewModel @Inject constructor(private val aiRepo : KakaoAi
fun onAcceptAiGenerated() {
userInputContent.value += aiGeneratedContent.value
}

fun initAiGenerated() {
aiGeneratedContent.value = null
}
Expand All @@ -57,9 +65,24 @@ class WriteNormalDiaryViewModel @Inject constructor(private val aiRepo : KakaoAi
isDialogOpened.value = isDialogOpened.value?.not()
}

fun uploadImageToFirebase() {
viewModelScope.async {
for (i in _selectedImages.value!!) {
imageDownloadPath.add(firebaseRepo.uploadProfileToFirebase(i.toUri()).toString())
}
}
postNormalDiary()
}

fun postNormalDiary() {
viewModelScope.launch {
userInputContent.value?.let { normalUseCase.invoke(it,selectedLocation.value) }
userInputContent.value?.let {
normalUseCase.invoke(
it,
selectedLocation.value,
imageDownloadPath
)
}
?.onSuccess { _diaryState.value = it }
?.onFailure { _diaryState.value = "등록에 실패했습니다." }
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_write_diary_normal.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/margin_20"
android:src="@drawable/ic_common_plane"
android:onClick="@{() -> viewModel.postNormalDiary()}"
android:onClick="@{() -> viewModel.uploadImageToFirebase()}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Expand Down

0 comments on commit a7f349d

Please sign in to comment.