-
Notifications
You must be signed in to change notification settings - Fork 0
/
SaveFormUseCase.kt
30 lines (26 loc) · 1.19 KB
/
SaveFormUseCase.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package io.github.gianpamx.android.architecture.usecase
import io.github.gianpamx.android.architecture.data.FormGateway
import io.github.gianpamx.android.architecture.entity.EmptyNameException
import io.github.gianpamx.android.architecture.entity.EmptyPhoneException
import io.github.gianpamx.android.architecture.entity.Form
import kotlin.concurrent.thread
interface SaveFormUseCase {
fun execute(name: String?, phone: String?, success: (() -> Unit)? = null, failure: ((throwable: Throwable) -> Unit)? = null)
}
class SaveFormUseCaseImpl(private val formGateway: FormGateway) : SaveFormUseCase {
override fun execute(name: String?, phone: String?, success: (() -> Unit)?, failure: ((throwable: Throwable) -> Unit)?) {
thread {
try {
executeSync(name, phone)
success?.invoke()
} catch (t: Throwable) {
failure?.invoke(t)
}
}
}
internal fun executeSync(name: String?, phone: String?) {
if (name.isNullOrEmpty()) throw EmptyNameException()
if (phone.isNullOrEmpty()) throw EmptyPhoneException()
formGateway.persist(Form(name!!, phone!!)) // Safe due to previous check
}
}