Slide 38
Slide 38 text
ઃఆ͕༗ޮͳΒϑϦʔμΠΞϧͷ൪߸ʹ1SFpYΛ͚ͭͳ͍
ͷ࣮
import io.reactivex.Observable
import jp.rakutencall.data.datasource.contacts.ContactsDataSource
import jp.rakutencall.data.datasource.settings.SettingDataSource
import jp.rakutencall.data.entity.number.PhoneNumber
import jp.rakutencall.data.entity.prefix.Prefix
import jp.rakutencall.utils.commons.ExecutionThreads
import jp.rakutencall.utils.commons.IoUseCase
import jp.rakutencall.utils.commons.UseCase
import javax.inject.Inject
class FreeDial @Inject constructor(
private val settingDataSource: SettingDataSource,
private val contactsDataSource: ContactsDataSource,
executionThreads: ExecutionThreads)
: IoUseCase(executionThreads) {
override fun execute(requestValue: Request):
Observable {
return settingDataSource.getFreeDialEnable()
.flatMap {
if (it)
validateFreeDial(requestValue)
else
Observable.just(Response(requestValue.prefix))
}
}
private fun validateFreeDial(requestValue: Request):
Observable {
return contactsDataSource.getFreeDialPrefix().map {
if (it.any { it.startWithPrefix(requestValue.phoneNumber) })
Response(Prefix.generateEmptyPrefix())
} else {
Response(requestValue.prefix)
}
}.defaultIfEmpty(Response(requestValue.prefix))
}
data class Request(val prefix: Prefix, val phoneNumber: PhoneNumber)
UseCase.RequestValue
data class Response(val prefix: Prefix) : UseCase.ResponseValue
}