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)

Robin Caroff

November 23, 2017
Tweet

More Decks by Robin Caroff

Other Decks in Programming

Transcript

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

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

    ‣ Tools ‣ Practices ‣ Architectures ‣ Activity —> God object ‣ Code highly framework dependent ‣ Dependencies hell Android Known issues ‣ Lifecycle
  3. 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 !)
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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;
  10. 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; }
  11. 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 } }
  12. 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) }
  13. 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
  14. 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
  15. 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
  16. 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
  17. Tests Mockito 1 Error Mockito cannot mock/spy following: - final

    classes - anonymous classes - -primitive type
  18. 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() }