feat(dm): 수신자 선택 상태를 추가한다
This commit is contained in:
+97
@@ -0,0 +1,97 @@
|
||||
package kr.co.vividnext.sodalive.v2.main.chat.recipient
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import io.reactivex.rxjava3.core.Scheduler
|
||||
import io.reactivex.rxjava3.core.Single
|
||||
import io.reactivex.rxjava3.disposables.Disposable
|
||||
import io.reactivex.rxjava3.disposables.SerialDisposable
|
||||
import io.reactivex.rxjava3.schedulers.Schedulers
|
||||
import kr.co.vividnext.sodalive.base.BaseViewModel
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
data class DmRecipientUiModel(
|
||||
val id: Long,
|
||||
val nickname: String,
|
||||
val profileImageUrl: String
|
||||
)
|
||||
|
||||
enum class DmRecipientPickerMode {
|
||||
FOLLOWERS,
|
||||
SEARCH
|
||||
}
|
||||
|
||||
data class DmRecipientPickerUiState(
|
||||
val mode: DmRecipientPickerMode = DmRecipientPickerMode.FOLLOWERS,
|
||||
val recipients: List<DmRecipientUiModel> = emptyList(),
|
||||
val searchResultCount: Int = 0,
|
||||
val isSearchEmpty: Boolean = false,
|
||||
val isError: Boolean = false
|
||||
)
|
||||
|
||||
class DmRecipientPickerViewModel(
|
||||
private val loadFollowers: () -> Single<List<DmRecipientUiModel>>,
|
||||
private val searchUsers: (String) -> Single<List<DmRecipientUiModel>>,
|
||||
private val debounceScheduler: Scheduler = Schedulers.computation()
|
||||
) : BaseViewModel() {
|
||||
|
||||
private val _recipientStateLiveData = MutableLiveData(DmRecipientPickerUiState())
|
||||
val recipientStateLiveData: LiveData<DmRecipientPickerUiState> get() = _recipientStateLiveData
|
||||
private var searchDisposable: Disposable? = null
|
||||
private val requestDisposable = SerialDisposable().also(compositeDisposable::add)
|
||||
|
||||
fun enter() {
|
||||
requestDisposable.set(null)
|
||||
requestDisposable.set(
|
||||
loadFollowers()
|
||||
.subscribe({ recipients ->
|
||||
_recipientStateLiveData.value = DmRecipientPickerUiState(
|
||||
mode = DmRecipientPickerMode.FOLLOWERS,
|
||||
recipients = recipients
|
||||
)
|
||||
}, {
|
||||
_recipientStateLiveData.value = DmRecipientPickerUiState(isError = true)
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
fun onSearchQueryChanged(query: String) {
|
||||
val keyword = query.trim()
|
||||
searchDisposable?.dispose()
|
||||
requestDisposable.set(null)
|
||||
|
||||
if (keyword.length < MIN_SEARCH_QUERY_LENGTH) {
|
||||
enter()
|
||||
return
|
||||
}
|
||||
|
||||
val scheduledSearch = debounceScheduler.scheduleDirect(
|
||||
{
|
||||
requestDisposable.set(
|
||||
searchUsers(keyword).subscribe({ recipients ->
|
||||
_recipientStateLiveData.value = DmRecipientPickerUiState(
|
||||
mode = DmRecipientPickerMode.SEARCH,
|
||||
recipients = recipients,
|
||||
searchResultCount = recipients.size,
|
||||
isSearchEmpty = recipients.isEmpty()
|
||||
)
|
||||
}, {
|
||||
_recipientStateLiveData.value = DmRecipientPickerUiState(
|
||||
mode = DmRecipientPickerMode.SEARCH,
|
||||
isError = true
|
||||
)
|
||||
})
|
||||
)
|
||||
},
|
||||
SEARCH_DEBOUNCE_MILLIS,
|
||||
TimeUnit.MILLISECONDS
|
||||
)
|
||||
searchDisposable = scheduledSearch
|
||||
compositeDisposable.add(scheduledSearch)
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val MIN_SEARCH_QUERY_LENGTH = 2
|
||||
const val SEARCH_DEBOUNCE_MILLIS = 500L
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user