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
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) } |
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
data class MovieResponse( | |
val errorCode: Int?, | |
val errorMessage: String?, | |
val results: List<MovieData> | |
) |
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
viewModelScope.launch { | |
try { | |
val movies = movieService.getUpcomingMovies() | |
} catch (e: Exception) { | |
//... | |
} | |
} |
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
interface MovieService { | |
@GET("movie/now_playing") | |
suspend fun getUpcomingMovies(@Query("api_key") apiKey: String): Response<MovieResponse> | |
} |
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
interface MovieService { | |
@GET("movie/now_playing") | |
suspend fun getUpcomingMovies(@Query("api_key") apiKey: String): Response<MovieResponse> | |
} |
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
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->{ |
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
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; |
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
override fun onCreate(savedInstanceState : Bundle?) { | |
setContentView(R.layout.activity_upload) | |
val digit = 2 | |
val wordTwo: String = digit.mapToWord() // return "Two" | |
} |
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
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 |
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
public class DigitMapper { | |
public DigitMapper() { | |
} | |
public String mapToWord(int digit) throws DigitRangeException { | |
if (digit < 0 || digit > 9) throw new DigitRangeException(); | |
switch (digit) { |
NewerOlder