Skip to content

Instantly share code, notes, and snippets.

View s4cha's full-sized avatar
🎯
Focusing

Sacha DSO s4cha

🎯
Focusing
View GitHub Profile
@s4cha
s4cha / kotlinJSON.kt
Last active October 19, 2018 17:43
Kotlin JSON parsing \o/
// Usage
fun parse(json: JSONObject): Car {
val car = Car()
car.apply {
::id < json.key("id")
::numberOfDoors < json.key("nb_of_doors")
::driverName < json.key("driver_name")
}
return car
@s4cha
s4cha / Notifier.kt
Last active June 20, 2019 14:58
EventBus in Kotlin under 50 lines \o/
import kotlinx.coroutines.*
typealias Callback<T> = (T) -> Unit
class CallbackWrapper<T>(val callback:Callback<T>, val eventName: String, val receiverHash: Int, val dispatcher: CoroutineDispatcher)
object Notifier: CoroutineScope {
private val supervisorJob = SupervisorJob()
@s4cha
s4cha / InjectionContainer.kt
Last active June 18, 2019 13:00
Single Injection Container in pure Kotlin
typealias ServiceBuilderBlock<T> = () -> T
class ServiceBuilderkWrapper<T>(val serviceBuilderBlock:ServiceBuilderBlock<T>, val serviceName: String)
object InjectionContainer {
var serviceBuilderkWrappers = mutableListOf<ServiceBuilderkWrapper<Any>>()
inline fun <reified T>registerService(noinline block: ServiceBuilderBlock<T>) {
val wrapper = ServiceBuilderkWrapper(block, T::class.simpleName ?: "noname") as ServiceBuilderkWrapper<Any>
{
"total": 622,
"countryVotes": [
{
"country": "AL",
"totalVotes": 12,
"votes": {
"CZ": 0,
"MT": 4,
"FI": 1,
@s4cha
s4cha / LiveReload.txt
Created June 9, 2020 14:04
Android live reload
while true; do adb shell am broadcast -a debug; sleep 0.5; done
@s4cha
s4cha / record.txt
Created June 9, 2020 14:40
Record iOS Simulator video
xcrun simctl io booted recordVideo --codec=h264 screenRecording.mp4
// MARK - Network Layer
// Repository implementation
// (here in an extension)
extension RestApi: OrganizationRepository {
public func fetchCurrent() -> AnyPublisher<Organization, Error> {
get("/organizations/current").map { (jsonOrganization: JSONOrganization) in
return jsonOrganization.toOrganization()
}
.eraseToAnyPublisher()