Skip to content

Commit

Permalink
[refactor] : #159 버튼 중복 클릭
Browse files Browse the repository at this point in the history
  • Loading branch information
lsakee committed Mar 6, 2024
1 parent 9413636 commit 894bbf6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.sopt.savelink.ui.savelinksetclip

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.fragment.app.viewModels
import androidx.lifecycle.viewModelScope
Expand All @@ -11,8 +12,6 @@ import designsystem.components.button.state.LinkMindButtonState
import designsystem.components.dialog.LinkMindDialog
import designsystem.components.toast.linkMindSnackBar
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import org.orbitmvi.orbit.viewmodel.observe
import org.sopt.mainfeature.R
import org.sopt.savelink.databinding.FragmentSaveLinkSetClipBinding
Expand Down Expand Up @@ -110,18 +109,13 @@ class SaveLinkSetClipFragment : BindingFragment<FragmentSaveLinkSetClipBinding>(
viewModel.showDialog()
}
}
private val mutex = Mutex()

private fun onClickCompleteBtn() {
binding.btnSaveLinkComplete.apply {
btnClick {
if (state == LinkMindButtonState.DISABLE) return@btnClick

if (!mutex.isLocked) {
viewModel.viewModelScope.launch {
mutex.withLock {
viewModel.saveLink(viewModel.container.stateFlow.value.url, viewModel.container.stateFlow.value.categoryId)
}
}
viewModel.viewModelScope.launch {
viewModel.saveLink(viewModel.container.stateFlow.value.url, viewModel.container.stateFlow.value.categoryId)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import org.orbitmvi.orbit.Container
import org.orbitmvi.orbit.ContainerHost
import org.orbitmvi.orbit.syntax.simple.intent
Expand Down Expand Up @@ -85,17 +87,22 @@ class SetLinkViewModel @Inject constructor(
}
}

private val mutex = Mutex()
fun saveLink(linkUrl: String, categoryId: Long?) = viewModelScope.launch {
saveLinkUseCase(
PostSaveLinkUseCase.Param(
linkUrl = linkUrl,
categoryId = if (categoryId == 0.toLong()) null else categoryId,
),
).onSuccess {
navigateSetLink()
if (it != 201) showSnackBar()
}.onFailure {
showSnackBar()
if (!mutex.isLocked) {
mutex.withLock {
saveLinkUseCase(
PostSaveLinkUseCase.Param(
linkUrl = linkUrl,
categoryId = if (categoryId == 0.toLong()) null else categoryId,
),
).onSuccess {
navigateSetLink()
if (it != 201) showSnackBar()
}.onFailure {
showSnackBar()
}
}
}
}

Expand Down

0 comments on commit 894bbf6

Please sign in to comment.