Reputation: 11
I'm trying to use rxjava on android following MVP Pattern.
I've studied more than a week but it is too hard for me to use it.. sadly.
When user clicks the button at MainActivity, onclick event runs networkPresenter.checkConnectivity(context).
Could you tell me where I can change code to rxjava?
public class NetworkPresenter {
private NetworkConnector nc;
private MessageSetter ms;
private View view;
public NetworkPresenter(NetworkPresenter.View view) {
this.view = view;
nc = new NetworkConnector();
ms = new MessageSetter();
}
public void checkConnectivity(Context context) {
int connType = nc.getConnectionStatus(context);
view.updateReceivedMessageTextView(ms.setMessage(connType));
}
public interface View {
void updateReceivedMessageTextView(String message);
}
}
.
public class NetworkConnector {
static final int NONE = 0;
static final int WIFI = 10;
static final int MOBILE = 20;
public int getConnectionStatus(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
int result = 0;
if (activeNetwork != null) {
if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) result = WIFI;
else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) result = MOBILE;
if (isOnline()) return result;
else return result + 1;
}
return NONE;
}
}
.
public class NetworkChecker extends Thread {
public static final String CONNECTION_CONFIRM_CLIENT_URL = "http://clients3.google.com/generate_204";
private boolean success;
private String host;
public NetworkChecker(String host){
this.host = host;
}
@Override
public void run() {
HttpURLConnection conn = null;
try {
conn = (HttpURLConnection)new URL(host).openConnection();
conn.setRequestProperty("User-Agent","Android");
conn.setConnectTimeout(300);
conn.connect();
int responseCode = conn.getResponseCode();
if(responseCode == 204) success = true;
else success = false;
}
catch (Exception e) {
e.printStackTrace();
success = false;
}
if(conn != null){
conn.disconnect();
}
}
public boolean isSuccess(){
return success;
}
public static boolean isOnline() {
NetworkChecker cc = new NetworkChecker(CONNECTION_CONFIRM_CLIENT_URL);
cc.start();
try{
cc.join();
return cc.isSuccess();
}catch (Exception e){
e.printStackTrace();
}
return false;
}
}
Upvotes: 0
Views: 76
Reputation: 3282
The point is, rxJava represent all events as data, coming out of one pipe and being redirected to another pipe. RxBinding library to bind your widgets in view to rxJava, so for every view you will have one observable. In MVP pattern presenter is responsible for logic itself, so, when you register view and model in presenter, take that Observable from View, subscribe to it and save Subscription object. You have to sever that subscription when view is detached - it is usually called on presenter from onPause() method. And, in onResume(), register view in presenter again. Workflow will be as foollows: - Widget raise event and passes it to rxJava's pipe (Observable). This observable is monitored by Presenter if View is attached to it. Presenter takes that event and handle it. In case View is not registered in Presenter, this event will go no further. Subscription objects represent the fact that some pipe in connected to some receiver, and, when you unsubscribe, that pipe gets cut off from event source (Widget).
Upvotes: 3