Created
October 21, 2018 09:44
-
-
Save Abhityagi16/2aade04c73e0b3c093a27ed0fdbd9854 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface UseCaseScheduler { | |
fun execute(runnable: Runnable) | |
fun <V : UseCase.ResponseValue> notifyResponse(response: V, | |
useCaseCallback: UseCase.UseCaseCallback<V>) | |
fun <V : UseCase.ResponseValue> onError( | |
useCaseCallback: UseCase.UseCaseCallback<V>, t: Throwable) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Handler | |
import java.util.concurrent.ArrayBlockingQueue | |
import java.util.concurrent.ThreadPoolExecutor | |
import java.util.concurrent.TimeUnit | |
class UseCaseThreadPoolScheduler : UseCaseScheduler { | |
val POOL_SIZE = 2 | |
val MAX_POOL_SIZE = 4 | |
val TIMEOUT = 30 | |
private val mHandler = Handler() | |
internal var mThreadPoolExecutor: ThreadPoolExecutor | |
init { | |
mThreadPoolExecutor = ThreadPoolExecutor(POOL_SIZE, MAX_POOL_SIZE, TIMEOUT.toLong(), | |
TimeUnit.SECONDS, ArrayBlockingQueue(POOL_SIZE)) | |
} | |
override fun execute(runnable: Runnable) { | |
mThreadPoolExecutor.execute(runnable) | |
} | |
override fun <V : UseCase.ResponseValue> notifyResponse(response: V, | |
useCaseCallback: UseCase.UseCaseCallback<V>) { | |
mHandler.post { useCaseCallback.onSuccess(response) } | |
} | |
override fun <V : UseCase.ResponseValue> onError( | |
useCaseCallback: UseCase.UseCaseCallback<V>, t: Throwable) { | |
mHandler.post { useCaseCallback.onError(t) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment