Skip to content

Commit 898f088

Browse files
committed
lecture 11 RxJava3
1 parent 00f5a03 commit 898f088

Some content is hidden

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

50 files changed

+1005
-359
lines changed

.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
root = true
2+
[*]
3+
indent_size=2
4+
end_of_line=lf
5+
charset=utf-8
6+
trim_trailing_whitespace=true
7+
insert_final_newline=true
8+
[*.{kt,kts}]
9+
ij_kotlin_imports_layout=*
10+
[*.xml]
11+
indent_size=4
12+
[*.java]
13+
indent_size=4

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ repositories {
1515
dependencies {
1616
testImplementation(kotlin("test"))
1717
implementation("org.jetbrains:annotations:24.0.1")
18+
implementation("io.reactivex.rxjava3:rxjava:3.1.8")
1819
}
1920

2021
tasks.test {

src/main/kotlin/Main.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
fun main(args: Array<String>) {
2-
println("Hello World!")
2+
println("Hello World!")
33

4-
// Try adding program arguments via Run/Debug configuration.
5-
// Learn more about running applications: https://www.jetbrains.com/help/idea/running-applications.html.
6-
println("Program arguments: ${args.joinToString()}")
7-
}
4+
// Try adding program arguments via Run/Debug configuration.
5+
// Learn more about running applications: https://www.jetbrains.com/help/idea/running-applications.html.
6+
println("Program arguments: ${args.joinToString()}")
7+
}

src/main/kotlin/com/rxmobileteam/lecture1/Exercise1Main.java

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,37 @@ public static void main(String[] args) {
99
ProductService productService = new ProductServiceFactory().createProductService();
1010

1111
Product iPhone12 = new Product(
12-
"1",
13-
"iPhone 12",
14-
"The iPhone 12 and iPhone 12 Mini (stylized as iPhone 12 mini) are smartphones designed, developed, and marketed by Apple Inc. " +
15-
"They are the fourteenth-generation, lower-priced iPhones, succeeding the iPhone 11. " +
16-
"They were unveiled at an Apple Special Event at Apple Park in Cupertino, California on October 13, 2020 " +
17-
"alongside the higher-end iPhone 12 Pro and iPhone 12 Pro Max flagships.",
18-
799.99
12+
"1",
13+
"iPhone 12",
14+
"The iPhone 12 and iPhone 12 Mini (stylized as iPhone 12 mini) are smartphones designed, developed, and marketed by Apple Inc. " +
15+
"They are the fourteenth-generation, lower-priced iPhones, succeeding the iPhone 11. " +
16+
"They were unveiled at an Apple Special Event at Apple Park in Cupertino, California on October 13, 2020 " +
17+
"alongside the higher-end iPhone 12 Pro and iPhone 12 Pro Max flagships.",
18+
799.99
1919
);
2020
Product iPhone12Pro = new Product(
21-
"2",
22-
"iPhone 12 Pro",
23-
"The iPhone 12 Pro and iPhone 12 Pro Max are smartphones designed, developed, and marketed by Apple Inc. " +
24-
"They are the flagship smartphones in the fourteenth-generation iPhone lineup, succeeding the iPhone 11 Pro and iPhone 11 Pro Max, " +
25-
"and were unveiled on October 13, 2020, at Apple Park in Cupertino, California, alongside the lower-end iPhone 12 and iPhone 12 Mini.",
26-
999.99
21+
"2",
22+
"iPhone 12 Pro",
23+
"The iPhone 12 Pro and iPhone 12 Pro Max are smartphones designed, developed, and marketed by Apple Inc. " +
24+
"They are the flagship smartphones in the fourteenth-generation iPhone lineup, succeeding the iPhone 11 Pro and iPhone 11 Pro Max, " +
25+
"and were unveiled on October 13, 2020, at Apple Park in Cupertino, California, alongside the lower-end iPhone 12 and iPhone 12 Mini.",
26+
999.99
2727
);
2828
Product samsungGalaxyS21 = new Product(
29-
"3",
30-
"Samsung Galaxy S21",
31-
"The Samsung Galaxy S21 is a series of Android-based smartphones designed, developed, marketed, and manufactured by Samsung Electronics as part of its Galaxy S series. " +
32-
"They collectively serve as the successor to the Galaxy S20 series. The S21 series is the first Galaxy S lineup to support the S Pen stylus, " +
33-
"which is only compatible with the Ultra model.",
34-
799.99
29+
"3",
30+
"Samsung Galaxy S21",
31+
"The Samsung Galaxy S21 is a series of Android-based smartphones designed, developed, marketed, and manufactured by Samsung Electronics as part of its Galaxy S series. " +
32+
"They collectively serve as the successor to the Galaxy S20 series. The S21 series is the first Galaxy S lineup to support the S Pen stylus, " +
33+
"which is only compatible with the Ultra model.",
34+
799.99
3535
);
3636
Product samsungGalaxyS21Ultra = new Product(
37-
"4",
38-
"Samsung Galaxy S21 Ultra",
39-
"The Samsung Galaxy S21 Ultra is a series of Android-based smartphones designed, developed, marketed, and manufactured by Samsung Electronics as part of its Galaxy S series. " +
40-
"They collectively serve as the successor to the Galaxy S20 series. The S21 series is the first Galaxy S lineup to support the S Pen stylus, " +
41-
"which is only compatible with the Ultra model.",
42-
1199.99
37+
"4",
38+
"Samsung Galaxy S21 Ultra",
39+
"The Samsung Galaxy S21 Ultra is a series of Android-based smartphones designed, developed, marketed, and manufactured by Samsung Electronics as part of its Galaxy S series. " +
40+
"They collectively serve as the successor to the Galaxy S20 series. The S21 series is the first Galaxy S lineup to support the S Pen stylus, " +
41+
"which is only compatible with the Ultra model.",
42+
1199.99
4343
);
4444

4545
productService.addProduct(iPhone12);

src/main/kotlin/com/rxmobileteam/lecture1/data/ProductDao.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ public Set<Product> findAll() {
3838
throw new ExerciseNotCompletedException();
3939
}
4040

41-
}
41+
}

src/main/kotlin/com/rxmobileteam/lecture1/factory/ProductServiceFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ public ProductService createProductService() {
1919
// TODO: implement this method
2020
throw new ExerciseNotCompletedException();
2121
}
22-
}
22+
}

src/main/kotlin/com/rxmobileteam/lecture1/service/Product.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ public class Product {
1212
private final double price;
1313

1414
public Product(
15-
@NotNull String id,
16-
@NotNull String name,
17-
@NotNull String description,
18-
double price
15+
@NotNull String id,
16+
@NotNull String name,
17+
@NotNull String description,
18+
double price
1919
) {
2020
this.id = id;
2121
this.name = name;
@@ -51,10 +51,10 @@ public int hashCode() {
5151
@Override
5252
public String toString() {
5353
return "Product{" +
54-
"id='" + id + '\'' +
55-
", name='" + name + '\'' +
56-
", description='" + description + '\'' +
57-
", price=" + price +
58-
'}';
54+
"id='" + id + '\'' +
55+
", name='" + name + '\'' +
56+
", description='" + description + '\'' +
57+
", price=" + price +
58+
'}';
5959
}
6060
}

src/main/kotlin/com/rxmobileteam/lecture1/service/ProductService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ public List<Product> searchProducts(String query) {
3535
// TODO: implement this method
3636
throw new ExerciseNotCompletedException();
3737
}
38-
}
38+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.rxmobileteam.lecture11
2+
3+
import com.rxmobileteam.utils.ExerciseNotCompletedException
4+
import io.reactivex.rxjava3.core.Observable
5+
6+
object Riddle1 {
7+
/**
8+
* Transform the given [value] into an Observable that emits the value and then completes.
9+
*
10+
* Use case: You want to transform some value to the reactive world.
11+
*/
12+
fun solve(value: Int): Observable<Int> {
13+
// TODO: implement this method
14+
throw ExerciseNotCompletedException()
15+
}
16+
}
17+
18+
fun main() {
19+
Riddle1.solve(5)
20+
.test()
21+
.assertResult(5)
22+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.rxmobileteam.lecture11
2+
3+
import com.rxmobileteam.utils.ExerciseNotCompletedException
4+
import io.reactivex.rxjava3.core.Observable
5+
6+
object Riddle10 {
7+
/**
8+
* Use the [first] Observable and flatten it with the results of the [function] that returns an Observable.
9+
*
10+
* Use case: Get some user data and perform a network request with the user data and have both data accessible afterwards.
11+
*/
12+
fun solve(first: Observable<Int>, function: (Int) -> Observable<String>): Observable<Pair<Int, String>> {
13+
// TODO: implement this method
14+
throw ExerciseNotCompletedException()
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.rxmobileteam.lecture11
2+
3+
import com.rxmobileteam.utils.ExerciseNotCompletedException
4+
import io.reactivex.rxjava3.core.Observable
5+
6+
object Riddle11 {
7+
/**
8+
* Let the first emission of the [source] within a time window of 300ms travel downstream but don't emit any other events until the next time window.
9+
*
10+
* Use case: Handle the click of a button right away but prevent double clicking by not handling multiple click events within a given time window.
11+
*/
12+
fun solve(source: Observable<Unit>): Observable<Unit> {
13+
// TODO: implement this method
14+
throw ExerciseNotCompletedException()
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.rxmobileteam.lecture11
2+
3+
import com.rxmobileteam.utils.ExerciseNotCompletedException
4+
import io.reactivex.rxjava3.core.Observable
5+
6+
object Riddle12 {
7+
/**
8+
* In case the [source] Observable emits an error, don't emit the error and instead complete the Observable with a value of 5.
9+
*
10+
* Use case: Getting a network error and you want to recover and show some default state.
11+
*/
12+
fun solve(source: Observable<Int>): Observable<Int> {
13+
// TODO: implement this method
14+
throw ExerciseNotCompletedException()
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.rxmobileteam.lecture11
2+
3+
import com.rxmobileteam.utils.ExerciseNotCompletedException
4+
import io.reactivex.rxjava3.core.Observable
5+
6+
object Riddle13 {
7+
/**
8+
* When the [source] emits the same value as it did last time, don't allow it to travel downstream.
9+
*
10+
* Use case: You only want to observe changes of a value but don't care if the same value has been emitted consecutively.
11+
*/
12+
fun solve(source: Observable<Int>): Observable<Int> {
13+
// TODO: implement this method
14+
throw ExerciseNotCompletedException()
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.rxmobileteam.lecture11
2+
3+
import com.rxmobileteam.utils.ExerciseNotCompletedException
4+
import io.reactivex.rxjava3.core.Single
5+
6+
object Riddle14 {
7+
/**
8+
* Try the given [source] up to three times unless an [IllegalArgumentException] has been emitted.
9+
*
10+
* Use case: Retry an operation for a number of times or until a valid error occurred.
11+
*/
12+
fun solve(source: Single<Unit>): Single<Unit> {
13+
// TODO: implement this method
14+
throw ExerciseNotCompletedException()
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.rxmobileteam.lecture11
2+
3+
import com.rxmobileteam.utils.ExerciseNotCompletedException
4+
import io.reactivex.rxjava3.core.Observable
5+
6+
object Riddle15 {
7+
/**
8+
* Concatenate the [first] Observable with the [second] while subscribing to both early.
9+
*
10+
* Use case: You have two sources of your data (cache & network request). You want to subscribe to both right away and keep the emission order.
11+
*/
12+
fun solve(first: Observable<Int>, second: Observable<Int>): Observable<Int> {
13+
// TODO: implement this method
14+
throw ExerciseNotCompletedException()
15+
}
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.rxmobileteam.lecture11
2+
3+
import com.rxmobileteam.utils.ExerciseNotCompletedException
4+
import io.reactivex.rxjava3.core.Observable
5+
import io.reactivex.rxjava3.core.Single
6+
7+
object Riddle16 {
8+
/**
9+
* For each emission of the [source] Observable use the [function] and return its value.
10+
* Dispose all previously non-terminated returned Singles from the [function] upon receiving a new emission from [source].
11+
*
12+
* Use case: The [source] Observable is a TextField and you want to issue a network request while disposing the old requests in case the user has typed something new.
13+
*/
14+
fun solve(source: Observable<String>, function: (String) -> Single<Int>): Observable<Int> {
15+
// TODO: implement this method
16+
throw ExerciseNotCompletedException()
17+
}
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.rxmobileteam.lecture11
2+
3+
import com.rxmobileteam.utils.ExerciseNotCompletedException
4+
import io.reactivex.rxjava3.core.Single
5+
6+
object Riddle17 {
7+
/**
8+
* Return a Single that emits the value from the given [function] when being subscribed to.
9+
*
10+
* Use case: Reactive types are lazy by default. Hence, you might also want to get the value upon the subscription and not execution time.
11+
*/
12+
fun solve(function: () -> Int): Single<Int> {
13+
// TODO: implement this method
14+
throw ExerciseNotCompletedException()
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.rxmobileteam.lecture11
2+
3+
import com.rxmobileteam.utils.ExerciseNotCompletedException
4+
import io.reactivex.rxjava3.core.Observable
5+
6+
object Riddle18 {
7+
/**
8+
* Return an Observable that mirrors either the [first] or [second] Observable depending on whoever emits or terminates first.
9+
*
10+
* Use case: You have multiple sources and want to get the data from either one and then be consistent and not switch between multiple sources.
11+
*/
12+
fun solve(first: Observable<Int>, second: Observable<Int>): Observable<Int> {
13+
// TODO: implement this method
14+
throw ExerciseNotCompletedException()
15+
}
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.rxmobileteam.lecture11
2+
3+
import com.rxmobileteam.utils.ExerciseNotCompletedException
4+
import io.reactivex.rxjava3.core.Observable
5+
6+
object Riddle19 {
7+
/**
8+
* Use the given [Interaction] interface and use its listener to transform the [Int] callback to an Observable that emits every time the listener is called.
9+
* When the Observable is being disposed the listener should be set to null.
10+
*
11+
* Use case: Transform any listener into an Observable.
12+
*/
13+
fun solve(interaction: Interaction): Observable<Int> {
14+
// TODO: implement this method
15+
throw ExerciseNotCompletedException()
16+
}
17+
18+
interface Interaction {
19+
var listener: ((Int) -> Unit)?
20+
}
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.rxmobileteam.lecture11
2+
3+
import com.rxmobileteam.utils.ExerciseNotCompletedException
4+
import io.reactivex.rxjava3.core.Observable
5+
6+
object Riddle2 {
7+
/**
8+
* Increment each emitted value of the given [source] by 1.
9+
*
10+
* Use case: You want to transform the data.
11+
*/
12+
fun solve(source: Observable<Int>): Observable<Int> {
13+
// TODO: implement this method
14+
throw ExerciseNotCompletedException()
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.rxmobileteam.lecture11
2+
3+
import com.rxmobileteam.utils.ExerciseNotCompletedException
4+
import io.reactivex.rxjava3.core.Observable
5+
6+
object Riddle20 {
7+
/**
8+
* Merge the [first] and [second] Observable together.
9+
*
10+
* Use case: There something you want to execute and in your UI you have multiple trigger points.
11+
*/
12+
fun solve(first: Observable<Int>, second: Observable<Int>): Observable<Int> {
13+
// TODO: implement this method
14+
throw ExerciseNotCompletedException()
15+
}
16+
}

0 commit comments

Comments
 (0)