Skip to content

Instantly share code, notes, and snippets.

View lukaszkalnik's full-sized avatar

Lukasz Kalnik lukaszkalnik

View GitHub Profile
private class EitherCall<R>(
private val delegate: Call<R>,
private val successType: Type
) : Call<Either<ApiError, R>> {
override fun enqueue(callback: Callback<Either<ApiError, R>>) = delegate.enqueue(
object : Callback<R> {
override fun onResponse(call: Call<R>, response: Response<R>) {
callback.onResponse(this@EitherCall, Response.success(response.toEither()))
interface TmdbApi {
@GET("configuration")
suspend fun getConfiguration(): Either<ApiError, TmdbConfiguration>
@GET("movie/popular")
suspend fun getPopularMovies(
@Query("language") language: String,
@Query("page") page: Int,
@Query("region") region: String
sealed class ApiError
data class HttpError(val code: Int, val body: String) : ApiError()
data class NetworkError(val throwable: Throwable) : ApiError()
data class UnknownApiError(val throwable: Throwable) : ApiError()
internal class DefaultGatewayApiService(
val getLightsUseCase: GetLightsUseCase,
val getSystemDetailUseCase: GetSystemUseCase
) : GatewayApiService {
override suspend fun getLights(roomId: String): Either<Throwable, List<Light>> =
getLightsUseCase(roomId)
override suspend fun getSystemDetail(): Either<Throwable, SystemDetail> = //...
}
package net.grandcentrix.gatewayapi.api
interface GatewayApiService {
suspend fun getLights(roomId: String): Either<Throwable, List<Light>>
suspend fun getSystemDetail(): Either<Throwable, SystemDetail>
companion object {
class GetLightsUseCaseTest {
private val testEntity = TestClassesProvider.entity()
private val mockApi = mockk<GatewayApi>()
private val lights = listOf(Light(id = "1234"))
private val mockConverter = mockk<EntityConverter<List<Light>>> converter@{
every { [email protected](testEntity) } returns lights
}
sealed class Either<out F, out S>
data class Failure<out F>(val failure: F) : Either<F, Nothing>()
data class Success<out S>(val success: S) : Either<Nothing, S>()
package net.grandcentrix.gatewayapi.domain.usecase
internal typealias GetLightsUseCase = suspend (String) -> Either<Throwable, List<Light>>
internal typealias EntityConverter<T> = (Entity) -> T
internal fun getLightsUseCaseFactory(
api: GatewayApi,
converter: EntityConverter<List<Light>>
): GetLightsUseCase = { roomId: String ->
api.getLights(roomId)
package net.grandcentrix.gatewayapi.data.remote
internal interface GatewayApi {
@GET("lights/{roomId}")
suspend fun getLights(@Path("roomId") roomId: String): Response<Entity>
@GET("system")
suspend fun getSystemDetail(): Response<Entity>
@lukaszkalnik
lukaszkalnik / MarkdownLinearizeNestedAsterisks.java
Created October 27, 2016 09:40
Markdown - linearizing nested asterisks (e.g. **Bold *bold italic*** => **Bold** ***bold italic***)
private static String preprocessNestedAsterisks(String markdownText) {
// Here we treat double/single asterisks (emphasis)
// because nested emphasis (**for example *like* this**)
// is not interpreted correctly by the Bypass lib
String[] doubleAsteriskSplitMarkdown = markdownText.split("\\*\\*", -1);
int numberOfDoubleAsteriskedTokens = doubleAsteriskSplitMarkdown.length;
// strings surrounded by double asterisks are always on the odd array positions
for (int i = 1; i < numberOfDoubleAsteriskedTokens; i += 2) {