Skip to content

Commit

Permalink
Merge pull request #63 from OpenSRP/make-heights-weights-sync-time-co…
Browse files Browse the repository at this point in the history
…nfigurable

Make height & weight sync time configurable in client apps
  • Loading branch information
ekigamba authored Feb 27, 2020
2 parents 1fc0dec + 05928df commit 52765bc
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 13 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=1.1.18-SNAPSHOT
VERSION_NAME=1.1.19-SNAPSHOT
VERSION_CODE=1
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Growth Monitoring Application
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.smartregister.growthmonitoring;

import android.support.annotation.NonNull;

import org.smartregister.Context;
import org.smartregister.growthmonitoring.repository.HeightRepository;
import org.smartregister.growthmonitoring.repository.HeightZScoreRepository;
Expand All @@ -10,6 +12,8 @@
import org.smartregister.repository.EventClientRepository;
import org.smartregister.repository.Repository;

import java.util.concurrent.TimeUnit;

/**
* Created by koros on 2/3/16.
*/
Expand All @@ -28,6 +32,8 @@ public class GrowthMonitoringLibrary {
private int databaseVersion;
private AppProperties appProperties;

private long growthMonitoringSyncTime = -1;

private GrowthMonitoringLibrary(Context context, Repository repository, int applicationVersion, int databaseVersion) {
this.repository = repository;
this.context = context;
Expand Down Expand Up @@ -125,4 +131,20 @@ public AppProperties getAppProperties() {
public void setAppProperties(AppProperties appProperties) {
this.appProperties = appProperties;
}

public long getGrowthMonitoringSyncTime() {
if (growthMonitoringSyncTime == -1) {
setGrowthMonitoringSyncTime(BuildConfig.GROWTH_MONITORING_SYNC_TIME);
}

return growthMonitoringSyncTime;
}

public void setGrowthMonitoringSyncTime(int growthMonitoringSyncTimeInHours) {
setGrowthMonitoringSyncTime(growthMonitoringSyncTimeInHours, TimeUnit.HOURS);
}

public void setGrowthMonitoringSyncTime(int growthMonitoringSyncTime, @NonNull TimeUnit timeUnit) {
this.growthMonitoringSyncTime = timeUnit.toMinutes(growthMonitoringSyncTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,13 @@ private void getHeight(Cursor cursor, List<Height> heights, Double zScore, Date
heights.add(height);
}

public List<Height> findUnSyncedBeforeTime(int hours) {
public List<Height> findUnSyncedBeforeTime(int minutes) {
List<Height> heights = new ArrayList<>();
Cursor cursor = null;
try {

try {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.HOUR_OF_DAY, -hours);
calendar.add(Calendar.MINUTE, -minutes);

Long time = calendar.getTimeInMillis();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,13 +331,13 @@ private void getWeight(Cursor cursor, List<Weight> weights, Double zScore, Date
weights.add(weight);
}

public List<Weight> findUnSyncedBeforeTime(int hours) {
public List<Weight> findUnSyncedBeforeTime(int minutes) {
List<Weight> weights = new ArrayList<>();
Cursor cursor = null;
try {

Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.HOUR_OF_DAY, -hours);
calendar.add(Calendar.MINUTE, -minutes);

long time = calendar.getTimeInMillis();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected void onHandleIntent(Intent intent) {

try {
List<Height> heights =
heightRepository.findUnSyncedBeforeTime(GrowthMonitoringConstants.GROWTH_MONITORING_SYNC_TIME);
heightRepository.findUnSyncedBeforeTime((int) GrowthMonitoringLibrary.getInstance().getGrowthMonitoringSyncTime());
if (!heights.isEmpty()) {
for (Height height : heights) {

Expand All @@ -57,9 +57,9 @@ protected void onHandleIntent(Intent intent) {
//Zscore
JSONObject zScoreObject = new JSONObject();
zScoreObject.put(GrowthMonitoringConstants.JsonForm.KEY, "Z_Score_Height_Age");
zScoreObject.put(GrowthMonitoringConstants.JsonForm.OPENMRS_ENTITY, "concept");
zScoreObject.put(GrowthMonitoringConstants.JsonForm.OPENMRS_ENTITY, "");
zScoreObject.put(GrowthMonitoringConstants.JsonForm.OPENMRS_ENTITY_ID,
"159429AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
"");
zScoreObject.put(GrowthMonitoringConstants.JsonForm.OPENMRS_ENTITY_PARENT, "");
zScoreObject.put(GrowthMonitoringConstants.JsonForm.OPENMRS_DATA_TYPE, "calculation");
zScoreObject.put(GrowthMonitoringConstants.JsonForm.VALUE, height.getZScore());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected void onHandleIntent(Intent intent) {

try {
List<Weight> weights =
weightRepository.findUnSyncedBeforeTime(GrowthMonitoringConstants.GROWTH_MONITORING_SYNC_TIME);
weightRepository.findUnSyncedBeforeTime((int) GrowthMonitoringLibrary.getInstance().getGrowthMonitoringSyncTime());
if (!weights.isEmpty()) {
for (Weight weight : weights) {

Expand All @@ -59,7 +59,7 @@ protected void onHandleIntent(Intent intent) {
zScoreObject.put(GrowthMonitoringConstants.JsonForm.KEY, "Z_Score_Weight_Age");
zScoreObject.put(GrowthMonitoringConstants.JsonForm.OPENMRS_ENTITY, "concept");
zScoreObject.put(GrowthMonitoringConstants.JsonForm.OPENMRS_ENTITY_ID,
"159428AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
"1854AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
zScoreObject.put(GrowthMonitoringConstants.JsonForm.OPENMRS_ENTITY_PARENT, "");
zScoreObject.put(GrowthMonitoringConstants.JsonForm.OPENMRS_DATA_TYPE, "calculation");
zScoreObject.put(GrowthMonitoringConstants.JsonForm.VALUE, weight.getZScore());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.smartregister.growthmonitoring.util;

import org.smartregister.growthmonitoring.BuildConfig;

/**
* Created by keyman on 26/07/2017.
Expand All @@ -12,7 +11,6 @@ public class GrowthMonitoringConstants {

public static final String CHILD_TABLE_NAME = "ec_client";

public static final int GROWTH_MONITORING_SYNC_TIME = BuildConfig.GROWTH_MONITORING_SYNC_TIME;
public static final String FIRST_NAME = "first_name";
public static final String LAST_NAME = "last_name";
public static final String FEMALE = "female";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.smartregister.growthmonitoring;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.util.ReflectionHelpers;
import org.smartregister.Context;
import org.smartregister.repository.Repository;

import static org.junit.Assert.assertEquals;

@RunWith(RobolectricTestRunner.class)
@Config(sdk = {27})
public class GrowthMonitoringLibraryTest {

@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();

@Before
public void setUp() throws Exception {
GrowthMonitoringLibrary.init(Mockito.mock(Context.class), Mockito.mock(Repository.class), 1, 1);
}

@After
public void tearDown() throws Exception {
ReflectionHelpers.setStaticField(GrowthMonitoringLibrary.class, "instance", null);
}

@Test
public void getGrowthMonitoringSyncTimeShouldReturnDefault15hoursInMinutes() {
assertEquals(60 * 15, GrowthMonitoringLibrary.getInstance().getGrowthMonitoringSyncTime());
}
}

0 comments on commit 52765bc

Please sign in to comment.