Skip to content

Instantly share code, notes, and snippets.

View yogacp's full-sized avatar
:octocat:
Exploring

Yoga C. Pranata yogacp

:octocat:
Exploring
View GitHub Profile
@yogacp
yogacp / AbstractAdapter.kt
Last active September 12, 2023 11:53
Recyclerview that use Abstract Adapter and viewbinding
/**
* Create the Abstract Adapter and the ViewHolder that we need to hold the item view
*/
abstract class AbstractAdapter<T : ViewBinding, ITEM> constructor(
protected var itemList: List<ITEM>,
private val bindingClass: (LayoutInflater, ViewGroup, Boolean) -> T
) : RecyclerView.Adapter<AbstractAdapter.Holder>() {
var binding: T? = null
@yogacp
yogacp / CustomAdapter.kt
Created February 20, 2019 16:08
Sample of Custom RecyclerView.Adapter class
class FriendListAdapter(val friendList: List<FriendListData.Friend>) :
RecyclerView.Adapter<CountryAdapter.MyViewHolder>() {
inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
var name: TextView = view.findViewById(R.id.text1)
var popText: TextView = view.findViewById(R.id.text2)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val friend = friendList[position]
/**
* High Order Functions
* that execute function only in debug build variant
*/
fun debugMode(function: () -> Unit) {
if (BuildConfig.DEBUG) {
function()
}
}
/**
* Can you simplify this code?
* ==================================
* Assesment Point:
* 1. Line of code
* 2. Variable naming
* ==================================
*
* Get Data from this API to test your code:
* http://demo3325159.mockable.io/getBatchConfig
/**
* Extension for view visibility
*/
fun View.visible() {
visibility = View.VISIBLE
}
fun View.gone() {
visibility = View.GONE
}
interface OnClickListener {
fun onClick(v: View)
}
interface OnLongClickListener {
fun onLongClick(v: View)
}
interface OnTouchListener {
fun onTouch(v: View, event: MotionEvent)
interface OnClickListener {
fun onClick(v: View)
fun onLongClick(v: View)
fun onTouch(v: View, event: MotionEvent)
}
val button = findViewById<Button>(R.id.button)
button.setOnClickListener(object: OnClickListener{
override fun onClick(v: View) {
// Want to do some magic here
interface Toy {
var color: String?
}
open class FlyingToy : Toy {
override var color: String? = "Red"
open fun fly() {
println("The toy is flying...")
}
}
interface Toy {
var color: String?
fun fly()
}
class Plane : Toy {
override var color: String? = "Blue"
override fun fly() {
println("The plane is flying...")
}
@yogacp
yogacp / sample_meaningful_names.kt
Last active August 12, 2019 13:05
Sample of meaningful names
/**
* Sample of Meaningful Names
*/
fun saveData(data: User) {
//...
}
fun deleteData(id: Int) {
//...
}