Skip to content

Instantly share code, notes, and snippets.

@u0705666
u0705666 / unit-test-helper.kt
Created July 19, 2023 02:10
unit test coWhenever
fun <T> coWhenever(block: suspend CoroutineScope.() -> T): OngoingStubbing<T> =
runBlocking {
whenever(block())
}
fun <T> coVerify(mock: T, block: suspend CoroutineScope.(T) -> Unit) {
runBlocking {
block(verify(mock))
}
}
@u0705666
u0705666 / suspend-api-call-multi.kt
Created June 8, 2023 04:42
concurrent suspend api calls
@Inject lateinit var suspendApi: SuspendApi
...
view!!.showTransparentLoading()
shownScope.launch {
val assetCategoriesResultDeferred = async{suspendApi.getAssetCategories()}
val categorizedAssetDeferred = async{suspendApi.getCategorizedAssets()}
val assetCategoriesResult = assetCategoriesResultDeferred.await()
val categorizedAssetsResult = categorizedAssetDeferred.await()
if (assetCategoriesResult is ApiResult.Success && categorizedAssetsResult is ApiResult.Success) {
assetCategoryList = assetCategoriesResult.data.assetCategories
@u0705666
u0705666 / suspend-api-single.kt
Created June 8, 2023 04:41
suspend api single call
@Inject lateinit var suspendApi: SuspendApi
shownScope.launch {
view.showSearchLoading()
val result = suspendApi.getCategorizedAssetsSearch(searchStr, assetCategory?.categoryIdentifier)
view.hideSearchLoading()
when (result) {
is ApiResult.Success -> {
view.replaceListItems(getAssetListItem(result.data.categorizedAssets))
@u0705666
u0705666 / api-result-def.kt
Created June 8, 2023 04:41
ApiResult definiation
sealed class ApiResult<out T> {
// HTTP 200
data class Success<T>(val data: T) : ApiResult<T>()
// HTTP 400..500
data class Failure(val code: Int, val message: String): ApiResult<Nothing>()
// Socket timeout, write in Demo, IO, , JSON parsing, NPE etc
data class NetworkError(val throwable: Throwable, val message: String, val networkErrorExceptions: ClassifiedNetworkErrorExceptions) : ApiResult<Nothing>()
@u0705666
u0705666 / api-def-coroutine.kt
Created June 8, 2023 04:40
api-def-coroutine
@RolesAllowed(Role.READONLY, Role.PRIVILEGED)
@GET("v1/instruments/categorized-assets/search")
suspend fun getCategorizedAssetsSearch(
@Query("search_term") searchTerm: String?,
@Query("category_identifier") categoryIdentifier: Int?
): ApiResult<CategorizedAssetsResponse>
@u0705666
u0705666 / rxjava-combined-api-call-example.kt
Created June 8, 2023 04:39
rxjava-combined-api-call-example
private fun fetchAccounts() {
val externalAccountLinkStatusObservable = linkCoordinator.linkStatusObservable
val wealthFrontAccountObservable = accountRepository.wealthfrontAccountObservable
val wealthFrontAccountRequestsObservable = accountRequestsRepository.wealthfrontAccountRequestObservable
val calloutObservable = calloutsRepository.getCalloutsObservable(AccountListSection::class.java)
autoDisposer.autoDispose(
combineLatest(
externalAccountLinkStatusObservable,
wealthFrontAccountObservable,
@u0705666
u0705666 / rxjava-api-call-example.java
Created June 8, 2023 04:38
rxjava-api-call-example
autoDispose(
api.getCategorizedAssetsSearch(searchStr, assetCategory?.categoryIdentifier)
.observeOn(mainThread())
.doOnSubscribe { view.showSearchLoading() }
.doOnTerminate { view.hideSearchLoading() }
.subscribe(
{
view.replaceListItems(getAssetListItem(it.categorizedAssets))
},
{
@u0705666
u0705666 / api-def-rxjava.java
Created June 8, 2023 04:34
API definition with RxJava observable
@GET("v1/instruments/categorized-assets/search")
Observable<CategorizedAssetsResponse> getCategorizedAssetsSearch(
@Nonnull @Query("search_term") String searchTerm,
@Nullable @Query("category_identifier") Integer categoryIdentifier
);