Skip to content
This repository was archived by the owner on Aug 16, 2023. It is now read-only.

Commit 37f5583

Browse files
committed
Add Advanced Coroutines codelab code
1 parent be5c1b2 commit 37f5583

File tree

89 files changed

+3031
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+3031
-0
lines changed
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.gradle
2+
build/
3+
gradle
4+
.idea
5+
*.iml
6+
local.properties

advanced-coroutines-codelab/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Will it blend?
2+
Quite well
+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// Top-level build file where you can add configuration options common to all sub-projects/modules.
2+
3+
buildscript {
4+
ext.kotlin_version = '1.3.50'
5+
6+
ext {
7+
8+
// global variables
9+
compile_sdk_version = 29
10+
kotlin_version = '1.3.60'
11+
min_sdk_version = 21
12+
13+
// local variables (use def)
14+
def androidx_test_version = '1.2.0'
15+
def annotations_version = '1.1.0'
16+
def appcompat_version = '1.1.0'
17+
def constraint_layout_version = '1.1.3'
18+
def coroutines_android_version = '1.3.0'
19+
def expresso_version = '3.2.0'
20+
def glide_version = '4.10.0'
21+
def gson_version = '2.8.6'
22+
def junit_version = '4.12'
23+
def lifecycle_version = '2.1.0'
24+
def livedata_version = '2.2.0-rc02'
25+
def material_version = '1.1.0-alpha09'
26+
def retrofit_gson_version = '2.6.2'
27+
def retrofit_version = '2.6.2'
28+
def room_version = '2.2.1'
29+
def truth_version = '1.0'
30+
def work_version = '2.2.0'
31+
32+
libraries = [
33+
// Kotlin standard library
34+
"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version",
35+
36+
// Coroutines
37+
"org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_android_version",
38+
"org.jetbrains.kotlinx:kotlinx-coroutines-android:${coroutines_android_version}",
39+
40+
// Android UI and appcompat
41+
"androidx.appcompat:appcompat:$appcompat_version",
42+
"com.google.android.material:material:$material_version",
43+
"androidx.constraintlayout:constraintlayout:$constraint_layout_version",
44+
"androidx.fragment:fragment-ktx:1.1.0",
45+
46+
// Glide
47+
"com.github.bumptech.glide:glide:$glide_version",
48+
49+
// ViewModel and LiveData
50+
"androidx.lifecycle:lifecycle-extensions:$lifecycle_version",
51+
"androidx.lifecycle:lifecycle-livedata-ktx:$livedata_version",
52+
"androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version",
53+
54+
// network & serialization
55+
"com.google.code.gson:gson:$gson_version",
56+
"com.squareup.retrofit2:converter-gson:$retrofit_gson_version",
57+
"com.squareup.retrofit2:retrofit:$retrofit_version",
58+
59+
// threading
60+
"androidx.annotation:annotation:$annotations_version"
61+
]
62+
63+
arch_libraries = [
64+
"androidx.work:work-runtime-ktx:$work_version",
65+
66+
// Room for database
67+
"androidx.room:room-ktx:$room_version"
68+
]
69+
70+
librariesKapt = [
71+
"androidx.room:room-compiler:$room_version",
72+
"com.github.bumptech.glide:compiler:$glide_version"
73+
]
74+
75+
librariesDebug = [
76+
"com.google.truth:truth:$truth_version"
77+
]
78+
79+
testLibraries = [
80+
"junit:junit:$junit_version",
81+
// Coroutines testing
82+
"org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutines_android_version",
83+
84+
// mocks
85+
"org.mockito:mockito-core:2.23.0",
86+
87+
// Architecture Components testing libraries
88+
"androidx.arch.core:core-testing:$lifecycle_version"
89+
]
90+
91+
androidTestLibraries = [
92+
"junit:junit:$junit_version",
93+
"androidx.test:runner:$androidx_test_version",
94+
"androidx.test:rules:$androidx_test_version",
95+
96+
// Espresso
97+
"androidx.test.espresso:espresso-core:$expresso_version",
98+
"androidx.test.espresso:espresso-contrib:$expresso_version",
99+
"androidx.test.espresso:espresso-intents:$expresso_version",
100+
101+
// Architecture Components testing libraries
102+
"androidx.arch.core:core-testing:$lifecycle_version",
103+
"androidx.work:work-testing:$work_version",
104+
105+
]
106+
}
107+
108+
repositories {
109+
google()
110+
jcenter()
111+
}
112+
dependencies {
113+
classpath 'com.android.tools.build:gradle:3.5.2'
114+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
115+
// NOTE: Do not place your application dependencies here; they belong
116+
// in the individual module build.gradle files
117+
}
118+
}
119+
120+
allprojects {
121+
repositories {
122+
google()
123+
jcenter()
124+
}
125+
}
126+
127+
task clean(type: Delete) {
128+
delete rootProject.buildDir
129+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
apply plugin: 'com.android.application'
2+
3+
apply plugin: 'kotlin-android'
4+
5+
apply plugin: 'kotlin-android-extensions'
6+
7+
apply plugin: 'kotlin-kapt'
8+
9+
android {
10+
compileSdkVersion compile_sdk_version
11+
dataBinding {
12+
enabled = true
13+
}
14+
defaultConfig {
15+
applicationId "com.example.android.advancedcoroutines"
16+
minSdkVersion min_sdk_version
17+
targetSdkVersion compile_sdk_version
18+
versionCode 1
19+
versionName "1.0"
20+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
21+
}
22+
buildTypes {
23+
release {
24+
minifyEnabled true
25+
}
26+
}
27+
compileOptions {
28+
sourceCompatibility = 1.8
29+
targetCompatibility = 1.8
30+
}
31+
kotlinOptions {
32+
jvmTarget = "1.8"
33+
}
34+
}
35+
36+
dependencies {
37+
// dependencies are defined in ext arrays in coroutines-codelab/build.gradle
38+
implementation project(':sunflower')
39+
40+
implementation libraries
41+
implementation arch_libraries
42+
kapt librariesKapt
43+
debugImplementation librariesDebug
44+
45+
testImplementation testLibraries
46+
androidTestImplementation androidTestLibraries
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.example.android.advancedcoroutines">
4+
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
7+
<application
8+
android:allowBackup="true"
9+
android:icon="@mipmap/ic_launcher"
10+
android:label="@string/app_name"
11+
android:roundIcon="@mipmap/ic_launcher_round"
12+
android:supportsRtl="true"
13+
android:theme="@style/Theme.Sunflower">
14+
<activity android:name=".ui.MainActivity">
15+
<intent-filter>
16+
<action android:name="android.intent.action.MAIN" />
17+
18+
<category android:name="android.intent.category.LAUNCHER" />
19+
</intent-filter>
20+
</activity>
21+
</application>
22+
23+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.example.android.advancedcoroutines
2+
3+
import kotlinx.coroutines.Dispatchers
4+
import kotlinx.coroutines.delay
5+
import kotlinx.coroutines.withContext
6+
import okhttp3.OkHttpClient
7+
import retrofit2.Retrofit
8+
import retrofit2.converter.gson.GsonConverterFactory
9+
import retrofit2.http.GET
10+
11+
class NetworkService {
12+
13+
private val retrofit = Retrofit.Builder()
14+
.baseUrl("https://raw.githubusercontent.com/")
15+
.client(OkHttpClient())
16+
.addConverterFactory(GsonConverterFactory.create())
17+
.build()
18+
19+
private val retrofitGist = Retrofit.Builder()
20+
.baseUrl("https://gist.githubusercontent.com/")
21+
.client(OkHttpClient())
22+
.addConverterFactory(GsonConverterFactory.create())
23+
.build()
24+
25+
private val sunflowerService = retrofit.create(SunflowerService::class.java)
26+
private val sunflowerServiceGist = retrofitGist.create(SunflowerService::class.java)
27+
28+
suspend fun allPlants(): List<Plant> = withContext(Dispatchers.Default) {
29+
delay(1500)
30+
val result = sunflowerService.getAllPlants()
31+
result.shuffled()
32+
}
33+
34+
suspend fun plantsByGrowZone(growZone: GrowZone) = withContext(Dispatchers.Default) {
35+
delay(1500)
36+
val result = sunflowerService.getAllPlants()
37+
result.filter { it.growZoneNumber == growZone.number }.shuffled()
38+
}
39+
40+
suspend fun customPlantSortOrder(): List<String> = withContext(Dispatchers.Default) {
41+
val result = sunflowerServiceGist.getCustomPlantSortOrder()
42+
result.map { plant -> plant.plantId }
43+
}
44+
}
45+
46+
interface SunflowerService {
47+
@GET("android/sunflower/master/app/src/main/assets/plants.json")
48+
suspend fun getAllPlants() : List<Plant>
49+
50+
@GET("tiembo/0829013ac578e60c87d7c7f2fb89d6a5/raw/fc98374a3d1a5c6f5df40e9fb3e1a03e6f540d86/custom_plant_sort_order.json")
51+
suspend fun getCustomPlantSortOrder() : List<Plant>
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.android.advancedcoroutines
18+
19+
import androidx.lifecycle.LiveData
20+
import androidx.room.Dao
21+
import androidx.room.Insert
22+
import androidx.room.OnConflictStrategy
23+
import androidx.room.Query
24+
import kotlinx.coroutines.flow.Flow
25+
26+
/**
27+
* The Data Access Object for the Plant class.
28+
*/
29+
@Dao
30+
interface PlantDao {
31+
@Query("SELECT * FROM plants ORDER BY name")
32+
fun getPlants(): LiveData<List<Plant>>
33+
34+
@Query("SELECT * from plants ORDER BY name")
35+
fun getPlantsFlow(): Flow<List<Plant>>
36+
37+
@Query("SELECT * FROM plants WHERE growZoneNumber = :growZoneNumber ORDER BY name")
38+
fun getPlantsWithGrowZoneNumber(growZoneNumber: Int): LiveData<List<Plant>>
39+
40+
@Query("SELECT * from plants WHERE growZoneNumber = :growZoneNumber ORDER BY name")
41+
fun getPlantsWithGrowZoneNumberFlow(growZoneNumber: Int): Flow<List<Plant>>
42+
43+
@Query("SELECT * FROM plants WHERE id = :plantId")
44+
fun getPlant(plantId: String): LiveData<Plant>
45+
46+
@Insert(onConflict = OnConflictStrategy.REPLACE)
47+
suspend fun insertAll(plants: List<Plant>)
48+
49+
@Query("delete from plants")
50+
suspend fun deleteAll()
51+
}

0 commit comments

Comments
 (0)