Skip to content

Instantly share code, notes, and snippets.

View ch8n's full-sized avatar
💻
Always on to writing my next article.

Chetan Gupta ch8n

💻
Always on to writing my next article.
View GitHub Profile
@ch8n
ch8n / DisplayKt.kt
Created February 16, 2020 06:54
Android display metrics
/**
* This method converts dp unit to equivalent pixels, depending on device density.
*
* @param dp A value in dp (density independent pixels) unit. Which we need to convert into pixels
* @param context Context to get resources and device specific display metrics
* @return A float value to represent px equivalent to dp depending on device density
*/
inline fun Context.dpToPixel(dp:Float) : Float {
return dp * (resource.displayMertics.densityDpi / DisplayMetrics.DENSITY_DEFAULT).toFloat();
@ch8n
ch8n / gist:65dba9188996b9b63ab749fd9d21f502
Created December 24, 2019 07:42
BottomSheet Fragment access state
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val bottomSheet: BottomSheetDialog = super.onCreateDialog(savedInstanceState) as BottomSheetDialog
bottomSheet.setOnShowListener { dialog ->
val bottomSheetDialog: BottomSheetDialog? = dialog as? BottomSheetDialog
val bottomSheetBehaviour = bottomSheetDialog?.findViewById(com.google.android.material.R.id.design_bottom_sheet) as? FrameLayout
BottomSheetBehavior.from(bottomSheetBehaviour).setState(BottomSheetBehavior.STATE_EXPANDED)
}
return bottomSheet
}
@ch8n
ch8n / docker-help.md
Created October 28, 2019 09:13 — forked from bradtraversy/docker-help.md
Docker Commands, Help & Tips

Docker Commands, Help & Tips

Show commands & management commands

$ docker

Docker version info

@ch8n
ch8n / Extension.kt
Last active February 25, 2020 12:01
Some Cool Extensions!
// View Visisibility toggle
fun View.setVisibility(isVisibe: Boolean) = if (isVisibe) {
this.setVisibility(View.VISIBLE)
} else {
this.setVisibility(View.GONE)
}
//concise start activity
inline fun <reified T : AppCompatActivity> AppCompatActivity.launchActivity(bundle: Bundle = android.os.Bundle()) {
//using person data class
var map: HashMap<Int, Person> = HashMap()
map.put(1, person)
for((key, value) in map){
println("Key: $key, Value: $value")
}
@ch8n
ch8n / retrunTypeDestucturing.kt
Last active July 31, 2019 11:40
return types destrcuturing
// Destructuring declarations can also be used when working with return values:
fun getPerson() = Person("ch8n", 25)
val(name, age) = getPerson()
//Or let’s say we need to return two values from a function:
fun getInfo(): Pair<Int, String> = Pair("ch8n", 25)
val (name, age) = twoValuesReturn()
@ch8n
ch8n / whenObejctDestructure.kt
Created July 31, 2019 07:05
when object destructing use
data class Contact(val email,val twitter)
data class Person(val contactInfo:Contact,name:String,Phone:Long)
val contact = Contact("[email protected]","twitter@Ch8n2")
val dev = Person(contact,"ch8n",99999999)
# unwrapping level one params
with(contact){
//do anything with email and twitter
...
//initally
data class Items(val one: Int, val two: Int, val three: Int)
val item = Items(1,2,3)
// destructing
val (one, two) = item.run { arrayOf(one, two) } // Enforcing position destructing
println("$one $two")
//after refactor
@ch8n
ch8n / refactoringErrorDestrcurting.kt
Created July 30, 2019 11:46
refactoring Error Destrcurting
//Gotcha #5 refactoring issue/ no compileTimeCheck
data class ContactMe(val email: String, val phone: String, val twitter: String)
val contact = ContactMe("[email protected]","999999999","ch8n2@twitter")
//destructing
val (email,phone,twitter) = contact
data class ContactMe(val email: String, val phone: String, val medium:String,val twitter: String)
val contact = ContactMe("[email protected]","999999999","ch8n@medium","ch8n2@twitter")
@ch8n
ch8n / unUsedDestrcuturing.kt
Created July 30, 2019 11:23
Unused items destructure
//Gotcha #3
data class Items(val one:Int,val two:Int,val three:Int)
val items = Items(1,2,3)
val (one, requiredOnlyThis ,three)= items // even if we require "requiredOnlyThis" paramter we still need to destructure all
println(requiredOnlyThis)
//best practice for destructure of unused variables
val (_,requiredOnlyThis,_)= items