Skip to content

Commit

Permalink
Merge pull request #31 from FakeDevelopers/feature/BDBD-240
Browse files Browse the repository at this point in the history
현재 작업 내용 sandbox에 적용
  • Loading branch information
minseonglove authored Jun 2, 2022
2 parents 5253c42 + f0e9544 commit c7def48
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/bidderbidderCI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:
name: artifact
- name: Get current date
id: date
run: echo "::set-output name=date::$(date +'%Y-%m-%d')"
run: echo "::set-output name=date::$(date +'%Y-%m-%d-%H')"
- name: Create Release
id: create_release
uses: actions/create-release@v1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class MainActivity : AppCompatActivity() {
}
true
}
navController.navigate(R.id.productRegistrationFragment)
// navController.navigate(R.id.productRegistrationFragment)
}

override fun onDestroy() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.fakedevelopers.bidderbidder.ui.product_registration

import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView

class DragAndDropCallback(
private val adapter: SelectedPictureListAdapter
) : ItemTouchHelper.Callback() {

private var isMoved = false

// 좌우 드래그
override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) =
makeMovementFlags(ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT, 0)

override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean {
adapter.onItemDragMove(viewHolder.adapterPosition, target.adapterPosition)
return true
}

override fun onMoved(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
fromPos: Int,
target: RecyclerView.ViewHolder,
toPos: Int,
x: Int,
y: Int
) {
super.onMoved(recyclerView, viewHolder, fromPos, target, toPos, x, y)
isMoved = true
}

override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
super.onSelectedChanged(viewHolder, actionState)
if (isMoved) {
isMoved = false
adapter.changeMoveEvent()
}
}

override fun isLongPressDragEnabled(): Boolean {
return true
}

override fun isItemViewSwipeEnabled(): Boolean {
return false
}

override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.ItemTouchHelper
import com.fakedevelopers.bidderbidder.R
import com.fakedevelopers.bidderbidder.databinding.FragmentProductRegistrationBinding
import com.orhanobut.logger.Logger
Expand Down Expand Up @@ -60,6 +61,8 @@ class ProductRegistrationFragment : Fragment() {
val args: ProductRegistrationFragmentArgs by navArgs()
if (!args.selectedImageList.isNullOrEmpty()) {
viewModel.setImageList(args.selectedImageList!!.toList())
ItemTouchHelper(DragAndDropCallback(viewModel.adapter))
.attachToRecyclerView(binding.recyclerProductRegistration)
}
initListener()
initCollector()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,46 @@ import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody
import retrofit2.Response
import java.util.Collections
import javax.inject.Inject

@HiltViewModel
class ProductRegistrationViewModel @Inject constructor(
private val repository: ProductRegistrationRepository
) : ViewModel() {

val adapter = SelectedPictureListAdapter()
val adapter = SelectedPictureListAdapter(
deleteSelectedImage = {
deleteSelectedImage(it)
}
) { fromPosition, toPosition ->
swapSelectedImage(fromPosition, toPosition)
}

// private val imageList = mutableListOf<MultipartBody.Part>()
private val urlList = MutableStateFlow<MutableList<String>>(mutableListOf())
private val _productRegistrationResponse = MutableSharedFlow<Response<String>>()

val productRegistrationResponse: SharedFlow<Response<String>> get() = _productRegistrationResponse

private fun deleteSelectedImage(uri: String) {
urlList.value.remove(uri)
adapter.submitList(urlList.value.toList())
}

private fun swapSelectedImage(fromPosition: Int, toPosition: Int) {
if (fromPosition < toPosition) {
for (i in fromPosition until toPosition) {
Collections.swap(urlList.value, i, i + 1)
}
} else {
for (i in fromPosition downTo toPosition + 1) {
Collections.swap(urlList.value, i, i - 1)
}
}
adapter.submitList(urlList.value.toList())
}

fun productRegistrationRequest() {
viewModelScope.launch {
val map = hashMapOf<String, RequestBody>()
Expand All @@ -42,8 +67,8 @@ class ProductRegistrationViewModel @Inject constructor(
}

fun setImageList(url: List<String>) {
urlList.value.addAll(url.toList())
adapter.submitList(urlList.value)
urlList.value.addAll(url)
adapter.submitList(urlList.value.toList())
}

private fun String?.toPlainRequestBody() = requireNotNull(this).toRequestBody("text/plain".toMediaTypeOrNull())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.fakedevelopers.bidderbidder.R
import com.fakedevelopers.bidderbidder.databinding.RecyclerProductRegistrationBinding
import com.orhanobut.logger.Logger

class SelectedPictureListAdapter : ListAdapter<String, SelectedPictureListAdapter.ViewHolder>(diffUtil) {
class SelectedPictureListAdapter(
private val deleteSelectedImage: (String) -> Unit,
private val swapSelectedImage: (Int, Int) -> Unit = { _, _ -> }
) : ListAdapter<String, SelectedPictureListAdapter.ViewHolder>(diffUtil) {

class ViewHolder(
inner class ViewHolder(
private val binding: RecyclerProductRegistrationBinding,
private val context: Context
) : RecyclerView.ViewHolder(binding.root) {
Expand All @@ -22,6 +26,10 @@ class SelectedPictureListAdapter : ListAdapter<String, SelectedPictureListAdapte
.placeholder(R.drawable.the_cat)
.error(R.drawable.error_cat)
.into(binding.imageviewProductRegistration)
// 선택 사진 터치 시 제거
binding.imageviewProductRegistration.setOnClickListener {
deleteSelectedImage(item)
}
}
}

Expand All @@ -38,6 +46,14 @@ class SelectedPictureListAdapter : ListAdapter<String, SelectedPictureListAdapte
holder.bind(getItem(position))
}

fun onItemDragMove(fromPosition: Int, toPosition: Int) {
swapSelectedImage(fromPosition, toPosition)
}

fun changeMoveEvent() {
Logger.i("")
}

companion object {
val diffUtil = object : DiffUtil.ItemCallback<String>() {
override fun areItemsTheSame(oldItem: String, newItem: String) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ class AlbumListViewModel : ViewModel() {
val albumListAdapter = AlbumListAdapter(
findSelectedImageIndex = { findSelectedImageIndex(it) },
setScrollFlag = { setScrollFlag() }
) { url, state ->
setSelectedState(url, state)
) { uri, state ->
setSelectedState(uri, state)
}
val selectedPictureAdapter = SelectedPictureListAdapter()
val selectedPictureAdapter = SelectedPictureListAdapter(
deleteSelectedImage = {
setSelectedState(it)
}
)
var scrollToTopFlag = false

fun setList(albumName: String = currentAlbum.value) {
Expand All @@ -47,11 +51,9 @@ class AlbumListViewModel : ViewModel() {
scrollToTopFlag = !scrollToTopFlag
}

fun findImageIndex(uri: String) = allImages[currentAlbum.value]!!.indexOf(uri)

private fun findSelectedImageIndex(uri: String) = selectedImageList.value.indexOf(uri)

private fun setSelectedState(uri: String, state: Boolean) {
private fun setSelectedState(uri: String, state: Boolean = false) {
if (state) {
_selectedImageList.add(uri)
} else {
Expand Down

0 comments on commit c7def48

Please sign in to comment.