Skip to content

Commit

Permalink
🐉 test post and subscribe Rxbus2.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jedsada Tiwongvorakul committed Apr 29, 2017
1 parent 9cb1dbf commit ac91010
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.wisdomlanna.www.dagger2_mvp_example;

import android.annotation.SuppressLint;
import android.app.Application;
import android.os.Build;
import android.os.StrictMode;

import com.wisdomlanna.www.dagger2_mvp_example.module.AndroidModule;
import com.wisdomlanna.www.dagger2_mvp_example.module.ApiModule;
Expand All @@ -14,12 +17,25 @@ public class MyApplication extends Application {

private ApplicationComponent component;

@SuppressLint("ObsoleteSdkInt")
@Override
public void onCreate() {
super.onCreate();
ButterKnife.setDebug(BuildConfig.DEBUG);
initDependencyInjection();
if (BuildConfig.DEBUG) Timber.plant(new Timber.DebugTree());
if (BuildConfig.DEBUG) {
Timber.plant(new Timber.DebugTree());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectAll()
.penaltyLog()
.build());
StrictMode.ThreadPolicy.Builder threadPolicyBuilder = new StrictMode.ThreadPolicy.Builder()
.detectAll()
.penaltyLog();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
threadPolicyBuilder.penaltyDeathOnNetwork();
StrictMode.setThreadPolicy(threadPolicyBuilder.build());
}
}

private void initDependencyInjection() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import android.os.Bundle;
import android.widget.TextView;

import com.google.gson.Gson;
import com.wisdomlanna.www.dagger2_mvp_example.ApplicationComponent;
import com.wisdomlanna.www.dagger2_mvp_example.R;
import com.wisdomlanna.www.dagger2_mvp_example.R2;
import com.wisdomlanna.www.dagger2_mvp_example.api.dao.UserInfoDao;
import com.wisdomlanna.www.dagger2_mvp_example.ui.base.BaseActivity;
import com.wisdomlanna.www.dagger2_mvp_example.ui.event.TestBusEvent;

import javax.inject.Inject;

Expand All @@ -25,6 +27,8 @@ public class MainActivity extends BaseActivity<MainPresenter> implements MainInt

@Inject
SharedPreferences sharedPreferences;
@Inject
Gson gson;

private int result;

Expand All @@ -37,10 +41,20 @@ public void showResultPlus(int result) {

@Override
public void showResultUserInfoGitHubApi(UserInfoDao dao) {
Timber.d("result userName : %s", dao.getName());
Timber.d("github result userName : %s", dao.getName());
tvUsername.setText(dao.getName());
}

@Override
public void showResultBusTag(int result) {
Timber.d("result bus tag : %d", result);
}

@Override
public void showResultBusTestBusEvent(TestBusEvent event) {
Timber.d("result bus TestBusEvent : %s", gson.toJson(event));
}

@Override
protected int layoutToInflate() {
return R.layout.activity_main;
Expand All @@ -54,11 +68,13 @@ protected void doInjection(ApplicationComponent component) {
@Override
protected void startActivity() {
getPresenter().loadUserInfo("pondthaitay");
getPresenter().onViewStart();
}

@Override
protected void stopActivity() {
Timber.d("stop activity");
getPresenter().onViewStop();
}

@Override
Expand All @@ -69,7 +85,7 @@ protected void bindView() {
@Override
protected void setupInstance() {
sharedPreferences.edit().putString("kk", "Jedsada").apply();
Timber.d(sharedPreferences.getString("kk", ""));
Timber.d("result shared preferences : %s", sharedPreferences.getString("kk", ""));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

import com.wisdomlanna.www.dagger2_mvp_example.api.dao.UserInfoDao;
import com.wisdomlanna.www.dagger2_mvp_example.ui.base.BaseInterface;
import com.wisdomlanna.www.dagger2_mvp_example.ui.event.TestBusEvent;

class MainInterface {

interface View extends BaseInterface.View {
void showResultPlus(int result);

void showResultUserInfoGitHubApi(UserInfoDao dao);

void showResultBusTag(int result);

void showResultBusTestBusEvent(TestBusEvent event);
}

interface Presenter {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.wisdomlanna.www.dagger2_mvp_example.ui;

import com.hwangjr.rxbus.RxBus;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;
import com.wisdomlanna.www.dagger2_mvp_example.R;
import com.wisdomlanna.www.dagger2_mvp_example.api.BaseSubscriber;
import com.wisdomlanna.www.dagger2_mvp_example.api.dao.UserInfoDao;
import com.wisdomlanna.www.dagger2_mvp_example.api.service.GitHubApi;
import com.wisdomlanna.www.dagger2_mvp_example.ui.base.BasePresenter;
import com.wisdomlanna.www.dagger2_mvp_example.ui.event.TestBusEvent;

import javax.inject.Inject;

Expand Down Expand Up @@ -76,11 +80,28 @@ public void onViewDestroy() {

@Override
public void onViewStart() {
if (getView() != null) RxBus.get().register(this);
if (getView() != null) {
RxBus.get().register(this);
TestBusEvent event = new TestBusEvent();
event.setName("Jedsada Tiwongvorakul");
event.setAge(22);
RxBus.get().post("tag_test", 555);
RxBus.get().post(event);
}
}

@Override
public void onViewStop() {
if (getView() != null) RxBus.get().unregister(this);
}

@Subscribe(thread = EventThread.MAIN_THREAD, tags = {@Tag("tag_test")})
public void busTestTag(Integer result) {
if (getView() != null) getView().showResultBusTag(result);
}

@Subscribe(thread = EventThread.MAIN_THREAD)
public void busTestObj(TestBusEvent event) {
if (getView() != null) getView().showResultBusTestBusEvent(event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,12 @@ public void unAuthorizedApi() {
protected void onStart() {
super.onStart();
startActivity();
getPresenter().onViewStart();
}

@Override
protected void onStop() {
super.onStop();
stopActivity();
getPresenter().onViewStop();
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.wisdomlanna.www.dagger2_mvp_example.ui.event;

public class TestBusEvent {

private String name;
private int age;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.wisdomlanna.www.dagger2_mvp_example.R;
import com.wisdomlanna.www.dagger2_mvp_example.api.dao.UserInfoDao;
import com.wisdomlanna.www.dagger2_mvp_example.api.service.GitHubApi;
import com.wisdomlanna.www.dagger2_mvp_example.ui.event.TestBusEvent;
import com.wisdomlanna.www.dagger2_mvp_example.utils.JsonMockUtility;
import com.wisdomlanna.www.dagger2_mvp_example.utils.RxSchedulersOverrideRule;

Expand All @@ -28,6 +29,7 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.times;
Expand Down Expand Up @@ -157,6 +159,8 @@ public void testViewCreate() throws Exception {
public void testRxBusRegister() throws Exception {
presenter.onViewStart();
verify(RxBus.get(), times(1)).register(presenter);
verify(RxBus.get(), times(1)).post("tag_test", 555);
verify(RxBus.get(), times(1)).post(any(TestBusEvent.class));
}

@Test
Expand Down

0 comments on commit ac91010

Please sign in to comment.