From 7a2d3517716b97e6af7cc07b9adcd333f5086a3c Mon Sep 17 00:00:00 2001 From: Hamza Ahmed Khan Date: Tue, 16 May 2023 12:14:23 +0500 Subject: [PATCH 1/5] Optimize Growth Chart Loading Time --- opensrp-growth-monitoring/build.gradle | 4 ++ .../fragment/HeightMonitoringFragment.java | 68 ++++++++++++++----- .../WeightForHeightMonitoringFragment.java | 60 +++++++++++++--- .../fragment/WeightMonitoringFragment.java | 66 +++++++++++++----- .../res/layout/growth_monitoring_fragment.xml | 32 +++++++++ .../src/main/res/values/strings.xml | 1 + 6 files changed, 188 insertions(+), 43 deletions(-) diff --git a/opensrp-growth-monitoring/build.gradle b/opensrp-growth-monitoring/build.gradle index 9c96171..4750e5c 100644 --- a/opensrp-growth-monitoring/build.gradle +++ b/opensrp-growth-monitoring/build.gradle @@ -79,6 +79,10 @@ android { includeAndroidResources = true } } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } } tasks.withType(Test) { diff --git a/opensrp-growth-monitoring/src/main/java/org/smartregister/growthmonitoring/fragment/HeightMonitoringFragment.java b/opensrp-growth-monitoring/src/main/java/org/smartregister/growthmonitoring/fragment/HeightMonitoringFragment.java index 758cd40..f103ab0 100644 --- a/opensrp-growth-monitoring/src/main/java/org/smartregister/growthmonitoring/fragment/HeightMonitoringFragment.java +++ b/opensrp-growth-monitoring/src/main/java/org/smartregister/growthmonitoring/fragment/HeightMonitoringFragment.java @@ -2,7 +2,11 @@ import android.graphics.DashPathEffect; import android.os.Bundle; + +import androidx.annotation.NonNull; +import androidx.constraintlayout.widget.ConstraintLayout; import androidx.fragment.app.Fragment; + import android.util.Log; import android.util.TypedValue; import android.view.Gravity; @@ -10,6 +14,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.ImageButton; +import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.TableLayout; import android.widget.TableRow; @@ -49,6 +54,9 @@ public class HeightMonitoringFragment extends Fragment { private boolean isExpanded = false; private String dobString; private Gender gender; + private LinearLayout progressBar; + private ConstraintLayout chartLayout; + private ConstraintLayout tableLayout; public static HeightMonitoringFragment createInstance(String dobString, Gender gender, List heights) { HeightMonitoringFragment heightMonitoringFragment = new HeightMonitoringFragment(); @@ -60,23 +68,51 @@ public static HeightMonitoringFragment createInstance(String dobString, Gender g @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - final ViewGroup heightTabView = (ViewGroup) inflater.inflate(R.layout.growth_monitoring_fragment, container, false); - heightTabView.setFilterTouchesWhenObscured(true); - final ImageButton scrollButton = heightTabView.findViewById(R.id.scroll_button); - CustomFontTextView textMetricLabel = heightTabView.findViewById(R.id.metric_label); - textMetricLabel.setText(getActivity().getString(R.string.height)); - - Date dob = getDate(); - scrollButtonClickAction(heightTabView, scrollButton); - - try { - refreshGrowthChart(heightTabView, getGender(), dob); - refreshPreviousHeightsTable(heightTabView, getGender(), dob); - } catch (Exception e) { - Timber.e(e); - } + return inflater.inflate(R.layout.growth_monitoring_fragment, container, false); + } + + @Override + public void onViewCreated(@NonNull View heightTabView, @androidx.annotation.Nullable Bundle savedInstanceState) { + super.onViewCreated(heightTabView, savedInstanceState); + initViews(heightTabView); + showLoader(); + new Thread(() -> { + try { + heightTabView.setFilterTouchesWhenObscured(true); + final ImageButton scrollButton = heightTabView.findViewById(R.id.scroll_button); + CustomFontTextView textMetricLabel = heightTabView.findViewById(R.id.metric_label); + textMetricLabel.setText(requireActivity().getString(R.string.height)); + + Date dob = getDate(); + scrollButtonClickAction((ViewGroup) heightTabView, scrollButton); + + refreshGrowthChart(heightTabView, getGender(), dob); + refreshPreviousHeightsTable(heightTabView, getGender(), dob); + } catch (Exception e) { + Timber.e(Log.getStackTraceString(e)); + } + if (isVisible()) + requireActivity().runOnUiThread(this::hideLoader); + }).start(); + + } + + private void initViews(View view) { + progressBar = view.findViewById(R.id.progress_chart); + chartLayout = view.findViewById(R.id.growth_chart_layout); + tableLayout = view.findViewById(R.id.growth_dialog_table_layout); + } + + private void hideLoader() { + progressBar.setVisibility(View.GONE); + chartLayout.setVisibility(View.VISIBLE); + tableLayout.setVisibility(View.VISIBLE); + } - return heightTabView; + private void showLoader() { + progressBar.setVisibility(View.VISIBLE); + chartLayout.setVisibility(View.GONE); + tableLayout.setVisibility(View.GONE); } @Nullable diff --git a/opensrp-growth-monitoring/src/main/java/org/smartregister/growthmonitoring/fragment/WeightForHeightMonitoringFragment.java b/opensrp-growth-monitoring/src/main/java/org/smartregister/growthmonitoring/fragment/WeightForHeightMonitoringFragment.java index 7c5968d..d1b6950 100644 --- a/opensrp-growth-monitoring/src/main/java/org/smartregister/growthmonitoring/fragment/WeightForHeightMonitoringFragment.java +++ b/opensrp-growth-monitoring/src/main/java/org/smartregister/growthmonitoring/fragment/WeightForHeightMonitoringFragment.java @@ -2,7 +2,12 @@ import android.graphics.DashPathEffect; import android.os.Bundle; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.constraintlayout.widget.ConstraintLayout; import androidx.fragment.app.Fragment; + import android.util.Log; import android.util.TypedValue; import android.view.Gravity; @@ -10,6 +15,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.ImageButton; +import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.TableLayout; import android.widget.TableRow; @@ -53,6 +59,9 @@ public class WeightForHeightMonitoringFragment extends Fragment { private Date dob; private List weightHeights; private Height currentHeight; + private LinearLayout progressBar; + private ConstraintLayout chartLayout; + private ConstraintLayout tableLayout; public static WeightForHeightMonitoringFragment createInstance(Gender gender, String dobString, List weights, List heights) { WeightForHeightMonitoringFragment weightMonitoringFragment = new WeightForHeightMonitoringFragment(); @@ -80,23 +89,52 @@ private void setDob(String dobString) { @Override public View onCreateView(@NotNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - final View weightHeightTableView = inflater.inflate(R.layout.growth_monitoring_fragment, container, false); - weightHeightTableView.setFilterTouchesWhenObscured(true); - if (getActivity() != null) { - final ImageButton scrollButton = weightHeightTableView.findViewById(R.id.scroll_button); - CustomFontTextView weightMetric = weightHeightTableView.findViewById(R.id.column_one_metric); - weightMetric.setText(getActivity().getString(R.string.weight)); - CustomFontTextView heightMetric = weightHeightTableView.findViewById(R.id.metric_label); - heightMetric.setText(getActivity().getString(R.string.height)); - scrollButtonClickAction(weightHeightTableView, scrollButton); + return inflater.inflate(R.layout.growth_monitoring_fragment, container, false); + } + + @Override + public void onViewCreated(@NonNull View weightHeightTableView, @Nullable Bundle savedInstanceState) { + super.onViewCreated(weightHeightTableView, savedInstanceState); + initViews(weightHeightTableView); + showLoader(); + new Thread(() -> { try { + weightHeightTableView.setFilterTouchesWhenObscured(true); + final ImageButton scrollButton = weightHeightTableView.findViewById(R.id.scroll_button); + scrollButtonClickAction(weightHeightTableView, scrollButton); + refreshGrowthChart(weightHeightTableView); refreshPreviousWeightHeightsTable(weightHeightTableView); + } catch (Exception e) { Timber.e(e); } - } - return weightHeightTableView; + if (isVisible()) + requireActivity().runOnUiThread(this::hideLoader); + }).start(); + } + + private void initViews(View view) { + progressBar = view.findViewById(R.id.progress_chart); + chartLayout = view.findViewById(R.id.growth_chart_layout); + tableLayout = view.findViewById(R.id.growth_dialog_table_layout); + + CustomFontTextView weightMetric = view.findViewById(R.id.column_one_metric); + weightMetric.setText(requireActivity().getString(R.string.weight)); + CustomFontTextView heightMetric = view.findViewById(R.id.metric_label); + heightMetric.setText(requireActivity().getString(R.string.height)); + } + + private void hideLoader() { + progressBar.setVisibility(View.GONE); + chartLayout.setVisibility(View.VISIBLE); + tableLayout.setVisibility(View.VISIBLE); + } + + private void showLoader() { + progressBar.setVisibility(View.VISIBLE); + chartLayout.setVisibility(View.GONE); + tableLayout.setVisibility(View.GONE); } private void refreshPreviousWeightHeightsTable(View weightHeightTableView) { diff --git a/opensrp-growth-monitoring/src/main/java/org/smartregister/growthmonitoring/fragment/WeightMonitoringFragment.java b/opensrp-growth-monitoring/src/main/java/org/smartregister/growthmonitoring/fragment/WeightMonitoringFragment.java index 105d85d..cafa6f1 100644 --- a/opensrp-growth-monitoring/src/main/java/org/smartregister/growthmonitoring/fragment/WeightMonitoringFragment.java +++ b/opensrp-growth-monitoring/src/main/java/org/smartregister/growthmonitoring/fragment/WeightMonitoringFragment.java @@ -2,6 +2,9 @@ import android.graphics.DashPathEffect; import android.os.Bundle; + +import androidx.annotation.NonNull; +import androidx.constraintlayout.widget.ConstraintLayout; import androidx.fragment.app.Fragment; import android.util.Log; import android.util.TypedValue; @@ -10,6 +13,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.ImageButton; +import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.TableLayout; import android.widget.TableRow; @@ -49,6 +53,9 @@ public class WeightMonitoringFragment extends Fragment { private boolean isExpanded = false; private String dobString; private Gender gender; + private LinearLayout progressBar; + private ConstraintLayout chartLayout; + private ConstraintLayout tableLayout; public static WeightMonitoringFragment createInstance(String dobString, Gender gender, List weights) { WeightMonitoringFragment weightMonitoringFragment = new WeightMonitoringFragment(); @@ -60,21 +67,48 @@ public static WeightMonitoringFragment createInstance(String dobString, Gender g @Override public View onCreateView(@NotNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - final View weightTabView = inflater.inflate(R.layout.growth_monitoring_fragment, container, false); - weightTabView.setFilterTouchesWhenObscured(true); - final ImageButton scrollButton = weightTabView.findViewById(R.id.scroll_button); - CustomFontTextView textMetricLabel = weightTabView.findViewById(R.id.metric_label); - textMetricLabel.setText(getActivity().getString(R.string.weight)); - Date dob = getDate(); - scrollButtonClickAction(weightTabView, scrollButton); - try { - refreshGrowthChart(weightTabView, getGender(), dob); - refreshPreviousWeightsTable(weightTabView, getGender(), dob); - } catch (Exception e) { - Timber.e(e); - } + return inflater.inflate(R.layout.growth_monitoring_fragment, container, false); + } + + @Override + public void onViewCreated(@NonNull View weightTabView, @androidx.annotation.Nullable Bundle savedInstanceState) { + super.onViewCreated(weightTabView, savedInstanceState); + initViews(weightTabView); + showLoader(); + new Thread(() -> { + try { + weightTabView.setFilterTouchesWhenObscured(true); + final ImageButton scrollButton = weightTabView.findViewById(R.id.scroll_button); + CustomFontTextView textMetricLabel = weightTabView.findViewById(R.id.metric_label); + textMetricLabel.setText(requireActivity().getString(R.string.weight)); + Date dob = getDate(); + scrollButtonClickAction(weightTabView, scrollButton); + refreshGrowthChart(weightTabView, getGender(), dob); + refreshPreviousWeightsTable(weightTabView, getGender(), dob); + } catch (Exception e) { + Timber.e(Log.getStackTraceString(e)); + } + if (isVisible()) + requireActivity().runOnUiThread(this::hideLoader); + }).start(); + } + + private void initViews(View weightTabView) { + progressBar = weightTabView.findViewById(R.id.progress_chart); + chartLayout = weightTabView.findViewById(R.id.growth_chart_layout); + tableLayout = weightTabView.findViewById(R.id.growth_dialog_table_layout); + } + + private void hideLoader() { + progressBar.setVisibility(View.GONE); + chartLayout.setVisibility(View.VISIBLE); + tableLayout.setVisibility(View.VISIBLE); + } - return weightTabView; + private void showLoader() { + progressBar.setVisibility(View.VISIBLE); + chartLayout.setVisibility(View.GONE); + tableLayout.setVisibility(View.GONE); } @Nullable @@ -119,9 +153,9 @@ private void refreshGrowthChart(View parent, Gender gender, Date dob) { double maxAge = minAge + GrowthMonitoringConstants.GRAPH_MONTHS_TIMELINE; List lines = new ArrayList<>(); for (int z = -3; z <= 3; z++) { - if (z != 1 && z != -1) { + if (z != 1 && z != -1 && isVisible()) { Line curLine = getZScoreLine(gender, minAge, maxAge, z, - getActivity().getResources().getColor(WeightZScore.getZScoreColor(z))); + requireActivity().getResources().getColor(WeightZScore.getZScoreColor(z))); if (z == -3) { curLine.setPathEffect(new DashPathEffect(new float[]{10, 20}, 0)); } diff --git a/opensrp-growth-monitoring/src/main/res/layout/growth_monitoring_fragment.xml b/opensrp-growth-monitoring/src/main/res/layout/growth_monitoring_fragment.xml index 21908fe..ace98ca 100644 --- a/opensrp-growth-monitoring/src/main/res/layout/growth_monitoring_fragment.xml +++ b/opensrp-growth-monitoring/src/main/res/layout/growth_monitoring_fragment.xml @@ -4,6 +4,38 @@ android:layout_width="match_parent" android:layout_height="match_parent"> + + + + + + + + User Image The patient weight cannot be empty. Child height should not be less than 0cm and not above 100 cms. + Loading Chart… \ No newline at end of file From 82c793d5def8d800ca0ec508208952592d93730e Mon Sep 17 00:00:00 2001 From: Hamza Ahmed Khan Date: Tue, 16 May 2023 13:35:50 +0500 Subject: [PATCH 2/5] Fix wrong UI thread exception for charts --- .../fragment/HeightMonitoringFragment.java | 16 ++++++---- .../WeightForHeightMonitoringFragment.java | 15 +++++---- .../fragment/WeightMonitoringFragment.java | 32 +++++++++---------- 3 files changed, 35 insertions(+), 28 deletions(-) diff --git a/opensrp-growth-monitoring/src/main/java/org/smartregister/growthmonitoring/fragment/HeightMonitoringFragment.java b/opensrp-growth-monitoring/src/main/java/org/smartregister/growthmonitoring/fragment/HeightMonitoringFragment.java index f103ab0..650bb36 100644 --- a/opensrp-growth-monitoring/src/main/java/org/smartregister/growthmonitoring/fragment/HeightMonitoringFragment.java +++ b/opensrp-growth-monitoring/src/main/java/org/smartregister/growthmonitoring/fragment/HeightMonitoringFragment.java @@ -233,8 +233,6 @@ private void refreshPreviousHeightsTable(final View heightTabView, Gender gender params.span = 3; divider.setLayoutParams(params); divider.setBackgroundColor(getResources().getColor(R.color.client_list_header_dark_grey)); - dividerRow.addView(divider); - tableLayout.addView(dividerRow); TableRow curRow = new TableRow(heightTabView.getContext()); @@ -245,7 +243,6 @@ private void refreshPreviousHeightsTable(final View heightTabView, Gender gender ageTextView.setText(DateUtil.getDuration(height.getDate().getTime() - dob.getTime())); ageTextView.setGravity(Gravity.START | Gravity.CENTER_VERTICAL); ageTextView.setTextColor(getResources().getColor(R.color.client_list_grey)); - curRow.addView(ageTextView); TextView heightTextView = new TextView(heightTabView.getContext()); heightTextView.setHeight(getResources().getDimensionPixelSize(R.dimen.table_contents_text_height)); @@ -254,7 +251,6 @@ private void refreshPreviousHeightsTable(final View heightTabView, Gender gender heightTextView.setGravity(Gravity.START | Gravity.CENTER_VERTICAL); heightTextView.setText(String.format("%s %s", String.valueOf(height.getCm()), getString(R.string.cm))); heightTextView.setTextColor(getResources().getColor(R.color.client_list_grey)); - curRow.addView(heightTextView); TextView zScoreTextView = new TextView(heightTabView.getContext()); zScoreTextView.setHeight(getResources().getDimensionPixelSize(R.dimen.table_contents_text_height)); @@ -269,8 +265,16 @@ private void refreshPreviousHeightsTable(final View heightTabView, Gender gender zScoreTextView.setTextColor(getResources().getColor(HeightZScore.getZScoreColor(zScore))); zScoreTextView.setText(String.valueOf(zScore)); } - curRow.addView(zScoreTextView); - tableLayout.addView(curRow); + + requireActivity().runOnUiThread(() -> { + dividerRow.addView(divider); + tableLayout.addView(dividerRow); + curRow.addView(ageTextView); + curRow.addView(heightTextView); + + curRow.addView(zScoreTextView); + tableLayout.addView(curRow); + }); } //Now set the expand button if items are too many diff --git a/opensrp-growth-monitoring/src/main/java/org/smartregister/growthmonitoring/fragment/WeightForHeightMonitoringFragment.java b/opensrp-growth-monitoring/src/main/java/org/smartregister/growthmonitoring/fragment/WeightForHeightMonitoringFragment.java index d1b6950..9f48792 100644 --- a/opensrp-growth-monitoring/src/main/java/org/smartregister/growthmonitoring/fragment/WeightForHeightMonitoringFragment.java +++ b/opensrp-growth-monitoring/src/main/java/org/smartregister/growthmonitoring/fragment/WeightForHeightMonitoringFragment.java @@ -151,8 +151,6 @@ private void refreshPreviousWeightHeightsTable(View weightHeightTableView) { params.span = 3; divider.setLayoutParams(params); divider.setBackgroundColor(getResources().getColor(R.color.client_list_header_dark_grey)); - dividerRow.addView(divider); - tableLayout.addView(dividerRow); TableRow curRow = new TableRow(weightHeightTableView.getContext()); @@ -163,7 +161,6 @@ private void refreshPreviousWeightHeightsTable(View weightHeightTableView) { weightTextView.setText(String.format("%s %s", String.valueOf(weightHeight.getWeight().getKg()), getString(R.string.kg))); weightTextView.setGravity(Gravity.START | Gravity.CENTER_VERTICAL); weightTextView.setTextColor(getResources().getColor(R.color.client_list_grey)); - curRow.addView(weightTextView); TextView heightTextView = new TextView(weightHeightTableView.getContext()); heightTextView.setHeight(getResources().getDimensionPixelSize(R.dimen.table_contents_text_height)); @@ -172,7 +169,6 @@ private void refreshPreviousWeightHeightsTable(View weightHeightTableView) { heightTextView.setGravity(Gravity.START | Gravity.CENTER_VERTICAL); heightTextView.setText(String.format("%s %s", String.valueOf(weightHeight.getHeight().getCm()), getString(R.string.cm))); heightTextView.setTextColor(getResources().getColor(R.color.client_list_grey)); - curRow.addView(heightTextView); TextView zScoreTextView = new TextView(weightHeightTableView.getContext()); zScoreTextView.setHeight(getResources().getDimensionPixelSize(R.dimen.table_contents_text_height)); @@ -184,8 +180,15 @@ private void refreshPreviousWeightHeightsTable(View weightHeightTableView) { zScoreTextView.setTextColor(getResources().getColor(HeightZScore.getZScoreColor(zScore))); zScoreTextView.setText(String.valueOf(zScore)); - curRow.addView(zScoreTextView); - tableLayout.addView(curRow); + requireActivity().runOnUiThread(() -> { + dividerRow.addView(divider); + tableLayout.addView(dividerRow); + curRow.addView(weightTextView); + curRow.addView(heightTextView); + + curRow.addView(zScoreTextView); + tableLayout.addView(curRow); + }); } //Now set the expand button if items are too many diff --git a/opensrp-growth-monitoring/src/main/java/org/smartregister/growthmonitoring/fragment/WeightMonitoringFragment.java b/opensrp-growth-monitoring/src/main/java/org/smartregister/growthmonitoring/fragment/WeightMonitoringFragment.java index cafa6f1..504c0f5 100644 --- a/opensrp-growth-monitoring/src/main/java/org/smartregister/growthmonitoring/fragment/WeightMonitoringFragment.java +++ b/opensrp-growth-monitoring/src/main/java/org/smartregister/growthmonitoring/fragment/WeightMonitoringFragment.java @@ -230,8 +230,6 @@ private void refreshPreviousWeightsTable(final View weightTabView, Gender gender params.span = 3; divider.setLayoutParams(params); divider.setBackgroundColor(getResources().getColor(R.color.client_list_header_dark_grey)); - dividerRow.addView(divider); - tableLayout.addView(dividerRow); TableRow curRow = new TableRow(weightTabView.getContext()); @@ -242,7 +240,6 @@ private void refreshPreviousWeightsTable(final View weightTabView, Gender gender ageTextView.setText(DateUtil.getDuration(weight.getDate().getTime() - dob.getTime())); ageTextView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL); ageTextView.setTextColor(getResources().getColor(R.color.client_list_grey)); - curRow.addView(ageTextView); TextView weightTextView = new TextView(weightTabView.getContext()); weightTextView.setHeight(getResources().getDimensionPixelSize(R.dimen.table_contents_text_height)); @@ -251,7 +248,6 @@ private void refreshPreviousWeightsTable(final View weightTabView, Gender gender weightTextView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL); weightTextView.setText(String.format("%s %s", String.valueOf(weight.getKg()), getString(R.string.kg))); weightTextView.setTextColor(getResources().getColor(R.color.client_list_grey)); - curRow.addView(weightTextView); TextView zScoreTextView = new TextView(weightTabView.getContext()); zScoreTextView.setHeight(getResources().getDimensionPixelSize(R.dimen.table_contents_text_height)); @@ -266,22 +262,26 @@ private void refreshPreviousWeightsTable(final View weightTabView, Gender gender zScoreTextView.setTextColor(getResources().getColor(WeightZScore.getZScoreColor(zScore))); zScoreTextView.setText(String.valueOf(zScore)); } - curRow.addView(zScoreTextView); - tableLayout.addView(curRow); + requireActivity().runOnUiThread(() -> { + dividerRow.addView(divider); + tableLayout.addView(dividerRow); + curRow.addView(ageTextView); + curRow.addView(weightTextView); + + curRow.addView(zScoreTextView); + tableLayout.addView(curRow); + }); } //Now set the expand button if items are too many final ScrollView weightsTableScrollView = weightTabView.findViewById(R.id.growth_scroll_view); - GrowthMonitoringUtils.getHeight(weightsTableScrollView, new ViewMeasureListener() { - @Override - public void onCompletedMeasuring(int height) { - int childHeight = weightsTableScrollView.getChildAt(0).getMeasuredHeight(); - ImageButton scrollButton = weightTabView.findViewById(R.id.scroll_button); - if (childHeight > height) { - scrollButton.setVisibility(View.VISIBLE); - } else { - scrollButton.setVisibility(View.GONE); - } + GrowthMonitoringUtils.getHeight(weightsTableScrollView, height -> { + int childHeight = weightsTableScrollView.getChildAt(0).getMeasuredHeight(); + ImageButton scrollButton = weightTabView.findViewById(R.id.scroll_button); + if (childHeight > height) { + scrollButton.setVisibility(View.VISIBLE); + } else { + scrollButton.setVisibility(View.GONE); } }); } From 7ba609c32022181b7d8267fb9845a80b2fb486be Mon Sep 17 00:00:00 2001 From: Hamza Ahmed Khan Date: Tue, 16 May 2023 17:46:37 +0500 Subject: [PATCH 3/5] Bump up version --- gradle.properties | 2 +- sample/build.gradle | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/gradle.properties b/gradle.properties index 087d654..266f21f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -VERSION_NAME=3.0.0-SNAPSHOT +VERSION_NAME=3.0.1-SNAPSHOT VERSION_CODE=1 GROUP=org.smartregister POM_SETTING_DESCRIPTION=OpenSRP Client Growth Monitoring Application diff --git a/sample/build.gradle b/sample/build.gradle index 1c3e2df..09b4c00 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -78,8 +78,8 @@ android { } compileOptions { - sourceCompatibility 1.8 - targetCompatibility 1.8 + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 } } From 08182b67af17ba3e615d42b4a3ad3b7e988a6c71 Mon Sep 17 00:00:00 2001 From: Hamza Ahmed Khan Date: Tue, 16 May 2023 17:17:42 +0500 Subject: [PATCH 4/5] Fix failing tests --- .../fragment/HeightMonitoringFragmentTest.java | 11 ++++++----- .../WeightForHeightMonitoringFragmentTest.java | 12 +++++++----- .../fragment/WeightMonitoringFragmentTest.java | 12 +++++++----- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/HeightMonitoringFragmentTest.java b/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/HeightMonitoringFragmentTest.java index 876e444..8b73ffe 100644 --- a/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/HeightMonitoringFragmentTest.java +++ b/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/HeightMonitoringFragmentTest.java @@ -33,16 +33,17 @@ public void setUp() { } @Test - public void testThatHeightForAgeViewIsCreated() { + public void testThatHeightForAgeViewIsCreated() throws InterruptedException { HeightMonitoringFragment fragment = Mockito.spy(HeightMonitoringFragment.createInstance("2018-09-12", Gender.FEMALE, getHeights())); activity.getSupportFragmentManager().beginTransaction().add(fragment, "Height-for-Age-Boys").commitNow(); View view = fragment.getView(); Assert.assertNotNull(view); Assert.assertTrue(view instanceof ConstraintLayout); ConstraintLayout constraintLayout = (ConstraintLayout) view; - Assert.assertEquals(constraintLayout.getChildCount(), 2); - Assert.assertEquals(((CustomFontTextView)constraintLayout.findViewById(R.id.column_one_metric)).getText(), constraintLayout.getContext().getString(R.string.age)); - Assert.assertEquals(((CustomFontTextView)constraintLayout.findViewById(R.id.metric_label)).getText(), constraintLayout.getContext().getString(R.string.height)); - Assert.assertEquals(((CustomFontTextView)constraintLayout.findViewById(R.id.column_three_metric)).getText(), constraintLayout.getContext().getString(R.string.z_score)); + Thread.sleep(3000); + Assert.assertEquals(3, constraintLayout.getChildCount()); + Assert.assertEquals(constraintLayout.getContext().getString(R.string.age), ((CustomFontTextView)constraintLayout.findViewById(R.id.column_one_metric)).getText()); + Assert.assertEquals(constraintLayout.getContext().getString(R.string.height), ((CustomFontTextView)constraintLayout.findViewById(R.id.metric_label)).getText()); + Assert.assertEquals(constraintLayout.getContext().getString(R.string.z_score), ((CustomFontTextView)constraintLayout.findViewById(R.id.column_three_metric)).getText()); } } \ No newline at end of file diff --git a/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/WeightForHeightMonitoringFragmentTest.java b/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/WeightForHeightMonitoringFragmentTest.java index a06c59a..f4c4fb3 100644 --- a/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/WeightForHeightMonitoringFragmentTest.java +++ b/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/WeightForHeightMonitoringFragmentTest.java @@ -2,6 +2,7 @@ import androidx.constraintlayout.widget.ConstraintLayout; import androidx.fragment.app.FragmentActivity; + import android.view.View; import org.junit.Assert; @@ -29,16 +30,17 @@ public void setUp() { } @Test - public void testThatWeightForHeightViewIsCreated() { + public void testThatWeightForHeightViewIsCreated() throws InterruptedException { WeightForHeightMonitoringFragment fragment = WeightForHeightMonitoringFragment.createInstance(Gender.MALE, "2018-09-12", getWeights(), getHeights()); activity.getSupportFragmentManager().beginTransaction().add(fragment, "Weight-for-Height-Boys").commitNow(); View view = fragment.getView(); Assert.assertNotNull(view); Assert.assertTrue(view instanceof ConstraintLayout); ConstraintLayout constraintLayout = (ConstraintLayout) view; - Assert.assertEquals(constraintLayout.getChildCount(), 2); - Assert.assertEquals(((CustomFontTextView)constraintLayout.findViewById(R.id.column_one_metric)).getText(), constraintLayout.getContext().getString(R.string.weight)); - Assert.assertEquals(((CustomFontTextView)constraintLayout.findViewById(R.id.metric_label)).getText(), constraintLayout.getContext().getString(R.string.height)); - Assert.assertEquals(((CustomFontTextView)constraintLayout.findViewById(R.id.column_three_metric)).getText(), constraintLayout.getContext().getString(R.string.z_score)); + Thread.sleep(3000); + Assert.assertEquals(3, constraintLayout.getChildCount()); + Assert.assertEquals(constraintLayout.getContext().getString(R.string.weight), ((CustomFontTextView) constraintLayout.findViewById(R.id.column_one_metric)).getText()); + Assert.assertEquals(constraintLayout.getContext().getString(R.string.height), ((CustomFontTextView) constraintLayout.findViewById(R.id.metric_label)).getText()); + Assert.assertEquals(constraintLayout.getContext().getString(R.string.z_score), ((CustomFontTextView) constraintLayout.findViewById(R.id.column_three_metric)).getText()); } } \ No newline at end of file diff --git a/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/WeightMonitoringFragmentTest.java b/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/WeightMonitoringFragmentTest.java index 1a6ab97..89d063c 100644 --- a/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/WeightMonitoringFragmentTest.java +++ b/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/WeightMonitoringFragmentTest.java @@ -2,6 +2,7 @@ import androidx.constraintlayout.widget.ConstraintLayout; import androidx.fragment.app.FragmentActivity; + import android.view.View; import org.junit.Assert; @@ -33,17 +34,18 @@ public void setUp() { } @Test - public void testThatWeightForAgeViewIsCreated() { + public void testThatWeightForAgeViewIsCreated() throws InterruptedException { WeightMonitoringFragment fragment = Mockito.spy(WeightMonitoringFragment.createInstance("2018-09-12", Gender.MALE, getWeights())); activity.getSupportFragmentManager().beginTransaction().add(fragment, "Weight-for-Age-Boys").commitNow(); View view = fragment.getView(); Assert.assertNotNull(view); Assert.assertTrue(view instanceof ConstraintLayout); ConstraintLayout constraintLayout = (ConstraintLayout) view; - Assert.assertEquals(constraintLayout.getChildCount(), 2); - Assert.assertEquals(((CustomFontTextView)constraintLayout.findViewById(R.id.column_one_metric)).getText(), constraintLayout.getContext().getString(R.string.age)); - Assert.assertEquals(((CustomFontTextView)constraintLayout.findViewById(R.id.metric_label)).getText(), constraintLayout.getContext().getString(R.string.weight)); - Assert.assertEquals(((CustomFontTextView)constraintLayout.findViewById(R.id.column_three_metric)).getText(), constraintLayout.getContext().getString(R.string.z_score)); + Thread.sleep(3000); + Assert.assertEquals(3, constraintLayout.getChildCount()); + Assert.assertEquals(constraintLayout.getContext().getString(R.string.age), ((CustomFontTextView) constraintLayout.findViewById(R.id.column_one_metric)).getText()); + Assert.assertEquals(constraintLayout.getContext().getString(R.string.weight), ((CustomFontTextView) constraintLayout.findViewById(R.id.metric_label)).getText()); + Assert.assertEquals(constraintLayout.getContext().getString(R.string.z_score), ((CustomFontTextView) constraintLayout.findViewById(R.id.column_three_metric)).getText()); } } \ No newline at end of file From 8882f0dbdb9940dffb1ab45ef7ec1105f20862f4 Mon Sep 17 00:00:00 2001 From: Hamza Ahmed Khan Date: Wed, 17 May 2023 15:20:07 +0500 Subject: [PATCH 5/5] Increase code coverage --- .../HeightMonitoringFragmentTest.java | 28 +++++++++++++++++ ...WeightForHeightMonitoringFragmentTest.java | 4 ++- .../WeightMonitoringFragmentTest.java | 31 ++++++++++++++++++- 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/HeightMonitoringFragmentTest.java b/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/HeightMonitoringFragmentTest.java index 8b73ffe..2287b4f 100644 --- a/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/HeightMonitoringFragmentTest.java +++ b/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/HeightMonitoringFragmentTest.java @@ -4,6 +4,7 @@ import androidx.fragment.app.FragmentActivity; import android.view.View; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -11,10 +12,16 @@ import org.mockito.MockitoAnnotations; import org.opensrp.api.constants.Gender; import org.robolectric.Robolectric; +import org.robolectric.util.ReflectionHelpers; import org.smartregister.growthmonitoring.BaseUnitTest; +import org.smartregister.growthmonitoring.GrowthMonitoringLibrary; import org.smartregister.growthmonitoring.R; +import org.smartregister.growthmonitoring.domain.HeightZScore; +import org.smartregister.growthmonitoring.repository.HeightZScoreRepository; import org.smartregister.view.customcontrols.CustomFontTextView; +import java.util.ArrayList; + /** * Created by ndegwamartin on 2020-04-15. */ @@ -34,7 +41,16 @@ public void setUp() { @Test public void testThatHeightForAgeViewIsCreated() throws InterruptedException { + ArrayList heightZScores = new ArrayList<>(); + heightZScores.add(getHeightZScore(56)); + heightZScores.add(getHeightZScore(44)); +// + HeightZScoreRepository heightZScoreRepository = Mockito.spy(GrowthMonitoringLibrary.getInstance().heightZScoreRepository()); + Mockito.doReturn(heightZScores).when(heightZScoreRepository).findByGender(Mockito.any()); + ReflectionHelpers.setField(GrowthMonitoringLibrary.getInstance(), "heightZScoreRepository", heightZScoreRepository); + HeightMonitoringFragment fragment = Mockito.spy(HeightMonitoringFragment.createInstance("2018-09-12", Gender.FEMALE, getHeights())); + Mockito.doReturn(true).when(fragment).isVisible(); activity.getSupportFragmentManager().beginTransaction().add(fragment, "Height-for-Age-Boys").commitNow(); View view = fragment.getView(); Assert.assertNotNull(view); @@ -46,4 +62,16 @@ public void testThatHeightForAgeViewIsCreated() throws InterruptedException { Assert.assertEquals(constraintLayout.getContext().getString(R.string.height), ((CustomFontTextView)constraintLayout.findViewById(R.id.metric_label)).getText()); Assert.assertEquals(constraintLayout.getContext().getString(R.string.z_score), ((CustomFontTextView)constraintLayout.findViewById(R.id.column_three_metric)).getText()); } + + private HeightZScore getHeightZScore(int age) { + HeightZScore heightZScore = new HeightZScore(); + heightZScore.setGender(Gender.MALE); + heightZScore.setMonth(age); + return heightZScore; + } + + @After + public void destroy(){ + ReflectionHelpers.setField(GrowthMonitoringLibrary.getInstance(), "heightZScoreRepository", null); + } } \ No newline at end of file diff --git a/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/WeightForHeightMonitoringFragmentTest.java b/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/WeightForHeightMonitoringFragmentTest.java index f4c4fb3..94e9f6a 100644 --- a/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/WeightForHeightMonitoringFragmentTest.java +++ b/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/WeightForHeightMonitoringFragmentTest.java @@ -8,6 +8,7 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; +import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.opensrp.api.constants.Gender; import org.robolectric.Robolectric; @@ -31,7 +32,8 @@ public void setUp() { @Test public void testThatWeightForHeightViewIsCreated() throws InterruptedException { - WeightForHeightMonitoringFragment fragment = WeightForHeightMonitoringFragment.createInstance(Gender.MALE, "2018-09-12", getWeights(), getHeights()); + WeightForHeightMonitoringFragment fragment = Mockito.spy(WeightForHeightMonitoringFragment.createInstance(Gender.MALE, "2018-09-12", getWeights(), getHeights())); + Mockito.doReturn(true).when(fragment).isVisible(); activity.getSupportFragmentManager().beginTransaction().add(fragment, "Weight-for-Height-Boys").commitNow(); View view = fragment.getView(); Assert.assertNotNull(view); diff --git a/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/WeightMonitoringFragmentTest.java b/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/WeightMonitoringFragmentTest.java index 89d063c..eda9b84 100644 --- a/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/WeightMonitoringFragmentTest.java +++ b/opensrp-growth-monitoring/src/test/java/org/smartregister/growthmonitoring/fragment/WeightMonitoringFragmentTest.java @@ -5,6 +5,7 @@ import android.view.View; +import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -12,10 +13,17 @@ import org.mockito.MockitoAnnotations; import org.opensrp.api.constants.Gender; import org.robolectric.Robolectric; +import org.robolectric.util.ReflectionHelpers; import org.smartregister.growthmonitoring.BaseUnitTest; +import org.smartregister.growthmonitoring.GrowthMonitoringLibrary; import org.smartregister.growthmonitoring.R; +import org.smartregister.growthmonitoring.domain.WeightZScore; +import org.smartregister.growthmonitoring.repository.WeightZScoreRepository; import org.smartregister.view.customcontrols.CustomFontTextView; +import java.util.ArrayList; +import java.util.concurrent.TimeUnit; + /** * Created by ndegwamartin on 2020-04-15. */ @@ -35,17 +43,38 @@ public void setUp() { @Test public void testThatWeightForAgeViewIsCreated() throws InterruptedException { + ArrayList weightZScores = new ArrayList<>(); + weightZScores.add(getWeightZScore(56)); + weightZScores.add(getWeightZScore(44)); + + WeightZScoreRepository weightZScoreRepository = Mockito.spy(GrowthMonitoringLibrary.getInstance().weightZScoreRepository()); + Mockito.doReturn(weightZScores).when(weightZScoreRepository).findByGender(Mockito.any()); + ReflectionHelpers.setField(GrowthMonitoringLibrary.getInstance(), "weightZScoreRepository", weightZScoreRepository); + WeightMonitoringFragment fragment = Mockito.spy(WeightMonitoringFragment.createInstance("2018-09-12", Gender.MALE, getWeights())); + Mockito.doReturn(true).when(fragment).isVisible(); activity.getSupportFragmentManager().beginTransaction().add(fragment, "Weight-for-Age-Boys").commitNow(); View view = fragment.getView(); Assert.assertNotNull(view); Assert.assertTrue(view instanceof ConstraintLayout); ConstraintLayout constraintLayout = (ConstraintLayout) view; - Thread.sleep(3000); + TimeUnit.SECONDS.toMillis(3); Assert.assertEquals(3, constraintLayout.getChildCount()); Assert.assertEquals(constraintLayout.getContext().getString(R.string.age), ((CustomFontTextView) constraintLayout.findViewById(R.id.column_one_metric)).getText()); Assert.assertEquals(constraintLayout.getContext().getString(R.string.weight), ((CustomFontTextView) constraintLayout.findViewById(R.id.metric_label)).getText()); Assert.assertEquals(constraintLayout.getContext().getString(R.string.z_score), ((CustomFontTextView) constraintLayout.findViewById(R.id.column_three_metric)).getText()); } + private WeightZScore getWeightZScore(int age) { + WeightZScore weightZScore = new WeightZScore(); + weightZScore.setGender(Gender.MALE); + weightZScore.setMonth(age); + return weightZScore; + } + + @After + public void destroy(){ + ReflectionHelpers.setField(GrowthMonitoringLibrary.getInstance(), "weightZScoreRepository", null); + } + } \ No newline at end of file