Last active
August 29, 2015 14:09
-
-
Save vizZ/704b965e679324b3ba40 to your computer and use it in GitHub Desktop.
Reactive Programming @ Aftonbladet
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 AftonbladetAPI { | |
@GET("/item/{id}.json") | |
Observable<Item> item(@Path("id") long id); | |
@GET("/topstories.json") | |
Observable<List<Long>> topStories(); | |
} | |
public void onRefresh() { | |
Aftonbladet.API.topStories() | |
.flatMap(longs -> Observable.from(longs)) | |
.flatMap(id -> Aftonbladet.API.item(id)) | |
.collect(new ArrayList<Item>(), (items, item) -> items.add(item)) | |
.subscribeOn(Schedulers.newThread()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe( | |
items -> { | |
listViewer.showList(); | |
list.setAdapter(new ArrayAdapter<Item>(getActivity(), android.R.layout.simple_list_item_1, items)); | |
}, | |
error -> { | |
listViewer.showError(); | |
}, | |
() -> { | |
if (ptr.isRefreshing()) ptr.setRefreshing(false); | |
} | |
); | |
} | |
public void onRefresh() { | |
HackerNews.API.topStories() | |
.flatMap(longs -> Observable.from(longs)) | |
.flatMap(id -> HackerNews.API.item(id)) | |
.filter(item -> item.time > 1412985600) | |
.limit(10) | |
.collect(new ArrayList<Item>(), (items, item) -> items.add(item)) | |
.subscribeOn(Schedulers.newThread()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe( | |
items -> { | |
listViewer.showList(); | |
list.setAdapter(new ArrayAdapter<Item>(getActivity(), android.R.layout.simple_list_item_1, items)); | |
}, | |
error -> { | |
listViewer.showError(); | |
}, | |
() -> { | |
if (ptr.isRefreshing()) ptr.setRefreshing(false); | |
} | |
); | |
} | |
.subscribe( | |
items -> list.setAdapter(new ArrayAdapter<Item>(..., items)), | |
error -> listViewer.showError(), | |
() -> { | |
if (ptr.isRefreshing()) ptr.setRefreshing(false); | |
} | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment