Skip to content

Instantly share code, notes, and snippets.

View miquelbeltran's full-sized avatar

Miguel Beltran miquelbeltran

View GitHub Profile
// 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);
@GET("weather?units=metric")
Observable<WeatherResponse> getWeather(@Query("q") String city,
@Query("appid") String apiKey);
private Realm realmUI;
protected void onCreate(Bundle savedInstanceState) {
// ...
realmUI = Realm.getDefaultInstance();
// ...
}
private WeatherRealm readFromRealm(String name) {
return findInRealm(realmUI, name);
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();
public class WeatherRealm extends RealmObject {
@PrimaryKey
private String name;
private Double temp;
public String getName() {
return name;
}
public void setName(String name) {
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())
public class MainActivity extends AppCompatActivity {
EditText editText;
ImageButton imageButton;
BooksAdapter adapter;
ListView listView;
TextView textNoDataFound;
GoogleBooksService service;
@Override
public interface BooksInteractor {
Call<BookSearchResult> search(String search);
}
public class BooksPresenter {
BooksView view;
private BooksInteractor interactor;
public BooksPresenter(BooksInteractor interactor) {
this.interactor = interactor;
}
public void bind(BooksView view) {
public interface BooksView {
void updateUi(List<Book> books);
}