This file contains hidden or 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
// Request API data on IO Scheduler | |
service.getWeather("Berlin", getString(R.string.api_key)) | |
.subscribeOn(Schedulers.io()) | |
// Read results in Android Main Thread (UI) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(this::display, this::processError); |
This file contains hidden or 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
@GET("weather?units=metric") | |
Observable<WeatherResponse> getWeather(@Query("q") String city, | |
@Query("appid") String apiKey); |
This file contains hidden or 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
private Realm realmUI; | |
protected void onCreate(Bundle savedInstanceState) { | |
// ... | |
realmUI = Realm.getDefaultInstance(); | |
// ... | |
} | |
private WeatherRealm readFromRealm(String name) { | |
return findInRealm(realmUI, name); |
This file contains hidden or 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
private String writeToRealm(WeatherResponse weatherResponse) { | |
Realm realm = Realm.getDefaultInstance(); | |
realm.executeTransaction(transactionRealm -> { | |
WeatherRealm weatherRealm = findInRealm(transactionRealm, weatherResponse.getName()); | |
if (weatherRealm == null) | |
weatherRealm = transactionRealm.createObject(WeatherRealm.class, weatherResponse.getName()); | |
weatherRealm.setTemp(weatherResponse.getMain().getTemp()); | |
}); | |
realm.close(); | |
return weatherResponse.getName(); |
This file contains hidden or 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 WeatherRealm extends RealmObject { | |
@PrimaryKey | |
private String name; | |
private Double temp; | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { |
This file contains hidden or 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
String name = "Berlin"; | |
// Request API data on IO Scheduler | |
Observable<WeatherRealm> observable = | |
service.getWeather(name, getString(R.string.api_key)) | |
// One second delay for demo purposes | |
.delay(1L, java.util.concurrent.TimeUnit.SECONDS) | |
.subscribeOn(Schedulers.io()) | |
// Write to Realm on Computation scheduler | |
.observeOn(Schedulers.computation()) |
This file contains hidden or 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 MainActivity extends AppCompatActivity { | |
EditText editText; | |
ImageButton imageButton; | |
BooksAdapter adapter; | |
ListView listView; | |
TextView textNoDataFound; | |
GoogleBooksService service; | |
@Override |
This file contains hidden or 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 interface BooksInteractor { | |
Call<BookSearchResult> search(String search); | |
} |
This file contains hidden or 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 BooksPresenter { | |
BooksView view; | |
private BooksInteractor interactor; | |
public BooksPresenter(BooksInteractor interactor) { | |
this.interactor = interactor; | |
} | |
public void bind(BooksView view) { |
This file contains hidden or 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 interface BooksView { | |
void updateUi(List<Book> books); | |
} |