10 Extension Functions of Kotlin in Android Development

F958fbf402a67a1742335a4894e041e5?s=47 magiepooh
November 06, 2015

10 Extension Functions of Kotlin in Android Development

10 Extension Functions of Kotlin in Android Development

F958fbf402a67a1742335a4894e041e5?s=128

magiepooh

November 06, 2015
Tweet

Transcript

  1. 10 Extension Functions of Kotlin in Android Development @magiepooh

  2. About me @magiepooh @magie_pooh Takuma Fujita

  3. Company CyberAgent, Inc.

  4. 10 Extension Functions of Kotlin in Android Development @magiepooh

  5. What’s Kotlin?

  6. Kotlin • By JetBrains • Lambdas • Nullable(NonNull) • Higher-Order

    Functions (Stream) • Extension Functions
  7. Kotlin • By JetBrains • Lambdas -> Retrolambda • Nullable(NonNull)

    • Higher-Order Functions (Stream) • Extension Functions
  8. Kotlin • By JetBrains • Lambdas -> Retrolambda • Nullable(NonNull)

    -> Optional • Higher-Order Functions (Stream) • Extension Functions
  9. Kotlin • By JetBrains • Lambdas -> Retrolambda • Nullable(NonNull)

    -> Optional • Higher-Order Functions (Stream) -> RxJava • Extension Functions
  10. Kotlin • By JetBrains • Lambdas -> Retrolambda • Nullable(NonNull)

    -> Optional • Higher-Order Functions (Stream) -> RxJava • Extension Functions
  11. Functions in Kotlin

  12. Functions in Kotlin fun sum(a: Int, b: Int): Int {


    return a + b
 }
  13. Functions in Kotlin fun sum(a: Int, b: Int): Int {


    return a + b
 }
  14. Functions in Kotlin fun sum(a: Int, b: Int): Int {


    return a + b
 }
  15. Functions in Kotlin fun sum(a: Int, b: Int): Int {


    return a + b
 }
  16. Functions in Kotlin fun sum(a: Int, b: Int): Int =

    a + b
  17. Functions in Kotlin fun sum(a: Int, b: Int): Int =

    a + b
  18. Functions in Kotlin fun sum(a: Int, b: Int) = a

    + b
  19. Extension Functions

  20. Extension Functions static void toast(Context context, String text) { Toast.makeText(context,

    text, Toast.LENGTH_SHORT).show(); }
  21. Extension Functions void toast(Context context, String text) { Toast.makeText(context, text,

    Toast.LENGTH_SHORT).show(); }
  22. Extension Functions fun toast(Context context, String text) { Toast.makeText(context, text,

    Toast.LENGTH_SHORT).show(); }
  23. Extension Functions fun toast(Context context, String text) { Toast.makeText(context, text,

    Toast.LENGTH_SHORT).show(); }
  24. Extension Functions fun toast(context: Context, text: String) { Toast.makeText(context, text,

    Toast.LENGTH_SHORT).show() }
  25. fun Activity.toast(context: Context, text: String) { Toast.makeText(context, text, Toast.LENGTH_SHORT).show() }

    Extension Functions
  26. fun Activity.toast(context: Context, text: String) { Toast.makeText(context, text, Toast.LENGTH_SHORT).show() }

    Extension Functions
  27. fun Activity.toast(context: Context, text: String) { Toast.makeText(this, text, Toast.LENGTH_SHORT).show() }

    Extension Functions
  28. fun Activity.toast(context: Context, text: String) { Toast.makeText(this, text, Toast.LENGTH_SHORT).show() }

    Extension Functions
  29. fun Activity.toast(text: String) { Toast.makeText(this, text, Toast.LENGTH_SHORT).show() } Extension Functions

  30. fun Activity.toast(text: String) { Toast.makeText(this, text, Toast.LENGTH_SHORT).show() } override fun

    onCreate(savedInstanceState : Bundle?) { toast(“Hello, Toast!”) } Extension Functions
  31. fun Activity.toast(text: String) = Toast.makeText(this, text, Toast.LENGTH_SHORT).show() override fun onCreate(savedInstanceState

    : Bundle?) { toast(“Hello, Toast!”) } Extension Functions
  32. Some Extension Functions

  33. toVisible

  34. toVisible fun View.toVisible() { visibility = View.VISIBLE } textView.toVisible()

  35. toVisible fun View.toVisible() { visibility = View.VISIBLE } textView.toVisible() textView.toGone()

    textView.isVisible() textView.isGone()
  36. setFragment

  37. setFragment fun FragmentActivity.setFragment(fragment: Fragment, containerId: Int) = supportFragmentManager .beginTransaction() .add(containerId,

    fragment) .commit()
  38. setFragment fun FragmentActivity.setFragment(fragment: Fragment, containerId: Int) = supportFragmentManager .beginTransaction() .add(containerId,

    fragment) .commit() setFragment(MainFragment.newInstance(), android.R.id.content)
  39. setFragment fun FragmentActivity.setFragment(fragment: Fragment, containerId: Int) = supportFragmentManager .beginTransaction() .add(containerId,

    fragment) .commit() setFragment(MainFragment.newInstance(), android.R.id.content)
  40. setFragment fun FragmentActivity.setFragment(fragment: Fragment, containerId: Int = android.R.id.content) = supportFragmentManager

    .beginTransaction() .add(containerId, fragment) .commit() setFragment(MainFragment.newInstance(), android.R.id.content)
  41. setFragment fun FragmentActivity.setFragment(fragment: Fragment, containerId: Int = android.R.id.content) = supportFragmentManager

    .beginTransaction() .add(containerId, fragment) .commit() setFragment(MainFragment.newInstance())
  42. setFragment fun FragmentActivity.setFragment(fragment: Fragment, containerId: Int = android.R.id.content) = supportFragmentManager

    .beginTransaction() .add(containerId, fragment) .commit() setFragment(MainFragment.newInstance()) setFragment(MainFragment.newInstance(), R.id.my_content)
  43. getString

  44. getString(Java) getActivity().getResources().getString(resId)

  45. getString fun Fragment.getString(@StringRes resId: Int) = getActivity().getResources().getString(resId)

  46. getString fun Fragment.getString(@StringRes resId: Int) = activity.resources.getString(resId)

  47. let

  48. let public inline fun <T, R> T.let(f: (T) -> R):

    R = f(this)
  49. let someObject?.let { // `it` is non-null }

  50. Compat

  51. isAttachedToWindow

  52. isAttachedToWindow fun View.isAttachedToWindowCompat () = if (Build.VERSION.SDK_INT < 19) {

    ViewCompat.isAttachedToWindow(this) } else { isAttachedToWindow() }
  53. Cast

  54. WindowManager

  55. WindowManager(Java) WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE)

  56. WindowManager fun Context.getWindowManager() = getSystemService(Context.WINDOW_SERVICE) as WindowManager

  57. WindowManager fun Context.getWindowManager() = getSystemService(Context.WINDOW_SERVICE) as WindowManager

  58. WindowManager fun Context.getWindowManager() = getSystemService(Context.WINDOW_SERVICE) as WindowManager val windowManager =

    getWindowManager() val display = windowManager.getDefaultDisplay() val width = display.width val height = display.height
  59. Extension Properties

  60. WindowManager fun Context.getWindowManager() = getSystemService(Context.WINDOW_SERVICE) as WindowManager val windowManager =

    getWindowManager() val display = windowManager.getDefaultDisplay() val width = display.width val height = display.height
  61. WindowManager val Context.windowManager = get() = getSystemService(Context.WINDOW_SERVICE) as WindowManager val

    windowManager = getWindowManager() val display = windowManager.getDefaultDisplay() val width = display.width val height = display.height
  62. WindowManager val Context.windowManager = get() = getSystemService(Context.WINDOW_SERVICE) as WindowManager val

    display = windowManager.getDefaultDisplay() val width = display.width val height = display.height
  63. Extends Library

  64. Glide Extension http://www.example.com/12345?width=300&height=300

  65. Glide Extension http://www.example.com/12345?width=300&height=300

  66. Glide Extension fun RequestManager.loadOptimize(url: String?, width: Int, height: Int): DrawableTypeRequest<String>

    = load(url?.let { "$it?width=$width&height=$height" })
  67. Glide Extension fun RequestManager.loadOptimize(url: String?, width: Int, height: Int): DrawableTypeRequest<String>

    = load(url?.let { "$it?width=$width&height=$height" })
  68. Glide Extension fun RequestManager.loadOptimize(url: String?, width: Int, height: Int): DrawableTypeRequest<String>

    = load(url?.let { "$it?width=$width&height=$height" })
  69. Glide Extension fun RequestManager.loadOptimize(url: String?, width: Int, height: Int): DrawableTypeRequest<String>

    = load(url?.let { "$it?width=$width&height=$height" }) Glide.with(context) .loadOptimize(url, 300, 300) .into(imageView)
  70. Listener

  71. addOnPageChangeListener

  72. ViewPager# OnPageChangeListener(Java) pager.addOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int position)

    { // no op } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { // no op } @Override public void onPageScrollStateChanged(int state) { onStateChange(state); } });
  73. ViewPager# OnPageChangeListener(Java) pager.addOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int position)

    { // no op } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { // no op } @Override public void onPageScrollStateChanged(int state) { onStateChange(state); } });
  74. ViewPager# OnPageChangeListener(Java) pager.addOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int position)

    { // no op } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { // no op } @Override public void onPageScrollStateChanged(int state) { onStateChange(state); } });
  75. fun ViewPager.addOnPageChangeListener( onPageScrolled: (position: Int, positionOffset: Float, positionOffsetPixels: Int) ->

    Unit = { position, positionOffset, positionOffsetPixels -> }, onPageScrollStateChanged: (state: Int) -> Unit = {}, onPageSelected: (position: Int) -> Unit = {} ) = addOnPageChangeListener(object : ViewPager.OnPageChangeListener { override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) { onPageScrolled(position, positionOffset, positionOffsetPixels) } override fun onPageScrollStateChanged(state: Int) { onPageScrollStateChanged(state) } override fun onPageSelected(position: Int) { onPageSelected(position) } })
  76. OnPageChangeListener(Java) pager.addOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int position) {

    // no op } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { // no op } @Override public void onPageScrollStateChanged(int state) { onStateChange(state); } });
  77. Extends addOnPageChangeListener pager.addOnPageChangeListener(onPageScrollStateChanged = { onStateChange(state) })

  78. Extends addOnPageChangeListener pager.addOnPageChangeListener(onPageScrollStateChanged = { onStateChange(state) })

  79. Animator

  80. Animator(Java) anim.addListener(new AnimatorListener() { ... @Override public void onAnimationEnd(Animator animation)

    { animDrawable.stop() } ... }); anim.setDuration(300) .start();
  81. Animator fun Animator.addListener( onAnimationStart: (Animator) -> Unit = {}, onAnimationEnd:

    (Animator) -> Unit = {}, onAnimationCancel: (Animator) -> Unit = {}, onAnimationRepeat: (Animator) -> Unit = {}): Animator { addListener(object : Animator.AnimatorListener { override fun onAnimationStart(animation: Animator) { onAnimationStart(animation) } // ….. }) return this }
  82. Animator fun Animator.addListener( onAnimationStart: (Animator) -> Unit = {}, onAnimationEnd:

    (Animator) -> Unit = {}, onAnimationCancel: (Animator) -> Unit = {}, onAnimationRepeat: (Animator) -> Unit = {}): Animator { addListener(object : Animator.AnimatorListener { override fun onAnimationStart(animation: Animator) { onAnimationStart(animation) } // ….. }) return this }
  83. Animator fun Animator.addListener( onAnimationStart: (Animator) -> Unit = {}, onAnimationEnd:

    (Animator) -> Unit = {}, onAnimationCancel: (Animator) -> Unit = {}, onAnimationRepeat: (Animator) -> Unit = {}): Animator { addListener(object : Animator.AnimatorListener { override fun onAnimationStart(animation: Animator) { onAnimationStart(animation) } // ….. }) return this }
  84. Animator anim .addListener(onAnimationEnd = { animDrawable.stop() }) .setDuration(300) .start();

  85. Animator anim .addListener(onAnimationEnd = { animDrawable.stop() }) .setDuration(300) .start();

  86. Thank you !!