Advertisement
Guest User

Untitled

a guest
Mar 16th, 2015
1,510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. public interface ExampleModel {
  2. Observable<String> getResponse(String request);
  3. }
  4.  
  5. public interface ExampleView {
  6. Observable<String> changeText();
  7. void showResponse(String response);
  8. }
  9.  
  10. public interface ExamplePresenter {
  11. void onCreate();
  12. }
  13.  
  14. public class ExamplePresenerImpl implementation ExamplePresenter {
  15. private ExampleView view;
  16. private Model model;
  17.  
  18. private Subscription subscription;
  19.  
  20. public ExamplePresenterImpl(ExampleView view) {
  21. this.view = view;
  22. this.model = new RetrofitAdapter.Builder(ExampleModel.class).addUrl(...).build();
  23. }
  24.  
  25. public void onCreate() {
  26. subscription = view
  27. .changeText()
  28. .flatMap(query -> model.getResponse(query))
  29. .subscribe(response -> view.showResponse(response));
  30. }
  31.  
  32. public void onDestroy() {
  33. subscription.unsubscribe();
  34. }
  35. }
  36.  
  37. public class ExampleActivity extends Activity implementation ExampleView {
  38. private EditText editText;
  39. private TextView textView;
  40. private ExamplePresenter presenter;
  41.  
  42. public void onCreate(...) {
  43. this.editText = (EditText) findViewById(...);
  44. this.textView = (TextView) findViewById(...);
  45.  
  46. presenter = new ExamplePresenterImpl(this);
  47. presenter.onCreate();
  48. }
  49.  
  50. public void onDestroy() {
  51. presenter.onDestroy();
  52. }
  53.  
  54. @Override
  55. public Observable<String> changeText() {
  56. return ObservableWidge.text(editText);
  57. }
  58.  
  59. @Override
  60. void showResponse(String response) {
  61. textView.setText(response);
  62. }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement