diff --git a/opensrp-chw-malaria/build.gradle b/opensrp-chw-malaria/build.gradle index 8748745..a3ec19e 100644 --- a/opensrp-chw-malaria/build.gradle +++ b/opensrp-chw-malaria/build.gradle @@ -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' } diff --git a/opensrp-chw-malaria/src/main/java/org/smartregister/chw/malaria/activity/BaseMalariaProfileActivity.java b/opensrp-chw-malaria/src/main/java/org/smartregister/chw/malaria/activity/BaseMalariaProfileActivity.java index 4478962..e1d9611 100644 --- a/opensrp-chw-malaria/src/main/java/org/smartregister/chw/malaria/activity/BaseMalariaProfileActivity.java +++ b/opensrp-chw-malaria/src/main/java/org/smartregister/chw/malaria/activity/BaseMalariaProfileActivity.java @@ -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 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); @@ -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); @@ -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 @@ -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(); + } } diff --git a/opensrp-chw-malaria/src/main/java/org/smartregister/chw/malaria/contract/MalariaProfileContract.java b/opensrp-chw-malaria/src/main/java/org/smartregister/chw/malaria/contract/MalariaProfileContract.java index 2879045..e0c7594 100644 --- a/opensrp-chw-malaria/src/main/java/org/smartregister/chw/malaria/contract/MalariaProfileContract.java +++ b/opensrp-chw-malaria/src/main/java/org/smartregister/chw/malaria/contract/MalariaProfileContract.java @@ -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(); } } \ No newline at end of file diff --git a/opensrp-chw-malaria/src/main/java/org/smartregister/chw/malaria/domain/MemberObject.java b/opensrp-chw-malaria/src/main/java/org/smartregister/chw/malaria/domain/MemberObject.java index 6ae491d..d8d92f3 100644 --- a/opensrp-chw-malaria/src/main/java/org/smartregister/chw/malaria/domain/MemberObject.java +++ b/opensrp-chw-malaria/src/main/java/org/smartregister/chw/malaria/domain/MemberObject.java @@ -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); diff --git a/opensrp-chw-malaria/src/main/java/org/smartregister/chw/malaria/presenter/BaseMalariaProfilePresenter.java b/opensrp-chw-malaria/src/main/java/org/smartregister/chw/malaria/presenter/BaseMalariaProfilePresenter.java index da3a191..19d2419 100644 --- a/opensrp-chw-malaria/src/main/java/org/smartregister/chw/malaria/presenter/BaseMalariaProfilePresenter.java +++ b/opensrp-chw-malaria/src/main/java/org/smartregister/chw/malaria/presenter/BaseMalariaProfilePresenter.java @@ -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 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(); + } } } diff --git a/opensrp-chw-malaria/src/test/java/org/smartregister/presenter/BaseMalariaProfilePresenterTest.java b/opensrp-chw-malaria/src/test/java/org/smartregister/presenter/BaseMalariaProfilePresenterTest.java new file mode 100644 index 0000000..9520354 --- /dev/null +++ b/opensrp-chw-malaria/src/test/java/org/smartregister/presenter/BaseMalariaProfilePresenterTest.java @@ -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(); + } +}