Permalink
Browse files

Replacing Handler messaging with EventBus approach + small code refac…

…toring and renamings
  • Loading branch information...
vzukanov committed Aug 30, 2015
1 parent be11d0a commit 07728670a4d04f45d5bea03971c637a792d8eeaf
Showing with 309 additions and 555 deletions.
  1. +0 −39 app/src/main/java/il/co/idocare/Constants.java
  2. +6 −6 app/src/main/java/il/co/idocare/controllers/activities/AbstractActivity.java
  3. +37 −46 app/src/main/java/il/co/idocare/controllers/activities/AuthenticatorActivity.java
  4. +3 −3 app/src/main/java/il/co/idocare/controllers/activities/MainActivity.java
  5. +10 −68 app/src/main/java/il/co/idocare/controllers/fragments/AbstractFragment.java
  6. +17 −17 app/src/main/java/il/co/idocare/controllers/fragments/CloseRequestFragment.java
  7. +28 −59 app/src/main/java/il/co/idocare/controllers/fragments/HomeFragment.java
  8. +1 −1 ...va/il/co/idocare/controllers/fragments/{MyFragmentInterface.java → IDoCareFragmentInterface.java}
  9. +17 −17 app/src/main/java/il/co/idocare/controllers/fragments/NewRequestFragment.java
  10. +31 −47 app/src/main/java/il/co/idocare/controllers/fragments/RequestDetailsFragment.java
  11. +5 −5 ...java/il/co/idocare/controllers/listadapters/{HomeFragmentListAdapter.java → HomeListAdapter.java}
  12. +0 −37 app/src/main/java/il/co/idocare/handlermessaging/HandlerMessagingMaster.java
  13. +0 −24 app/src/main/java/il/co/idocare/handlermessaging/HandlerMessagingSlave.java
  14. +0 −86 app/src/main/java/il/co/idocare/views/AbstractViewMVC.java
  15. +42 −18 app/src/main/java/il/co/idocare/views/AuthenticateViewMVC.java
  16. +18 −10 app/src/main/java/il/co/idocare/views/CloseRequestViewMVC.java
  17. +42 −7 app/src/main/java/il/co/idocare/views/HomeViewMVC.java
  18. +19 −9 app/src/main/java/il/co/idocare/views/NewRequestViewMVC.java
  19. +32 −22 app/src/main/java/il/co/idocare/views/RequestDetailsViewMVC.java
  20. +1 −34 app/src/main/java/il/co/idocare/views/RequestThumbnailViewMVC.java
@@ -122,43 +122,4 @@ public String getValue() {
}


/**
* Various message types that might be passed between MVC components.<br>
* Prefix values:<br>
* M_ - messages originating in MVC Models<br>
* V_ - messages originating in MVC Views<br>
* C_ - messages originating in MVC Controllers<br>
* Implementation note: the members of this enum might be used as regular enums are used, or
* might be referenced/compared by their {@link Enum#ordinal()} value.
*
*/
public enum MessageType {
V_REQUEST_ITEM_CLICKED,
V_LOGIN_BUTTON_CLICK,
V_CREATE_NEW_REQUEST_BUTTON_CLICKED,
V_CLOSE_REQUEST_BUTTON_CLICKED,
V_PICKUP_REQUEST_BUTTON_CLICKED,
V_TAKE_PICTURE_BUTTON_CLICKED,
V_CREATED_VOTE_UP_BUTTON_CLICKED,
V_CREATED_VOTE_DOWN_BUTTON_CLICKED,
V_CLOSED_VOTE_UP_BUTTON_CLICKED,
V_CLOSED_VOTE_DOWN_BUTTON_CLICKED,

C_LOGIN_REQUEST_SENT,
C_LOGIN_RESPONSE_RECEIVED,

M_USER_DATA_UPDATE,
}

/**
* This array is required in order to use MessageType enum in switch-case comparisons.
*/
public static final MessageType[] MESSAGE_TYPE_VALUES = MessageType.values();







}
@@ -17,7 +17,7 @@
import il.co.idocare.R;
import il.co.idocare.authentication.AccountAuthenticator;
import il.co.idocare.controllers.fragments.IDoCareFragmentCallback;
import il.co.idocare.controllers.fragments.MyFragmentInterface;
import il.co.idocare.controllers.fragments.IDoCareFragmentInterface;

/**
* This is a wrapper around a standard Activity class which provides few convenience methods
@@ -157,8 +157,8 @@ private void manageNavigateUpButton() {
Fragment currFragment = getFragmentManager().findFragmentById(R.id.frame_contents);

boolean hasHierParent = currFragment != null &&
MyFragmentInterface.class.isAssignableFrom(currFragment.getClass()) &&
((MyFragmentInterface)currFragment).getNavHierParentFragment() != null;
IDoCareFragmentInterface.class.isAssignableFrom(currFragment.getClass()) &&
((IDoCareFragmentInterface)currFragment).getNavHierParentFragment() != null;

getActionBar().setDisplayHomeAsUpEnabled(hasBackstackEntries || hasHierParent);
}
@@ -172,12 +172,12 @@ public boolean onNavigateUp() {
return true;
} else {
Fragment currFragment = getFragmentManager().findFragmentById(R.id.frame_contents);
// Check if currently shown fragment is of type MyFragmentInterface
// Check if currently shown fragment is of type IDoCareFragmentInterface
if (currFragment != null &&
MyFragmentInterface.class.isAssignableFrom(currFragment.getClass())) {
IDoCareFragmentInterface.class.isAssignableFrom(currFragment.getClass())) {
// Get the hierarchical parent of the currently shown fragment
Class<? extends Fragment> hierParent =
((MyFragmentInterface)currFragment).getNavHierParentFragment();
((IDoCareFragmentInterface)currFragment).getNavHierParentFragment();

if (hierParent != null) {
replaceFragment(hierParent, false, true, null);
@@ -7,7 +7,6 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Base64;
import android.util.Log;
@@ -21,6 +20,7 @@
import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;
import il.co.idocare.Constants;
import il.co.idocare.authentication.AccountAuthenticator;
import il.co.idocare.networking.ServerHttpRequest;
@@ -30,7 +30,8 @@
/**
* Created by Vasiliy on 4/30/2015.
*/
public class AuthenticatorActivity extends AccountAuthenticatorActivity implements ServerHttpRequest.OnServerResponseCallback {
public class AuthenticatorActivity extends AccountAuthenticatorActivity implements
ServerHttpRequest.OnServerResponseCallback {

public final static String ARG_ACCOUNT_NAME = "arg_account_name";
public final static String ARG_AUTH_TOKEN_TYPE = "arg_auth_type";
@@ -46,17 +47,12 @@

private AuthenticateViewMVC mViewMVC;

private final List<Handler> mOutboxHandlers = new ArrayList<Handler>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mViewMVC = new AuthenticateViewMVC(LayoutInflater.from(this), null);

mViewMVC.addOutboxHandler(getInboxHandler());
addOutboxHandler(mViewMVC.getInboxHandler());

setContentView(mViewMVC.getRootView());

// if (accountName != null) {
@@ -65,6 +61,33 @@ protected void onCreate(Bundle savedInstanceState) {

}

@Override
protected void onStart() {
super.onStart();
EventBus.getDefault().register(this);
EventBus.getDefault().register(mViewMVC);
}

@Override
protected void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
EventBus.getDefault().unregister(mViewMVC);
}


// ---------------------------------------------------------------------------------------------
//
// EventBus events handling

public void onEvent(AuthenticateViewMVC.LoginButtonClickEvent event) {
sendLoginRequest();
}

// End of EventBus events handling
//
// ---------------------------------------------------------------------------------------------


/**
* Initiate login server request
@@ -95,7 +118,7 @@ private void sendLoginRequest() {

new Thread(serverRequest).start();

notifyOutboxHandlers(Constants.MessageType.C_LOGIN_REQUEST_SENT.ordinal(), 0, 0, null);
EventBus.getDefault().post(new AuthenticateViewMVC.LoginRequestSentEvent());

}

@@ -106,8 +129,6 @@ public void serverResponse(int statusCode, String reasonPhrase, String entityStr
String url = (String) asyncCompletionToken;
if (url.equals(LOGIN_URL)) {

notifyOutboxHandlers(Constants.MessageType.C_LOGIN_RESPONSE_RECEIVED.ordinal(), 0, 0, null);

Bundle data = null;

// TODO: use a responsehandler here just like in DataUploader/Downloader classes!
@@ -127,6 +148,9 @@ public void serverResponse(int statusCode, String reasonPhrase, String entityStr
} else {
Log.e(LOG_TAG, "receiver serverResponse() callback for unrecognized URL: " + url);
}

// If we got here - login was unsuccessful
EventBus.getDefault().post(new AuthenticateViewMVC.LoginFailedEvent());
}


@@ -176,6 +200,9 @@ public void run() {
prefs.edit().putString(AccountManager.KEY_ACCOUNT_NAME, accountName).apply();
prefs.edit().putString(AccountManager.KEY_ACCOUNT_TYPE, accountType).apply();

// Notify of successful login
EventBus.getDefault().post(new AuthenticateViewMVC.LoginSuccessfulEvent());


final Intent result = new Intent();
result.putExtras(data);
@@ -213,41 +240,5 @@ private Bundle extractResponseData(String jsonData) {
return data;
}

private Handler getInboxHandler() {
return new Handler() {
@Override
public void handleMessage(Message msg) {
switch (Constants.MESSAGE_TYPE_VALUES[msg.what]) {
case V_LOGIN_BUTTON_CLICK:
sendLoginRequest();
break;
default:
Log.w(LOG_TAG, "Message of type "
+ Constants.MESSAGE_TYPE_VALUES[msg.what].toString() + " wasn't consumed");
}
}
};
}


private void addOutboxHandler(Handler handler) {
// Not sure that there will be use case that requires sync, but just as precaution...
synchronized (mOutboxHandlers) {
if (!mOutboxHandlers.contains(handler)) {
mOutboxHandlers.add(handler);
}
}
}


private void notifyOutboxHandlers(int what, int arg1, int arg2, Object obj) {
// Not sure that there will be use case that requires sync, but just as precaution...
synchronized (mOutboxHandlers) {
for (Handler handler : mOutboxHandlers) {
Message msg = Message.obtain(handler, what, arg1, arg2, obj);
msg.sendToTarget();
}
}
}

}
@@ -23,7 +23,7 @@

import il.co.idocare.Constants;
import il.co.idocare.contentproviders.IDoCareContract;
import il.co.idocare.controllers.fragments.MyFragmentInterface;
import il.co.idocare.controllers.fragments.IDoCareFragmentInterface;
import il.co.idocare.controllers.listadapters.NavigationDrawerListAdapter;
import il.co.idocare.controllers.fragments.HomeFragment;
import il.co.idocare.R;
@@ -198,8 +198,8 @@ public void onDrawerSlide(View view, float v) {
} else {
Fragment currFragment = getFragmentManager().findFragmentById(R.id.frame_contents);
if (currFragment != null &&
MyFragmentInterface.class.isAssignableFrom(currFragment.getClass())) {
setActionBarTitle(((MyFragmentInterface)currFragment).getTitle());
IDoCareFragmentInterface.class.isAssignableFrom(currFragment.getClass())) {
setActionBarTitle(((IDoCareFragmentInterface)currFragment).getTitle());
}
}

@@ -8,16 +8,10 @@
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import il.co.idocare.handlermessaging.HandlerMessagingMaster;
import il.co.idocare.handlermessaging.HandlerMessagingSlave;
import de.greenrobot.event.EventBus;


/**
@@ -26,15 +20,10 @@
* Fragments of this app should extend this class.
*/
public abstract class AbstractFragment extends Fragment implements
MyFragmentInterface,
HandlerMessagingMaster,
HandlerMessagingSlave {
IDoCareFragmentInterface {

IDoCareFragmentCallback mCallback;

HandlerThread mInboxHandlerThread;
Handler mInboxHandler;
final List<Handler> mOutboxHandlers = new ArrayList<Handler>();
private ProgressDialog mProgressDialog;


@@ -113,68 +102,21 @@ public Account getActiveAccount() {

// ---------------------------------------------------------------------------------------------
//
// MVC Controller methods


/**
* Handle the message received by the inbox Handler
* @param msg message to handle
*/
protected abstract void handleMessage(Message msg);
// EventBus configuration

@Override
public Handler getInboxHandler() {
// TODO: consider changing all handlers to run on the main thread.
// Inbox Handler will be running on a separate thread
// The cast into Object is due to this:
// http://stackoverflow.com/questions/18505973/android-studio-ambiguous-method-call-getclass
if (mInboxHandlerThread == null) {
mInboxHandlerThread = new HandlerThread(((Object)this).getClass().getSimpleName());
mInboxHandlerThread.start();
}
if (mInboxHandler == null) {
mInboxHandler = new Handler(mInboxHandlerThread.getLooper()) {
@Override
public void handleMessage(Message msg) {
AbstractFragment.this.handleMessage(msg);
}
};
}
return mInboxHandler;
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}

@Override
public void addOutboxHandler(Handler handler) {
// Not sure that there will be use case that requires sync, but just as precaution...
synchronized (mOutboxHandlers) {
if (!mOutboxHandlers.contains(handler)) {
mOutboxHandlers.add(handler);
}
}
public void onStop() {
super.onStop();
EventBus.getDefault().unregister(this);
}

@Override
public void removeOutboxHandler(Handler handler) {
// Not sure that there will be use case that requires sync, but just as precaution...
synchronized (mOutboxHandlers) {
mOutboxHandlers.remove(handler);
}
}

@Override
public void notifyOutboxHandlers(int what, int arg1, int arg2, Object obj) {
// Not sure that there will be use case that requires sync, but just as precaution...
synchronized (mOutboxHandlers) {
for (Handler handler : mOutboxHandlers) {
Message msg = Message.obtain(handler, what, arg1, arg2, obj);
msg.sendToTarget();
}
}
}



// End of MVC Controller methods
// End of EventBus configuration
//
// ---------------------------------------------------------------------------------------------

Oops, something went wrong.

0 comments on commit 0772867

Please sign in to comment.