Skip to content

Commit

Permalink
[feat] MutiPartResolver 생성과 post 보류 (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
visionWWW committed Jul 21, 2022
1 parent 4eac7a2 commit 7d70198
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.photosurfer.android.core.util

import android.content.Context
import android.database.Cursor
import android.net.Uri
import android.provider.DocumentsContract
import android.provider.MediaStore
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File

class MutiPartResolver {
fun createImgMultiPart(uri: Uri, context: Context): MultipartBody.Part {
val file = File((getPath(context, uri).toString()))
val surveyBody = file.asRequestBody("image/*".toMediaTypeOrNull())
return MultipartBody.Part.createFormData("image", file.name, surveyBody)
}

private fun getPath(context: Context, uri: Uri): String? {
val docId = DocumentsContract.getDocumentId(uri)
val split = docId.split(":".toRegex()).toTypedArray()
val contentUri: Uri? = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
val selection = "_id=?"
val selectionArgs = arrayOf(split[1])
return getDataColumn(context, contentUri!!, selection, selectionArgs)
}

private fun getDataColumn(
context: Context,
uri: Uri,
selection: String?,
selectionArgs: Array<String>?
): String? {
var cursor: Cursor? = null
val column = "_data"
val projection = arrayOf(column)
try {
cursor = context.contentResolver.query(uri, projection, selection, selectionArgs, null)
if (cursor != null && cursor.moveToFirst()) {
val columnIndex: Int = cursor.getColumnIndexOrThrow(column)
return cursor.getString(columnIndex)
}
} finally {
cursor?.close()
}
return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ interface ChooseTagService {
@Body body: ChooseTagRequest
): NetworkState<BaseResponse<ChooseTagResponse>>

/*
suspend fun postTag(
@Part image: MultipartBody.Part?,
@Part("contents") contents: List<TagNameType>
): NetworkState<BaseResponse<ChooseTagResponse>>
*/

@GET("tag/main")
suspend fun getTagList(): NetworkState<BaseResponse<TagListResponse>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.fragment.app.viewModels
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.photosurfer.android.core.base.BaseFragment
import com.photosurfer.android.core.util.PhotoSurferSnackBar
import com.photosurfer.android.domain.entity.TagInfo
import com.photosurfer.android.register_tag.databinding.FragmentChooseTagBinding
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -34,6 +35,7 @@ class ChooseTagFragment : BaseFragment<FragmentChooseTagBinding>(R.layout.fragme
setRecentList()
setOftenList()
setPlatformList()
chooseTagViewModel.setEmptyInput()
initAdapter()
setDataOnRecyclerView()
observeInputChipGroup()
Expand All @@ -43,15 +45,15 @@ class ChooseTagFragment : BaseFragment<FragmentChooseTagBinding>(R.layout.fragme
checkInputNum()
initRecyclerViewLayout()


val file: File = setImgToFile(getImgToUri())

initButtonSaveClickListener()
}

private fun setPlatformList() {
chooseTagViewModel.platformList.observe(viewLifecycleOwner) {
platformTagAdapter.submitList(chooseTagViewModel.platformList.value)
chooseTagViewModel.setPlatformIdList()

}
}

Expand Down Expand Up @@ -108,7 +110,7 @@ class ChooseTagFragment : BaseFragment<FragmentChooseTagBinding>(R.layout.fragme

private fun setDataOnRecyclerView() {
Log.d(TAG, "setDataOnRecyclerView: recent ${chooseTagViewModel.recentList}")
inputTagAdapter.submitList(chooseTagViewModel.inputList.value)
inputTagAdapter.submitList(chooseTagViewModel.inputList)
recentTagAdapter.submitList(chooseTagViewModel.recentList.value)
oftenTagAdapter.submitList(chooseTagViewModel.oftenList.value)
platformTagAdapter.submitList(chooseTagViewModel.platformList.value)
Expand All @@ -127,9 +129,9 @@ class ChooseTagFragment : BaseFragment<FragmentChooseTagBinding>(R.layout.fragme
}

private fun checkInputNum() {
// if (chooseTagViewModel.inputList.value?.size > 6) {
// PhotoSurferSnackBar.make(requireView(), PhotoSurferSnackBar.CHOOSE_TAG_FRAGMENT).show()
// }
if (chooseTagViewModel.inputList.size > 6) {
PhotoSurferSnackBar.make(requireView(), PhotoSurferSnackBar.CHOOSE_TAG_FRAGMENT).show()
}
}

private fun deleteInput() {
Expand All @@ -140,6 +142,7 @@ class ChooseTagFragment : BaseFragment<FragmentChooseTagBinding>(R.layout.fragme

private fun observeInputChipGroup() {
chooseTagViewModel.isEmptyInput.observe(viewLifecycleOwner) {
Log.d("count::", "${chooseTagViewModel.isEmptyInput}")
if (chooseTagViewModel.isEmptyInput.value!! > 0) {
binding.tvSave.isSelected = binding.ivCheckPlatform.isSelected != true
binding.tvSave.isEnabled = true
Expand All @@ -163,23 +166,44 @@ class ChooseTagFragment : BaseFragment<FragmentChooseTagBinding>(R.layout.fragme
}

private fun addTagWithInputText() {
chooseTagViewModel.inputList.value?.add(TagInfo(0, binding.etTag.text.toString()))
//chooseTagViewModel.setEmptyInput(chooseTagViewModel.inputList.size)
chooseTagViewModel.inputList.add(TagInfo(0, binding.etTag.text.toString()))
binding.etTag.text.clear()
}

private fun selectTag(tagInfo: TagInfo) {
chooseTagViewModel.selectTag(tagInfo)
inputTagAdapter.submitList(chooseTagViewModel.inputList.value)
inputTagAdapter.submitList(chooseTagViewModel.inputList)
inputTagAdapter.notifyDataSetChanged()
}

private fun deleteTag(tagInfo: TagInfo) {
chooseTagViewModel.deleteTag(tagInfo)
inputTagAdapter.submitList(chooseTagViewModel.inputList.value)
inputTagAdapter.submitList(chooseTagViewModel.inputList)
}
}

// TODO: POST 관련
//fun checkPlatform() {
// var filteredList: MutableList<TagInfo> = mutableListOf()
//
// for (i in 0 until 6) {
// filteredList = chooseTagViewModel.inputList.value?.filter {
// it.id == chooseTagViewModel.platformList.value?.get(i)?.id
// } as MutableList<TagInfo>
// }
//
// Log.d("filtered List", "$filteredList")
//}
//
//private fun initCompleteButtonCLick() {
// binding.tvSave.setOnClickListener {
// uploadPost(
// contents = chooseTagViewModel.inputList,
// image = MultiPartResolver().createImgMultiPart(requireNotNull(imageUri), this)
// )
// }
//}

enum class Platform(val platformName: String) {
KAKAOTALK("카카오톡"),
YOUTUBE("유튜브"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package com.photosurfer.android.register_tag

import android.nfc.Tag
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.photosurfer.android.core.base.BaseViewModel
import com.photosurfer.android.core.util.Event
import com.photosurfer.android.domain.entity.TagInfo
import com.photosurfer.android.domain.entity.request.DomainChooseTagRequest
import com.photosurfer.android.domain.repository.ChooseTagRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import timber.log.Timber
import java.io.File
import javax.inject.Inject

@HiltViewModel
Expand All @@ -24,8 +19,8 @@ class ChooseTagViewModel @Inject constructor(
private var _isEmptyInput = MutableLiveData<Int>()
val isEmptyInput: LiveData<Int> get() = _isEmptyInput

private val _inputList = MutableLiveData<MutableList<TagInfo>>()
val inputList: MutableLiveData<MutableList<TagInfo>> = _inputList
private val _inputList: MutableList<TagInfo> = mutableListOf()
val inputList: MutableList<TagInfo> = _inputList

private val _recentList = MutableLiveData<MutableList<TagInfo>>()
val recentList: MutableLiveData<MutableList<TagInfo>> = _recentList
Expand All @@ -36,28 +31,43 @@ class ChooseTagViewModel @Inject constructor(
private val _platformList = MutableLiveData<MutableList<TagInfo>>()
val platformList: MutableLiveData<MutableList<TagInfo>> = _platformList

private val _platformListId = MutableLiveData<MutableList<Int>>()
val platformListId: MutableLiveData<MutableList<Int>> = _platformListId

private val _chooseTagSuccess = MutableLiveData<Event<Boolean>>()
val chooseTagSuccess: LiveData<Event<Boolean>> = _chooseTagSuccess

private val _chooseTagFailure = MutableLiveData<Event<Boolean>>()
val chooseTagFailure: LiveData<Event<Boolean>> = _chooseTagFailure

fun setPlatformIdList() {
_platformListId.value = platformList.value?.map { it.id }?.toMutableList()
}

val imageFile: File
get() {
TODO()
}

fun setEmptyInput(value: Int) {
_isEmptyInput.value = value
fun setEmptyInput() {
_isEmptyInput.value = inputList.size
}

fun selectTag(item: TagInfo) {
inputList.value?.add(item)
inputList.add(item)
}

fun deleteTag(item: TagInfo) {
inputList.value?.remove(item)
inputList.remove(item)
}

fun getTagList() {
viewModelScope.launch {
chooseTagRepository.getTagList()
.onSuccess {
_recentList.value = it.recentTagList
_oftenList.value = it.oftenTagList
_platformList.value = it.platformTagList

}.onFailure {
Timber.d(it, "${this.javaClass.name}_getTagList")
}
}
}

// TODO: GET 서버 로직 붙이고 추가할 예정
Expand All @@ -76,18 +86,4 @@ class ChooseTagViewModel @Inject constructor(
// }
// }
// }

fun getTagList() {
viewModelScope.launch {
chooseTagRepository.getTagList()
.onSuccess {
_recentList.value = it.recentTagList
_oftenList.value = it.oftenTagList
_platformList.value = it.platformTagList

}.onFailure {
Timber.d(it, "${this.javaClass.name}_getTagList")
}
}
}
}

0 comments on commit 7d70198

Please sign in to comment.