Skip to content

Instantly share code, notes, and snippets.

View StratusHunter's full-sized avatar

Terence Baker StratusHunter

  • Market Harborough
View GitHub Profile
@StratusHunter
StratusHunter / HeaderItemAdapter.kt
Created February 21, 2018 11:11
Attempting to remove some of the boilerplate from RecyclerView Adapters. Also off the back of this creating an adapter that accepts a header view similar to iOS tableHeaderView. By using getViewType(position:Int) you can provide different cell types to then create within onCreateItemViewHolder.
import android.support.v7.widget.RecyclerView
import android.view.ViewGroup
/**
* Created by Terence Baker on 08/02/2018.
*/
abstract class HeaderItemAdapter<H : RecyclerView.ViewHolder, I : RecyclerView.ViewHolder, T>(items: Array<T>, itemClickListener: OnRecyclerItemClickListener<T>? = null) : ItemAdapter<I, T>(items, itemClickListener) {
companion object {
@StratusHunter
StratusHunter / JsonElement_Safe.kt
Created February 13, 2018 12:54
I was having trouble with safely parsing Gson objects without loads of try catch statements or a single try catch which would cause the whole object parsing to fail if a single field was incorrect. I also wanted to replicate org.json.JSONObject opt style functions to return null instead of throwing an exception so I can use Kotlin's null handlin…
import com.google.gson.*
import java.math.BigDecimal
import java.math.BigInteger
/**
* Created by Terence Baker on 13/02/2018.
*/
val JsonElement.optString: String?
get() = safeConversion { asString }