Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Feedback: How I brought Kotlin into my Room (short)

Feedback: How I brought Kotlin into my Room (short)

7352e4e763e3b54ac7a91f013dc7c709?s=128

Robin Caroff

November 23, 2017
Tweet

More Decks by Robin Caroff

Other Decks in Programming

Transcript

  1. HOW I BROUGHT KOTLIN INTO MY ROOM FEEDBACK Robin Caroff

    @RobinCaroff robin.caroff@gmail.com
  2. Previously in Android…

  3. Google announced Android Architecture Components

  4. and…

  5. Kotlin support as an official Android language

  6. Context

  7. Android Known issues ‣ Lifecycle

  8. Android Known issues ‣ Lifecycle

  9. ‣ Activity —> God object ‣ Code highly framework dependent

    ‣ Dependencies hell Android Known issues ‣ Lifecycle
  10. Power : Its community ‣ Libraries ‣ Blogs, conferences, etc.

    ‣ Tools ‣ Practices ‣ Architectures ‣ Activity —> God object ‣ Code highly framework dependent ‣ Dependencies hell Android Known issues ‣ Lifecycle
  11. Librairies

  12. Librairies Architecture and

  13. Architecture Model View Presenter

  14. What about the Architecture Components ?

  15. Architecture Components View Model Live Data Lifecycles Room Paging Library

  16. Architecture Components View Model Live Data Lifecycles Room Paging Library

  17. Architecture Components View Model Live Data Lifecycles Room Paging Library

  18. Architecture Components View Model Live Data Lifecycles Room Paging Library

  19. Architecture Components View Model Live Data Lifecycles Room Paging Library

  20. Let’s get started !

  21. Let’s get started ! Requirements Kotlin —> Android Studio 3.0

    (now in stable channel) Architecture Components —> Google Maven repository (1.0.0 now stable as well !)
  22. Let’s get started ! Kotlin —> Android Studio 3.0 (now

    in stable channel) Architecture Components —> Google Maven repository (1.0.0 now stable as well !) Requirements
  23. Let’s get started ! Kotlin —> Android Studio 3.0 (now

    in stable channel) Architecture Components —> Google Maven repository (1.0.0 now stable as well !) Requirements References
  24. Let’s get started ! Kotlin —> Android Studio 3.0 (now

    in stable channel) Architecture Components —> Google Maven repository (1.0.0 now stable as well !) Requirements References Books Talks / Podcasts Code
  25. Let’s get started ! Kotlin —> Android Studio 3.0 (now

    in stable channel) Architecture Components —> Google Maven repository (1.0.0 now stable as well !) Requirements References Books Talks / Podcasts Code
  26. Let’s get started ! Kotlin —> Android Studio 3.0 (now

    in stable channel) Architecture Components —> Google Maven repository (1.0.0 now stable as well !) Requirements References Books Talks / Podcasts Code
  27. References Code Look at the generated Java code !

  28. References Code Look at the generated Java code !

  29. References Code Look at the generated Java code ! weightsChart

    = this.chart_weights WeightsChart var3 = (WeightsChart)this._$_findCachedViewById(id.chart_weights); Intrinsics.checkExpressionValueIsNotNull(var3, "this.chart_weights"); this.weightsChart = var3;
  30. References Code Look at the generated Java code ! public

    android.view.View _$_findCachedViewById(int var1) { if(this._$_findViewCache == null) { this._$_findViewCache = new HashMap(); } android.view.View var2 = (android.view.View)this._$_findViewCache.get(Integer.valueOf(var1)); if(var2 == null) { var2 = this.findViewById(var1); this._$_findViewCache.put(Integer.valueOf(var1), var2); } return var2; }
  31. References Code Android Architecture Blueprints https://github.com/googlesamples/android- architecture

  32. Room

  33. Room ORM: Object Relational Mapping

  34. Room ORM: Object Relational Mapping Uses annotations

  35. Room ORM: Object Relational Mapping Uses annotations Uses SQL syntax

  36. Room ORM: Object Relational Mapping Uses annotations Uses SQL syntax

    You can provide type converters class DateConverter { @TypeConverter fun toDate(timestamp: Long?): Date? { return if (timestamp == null) null else Date(timestamp) } @TypeConverter fun toTimestamp(date: Date?): Long? { return date?.time } }
  37. Room ORM: Object Relational Mapping Uses annotations Uses SQL syntax

    You can provide type converters Offers conflict resolution strategies @Dao interface UserDao { @Query("select * from user") fun loadAllUsers(): Single<List<User>> @Insert(onConflict = REPLACE) fun insertUser(user: User) }
  38. Room ORM: Object Relational Mapping Uses annotations Uses SQL syntax

    You can provide type converters Even better in Kotlin @Entity @TypeConverters(DateConverter::class) class Weight { @ColumnInfo(name = "kilogramsValue") var kilogramsValue: Float = 0.0f @ColumnInfo(name = "date") @PrimaryKey(autoGenerate = false) var date: Date? = null } Offers conflict resolution strategies
  39. Room ORM: Object Relational Mapping Uses annotations Uses SQL syntax

    You can provide type converters @Dao interface UserDao { @Query("select * from user") fun loadAllUsers(): Single<List<User>> @Query("select * from user where id = :arg0") fun loadUserById(id: Int): Maybe<User> } Even better in Kotlin Offers conflict resolution strategies *ish
  40. Room ORM: Object Relational Mapping Uses annotations Uses SQL syntax

    You can provide type converters @Dao @TypeConverters(DateConverter::class) interface WeightDao { @Query("select * from weight") fun loadAllWeights(): Flowable<List<Weight>> @Query("select * from weight where date = :arg0") fun loadWeightByDate(date: Date): Maybe<Weight> } Even better in Kotlin Offers conflict resolution strategies Compatible with Live Data or RxJava *ish
  41. Room ORM: Object Relational Mapping Uses annotations Uses SQL syntax

    You can provide type converters Offers conflict resolution strategies Compatible with Live Data or RxJava Threads, Benchmarks, etc. ? Even better in Kotlin *ish
  42. Tests

  43. Tests Mockito 1 Error Mockito cannot mock/spy following: - final

    classes - anonymous classes - -primitive type
  44. Tests Mockito 2 Error Mockito cannot mock/spy following: - final

    classes
  45. Tests Mockito 2 Error Mockito cannot mock/spy following: - final

    classes Solution : mock-maker-inline
  46. Tests Room Test migrations

  47. Tests Room Test migrations Test Database with in-memory database

  48. Tests Room Test migrations Test Database with in-memory database

  49. Tests Room Test migrations Test Database with in-memory database

  50. Tests Room Test migrations Test Database with in-memory database Test

    RxJava flowable queries @get:Rule var instantTaskExecutorRule = InstantTaskExecutorRule() @Before fun initDb() { database = Room.inMemoryDatabaseBuilder(InstrumentationRegistry.getContext(), UsersDatabase::class.java) // allowing main thread queries, just for testing .allowMainThreadQueries() .build() }
  51. What’s next ?

  52. Take away