$ docker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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()) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//using person data class | |
var map: HashMap<Int, Person> = HashMap() | |
map.put(1, person) | |
for((key, value) in map){ | |
println("Key: $key, Value: $value") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 |