Skip to content

Instantly share code, notes, and snippets.

View khunzohn's full-sized avatar
🏠
Working from home

khunzohn khunzohn

🏠
Working from home
  • BandLab
  • Myanmar
View GitHub Profile
class FetchMacsUseCase constructor(
private val productRepository: ProductRepository
) {
fun run(): Observable<MacsPartialState> {
return productRepository.fetchMacs()
.toObservable()
.map { MacsPartialState.MacsResult(it) }
.cast(MacsPartialState::class.java)
.startWith(MacsPartialState.LoadingMacs)
.onErrorReturn { error -> MacsPartialState.LoadMacsError(error) }
data class MovieResponse(
val errorCode: Int?,
val errorMessage: String?,
val results: List<MovieData>
)
viewModelScope.launch {
try {
val movies = movieService.getUpcomingMovies()
} catch (e: Exception) {
//...
}
}
interface MovieService {
@GET("movie/now_playing")
suspend fun getUpcomingMovies(@Query("api_key") apiKey: String): Response<MovieResponse>
}
interface MovieService {
@GET("movie/now_playing")
suspend fun getUpcomingMovies(@Query("api_key") apiKey: String): Response<MovieResponse>
}
public void getUser(String userId) {
getView().showLoading(“fetching user…”);
Subscription sub = userRepository.getUser(userId
.subscribeOn(Schedulers.io())
.observeOn(AndroidScheduler.mainThread())
.subscribe(user -> {
getView().stopLoading();
getView().render(user);
},e->{
@khunzohn
khunzohn / User.java
Last active January 23, 2018 18:27
Data object that contains States information
public final class User {
public final String id;
public final String name;
public final Throwable error; // the reason for failing to fetch will be stored here
public final State state; //either of SUCCESS,PROGRESS,ERROR enum
public User(Throwable error, State state, String id, String name) {
this.error = error;
this.state = state;
this.id = id;
override fun onCreate(savedInstanceState : Bundle?) {
setContentView(R.layout.activity_upload)
val digit = 2
val wordTwo: String = digit.mapToWord() // return "Two"
}
fun Int.mapToWord(): String {
val word: String
when (this) { // this refers to Int with its scope limited to the method level
0 -> word = "Zero"
1 -> word = "One"
2 -> word = "Two"
// 3,4,5,6,7,8,9
else -> word = "Invalid Digit Range"
}
return word
public class Utils {
public static String mapDigitToWord(int digit) throws DigitRangeException {
if(digit < 0 || digit > 9)
throw new DigitRangeException();
switch (digit) {
case 0: return "Zero";
case 1: return "One";
// case 2,3,...9
default: return "";