Permalink
Browse files

Refactoring of CloseRequest MVC view into interface-implementation pa…

…ir and replacing EventBus with direct method calls
  • Loading branch information...
vzukanov committed May 14, 2016
1 parent ca9c95c commit 8f4bfef746951f77893d382046dcea4ed3b60aaa
@@ -30,18 +30,19 @@
import il.co.idocare.authentication.LoginStateManager;
import il.co.idocare.contentproviders.IDoCareContract;
import il.co.idocare.eventbusevents.LocationEvents;
import il.co.idocare.mvcviews.closerequest.CloseRequestViewMVC;
import il.co.idocare.mvcviews.closerequest.CloseRequestViewMvc;
import il.co.idocare.mvcviews.closerequest.CloseRequestViewMvcImpl;
import il.co.idocare.pictures.CameraAdapter;
import il.co.idocare.utils.UtilMethods;


public class CloseRequestFragment extends AbstractFragment {
public class CloseRequestFragment extends AbstractFragment implements CloseRequestViewMvc.CloseRequestViewMvcListener {

private final static String TAG = CloseRequestFragment.class.getSimpleName();



private CloseRequestViewMVC mCloseRequestViewMVC;
private CloseRequestViewMvc mCloseRequestViewMvc;

@Inject LoginStateManager mLoginStateManager;

@@ -54,7 +55,8 @@

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mCloseRequestViewMVC = new CloseRequestViewMVC(inflater, container);
mCloseRequestViewMvc = new CloseRequestViewMvcImpl(inflater, container);
mCloseRequestViewMvc.registerListener(this);

getControllerComponent().inject(this);

@@ -74,7 +76,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa

setActionBarTitle(getTitle());

return mCloseRequestViewMVC.getRootView();
return mCloseRequestViewMvc.getRootView();
}


@@ -125,15 +127,6 @@ public String getTitle() {
//
// EventBus events handling

@Subscribe
public void onEvent(CloseRequestViewMVC.TakePictureButtonClickEvent event) {
takePictureWithCamera();
}

@Subscribe
public void onEvent(CloseRequestViewMVC.CloseRequestButtonClickEvent event) {
closeRequest();
}

@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent(LoginStateManager.UserLoggedOutEvent event) {
@@ -146,6 +139,28 @@ public void onEvent(LoginStateManager.UserLoggedOutEvent event) {
// ---------------------------------------------------------------------------------------------


// ---------------------------------------------------------------------------------------------
//
// Callbacks from MVC view(s)


@Override
public void onCloseRequestClicked() {
closeRequest();
}

@Override
public void onTakePictureClicked() {
takePictureWithCamera();
}

// End callbacks from MVC view(s)
//
// ---------------------------------------------------------------------------------------------




@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
@@ -188,7 +203,7 @@ private void showPicture(int position, String cameraPicturePath) {
mCameraPicturesPaths.remove(position);
}
mCameraPicturesPaths.add(position, cameraPicturePath);
mCloseRequestViewMVC.showPicture(position, cameraPicturePath);
mCloseRequestViewMvc.showPicture(position, cameraPicturePath);
}


@@ -221,9 +236,9 @@ private void closeRequest() {
}
String closedPictures = sb.toString();

Bundle bundleCloseRequest = mCloseRequestViewMVC.getViewState();
Bundle bundleCloseRequest = mCloseRequestViewMvc.getViewState();
String closedComment =
bundleCloseRequest.getString(CloseRequestViewMVC.KEY_CLOSED_COMMENT);
bundleCloseRequest.getString(CloseRequestViewMvc.KEY_CLOSED_COMMENT);

if (!validRequestParameters(closedBy, closedPictures)) {
Log.d(TAG, "aborting request close due to invalid parameters");
@@ -161,7 +161,7 @@ public void onEvent(LoginStateManager.UserLoggedOutEvent event) {
// Callbacks from MVC view(s)

@Override
public void createRequestClicked() {
public void onCreateRequestClicked() {
createRequest();
}

@@ -1,98 +1,34 @@
package il.co.idocare.mvcviews.closerequest;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.ImageLoader;

import org.greenrobot.eventbus.EventBus;

import il.co.idocare.Constants;
import il.co.idocare.R;
import il.co.idocare.mvcviews.ObservableViewMVC;
import il.co.idocare.mvcviews.ViewMVC;

/**
* MVC View for Close Request screen.
* This MVC view represents "close request" screen
*/
public class CloseRequestViewMVC implements ViewMVC {

public static final String KEY_CLOSED_COMMENT = Constants.FIELD_NAME_CLOSED_COMMENT;

private final static String LOG_TAG = CloseRequestViewMVC.class.getSimpleName();

public interface CloseRequestViewMvc
extends ObservableViewMVC<CloseRequestViewMvc.CloseRequestViewMvcListener> {

private View mRootView;

private EditText mEdtClosedComment;
private ImageView[] mImgPictures = new ImageView[3];
interface CloseRequestViewMvcListener {
/**
* Will be invoked when the user clicks on "create request" button
*/
void onCloseRequestClicked();


public CloseRequestViewMVC(LayoutInflater inflater, ViewGroup container) {
mRootView = inflater.inflate(R.layout.layout_close_request, container, false);

mEdtClosedComment = (EditText) mRootView.findViewById(R.id.edt_closed_comment);
mImgPictures[0] = (ImageView) mRootView.findViewById(R.id.img_picture0);
mImgPictures[1] = (ImageView) mRootView.findViewById(R.id.img_picture1);
mImgPictures[2] = (ImageView) mRootView.findViewById(R.id.img_picture2);

View viewTakePicture = mRootView.findViewById(R.id.view_take_picture);
viewTakePicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EventBus.getDefault().post(new TakePictureButtonClickEvent());
}
});

Button btnCloseRequest = (Button) mRootView.findViewById(R.id.btn_close_request);
btnCloseRequest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EventBus.getDefault().post(new CloseRequestButtonClickEvent());
}
});
/**
* Will be invoked when the user clicks on "take picture" button
*/
void onTakePictureClicked();
}

public void showPicture(int position, String cameraPicturePath) {
if (position >= 3) {
Log.e(LOG_TAG, "maximal number of pictures exceeded!");
return;
}

ImageLoader.getInstance().displayImage(
Constants.UIL_LOCAL_FILE_PREFIX + cameraPicturePath,
mImgPictures[position],
Constants.DEFAULT_DISPLAY_IMAGE_OPTIONS);
}

@Override
public View getRootView() {
return mRootView;
}

@Override
public Bundle getViewState() {
Bundle bundle = new Bundle();
bundle.putString(KEY_CLOSED_COMMENT, mEdtClosedComment.getText().toString());
return bundle;
}

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

public static class CloseRequestButtonClickEvent {}

public static class TakePictureButtonClickEvent {}


// End of EventBus events
//
// ---------------------------------------------------------------------------------------------
String KEY_CLOSED_COMMENT = Constants.FIELD_NAME_CLOSED_COMMENT;

/**
* Show picture at position
* @param position the position (i.e. index) of the picture
* @param cameraPicturePath picture's URI
*/
void showPicture(int position, String cameraPicturePath);
}
@@ -0,0 +1,82 @@
package il.co.idocare.mvcviews.closerequest;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.ImageLoader;

import org.greenrobot.eventbus.EventBus;

import il.co.idocare.Constants;
import il.co.idocare.R;
import il.co.idocare.mvcviews.AbstractViewMVC;

/**
* Implementation of CloseRequestViewMvc interface
*/
public class CloseRequestViewMvcImpl
extends AbstractViewMVC<CloseRequestViewMvc.CloseRequestViewMvcListener>
implements CloseRequestViewMvc {

private final static String TAG = CloseRequestViewMvcImpl.class.getSimpleName();

private EditText mEdtClosedComment;
private ImageView[] mImgPictures = new ImageView[3];


public CloseRequestViewMvcImpl(LayoutInflater inflater, ViewGroup container) {
setRootView(inflater.inflate(R.layout.layout_close_request, container, false));

mEdtClosedComment = (EditText) getRootView().findViewById(R.id.edt_closed_comment);
mImgPictures[0] = (ImageView) getRootView().findViewById(R.id.img_picture0);
mImgPictures[1] = (ImageView) getRootView().findViewById(R.id.img_picture1);
mImgPictures[2] = (ImageView) getRootView().findViewById(R.id.img_picture2);

View viewTakePicture = getRootView().findViewById(R.id.view_take_picture);
viewTakePicture.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (CloseRequestViewMvcListener listener : getListeners()) {
listener.onTakePictureClicked();
}
}
});

Button btnCloseRequest = (Button) getRootView().findViewById(R.id.btn_close_request);
btnCloseRequest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (CloseRequestViewMvcListener listener : getListeners()) {
listener.onCloseRequestClicked();
}
}
});
}

@Override
public void showPicture(int position, String cameraPicturePath) {
if (position >= 3) {
Log.e(TAG, "maximal number of pictures exceeded!");
return;
}

ImageLoader.getInstance().displayImage(
Constants.UIL_LOCAL_FILE_PREFIX + cameraPicturePath,
mImgPictures[position],
Constants.DEFAULT_DISPLAY_IMAGE_OPTIONS);
}

@Override
public Bundle getViewState() {
Bundle bundle = new Bundle();
bundle.putString(KEY_CLOSED_COMMENT, mEdtClosedComment.getText().toString());
return bundle;
}

}
@@ -14,7 +14,7 @@
/**
* Will be invoked when the user clicks on "create request" button
*/
void createRequestClicked();
void onCreateRequestClicked();

/**
* Will be invoked when the user clicks on "take picture" button
@@ -1,17 +1,12 @@
package il.co.idocare.mvcviews.newrequest;

import android.os.Bundle;
import android.support.v7.widget.LinearSmoothScroller;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.nostra13.universalimageloader.core.ImageLoader;

import il.co.idocare.Constants;
import il.co.idocare.R;
@@ -55,7 +50,7 @@ public void onTakePictureClicked() {
@Override
public void onClick(View view) {
for (NewRequestViewMvcListener listener : getListeners()) {
listener.createRequestClicked();
listener.onCreateRequestClicked();
}
}
});

0 comments on commit 8f4bfef

Please sign in to comment.