@Test public void testAttachView() { when(chatServiceMock.getMessages()).thenReturn(Observable.just(testMessages)); when(chatServiceMock.addMessage(chatMessageCaptor.capture())).thenReturn(Observable.just(testMessages.get(0))); when(chatPresenterViewMock.getNewMessage()).thenReturn(Observable.just(testMessageString)); chatPresenter.attachView(chatPresenterViewMock); verify(chatPresenterViewMock).setListOfMessage(listChatMessageCaptor.capture()); for (int i = 0; i < testMessages.size(); i++) { assertEquals(listChatMessageCaptor.getValue().get(i).toString(), testMessages.get(i).toString()); } ChatMessage messageSend = chatMessageCaptor.getValue(); assertEquals(messageSend.pseudo(), "test"); assertEquals(messageSend.message(), testMessageString); assertNotNull(messageSend.date()); chatPresenter.detachView(); when(chatPresenterViewSecondMock.getNewMessage()).thenReturn(Observable.just(testMessageString)); chatPresenter.attachView(chatPresenterViewSecondMock); verify(chatPresenterViewSecondMock).setListOfMessage(listChatMessageCaptor.capture()); for (int i = 0; i < testMessages.size(); i++) { assertEquals(listChatMessageCaptor.getValue().get(i).toString(), testMessages.get(i).toString()); } chatPresenter.destroy(); }
@Override public void attachView(View view) { super.attachView(view); if (messages.size() == 0) { oldMessagesDisposable = chatService.getMessages() .repeatWhen(o -> o.concatMap(v -> Observable.interval(3, TimeUnit.SECONDS))) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe(messages -> { if (this.messages.size() != messages.size()) { this.messages.clear(); this.messages.addAll(messages); if (isViewAttached()) { getView().setListOfMessage(new ArrayList<>(messages)); } } }, error -> { }); } else { getView().setListOfMessage(new ArrayList<>(messages)); } if (newMessagesDisposable == null || newMessagesDisposable.isDisposed()) { newMessagesDisposable = getView().getNewMessage() .flatMap(message -> chatService.addMessage(ChatMessage.create("test", message, new Date())) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread())) .subscribe(); } }
ChatPresenter extends Presenter<ChatPresenter.View> { @Override public void attachView(View view) { super.attachView(view); if (messages.size() == 0) { oldMessagesDisposable = chatService.getMessages() .repeatWhen(o -> o.concatMap(v -> Observable.interval(3, TimeUnit.SECONDS))) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe(messages -> { if (this.messages.size() != messages.size()) { this.messages.clear(); this.messages.addAll(messages); if (isViewAttached()) { getView().setListOfMessage(new ArrayList<>(messages)); } } }, error -> { }); } else { getView().setListOfMessage(new ArrayList<>(messages)); } if (newMessagesDisposable == null || newMessagesDisposable.isDisposed()) { newMessagesDisposable = getView().getNewMessage() .flatMap(message -> chatService.addMessage(ChatMessage.create("test", message, new Date())) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread())) .subscribe(); } } }
ChatPresenter extends Presenter<ChatPresenter.View> { @Override public void attachView(View view) { super.attachView(view); if (messages.size() == 0) { oldMessagesDisposable = chatService.getMessages() .repeatWhen(o -> o.concatMap(v -> Observable.interval(3, TimeUnit.SECONDS))) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe(messages -> { if (this.messages.size() != messages.size()) { this.messages.clear(); this.messages.addAll(messages); if (isViewAttached()) { getView().setListOfMessage(new ArrayList<>(messages)); } } }, error -> { }); } else { getView().setListOfMessage(new ArrayList<>(messages)); } if (newMessagesDisposable == null || newMessagesDisposable.isDisposed()) { newMessagesDisposable = getView().getNewMessage() .flatMap(message -> chatService.addMessage(ChatMessage.create("test", message, new Date())) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread())) .subscribe(); } } ChatPresenter(ChatService chatService); }
ChatPresenter extends Presenter<ChatPresenter.View> { @Override public void attachView(View view) { super.attachView(view); if (messages.size() == 0) { oldMessagesDisposable = chatService.getMessages() .repeatWhen(o -> o.concatMap(v -> Observable.interval(3, TimeUnit.SECONDS))) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe(messages -> { if (this.messages.size() != messages.size()) { this.messages.clear(); this.messages.addAll(messages); if (isViewAttached()) { getView().setListOfMessage(new ArrayList<>(messages)); } } }, error -> { }); } else { getView().setListOfMessage(new ArrayList<>(messages)); } if (newMessagesDisposable == null || newMessagesDisposable.isDisposed()) { newMessagesDisposable = getView().getNewMessage() .flatMap(message -> chatService.addMessage(ChatMessage.create("test", message, new Date())) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread())) .subscribe(); } } ChatPresenter(ChatService chatService); @Override void attachView(View view); @Override void detachView(); @Override void destroy(); }
ChatPresenter extends Presenter<ChatPresenter.View> { @Override public void attachView(View view) { super.attachView(view); if (messages.size() == 0) { oldMessagesDisposable = chatService.getMessages() .repeatWhen(o -> o.concatMap(v -> Observable.interval(3, TimeUnit.SECONDS))) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.io()) .subscribe(messages -> { if (this.messages.size() != messages.size()) { this.messages.clear(); this.messages.addAll(messages); if (isViewAttached()) { getView().setListOfMessage(new ArrayList<>(messages)); } } }, error -> { }); } else { getView().setListOfMessage(new ArrayList<>(messages)); } if (newMessagesDisposable == null || newMessagesDisposable.isDisposed()) { newMessagesDisposable = getView().getNewMessage() .flatMap(message -> chatService.addMessage(ChatMessage.create("test", message, new Date())) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread())) .subscribe(); } } ChatPresenter(ChatService chatService); @Override void attachView(View view); @Override void detachView(); @Override void destroy();  }