Skip to content

Commit

Permalink
factor out profile activity
Browse files Browse the repository at this point in the history
  • Loading branch information
whoisladleo committed Jul 1, 2019
1 parent 5cf6bb7 commit 38afcea
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 89 deletions.
1 change: 1 addition & 0 deletions opensrp-chw-malaria/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ dependencies {
implementation 'id.zelory:compressor:1.0.4'

testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:2.+'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
package org.smartregister.chw.malaria.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.design.widget.AppBarLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.widget.TextView;

import org.joda.time.DateTime;
import org.joda.time.Period;
import org.smartregister.chw.malaria.contract.MalariaProfileContract;
import org.smartregister.chw.malaria.domain.MemberObject;
import org.smartregister.chw.malaria.presenter.BaseMalariaProfilePresenter;
import org.smartregister.chw.malaria.util.Constants;
import org.smartregister.helper.ImageRenderHelper;
import org.smartregister.malaria.R;
import org.smartregister.view.activity.BaseProfileActivity;

import java.lang.ref.WeakReference;

public class BaseMalariaProfileActivity extends BaseProfileActivity implements MalariaProfileContract.View, MalariaProfileContract.Presenter {
private Context context;
public class BaseMalariaProfileActivity extends BaseProfileActivity implements MalariaProfileContract.View {
protected MemberObject MEMBER_OBJECT;
private WeakReference<MalariaProfileContract.View> view;
private BaseMalariaProfilePresenter profilePresenter;
private TextView textViewName, textViewGender, textViewLocation, textViewUniqueID;

public static void startProfileActivity(Activity activity, MemberObject memberObject) {
Intent intent = new Intent(activity, BaseMalariaProfileActivity.class);
Expand All @@ -42,8 +38,6 @@ protected void onCreation() {
Toolbar toolbar = findViewById(R.id.collapsing_toolbar);
setSupportActionBar(toolbar);

MEMBER_OBJECT = (MemberObject) getIntent().getSerializableExtra(Constants.MALARIA_MEMBER_OBJECT.MEMBER_OBJECT);

ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
Expand All @@ -57,55 +51,39 @@ public void onClick(View v) {
BaseMalariaProfileActivity.this.finish();
}
});
appBarLayout = (AppBarLayout)this.findViewById(R.id.collapsing_toolbar_appbarlayout);
appBarLayout = this.findViewById(R.id.collapsing_toolbar_appbarlayout);
if (Build.VERSION.SDK_INT >= 21) {
appBarLayout.setOutlineProvider((ViewOutlineProvider)null);
appBarLayout.setOutlineProvider(null);
}

imageRenderHelper = new ImageRenderHelper(this);
setupViews();
}

@Override
protected void setupViews() {
int age = new Period(new DateTime(MEMBER_OBJECT.getAge()), new DateTime()).getYears();

TextView textViewName = findViewById(R.id.textview_name);
textViewName.setText(String.format("%s %s %s, %d", MEMBER_OBJECT.getFirstName(), MEMBER_OBJECT.getMiddleName(), MEMBER_OBJECT.getLastName(), age));
textViewName = findViewById(R.id.textview_name);
textViewGender = findViewById(R.id.textview_gender);
textViewLocation = findViewById(R.id.textview_address);
textViewUniqueID = findViewById(R.id.textview_id);

TextView textViewGender = findViewById(R.id.textview_gender);
textViewGender.setText(MEMBER_OBJECT.getGender());
MEMBER_OBJECT = (MemberObject) getIntent().getSerializableExtra(Constants.MALARIA_MEMBER_OBJECT.MEMBER_OBJECT);

TextView textViewLocation = findViewById(R.id.textview_address);
textViewLocation.setText(MEMBER_OBJECT.getAddress());
initializePresenter();

TextView textViewUniqueID = findViewById(R.id.textview_id);
textViewUniqueID.setText(MEMBER_OBJECT.getUniqueId());
}
profilePresenter.attachView(this);

@Override
public Context getContext() {
return context;
}
profilePresenter.fillProfileData(MEMBER_OBJECT);

@Override
public MalariaProfileContract.View getView() {
if(view != null) {
return view.get();
} else {
return null;
}
}

@Override
public MalariaProfileContract.Presenter presenter() {
return (MalariaProfileContract.Presenter) presenter;
public void setProfileViewWithData() {
int age = new Period(new DateTime(MEMBER_OBJECT.getAge()), new DateTime()).getYears();
textViewName.setText(String.format("%s %s %s, %d", MEMBER_OBJECT.getFirstName(), MEMBER_OBJECT.getMiddleName(), MEMBER_OBJECT.getLastName(), age));
textViewGender.setText(MEMBER_OBJECT.getGender());
textViewLocation.setText(MEMBER_OBJECT.getAddress());
textViewUniqueID.setText(MEMBER_OBJECT.getUniqueId());
}


@Override
protected void initializePresenter() {
presenter = new BaseMalariaProfilePresenter(this, MEMBER_OBJECT);
profilePresenter = new BaseMalariaProfilePresenter(this, MEMBER_OBJECT);
}

@Override
Expand All @@ -120,13 +98,15 @@ protected void fetchProfileData() {

@Override
public void onClick(View view) {
if(view.getId() == R.id.title_layout) {
if (view.getId() == R.id.title_layout) {
onBackPressed();
}
}

@Override
public void onDestroy(boolean b) {
//
protected void onDestroy() {
profilePresenter.detachView();
super.onDestroy();

}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
package org.smartregister.chw.malaria.contract;

import android.content.Context;
import org.smartregister.view.contract.BaseProfileContract;

public interface MalariaProfileContract {
interface View {
Context getContext();

MalariaProfileContract.View getView();

MalariaProfileContract.Presenter presenter();

}

interface Presenter extends BaseProfileContract.Presenter {
void setProfileViewWithData();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
import java.io.Serializable;

public class MemberObject implements Serializable {
protected String firstName;
protected String middleName;
protected String lastName;
protected String address;
protected String gender;
protected String uniqueId;
protected String age;
private String firstName;
private String middleName;
private String lastName;
private String address;
private String gender;
private String uniqueId;
private String age;

public MemberObject(CommonPersonObjectClient client) {
firstName = client.getColumnmaps().get(DBConstants.KEY.FIRST_NAME);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,34 @@
package org.smartregister.chw.malaria.presenter;

import android.content.Context;

import org.smartregister.chw.malaria.contract.MalariaProfileContract;
import org.smartregister.chw.malaria.domain.MemberObject;

import java.lang.ref.WeakReference;

public class BaseMalariaProfilePresenter implements MalariaProfileContract.Presenter, MalariaProfileContract.View {
protected WeakReference<MalariaProfileContract.View> view;
protected MemberObject memberObject;
public class BaseMalariaProfilePresenter {
protected MalariaProfileContract.View view;
private MemberObject memberObject;
protected Context context;


public BaseMalariaProfilePresenter(MalariaProfileContract.View contractView, MemberObject memberObject) {
this.view = new WeakReference<>(contractView);
public BaseMalariaProfilePresenter(MalariaProfileContract.View view, MemberObject memberObject) {
this.view = view;
this.memberObject = memberObject;

}

@Override
public Context getContext() {
return null;
public void attachView(MalariaProfileContract.View view) {
this.view = view;
}

@Override
public MalariaProfileContract.View getView() {
if(view != null) {
return view.get();
}
return null;
public void detachView() {
this.view = null;
}

@Override
public MalariaProfileContract.Presenter presenter() {
return null;
}


@Override
public void onDestroy(boolean b) {
//destroy
public void fillProfileData(MemberObject memberObject) {
if (memberObject != null) {
view.setProfileViewWithData();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.smartregister.presenter;

import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.smartregister.chw.malaria.contract.MalariaProfileContract;
import org.smartregister.chw.malaria.domain.MemberObject;
import org.smartregister.chw.malaria.presenter.BaseMalariaProfilePresenter;

import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

public class BaseMalariaProfilePresenterTest {
@Mock
private MalariaProfileContract.View view = Mockito.mock(MalariaProfileContract.View.class);
@Mock
BaseMalariaProfilePresenter profilePresenter = Mockito.mock(BaseMalariaProfilePresenter.class);

@Mock
MemberObject memberObject = Mockito.mock(MemberObject.class);

@Test
public void fillProfileData_doesntCallsSetProfileViewWithDataIfMemberObjectEmpty() {
profilePresenter.fillProfileData(memberObject);
verify(view, never()).setProfileViewWithData();
}
}

0 comments on commit 38afcea

Please sign in to comment.