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
/**
* 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
/**
* High Order Functions
* that execute function only in debug build variant
*/
fun debugMode(function: () -> Unit) {
if (BuildConfig.DEBUG) {
function()
}
}
/**
* 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) {
//...
}
@yogacp
yogacp / MeaningfulNames.kt
Last active May 21, 2019 08:57
Sample of how to create meaningful names
// Bad variables naming
var a = 0 // user ages
var w = 0 // user weight
var h = 0 // user height
// Bad functions naming
fun age()
fun weight()
fun height()