Skip to content

Commit

Permalink
Merge pull request #96 from OpenSRP/hf_in_app_reporting
Browse files Browse the repository at this point in the history
Hf in app reporting
  • Loading branch information
paulinembabu authored May 5, 2020
2 parents d203d01 + d547a3f commit 66c6cf2
Show file tree
Hide file tree
Showing 24 changed files with 3,679 additions and 151 deletions.
5 changes: 2 additions & 3 deletions opensrp-chw-hf/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ android {
buildConfigField "String", 'opensrp_url_debug', '"https://boresha-afya-stage.smartregister.org/opensrp/"'
// buildConfigField "String", 'opensrp_url_debug', '"https://ba-unified-stage.smartregister.org/opensrp/"'
buildConfigField "boolean", 'SUPPORT_QR', 'true'
buildConfigField "int", "DATABASE_VERSION", '6'
buildConfigField "int", "DATABASE_VERSION", '7'
buildConfigField "int", "DATA_SYNC_DURATION_MINUTES", '15'
buildConfigField "long", "MAX_SERVER_TIME_DIFFERENCE", "1800000l"
buildConfigField "boolean", "TIME_CHECK", "false"
Expand Down Expand Up @@ -119,8 +119,7 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation('org.smartregister:opensrp-client-chw-core:1.1.66-SNAPSHOT@aar') {
implementation ('org.smartregister:opensrp-client-chw-core:1.1.67-SNAPSHOT@aar') {
transitive = true
exclude group: 'org.smartregister', module: 'opensrp-client-native-form'
exclude group: 'org.smartregister', module: 'opensrp-client-chw-referral'
Expand Down
20 changes: 19 additions & 1 deletion opensrp-chw-hf/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
android:name=".activity.HfStockInventoryReportActivity"
android:theme="@style/HFTheme.NoActionBar" />
<activity
android:name=".activity.ProviderStockUsageReportListActivity"
android:name=".activity.ProvidersReportListActivity"
android:theme="@style/HFTheme.NoActionBar" />
<activity
android:name="org.smartregister.chw.core.activity.CoreStockInventoryReportActivity"
Expand All @@ -151,6 +151,24 @@
android:name="org.smartregister.chw.core.activity.CoreStockInventoryItemDetailsReportActivity"
android:theme="@style/HFTheme.NoActionBar" />

<activity
android:name=".activity.ServiceActivity"
android:theme="@style/HFTheme.NoActionBar" />
<activity
android:name="org.smartregister.chw.core.activity.ReportSummaryActivity"
android:theme="@style/HFTheme.NoActionBar" />
<activity
android:name="org.smartregister.chw.core.activity.ServiceJsonFormActivity"
android:theme="@style/HFTheme.NoActionBar" />
<activity
android:name=".activity.HfHIA2ReportsActivity"
android:theme="@style/HFTheme.NoActionBar" />

<activity
android:name=".activity.InAppInventoryReportActivity"
android:theme="@style/HFTheme.NoActionBar" />


<meta-data
android:name="io.fabric.ApiKey"
android:value="3f94f9c3caeff331b3a2bd8c2182b663ad6a955a" />
Expand Down
1,192 changes: 1,105 additions & 87 deletions opensrp-chw-hf/src/main/assets/config/indicator-definitions.yml

Large diffs are not rendered by default.

113 changes: 113 additions & 0 deletions opensrp-chw-hf/src/main/assets/json.form/monthly_report.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
{
"count": "12",
"encounter_type": "HIA2 Monthly Report",
"entity_id": "",
"relational_id": "",
"metadata": {
"start": {
"openmrs_entity_parent": "",
"openmrs_entity": "concept",
"openmrs_data_type": "start",
"openmrs_entity_id": "163137AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
},
"end": {
"openmrs_entity_parent": "",
"openmrs_entity": "concept",
"openmrs_data_type": "end",
"openmrs_entity_id": "163138AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
},
"today": {
"openmrs_entity_parent": "",
"openmrs_entity": "encounter",
"openmrs_entity_id": "encounter_date"
},
"deviceid": {
"openmrs_entity_parent": "",
"openmrs_entity": "concept",
"openmrs_data_type": "deviceid",
"openmrs_entity_id": "163149AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
},
"subscriberid": {
"openmrs_entity_parent": "",
"openmrs_entity": "concept",
"openmrs_data_type": "subscriberid",
"openmrs_entity_id": "163150AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
},
"simserial": {
"openmrs_entity_parent": "",
"openmrs_entity": "concept",
"openmrs_data_type": "simserial",
"openmrs_entity_id": "163151AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
},
"phonenumber": {
"openmrs_entity_parent": "",
"openmrs_entity": "concept",
"openmrs_data_type": "phonenumber",
"openmrs_entity_id": "163152AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
},
"encounter_location": "",
"look_up": {
"entity_id": "",
"value": ""
}
},
"step1": {
"title": "Women Clients (1 of 12)",
"next": "step2",
"fields": []
},
"step2": {
"title": "Newborns and Children Clients (2 of 12)",
"next": "step3",
"fields": []
},
"step3": {
"title": "Households (3 of 12)",
"next": "step4",
"fields": []
},
"step4": {
"title": "Referrals given (4 of 12)",
"next": "step5",
"fields": []
},
"step5": {
"title": "Health education meetings (5 of 12)",
"next": "step6",
"fields": []
},
"step6": {
"title": "Birth and death information (6 of 12)",
"next": "step7",
"fields": []
},
"step7": {
"title": "Family Planning (7 of 12)",
"next": "step8",
"fields": []
},
"step8": {
"title": "Oral Contraceptive Pill (8 of 12)",
"next": "step9",
"fields": []
},
"step9": {
"title": "Condoms (9 of 12)",
"next": "step10",
"fields": []
},
"step10": {
"title": "Cycles (10 of 12)",
"next": "step11",
"fields": []
},
"step11": {
"title": "Counseling (11 of 12)",
"next": "step12",
"fields": []
},
"step12": {
"title": "Fp Referrals (12 of 12)",
"fields": []
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
package org.smartregister.chw.hf.activity;

import android.app.FragmentTransaction;
import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;

import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.tabs.TabLayout;

import org.json.JSONObject;
import org.smartregister.chw.core.activity.HIA2ReportsActivity;
import org.smartregister.chw.core.application.CoreChwApplication;
import org.smartregister.chw.core.domain.MonthlyTally;
import org.smartregister.chw.core.fragment.SendMonthlyDraftDialogFragment;
import org.smartregister.chw.core.task.FetchEditedMonthlyTalliesTask;
import org.smartregister.chw.core.task.StartDraftMonthlyFormTask;
import org.smartregister.chw.hf.R;
import org.smartregister.chw.hf.adapter.HfSectionsPagerAdapter;
import org.smartregister.chw.hf.fragment.HfDraftMonthlyFragment;
import org.smartregister.domain.Response;
import org.smartregister.repository.Hia2ReportRepository;
import org.smartregister.service.HTTPAgent;
import org.smartregister.util.Utils;
import org.smartregister.view.customcontrols.CustomFontTextView;

import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class HfHIA2ReportsActivity extends HIA2ReportsActivity {
protected AppBarLayout appBarLayout;
private HfSectionsPagerAdapter mSectionsPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_hf_hia2_reports);
Toolbar toolbar = findViewById(R.id.back_to_nav_toolbar);
CustomFontTextView toolBarTextView = toolbar.findViewById(R.id.toolbar_title);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
final Drawable upArrow = getResources().getDrawable(R.drawable.ic_arrow_back_white_24dp);
upArrow.setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_ATOP);
upArrow.setVisible(true, true);
actionBar.setHomeAsUpIndicator(upArrow);
actionBar.setElevation(0);
}
toolbar.setNavigationOnClickListener(v -> finish());
toolBarTextView.setOnClickListener(v -> finish());
toolBarTextView.setText(this.getString(R.string.service_activity_reporting));

appBarLayout = findViewById(R.id.app_bar);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
appBarLayout.setOutlineProvider(null);
}

tabLayout = findViewById(org.smartregister.chw.core.R.id.hia_tabs);
mSectionsPagerAdapter = new HfSectionsPagerAdapter(getSupportFragmentManager(), this);

mViewPager = findViewById(org.smartregister.chw.core.R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);

tabLayout.setupWithViewPager(mViewPager);
refreshDraftMonthlyTitle();
mSectionsPagerAdapter.getItem(0);
mViewPager.setCurrentItem(0);
}

@Override
protected Fragment currentFragment() {
if (mViewPager == null || mSectionsPagerAdapter == null) {
return null;
}

return mSectionsPagerAdapter.getItem(mViewPager.getCurrentItem());
}

@Override
public void refreshDraftMonthlyTitle() {
Utils.startAsyncTask(new FetchEditedMonthlyTalliesTask(new FetchEditedMonthlyTalliesTask.TaskListener() {
@Override
public void onPostExecute(final List<MonthlyTally> monthlyTallies) {
tabLayout.post(new Runnable() {
@Override
public void run() {
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
if (tab != null && tab.getText() != null && tab.getText().toString()
.contains(getString(org.smartregister.chw.core.R.string.hia2_draft_monthly))) {
tab.setText(String.format(
getString(org.smartregister.chw.core.R.string.hia2_draft_monthly_with_count),
monthlyTallies == null ? 0 : monthlyTallies.size()));
}
}
}
});
}
}), null);
}

@Override
public void startMonthlyReportForm(String formName, Date date) {
try {
Fragment currentFragment = currentFragment();
if (currentFragment instanceof HfDraftMonthlyFragment) {
Utils.startAsyncTask(new StartDraftMonthlyFormTask(this, date, formName), null);
}
} catch (Exception e) {
Log.e(TAG, Log.getStackTraceString(e));
}

}

@Override
protected void sendReport(final Date month) {
if (month != null) {
FragmentTransaction ft = getFragmentManager()
.beginTransaction();
android.app.Fragment prev = getFragmentManager()
.findFragmentByTag("SendMonthlyDraftDialogFragment");
if (prev != null) {
ft.remove(prev);
}

String monthString = new SimpleDateFormat("MMM yyyy", Locale.getDefault()).format(month);
// Create and show the dialog.
SendMonthlyDraftDialogFragment newFragment = SendMonthlyDraftDialogFragment
.newInstance(monthString,
new SimpleDateFormat("dd/MM/yy", Locale.getDefault()).format(Calendar.getInstance().getTime()),
new View.OnClickListener() {
@Override
public void onClick(View view) {
String[] params = new String[]{new SimpleDateFormat("yyyy-MM", Locale.getDefault()).format(month)};
Utils.startAsyncTask(new HfGenerateAndSendMonthlyTask(), params);
}
});
ft.add(newFragment, "SendMonthlyDraftDialogFragment");
ft.commitAllowingStateLoss();
}
}


protected class HfGenerateAndSendMonthlyTask extends GenerateAndSendMonthlyTask {

@Override
protected void onPostExecute(Boolean res) {
super.onPostExecute(res);
hideProgressDialog();
// update drafts view
refreshDraftMonthlyTitle();
Utils.startAsyncTask(new FetchEditedMonthlyTalliesTask(new FetchEditedMonthlyTalliesTask.TaskListener() {
@Override
public void onPostExecute(List<MonthlyTally> monthlyTallies) {
Fragment fragment = getSupportFragmentManager().findFragmentByTag("android:switcher:" + org.smartregister.chw.core.R.id.container + ":" + mViewPager.getCurrentItem());
((HfDraftMonthlyFragment) fragment).updateDraftsReportListView(monthlyTallies);
}
}), null);
}

@Override
protected void pushReportsToServer() {
final String REPORTS_SYNC_PATH = "/rest/report/add";
final Context context = CoreChwApplication.getInstance().getContext().applicationContext();
HTTPAgent httpAgent = CoreChwApplication.getInstance().getContext().getHttpAgent();
Hia2ReportRepository hia2ReportRepository = CoreChwApplication.getInstance().hia2ReportRepository();
try {
boolean keepSyncing = true;
int limit = 50;
while (keepSyncing) {
List<JSONObject> pendingReports = hia2ReportRepository.getUnSyncedReports(limit);

if (pendingReports.isEmpty()) {
return;
}

String baseUrl = CoreChwApplication.getInstance().getContext().configuration().dristhiBaseURL();
if (baseUrl.endsWith(context.getString(org.smartregister.chw.core.R.string.url_separator))) {
baseUrl = baseUrl.substring(0, baseUrl.lastIndexOf(context.getString(org.smartregister.chw.core.R.string.url_separator)));
}
// create request body
JSONObject request = new JSONObject();

request.put("reports", pendingReports);
String jsonPayload = request.toString();
Response<String> response = httpAgent.post(
MessageFormat.format("{0}/{1}",
baseUrl,
REPORTS_SYNC_PATH),
jsonPayload);
if (response.isFailure()) {
Log.e(getClass().getName(), "Reports sync failed.");
return;
}
hia2ReportRepository.markReportsAsSynced(pendingReports);
Log.i(getClass().getName(), "Reports synced successfully.");

// update drafts view
refreshDraftMonthlyTitle();
Utils.startAsyncTask(new FetchEditedMonthlyTalliesTask(new FetchEditedMonthlyTalliesTask.TaskListener() {
@Override
public void onPostExecute(List<MonthlyTally> monthlyTallies) {
Fragment fragment = getSupportFragmentManager().findFragmentByTag("android:switcher:" + org.smartregister.chw.core.R.id.container + ":" + mViewPager.getCurrentItem());
((HfDraftMonthlyFragment) fragment).updateDraftsReportListView(monthlyTallies);
}
}), null);
}
} catch (Exception e) {
Log.e(getClass().getName(), e.getMessage());
}
}
}
}
Loading

0 comments on commit 66c6cf2

Please sign in to comment.