Skip to content

Instantly share code, notes, and snippets.

View dperez37's full-sized avatar

David Perez dperez37

  • Livefront
  • 2913 Harriet Avenue #101, Minneapolis, MN 55408
View GitHub Profile
@dperez37
dperez37 / sample-kotlin-custom-edittext-constructor.kt
Last active August 30, 2021 21:38
Sample Kotlin Custom EditText Constructor
class CustomView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = androidx.appcompat.R.attr.editTextStyle
) : AppCompatEditText(context, attrs, defStyleAttr)
@dperez37
dperez37 / CustomGson.kt
Last active September 10, 2020 03:25
Gson Kotlin Adapter - Gson setup without Adapter
GsonBuilder()
.registerTypeAdapter(DateTime::class.java, DateTimeTypeConverter())
.registerTypeAdapterFactory(CustomTypeAdapterFactory())
.create()
@dperez37
dperez37 / CustomGson.kt
Last active September 19, 2020 18:24
Gson Kotlin Adapter - Setup The Adapter
GsonBuilder()
.registerTypeAdapter(DateTime::class.java, DateTimeTypeConverter())
.registerTypeAdapterFactory(KotlinReflectiveTypeAdapterFactory.create(true))
.registerTypeAdapterFactory(CustomTypeAdapterFactory())
.create()
@dperez37
dperez37 / GsonAdapter.java
Created September 10, 2020 01:14
Kotlin Gson Adapter - Sample Gson code
private final ObjectConstructor<T> constructor;
@Override
public T read(JsonReader in) throws IOException {
...
T instance = constructor.construct();
...
in.beginObject();
while (in.hasNext()) {
String name = in.nextName();
@dperez37
dperez37 / ComplexModel.kt
Created September 10, 2020 00:26
Kotlin Gson Adapter - Simple Model with additional Initialization code
data class SimpleModel(
val hello: String,
val world: Int
) {
val secondHello: String = hello
val secondWorld: Int
init {
secondWorld = world
}
@dperez37
dperez37 / SimpleModel.kt
Last active September 10, 2020 00:21
Gson Kotlin Adapter - Simple Sample Model
data class SimpleModel(
val hello: String,
val world: Int
)
@dperez37
dperez37 / sample-model-constructor.java
Last active February 20, 2019 02:09
Sample Model with all Constructors; Equivalent to using @jvmoverloads
class TestModel(
val required: Any,
val nonnull: Any = Any(),
val nullable: Any? = null
) {
constructor(
required: Any,
nonnull: Any
) : this(required, nonnull, null)
@dperez37
dperez37 / sample-model-builder.kt
Last active February 20, 2019 02:53
Sample Model with a Builder
data class TestModel @JvmOverloads constructor(
val requiredValue: Any,
val nonnullValue: Any = Any(),
val nullableValue: Any? = null
) {
fun newBuilder(): Builder = Builder(this)
class Builder() {
lateinit var required: Any
var nonnull: Any = Any()
@dperez37
dperez37 / sample-model-jvmoverloads-constructor.kt
Last active February 19, 2019 03:30
Sample Model with JvmOverloads
data class TestModel @JvmOverloads constructor(
val required: Any,
val nonnull: Any = Any(),
val nullable: Any? = null
)
@dperez37
dperez37 / sample-kotlin-custom-view-constructor.kt
Last active November 11, 2020 18:48
Sample Kotlin Constructor for a Custom View
class CustomView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0,
defStyleRes: Int = 0
) : LinearLayout(context, attrs, defStyleAttr, defStyleRes) {
init {
orientation = VERTICAL
inflate(context, R.layout.custom_view, this)
}