Skip to content

Instantly share code, notes, and snippets.

Avatar

Alex Gotev gotev

View GitHub Profile
@gotev
gotev / SafeClickListener.kt
Created Apr 8, 2020
Android Safe Click Listener
View SafeClickListener.kt
import android.os.SystemClock
import android.view.View
/**
* Implements the "throttle first" mechanism for click listeners, to prevent double taps.
*
* How it works:
* - Define a sampling window time (default: 500ms)
* - when you click at time T0, the first click gets dispatched and the subsequent ones happening
* between T0 and T0 + WindowTime gets ignored
@gotev
gotev / ISO8601DateJSONDecoder.swift
Created Feb 21, 2020
iOS ISO8601 Date JSON Decoder
View ISO8601DateJSONDecoder.swift
let jsonDecoder: JSONDecoder = {
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .custom { decoder in
let container = try decoder.singleValueContainer()
let string = try container.decode(String.self)
let formatter1 = ISO8601DateFormatter()
formatter1.formatOptions = [.withInternetDateTime]
let formatter2 = ISO8601DateFormatter()
formatter2.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
guard let date = formatter1.date(from: string) ?? formatter2.date(from: string) else {
@gotev
gotev / NavigationBottomBarSectionsStateKeeperWorkaround.kt
Last active May 28, 2020
JetPack Bottom Bar Navigation with Sections State
View NavigationBottomBarSectionsStateKeeperWorkaround.kt
package jetpack.navigation.workaround
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.navigation.NavController
import androidx.navigation.ui.setupActionBarWithNavController
import com.google.android.material.bottomnavigation.BottomNavigationView
import java.lang.ref.WeakReference
@gotev
gotev / resize-images
Created Oct 30, 2019
Batch resize images
View resize-images
sips -Z 640 *.jpg
@gotev
gotev / static-json-server
Last active Sep 27, 2019
Static JSON File Server in Python 3
View static-json-server
#!/usr/bin/env python3
from http.server import HTTPServer, BaseHTTPRequestHandler
import os
base_path = os.path.dirname(__file__)
class StaticServer(BaseHTTPRequestHandler):
def execute_request(self):
filename = 'cached-responses' + self.path + '.json'
@gotev
gotev / GitFlow-SourceTree.md
Last active Jun 16, 2019
Enable GitFlow in SourceTree
View GitFlow-SourceTree.md

Clone both master and develop branches and switch on develop:

01

Right click on an empty space on the toolbar:

02

Drag GitFlow on your toolbar and click Done:

@gotev
gotev / ISO8601DateTimeAdapters.kt
Created Jun 6, 2019
ISO8601 Gson Type Adapters
View ISO8601DateTimeAdapters.kt
import com.google.gson.GsonBuilder
import com.google.gson.TypeAdapter
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonWriter
import org.threeten.bp.*
private fun LocalDateTime.toISO8601UTCString() = withNano(0).atOffset(ZoneOffset.UTC).toString()
abstract class NullableTypeAdapter<T> : TypeAdapter<T>() {
override fun write(writer: JsonWriter?, value: T) {
View ExtendedGsonFactory.kt
import com.google.gson.Gson
import com.google.gson.TypeAdapter
import com.google.gson.reflect.TypeToken
import okhttp3.RequestBody
import okhttp3.ResponseBody
import retrofit2.Converter
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.lang.reflect.Type
@gotev
gotev / Optional.kt
Created May 27, 2019
Optional Subject
View Optional.kt
import io.reactivex.subjects.BehaviorSubject
class Optional<T>(private val value: T? = null) {
companion object {
fun <T> subject() = BehaviorSubject.createDefault(Optional<T>())
}
fun isPresent() = value != null
@gotev
gotev / xcode-clean.md
Created Jan 13, 2019
Free Up Space taken by Xcode
View xcode-clean.md

Remove unavailable simulators:

xcrun simctl delete unavailable

Delete all the unneeded directories from:

cd ~/Library/Developer/Xcode/iOS\ DeviceSupport
You can’t perform that action at this time.