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 a49c5b9..9f0b997 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,42 +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.content.ContextCompat; 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 android.widget.Toast; + 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; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.Locale; -public class BaseMalariaProfileActivity extends BaseProfileActivity implements MalariaProfileContract.View, MalariaProfileContract.Presenter { - private Context context; - private MemberObject MEMBER_OBJECT; - private View recordMalariaView; - private TextView textViewRecordMalaria; - private WeakReference view; +public class BaseMalariaProfileActivity extends BaseProfileActivity implements MalariaProfileContract.View { + protected MemberObject MEMBER_OBJECT; + private BaseMalariaProfilePresenter profilePresenter; + private TextView textViewName, textViewGender, textViewLocation, textViewUniqueID; public static void startProfileActivity(Activity activity, MemberObject memberObject) { Intent intent = new Intent(activity, BaseMalariaProfileActivity.class); @@ -50,9 +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); @@ -66,31 +51,32 @@ 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(); + textViewName = findViewById(R.id.textview_name); + textViewGender = findViewById(R.id.textview_gender); + textViewLocation = findViewById(R.id.textview_address); + textViewUniqueID = findViewById(R.id.textview_id); + + MEMBER_OBJECT = (MemberObject) getIntent().getSerializableExtra(Constants.MALARIA_MEMBER_OBJECT.MEMBER_OBJECT); + + initializePresenter(); + + profilePresenter.attachView(this); + + profilePresenter.fillProfileData(MEMBER_OBJECT); + } @Override - protected void setupViews() { + public void setProfileViewWithData() { int age = new Period(new DateTime(MEMBER_OBJECT.getAge()), new DateTime()).getYears(); - recordMalariaView = findViewById(R.id.record_visit_malaria); - textViewRecordMalaria = findViewById(R.id.textview_record_malaria); - - 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)); - - TextView textViewGender = findViewById(R.id.textview_gender); textViewGender.setText(MEMBER_OBJECT.getGender()); - - TextView textViewLocation = findViewById(R.id.textview_address); textViewLocation.setText(MEMBER_OBJECT.getAddress()); - - TextView textViewUniqueID = findViewById(R.id.textview_id); textViewUniqueID.setText(MEMBER_OBJECT.getUniqueId()); recordMalariaButton(MEMBER_OBJECT.getMalariaTestDate()); @@ -116,29 +102,10 @@ private void recordMalariaButton(String malaria_test_date) { } } - @Override - public Context getContext() { - return context; - } - - @Override - public MalariaProfileContract.View getView() { - if(view != null) { - return view.get(); - } else { - return null; - } - } - - @Override - public MalariaProfileContract.Presenter presenter() { - return (MalariaProfileContract.Presenter) presenter; - } - @Override protected void initializePresenter() { - presenter = new BaseMalariaProfilePresenter(this, MEMBER_OBJECT); + profilePresenter = new BaseMalariaProfilePresenter(this, MEMBER_OBJECT); } @Override @@ -168,7 +135,9 @@ public void onClick(View view) { } @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/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(); + } +}