diff --git a/.github/workflows/bidderbidderCI.yml b/.github/workflows/bidderbidderCI.yml index 6efa0049..af012441 100644 --- a/.github/workflows/bidderbidderCI.yml +++ b/.github/workflows/bidderbidderCI.yml @@ -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 diff --git a/app/src/main/kotlin/com/fakedevelopers/bidderbidder/MainActivity.kt b/app/src/main/kotlin/com/fakedevelopers/bidderbidder/MainActivity.kt index f96fafaa..4f9b99d0 100644 --- a/app/src/main/kotlin/com/fakedevelopers/bidderbidder/MainActivity.kt +++ b/app/src/main/kotlin/com/fakedevelopers/bidderbidder/MainActivity.kt @@ -32,7 +32,7 @@ class MainActivity : AppCompatActivity() { } true } - navController.navigate(R.id.productRegistrationFragment) + // navController.navigate(R.id.productRegistrationFragment) } override fun onDestroy() { diff --git a/app/src/main/kotlin/com/fakedevelopers/bidderbidder/ui/product_registration/DragAndDropCallback.kt b/app/src/main/kotlin/com/fakedevelopers/bidderbidder/ui/product_registration/DragAndDropCallback.kt new file mode 100644 index 00000000..d734f0d2 --- /dev/null +++ b/app/src/main/kotlin/com/fakedevelopers/bidderbidder/ui/product_registration/DragAndDropCallback.kt @@ -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) {} +} diff --git a/app/src/main/kotlin/com/fakedevelopers/bidderbidder/ui/product_registration/ProductRegistrationFragment.kt b/app/src/main/kotlin/com/fakedevelopers/bidderbidder/ui/product_registration/ProductRegistrationFragment.kt index 75009483..41518032 100644 --- a/app/src/main/kotlin/com/fakedevelopers/bidderbidder/ui/product_registration/ProductRegistrationFragment.kt +++ b/app/src/main/kotlin/com/fakedevelopers/bidderbidder/ui/product_registration/ProductRegistrationFragment.kt @@ -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 @@ -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() diff --git a/app/src/main/kotlin/com/fakedevelopers/bidderbidder/ui/product_registration/ProductRegistrationViewModel.kt b/app/src/main/kotlin/com/fakedevelopers/bidderbidder/ui/product_registration/ProductRegistrationViewModel.kt index 25c119f3..73f9bd93 100644 --- a/app/src/main/kotlin/com/fakedevelopers/bidderbidder/ui/product_registration/ProductRegistrationViewModel.kt +++ b/app/src/main/kotlin/com/fakedevelopers/bidderbidder/ui/product_registration/ProductRegistrationViewModel.kt @@ -12,6 +12,7 @@ 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 @@ -19,7 +20,13 @@ 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() private val urlList = MutableStateFlow>(mutableListOf()) @@ -27,6 +34,24 @@ class ProductRegistrationViewModel @Inject constructor( val productRegistrationResponse: SharedFlow> 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() @@ -42,8 +67,8 @@ class ProductRegistrationViewModel @Inject constructor( } fun setImageList(url: List) { - 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()) diff --git a/app/src/main/kotlin/com/fakedevelopers/bidderbidder/ui/product_registration/SelectedPictureListAdapter.kt b/app/src/main/kotlin/com/fakedevelopers/bidderbidder/ui/product_registration/SelectedPictureListAdapter.kt index 6caacf74..5d0062ef 100644 --- a/app/src/main/kotlin/com/fakedevelopers/bidderbidder/ui/product_registration/SelectedPictureListAdapter.kt +++ b/app/src/main/kotlin/com/fakedevelopers/bidderbidder/ui/product_registration/SelectedPictureListAdapter.kt @@ -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(diffUtil) { +class SelectedPictureListAdapter( + private val deleteSelectedImage: (String) -> Unit, + private val swapSelectedImage: (Int, Int) -> Unit = { _, _ -> } +) : ListAdapter(diffUtil) { - class ViewHolder( + inner class ViewHolder( private val binding: RecyclerProductRegistrationBinding, private val context: Context ) : RecyclerView.ViewHolder(binding.root) { @@ -22,6 +26,10 @@ class SelectedPictureListAdapter : ListAdapter() { override fun areItemsTheSame(oldItem: String, newItem: String) = diff --git a/app/src/main/kotlin/com/fakedevelopers/bidderbidder/ui/product_registration/album_list/AlbumListViewModel.kt b/app/src/main/kotlin/com/fakedevelopers/bidderbidder/ui/product_registration/album_list/AlbumListViewModel.kt index 9608e603..3b59900c 100644 --- a/app/src/main/kotlin/com/fakedevelopers/bidderbidder/ui/product_registration/album_list/AlbumListViewModel.kt +++ b/app/src/main/kotlin/com/fakedevelopers/bidderbidder/ui/product_registration/album_list/AlbumListViewModel.kt @@ -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) { @@ -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 {