Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

khunzohn khunzohn

🏠
Working from home
  • Codigo
  • Myanmar
View GitHub Profile
View Clean.kt
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) }
View MovieResponse.kt
data class MovieResponse(
val errorCode: Int?,
val errorMessage: String?,
val results: List<MovieData>
)
View MovieViewModel.kt
viewModelScope.launch {
try {
val movies = movieService.getUpcomingMovies()
} catch (e: Exception) {
//...
}
}
View MovieService.kt
interface MovieService {
@GET("movie/now_playing")
suspend fun getUpcomingMovies(@Query("api_key") apiKey: String): Response<MovieResponse>
}
View MovieService.kt
interface MovieService {
@GET("movie/now_playing")
suspend fun getUpcomingMovies(@Query("api_key") apiKey: String): Response<MovieResponse>
}
View UserPresenter.java
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 Jan 23, 2018
Data object that contains States information
View User.java
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;
View MainActivity.kt
override fun onCreate(savedInstanceState : Bundle?) {
setContentView(R.layout.activity_upload)
val digit = 2
val wordTwo: String = digit.mapToWord() // return "Two"
}
View Util.kt
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
View DigitMapper.java
public class DigitMapper {
public DigitMapper() {
}
public String mapToWord(int digit) throws DigitRangeException {
if (digit < 0 || digit > 9) throw new DigitRangeException();
switch (digit) {