-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix rx java dep, add part4, progress
- Loading branch information
Showing
4 changed files
with
126 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
dev_projects/RxJava/RxJava/src/main/java/com/yen/courseV1/part4.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package com.yen.courseV1; | ||
|
||
// https://youtu.be/EIwAerzQDvA?si=NQNL6Lgl9tc9qsU8 | ||
|
||
import io.reactivex.rxjava3.annotations.NonNull; | ||
import io.reactivex.rxjava3.core.Maybe; | ||
import io.reactivex.rxjava3.core.MaybeObserver; | ||
import io.reactivex.rxjava3.core.Single; | ||
import io.reactivex.rxjava3.core.SingleObserver; | ||
import io.reactivex.rxjava3.disposables.Disposable; | ||
|
||
|
||
public class part4 { | ||
public static void main(String[] args) { | ||
|
||
/** Example 1 : Single | ||
* | ||
* Single : ONLY emit once and ONLY one item, other situation treated as error | ||
*/ | ||
// Single<String>, NOTE how this different from regular Obervable | ||
Single<String> single = createSingle(); | ||
single.subscribe(new SingleObserver<String>(){ | ||
@Override | ||
public void onSubscribe(@NonNull Disposable d) { | ||
|
||
} | ||
|
||
@Override | ||
public void onSuccess(@NonNull String s) { | ||
System.out.println("(onSuccess) s = " + s); | ||
} | ||
|
||
@Override | ||
public void onError(@NonNull Throwable e) { | ||
System.out.println("(onError) = " + e.getMessage()); | ||
} | ||
}); | ||
|
||
/** Example 2 : Maybe | ||
* | ||
* Maybe : emit one or no item | ||
*/ | ||
|
||
// Maybe | ||
Maybe<String> maybe = createMaybe(); | ||
|
||
maybe.subscribe(new MaybeObserver<String>() { | ||
@Override | ||
public void onSubscribe(@NonNull Disposable d) { | ||
|
||
} | ||
|
||
@Override | ||
public void onSuccess(@NonNull String s) { | ||
System.out.println("(onSuccess) s = " + s); | ||
} | ||
|
||
@Override | ||
public void onError(@NonNull Throwable e) { | ||
System.out.println("(onError) = " + e.getMessage()); | ||
} | ||
|
||
@Override | ||
public void onComplete() { | ||
System.out.println("completed! no new content"); | ||
} | ||
}); | ||
|
||
} | ||
|
||
public static Single<String> createSingle(){ | ||
return Single.create(emitter -> { | ||
String user = fetchUser(); | ||
if (user != null){ | ||
emitter.onSuccess(user); | ||
}else{ | ||
emitter.onError(new Exception("user not found")); | ||
} | ||
}); | ||
} | ||
|
||
public static String fetchUser(){ | ||
//return "May"; | ||
return null; | ||
} | ||
|
||
public static Maybe createMaybe(){ | ||
return Maybe.create(emitter -> { | ||
String newContent = readFile(); | ||
if (newContent != null){ | ||
emitter.onSuccess(newContent); | ||
}else{ | ||
emitter.onComplete(); | ||
} | ||
}); | ||
} | ||
|
||
public static String readFile(){ | ||
//return "content of some file"; | ||
return null; | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters