Skip to content

Instantly share code, notes, and snippets.

View monday8am's full-sized avatar
🙃
meehh

Angel Anton monday8am

🙃
meehh
View GitHub Profile
@monday8am
monday8am / result.kt
Created April 15, 2018 20:37
Result variant of Either pattern
sealed class Result <out V, out E> {
// Idle state
object Loading : Result<Nothing, Nothing>()
// Operation finished correctly with result saved on value
data class Ok <out V > constructor (val value: V): Result<V, Nothing>()
// Operation finished with error
data class Err <out E> constructor (val error: E): Result<Nothing, E>()
@monday8am
monday8am / result_deserialization.kt
Last active February 17, 2021 20:34
Result deserialization
override fun deserialize(json: JsonElement?, typeOfT: Type?, context: JsonDeserializationContext?): Result<*, *>? {
return when {
// Is it a JsonObject? Bingo!
json?.isJsonObject == true -> {
// Let's try to extract the type in order
// to deserialize this object
val parameterizedType = typeOfT as ParameterizedType
// Returns an Ok with the value deserialized
@monday8am
monday8am / result_serializer.kt
Last active April 23, 2018 09:48
Result serializer
override fun serialize(src: Result<*, *>?, typeOfSrc: Type?, context: JsonSerializationContext?): JsonElement? {
return when (src) {
// Ignore Loading or Err states
is Result.Loading -> context?.serialize("")
is Result.Err -> context?.serialize("")
// Thanks God that the original type is passed inside the
// ParameterizedType field.
// It lets us to use it for a real serialization
@monday8am
monday8am / Result.kt
Created April 20, 2018 17:16
Original Result.kt from Fidesmo code
package com.fidesmo.devicemanager.helpers
// Based on https://github.com/danneu/kotlin-result
typealias NetworkResult<V, E> = Result<V, E>
fun <V, E> Result<V, E>.getOrElse(default: V) = when (this) {
is Result.Ok<V> -> value
is Result.Err<E> -> default
is Result.Loading -> default
@monday8am
monday8am / ResultSerializer.kt
Created April 20, 2018 17:18
Original Fidesmo Result JsonSerializer
package com.fidesmo.devicemanager.helpers
import com.google.gson.*
import com.google.gson.internal.LinkedTreeMap
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
import java.lang.reflect.WildcardType
class ResultSerializer : JsonSerializer<Result<*, *>>, JsonDeserializer<Result<*, *>> {
@monday8am
monday8am / async_seudo_code.swift
Last active July 29, 2018 16:16
Async seudo code call
if state.needsAsyncData {
dispatch(LoadingAction)
asyncCall()
.whenOk { dispatch(OkAction(result) }
.whenError { dispatch(ErrorAction(error)) }
}
@monday8am
monday8am / action_creator.swift
Last active October 10, 2018 20:34
Simple action creator in Swift
// Action creator definition
func fetchAppsFromStore(state: State, store: Store<State>) -> Action? {
let request = AppStoreRequests.apps(showDevelopment: state.settingsState.isDevelopmentMode)
let task = JsonRequestTask<[AppId]>(dispatcher: dispatcher)
// Async task call
task.perform(request)
.subscribe(onNext: { result in
@monday8am
monday8am / middleware_kotlin.kt
Last active September 24, 2018 19:11
Kotlin middleware
internal val deviceMiddleware: Middleware<AppState> = { dispatch, state ->
// dispatch: Function that send actions to the begining of the middleware list
// state: Function that returns an atomic copy of the state
// next: Function to send the action to the next middleware
// action: New incoming action
{ next ->
{ action ->
when (action) {
is DeviceConnected -> {
val currentState = state()?.appStoreState
@monday8am
monday8am / RxCBCentralManager.swift
Last active January 7, 2019 13:25
Rx implementation for CBCentralManager API
/**
Lightweight Rx wrapper to CBCentralManager
to hide all implementation details.
*/
public class RxCBCentralManager {
// system API manager
private let centralManager: CBCentralManager
// internal delegate
private let internalDelegate = InternalDelegate()
private var lastConnectedDevice: CBPeripheral?
@monday8am
monday8am / RxCBCentralManagerExample.swift
Last active January 7, 2019 13:27
Example of how to use the RxCBCentralManager utility
// Creates the Rx version of the API passing the same parameters as the system version
let centralManager: RxCBCentralManager = RxCBCentralManager(queue: DispatchQueue.main, options: nil)
// Creates a Rx pipe using the published methods
centralManager.scanForPeripherals(withServices: [requiredServiceUIDD], options: nil)
.filter { (peripheral: CBPeripheral) -> Bool in
if let name = peripheral.name {
return name.contains("peripheralName")
}
return false