Skip to content

Instantly share code, notes, and snippets.

View Miha-x64's full-sized avatar
🍵
へ‿(◉‿◉)‿ㄏ

Mike Miha-x64

🍵
へ‿(◉‿◉)‿ㄏ
View GitHub Profile
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
public abstract class BundleKey<T> {
@NonNull final String key;
BundleKey(@NonNull String key) {
this.key = key;
@Miha-x64
Miha-x64 / enums.kt
Last active February 9, 2022 10:00
Enum extensions for Kotlin, superseded by https://github.com/Miha-x64/Kotlin-MPP_Collection_utils
package net.aquadc.common
// Gist: https://gist.github.com/Miha-x64/5f626228b34175f827734596d6701008
import java.util.*
// maps
inline fun <reified K : Enum<K>, V> enumMapOf(): MutableMap<K, V> {
return EnumMap<K, V>(K::class.java)
}
@Miha-x64
Miha-x64 / EnumSetTypeAdapterFactory.kt
Last active February 9, 2022 10:01
TypeAdapter(Factory) for enums with no reflective @SerializedName lookup; TypeAdapterFactory whichs treats Set<E> as EnumSet<E>
package net.aquadc.common
import com.google.gson.Gson
import com.google.gson.TypeAdapter
import com.google.gson.TypeAdapterFactory
import com.google.gson.reflect.TypeToken
import java.lang.reflect.ParameterizedType
import java.util.*
/**
@Miha-x64
Miha-x64 / RetrofitCallback.kt
Last active February 9, 2022 10:02
A wrapper around retrofit2.Callback
abstract class RetrofitCallback<T> : retrofit2.Callback<T> {
final override fun onResponse(call: Call<T>, response: Response<T>) =
if (response.isSuccessful) onSuccess(response.body())
else
onError(when (response.code()) {
401 -> Failure.Unauthorized()
404 -> Failure.NotFound()
409 -> Failure.Conflict()
500 -> Failure.InternalServerError()
@Miha-x64
Miha-x64 / BadBackendResponseDeserializer.kt
Created March 31, 2017 18:44
Such deseriazers will help you in processing responses from back-ends which return 200 HTTP code all the time.
object class BadBackendResponseDeserializer : JsonDeserializer<Any?> {
// register all adapters you need
private val gson = GsonBuilder()
.registerTypeAdapter(Post::class.java, WallPostAdapter)
.registerTypeAdapter(Attachment::class.java, AttachmentAdapter)
.registerTypeAdapter(User::class.java, UserAdapter)
.registerTypeAdapter(Group::class.java, GroupAdapter)
.create()
@Miha-x64
Miha-x64 / GsonNullValueSkipping.java
Last active June 9, 2022 14:19
Gson has serializeNulls() setting but doesn't have dontDeserializeNulls(). This is useful to avoid assigning nulls to properties keeping their default values assigned in constructor instead.
import com.google.gson.Gson;
import com.google.gson.JsonIOException;
import com.google.gson.TypeAdapter;
import com.google.gson.internal.JsonReaderInternalAccess;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import com.google.gson.stream.MalformedJsonException;
import okhttp3.MediaType;
class GradientStrokeDrawable : Drawable() {
var radius: Float = 0f
set(radius) {
if (field != radius) {
check(radius.isFinite() && radius >= 0f)
field = radius
path.rewind()
invalidateSelf()
}
@Miha-x64
Miha-x64 / Xml.java
Last active December 22, 2022 14:03
HTML/XML escaping utils, answering https://stackoverflow.com/a/61215915/3050249
import java.io.IOException;
import java.io.UncheckedIOException;
/**
* XML escaping utils.
*
* Source: https://gist.github.com/Miha-x64/4ed50f1d5593e45a452efbf456aa1db4
*/
public final class Xml {
@Miha-x64
Miha-x64 / DynamicActivity.java
Created May 23, 2017 14:58
An activity which copies an APK from assets to private directory, and loads a class and resources from it.
package whatever.dynamicapkloading;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.widget.TextView;
@Miha-x64
Miha-x64 / proguard-rules.pro
Last active March 13, 2023 07:27
Healthy person's ProGuard rules for Android and Kotlin.
# preverify is useful only for J2ME
# R8 doesn't preverify by default
#-dontpreverify
-optimizationpasses 5
# this seems to be enough
# remove indirection
-allowaccessmodification