Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/BDBD-240' into stage/san…
Browse files Browse the repository at this point in the history
…dbox
  • Loading branch information
minseonglove committed Jun 5, 2022
2 parents de9a0ae + dda6464 commit f23c51d
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ class ProductRegistrationFragment : Fragment() {
// 미디어 접근 권한이 없으면 안됩니다
val permissionCheck = checkCallingOrSelfPermission(requireContext(), Manifest.permission.READ_EXTERNAL_STORAGE)
if (permissionCheck == PermissionChecker.PERMISSION_GRANTED) {
findNavController().navigate(R.id.action_productRegistrationFragment_to_pictureSelectFragment)
findNavController().navigate(
ProductRegistrationFragmentDirections
.actionProductRegistrationFragmentToPictureSelectFragment(
viewModel.urlList.value.toTypedArray()
)
)
} else {
permissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody
Expand All @@ -28,34 +29,35 @@ class ProductRegistrationViewModel @Inject constructor(
}

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

val urlList: StateFlow<List<String>> get() = _urlList
val productRegistrationResponse: SharedFlow<Response<String>> get() = _productRegistrationResponse

private fun deleteSelectedImage(uri: String) {
urlList.value.remove(uri)
adapter.submitList(urlList.value.toList())
_urlList.value.remove(uri)
adapter.submitList(_urlList.value.toList())
// 사진이 삭제 된다면 다음 사진에게 대표직을 물려줌
if (urlList.value.isNotEmpty()) {
if (_urlList.value.isNotEmpty()) {
adapter.notifyItemChanged(1)
}
}

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

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

fun productRegistrationRequest() {
viewModelScope.launch {
Expand All @@ -72,8 +74,8 @@ class ProductRegistrationViewModel @Inject constructor(
}

fun setImageList(url: List<String>) {
urlList.value.addAll(url)
adapter.submitList(urlList.value.toList())
_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 @@ -15,6 +15,7 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
Expand Down Expand Up @@ -48,7 +49,10 @@ class AlbumListFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
getPictures()
initCollector()
val args: AlbumListFragmentArgs by navArgs()
if (!args.selectedImageList.isNullOrEmpty()) {
viewModel.initSelectedImageList(args.selectedImageList!!.toList())
}
binding.buttonAlbumListComplete.setOnClickListener {
// 선택한 이미지 uri를 들고 돌아갑니다
findNavController().navigate(
Expand All @@ -74,6 +78,7 @@ class AlbumListFragment : Fragment() {
if (it != null) {
val albums = mutableMapOf<String, MutableList<String>>()
albums[ALL_PICTURES] = mutableListOf()
val albumNameSummary = mutableMapOf<String, String>()
while (it.moveToNext()) {
// 이미지 Uri
val imageUri = ContentUris.withAppendedId(
Expand All @@ -84,15 +89,19 @@ class AlbumListFragment : Fragment() {
albums[ALL_PICTURES]!!.add(imageUri)
// 이미지 상대 경로에 저장
it.getString(it.getColumnIndexOrThrow(MediaStore.Images.Media.RELATIVE_PATH)).let { path ->
if (!albums.containsKey(path)) {
albums[path] = mutableListOf()
if (!albumNameSummary.containsKey(path)) {
path.split("/").let { split ->
albumNameSummary[path] = split[split.lastIndex - 1]
albums[split[split.lastIndex - 1]] = mutableListOf()
}
}
albums[path]!!.add(imageUri)
albums[albumNameSummary[path]]!!.add(imageUri)
}
}
viewModel.setAllImages(albums)
viewModel.setList(ALL_PICTURES)
viewModel.initAlbumInfo(albums)
viewModel.setAlbumList(ALL_PICTURES)
initSpinner(albums.keys.toTypedArray())
initCollector()
// 앨범 전환 시 가장 위로 스크롤
binding.recyclerAlbemList.layoutManager = object : GridLayoutManager(requireContext(), 3) {
override fun onLayoutCompleted(state: RecyclerView.State?) {
Expand All @@ -117,7 +126,7 @@ class AlbumListFragment : Fragment() {
)
onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
viewModel.setList(albumArray[position])
viewModel.setAlbumList(albumArray[position])
}

override fun onNothingSelected(parent: AdapterView<*>?) {}
Expand All @@ -126,12 +135,11 @@ class AlbumListFragment : Fragment() {
}

private fun initCollector() {
// 이미지 선택 순서를 보여주기 위한 콜렉터
lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.selectedImageList.collectLatest {
binding.buttonAlbumListComplete.visibility = if (it.isEmpty()) View.INVISIBLE else View.VISIBLE
viewModel.setList()
viewModel.setAlbumList()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class AlbumListViewModel : ViewModel() {
}
var scrollToTopFlag = false

fun setList(albumName: String = currentAlbum.value) {
fun setAlbumList(albumName: String = currentAlbum.value) {
viewModelScope.launch {
// ViewHolder의 bind를 통해 사진 선택 순서를 표시한다.
// 하지만 사진을 선택하는 행위는 리스트에 영향을 주지 않는다.
Expand All @@ -46,14 +46,26 @@ class AlbumListViewModel : ViewModel() {
}
}

fun setAllImages(map: Map<String, MutableList<String>>) {
fun initSelectedImageList(list: List<String>) {
_selectedImageList.addAll(list)
setSelectedImageList()
}

fun initAlbumInfo(map: Map<String, MutableList<String>>) {
allImages = map
}

fun setScrollFlag() {
scrollToTopFlag = !scrollToTopFlag
}

private fun setSelectedImageList() {
selectedPictureAdapter.submitList(_selectedImageList.toList())
viewModelScope.launch {
selectedImageList.emit(_selectedImageList.toList())
}
}

private fun swapComplete() {
viewModelScope.launch {
selectedImageList.emit(_selectedImageList.toList())
Expand Down Expand Up @@ -85,9 +97,6 @@ class AlbumListViewModel : ViewModel() {
selectedPictureAdapter.notifyItemChanged(1)
}
}
selectedPictureAdapter.submitList(_selectedImageList.toList())
viewModelScope.launch {
selectedImageList.emit(_selectedImageList.toList())
}
setSelectedImageList()
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,10 @@
<action
android:id="@+id/action_pictureSelectFragment_to_productRegistrationFragment"
app:destination="@id/productRegistrationFragment" />
<argument
android:name="selectedImageList"
app:argType="string[]"
app:nullable="true"
android:defaultValue="@null" />
</fragment>
</navigation>

0 comments on commit f23c51d

Please sign in to comment.