This document discusses key concepts for Android app development including:
- The repository layer for managing app data from both local databases and remote sources.
- Using the Room library as an ORM for local SQLite databases.
- Making network requests using Retrofit and Moshi to fetch and parse JSON API responses.
- Scheduling background tasks with WorkManager.
- Following Material Design principles and testing apps thoroughly.
Code samples and demos are provided to illustrate implementing these concepts.
WSO2's API Vision: Unifying Control, Empowering Developers
Android Study Jam 2021 Session 4 slides
1. This work is licensed under the Apache 2.0 License
Android Study Jams
Prior Programming Experience Track: Session 4
2. This work is licensed under the Apache 2.0 License
Prior Programming Experience Track: Session 4
Android Study Jams
3. This work is licensed under the Apache 2.0 License
Learning Objectives
० Learn about Repository Layer to manage Data
० Using WorkManager for scheduled tasks
० Design your app using Material Design
० Test your app
4. This work is licensed under the Apache 2.0 License
A little about me
5. This work is licensed under the Apache 2.0 License
Concept Overview
What is the Repository Layer?
6. This work is licensed under the Apache 2.0 License
Repository Modules handle data operations.
7. This work is licensed under the Apache 2.0 License
Repository Components
● Model (Database)
● Remote Data (Network)
● Provides API for accessing both
8. This work is licensed under the Apache 2.0 License
Why Repository
● Separation of Concerns
● Activity/Fragment are glue
● UI driven by data
● Testable and Consistent
9. This work is licensed under the Apache 2.0 License
Repository cont...
● DI vs Service Lookup
● View Models and Streams
● ORMs
10. This work is licensed under the Apache 2.0 License
Databases
● Caches
● Room Library - ORM
● Database Refresh Strategy
● Observables
11. This work is licensed under the Apache 2.0 License
Room
● @Entity @ColumnInfo
● @Dao
● @Insert, @Delete, and @Update
● @Query
12. This work is licensed under the Apache 2.0 License
Don’t forget to test!
13. This work is licensed under the Apache 2.0 License
@Dao
interface VideoDao {
@Query("select * from databasevideo")
fun getVideos(): LiveData<List<DatabaseVideo>>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll( videos: List<DatabaseVideo>)
}
@Database(entities = [DatabaseVideo::class], version = 1)
abstract class VideosDatabase: RoomDatabase() {
abstract val videoDao: VideoDao
}
private lateinit var INSTANCE: VideosDatabase
fun getDatabase(context: Context): VideosDatabase {
synchronized(VideosDatabase::class.java) {
if (!::INSTANCE.isInitialized) {
INSTANCE = Room.databaseBuilder(context.applicationContext,
VideosDatabase::class.java,
"videos").build()
}
}
return INSTANCE
}
Room.kt
14. This work is licensed under the Apache 2.0 License
@Entity
data class DatabaseVideo constructor(
@PrimaryKey
val url: String,
val updated: String,
val title: String,
val description: String,
val thumbnail: String)
/**
* Map DatabaseVideos to domain entities
*/
fun List<DatabaseVideo>.asDomainModel(): List<DevByteVideo> {
return map {
DevByteVideo(
url = it.url,
title = it.title,
description = it.description,
updated = it.updated,
thumbnail = it.thumbnail)
}
}
DatabaseEntities.kt
15. This work is licensed under the Apache 2.0 License
Network
● REST Services
● Retrofit Library - for API Calls
● Moshi - for JSON to Model
● Observables
16. This work is licensed under the Apache 2.0 License
interface DevbyteService {
@GET("devbytes")
suspend fun getPlaylist(): NetworkVideoContainer
}
object DevByteNetwork {
// Configure retrofit to parse JSON and use coroutines
private val retrofit = Retrofit.Builder()
.baseUrl("https://android-kotlin-fun-mars-server.appspot.com/")
.addConverterFactory(MoshiConverterFactory.create())
.build()
val devbytes = retrofit.create(DevbyteService::class.java)
}
Service.kt
17. This work is licensed under the Apache 2.0 License
@JsonClass(generateAdapter = true)
data class NetworkVideoContainer(val videos: List<NetworkVideo>)
@JsonClass(generateAdapter = true)
data class NetworkVideo(
val title: String,
val description: String,
val url: String,
val updated: String,
val thumbnail: String,
val closedCaptions: String?)
fun NetworkVideoContainer.asDomainModel(): List<DevByteVideo> {}
fun NetworkVideoContainer.asDatabaseModel(): List<DatabaseVideo> {}
DTO.kt
18. This work is licensed under the Apache 2.0 License
Work Manager
19. This work is licensed under the Apache 2.0 License
Work Manager
● For scheduling repeating tasks
● Define constraints
● Retry
● Chaining
● Threading
20. This work is licensed under the Apache 2.0 License
Material Design
21. This work is licensed under the Apache 2.0 License
Testing
22. This work is licensed under the Apache 2.0 License
Testing
23. This work is licensed under the Apache 2.0 License
Demos
24. This work is licensed under the Apache 2.0 License
Learn More
25. This work is licensed under the Apache 2.0 License
Want to learn more?
● Official Android Developers Site: https://developer.android.com/
● Android Samples on GitHub
● Official Android Developers Blog (for announcements)
● Android Developers Medium Blog (for more technical articles)
● Android Developers YouTube channel
● Follow @AndroidDev on Twitter
● Subscribe to the Android Developer Newsletter
● Kotlin Vocabulary series
● Official Kotlin language site
Version 1.0