31DaysOfKotlin
A summary of all the Kotlin tips from Google's Android Developer #31DaysofKotlin on Twitter :computer:
Install / Use
/learn @andyb129/31DaysOfKotlinREADME
31 Days Of Kotlin
<p> <img src="https://github.com/andyb129/31DaysOfKotlin/blob/master/app/src/main/res/drawable/android_tools.png" height="100"> <img src="https://github.com/andyb129/31DaysOfKotlin/blob/master/app/src/main/res/drawable/kotlin_logo.png" width="375" height="95" alt="kotlin logo"/> </p>A summary of all the kotlin tips from Google's Android Developer Twitter account with the hastag #31DaysofKotlin
A Twitter Moment summary of all the tweets are here -> https://twitter.com/i/moments/980488782406303744
TODO
- Add code samples in this repo for all 31 days
- Create an app for quick reference
- Create ref app in the Play Store
Contents
Day 1 - let, apply, with and run
Day 2 - KTX view padding
Day 3 - Parcelable
Day 4 - Spans
Day 5 - Lambdas
Day 6 - Bundles
Day 7 - Type safe builders
Day 8 - KTX Content Values
Day 9 - KTX iterators for ViewGroup & SparseArray
Day 10 - Basic syntax
Day 11 - Operator overload
Day 12 - Sequence
Day 13 - KTX Graphics
Day 14 - Extension functions
Day 15 - By
Day 16 - KTX reified
Day 17 - @JvmName
Day 18 - inline
Day 19 - require
Day 20 - lateinit
Day 21 - lazy
Day 22 - sealed classes
Day 23 - default parameters
Day 24 - access modifiers
Day 25 - data classes
Day 26 - properties
Day 27 - ranges
Day 28 - when statement
Day 29 - KTX destructuring
Day 30 - string templates
Day 31 - elvis operator
<a name="day1"></a>
Day 1 - let, apply, with and run
Let’s run with some standard Kotlin functions! Short and powerful, let, apply, with and run all have a receiver (this), may have an argument (it) and may have a return value. See the differences
<a name="day2"></a>
Day 2 - KTX view padding
Extending existing APIs with default arguments usually makes everyone happy. Android KTX lets you set the padding on one side of a view using default parameters.
A one line function that saves so much code!
Code: https://github.com/android/android-ktx/blob/master/src/main/java/androidx/core/view/View.kt#L117
<p> <img src="https://github.com/andyb129/31DaysOfKotlin/blob/master/app/src/main/res/drawable/day2.png" width="512" height="287" alt="day2"/> </p><a name="day3"></a>
Day 3 - Parcelable
Love the speed of Parcelable, but don’t like writing all that code? Say hello to Parcelize
Spec: https://github.com/Kotlin/KEEP/blob/master/proposals/extensions/android-parcelable.md
<p> <img src="https://github.com/andyb129/31DaysOfKotlin/blob/master/app/src/main/res/drawable/day3.png" width="512" height="287" alt="day3"/> </p><a name="day4"></a>
Day 4 - Spans
Powerful but hard to use - that’s how the text styling Spans API feels. Android KTX adds extension functions for some of the most common spans and makes the API easier to use
Android KTX: https://github.com/android/android-ktx/blob/master/src/main/java/androidx/core/text/SpannableStringBuilder.kt
<p> <img src="https://github.com/andyb129/31DaysOfKotlin/blob/master/app/src/main/res/drawable/day4.png" width="512" height="287" alt="day4"/> </p><a name="day5"></a>
Day 5 - Lambdas
Lambdas are sweet. With last parameter call syntax, you can cleanup callbacks, Callable, and Runnable.
For example, Android KTX sweetens postDelayed with a small wrapper.
Docs: https://kotlinlang.org/docs/reference/lambdas.html Android KTX: https://github.com/android/android-ktx/blob/master/src/main/java/androidx/core/os/Handler.kt#L38
<p> <img src="https://github.com/andyb129/31DaysOfKotlin/blob/master/app/src/main/res/drawable/day5.png" width="512" height="287" alt="day5"/> </p><a name="day6"></a>
Day 6 - Bundles
Bundle up, and get ready for the concise bundle creator in Android KTX. No more calls to putString, putInt, or any of their 20 friends.
One call will make you a new bundle, and it’ll even handle Arrays!
Code: https://github.com/android/android-ktx/blob/master/src/main/java/androidx/core/os/Bundle.kt#L27
<p> <img src="https://github.com/andyb129/31DaysOfKotlin/blob/master/app/src/main/res/drawable/day6.png" width="512" height="287" alt="day6"/> </p><a name="day7"></a>
Day 7 - Type safe builders
[1/4] Specifically terrific? Domain specific languages can be made by using type safe builders. They make for clean APIs; and you can build them yourself too.
Extension Lambdas: https://kotlinlang.org/docs/reference/lambdas.html#function-literals-with-receiver Type Safe Builders: https://kotlinlang.org/docs/reference/type-safe-builders.html
<p> <img src="https://github.com/andyb129/31DaysOfKotlin/blob/master/app/src/main/res/drawable/day7.png" width="512" height="287" alt="day7"/> </p><a name="day8"></a>
Day 8 - KTX Content Values
Combine the power of Content Values with the brevity of Kotlin. Use the Android KTX Content Values creator and just pass a Pair<StringKey, Value>.
Android KTX: https://github.com/android/android-ktx/blob/master/src/main/java/androidx/core/content/ContentValues.kt#L21
<p> <img src="https://github.com/andyb129/31DaysOfKotlin/blob/master/app/src/main/res/drawable/day8.png" width="512" height="287" alt="day8"/> </p><a name="day9"></a>
Day 9 - KTX iterators for ViewGroup & SparseArray
Iterators in interesting places? Android KTX adds iterators to ViewGroup and SparseArray
To define iterator extensions use the operator keyword. Foreach loops will use the extensions!
Docs: https://kotlinlang.org/docs/reference/control-flow.html#for-loops Android KTX: https://github.com/android/android-ktx/blob/master/src/main/java/androidx/core/view/ViewGroup.kt#L66
<p> <img src="https://github.com/andyb129/31DaysOfKotlin/blob/master/app/src/main/res/drawable/day9.png" width="512" height="287" alt="day9"/> </p><a name="day10"></a>
Day 10 - Basic syntax
[1/2] Utility methods for a class? Add them to the top level of the source file. In Java, they are compiled as static methods of that class.
Doc: https://kotlinlang.org/docs/reference/basic-syntax.html
<p> <img src="https://github.com/andyb129/31DaysOfKotlin/blob/master/app/src/main/res/drawable/day10.png" width="512" height="287" alt="day10"/> </p><a name="day11"></a>
Day 11 - Operator overload
Write Kotlin (time * 2) faster with operator overloading. Objects like Path, Range or SpannableStrings naturally allow for operations like addition or subtraction. With Kotlin, you can implement your own operators
https://goo.gl/EGipGz https://goo.gl/iy8tZ9
<p> <img src="https://github.com/andyb129/31DaysOfKotlin/blob/master/app/src/main/res/drawable/day11.png" width="512" height="287" alt="day11"/> </p><a name="day12"></a>
Day 12 - Sequence
[1/2] Sequences are lists that never existed. A Sequence is a cousin of Iterator, lazily generating one value at a time. This matters when using map and filter - they’ll create Sequences instead of copying the list for every step!
Docs: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.sequences/index.html
<p> <img src="https://github.com/andyb129/31DaysOfKotlin/blob/master/app/src/main/res/drawable/day12.png" width="512" height="287" alt="day12"/> </p><a name="day13"></a>
Day 13 - KTX Graphics
If you ever converted a Drawable to a Bitmap then you know how much boilerplate you need.
Android KTX has a great set of functions to make your code more concise when working with classes from the graphics package:
https://github.com/android/android-ktx/tree/master/src/main/java/androidx/core/graphics/drawable
<p> <img src="https://github.com/andyb129/31DaysOfKotlin/blob/master/app/src/main/res/drawable/day13.png" width="512" height="287" alt="day13"/> </p><a name="day14"></a>
Day 14 - Extension functions
[1/2] No more Util classes! Extend the fu
