Skip to content

Commit

Permalink
Merge pull request #119 from opensrp/fix-offline-maps-for-regions
Browse files Browse the repository at this point in the history
Fix offline maps for regions
  • Loading branch information
hamza-vd authored Mar 8, 2023
2 parents 06d372f + 7c3718e commit b87b648
Show file tree
Hide file tree
Showing 14 changed files with 830 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

import org.smartregister.eusm.R;
import org.smartregister.eusm.fragment.EusmAvailableOfflineMapsFragment;
import org.smartregister.eusm.fragment.EusmDownloadedOfflineMapsFragment;
import org.smartregister.tasking.activity.OfflineMapsActivity;
import org.smartregister.tasking.fragment.AvailableOfflineMapsFragment;
import org.smartregister.tasking.fragment.DownloadedOfflineMapsFragment;

public class EusmOfflineMapsActivity extends OfflineMapsActivity {

Expand All @@ -19,4 +21,9 @@ public String getMapStyleAssetPath() {
protected AvailableOfflineMapsFragment getAvailableOfflineMapsFragment() {
return EusmAvailableOfflineMapsFragment.newInstance(this.getIntent().getExtras(), this.getMapStyleAssetPath());
}

@Override
protected DownloadedOfflineMapsFragment getDownloadedOfflineMapsFragment() {
return EusmDownloadedOfflineMapsFragment.newInstance(this.getIntent().getExtras(), this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.smartregister.eusm.adapter;

import android.content.Context;
import android.view.View;

import androidx.annotation.NonNull;

import org.smartregister.tasking.adapter.DownloadedOfflineMapAdapter;
import org.smartregister.tasking.model.OfflineMapModel;
import org.smartregister.tasking.viewholder.DownloadedOfflineMapViewHolder;

import java.util.ArrayList;
import java.util.List;

public class EUSMDownloadedOfflineMapAdapter extends DownloadedOfflineMapAdapter {

private List<OfflineMapModel> offlineMapModels = new ArrayList<>();

public EUSMDownloadedOfflineMapAdapter(Context context, View.OnClickListener offlineMapClickHandler) {
super(context, offlineMapClickHandler);
}

@Override
public void setOfflineMapModels(List<OfflineMapModel> offlineMapModels) {
this.offlineMapModels = offlineMapModels;
super.setOfflineMapModels(offlineMapModels);
}

@Override
public void onBindViewHolder(@NonNull DownloadedOfflineMapViewHolder viewHolder, int position) {
super.onBindViewHolder(viewHolder, position);
OfflineMapModel offlineMapModel = offlineMapModels.get(position);

switch (offlineMapModel.getOfflineMapStatus()) {
case READY:
case DOWNLOADED:
viewHolder.checkCheckBox(false);
viewHolder.enableCheckBox(true);
break;
case SELECTED_FOR_DOWNLOAD:
viewHolder.checkCheckBox(true);
break;
case DOWNLOAD_STARTED:
viewHolder.checkCheckBox(true);
viewHolder.enableCheckBox(false);
break;
default:
break;

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.smartregister.eusm.helper.EusmTaskingMapHelper;
import org.smartregister.eusm.job.LocationTaskServiceJob;
import org.smartregister.eusm.layer.SatelliteStreetsLayer;
import org.smartregister.eusm.repository.AppLocationRepository;
import org.smartregister.eusm.util.AppConstants;
import org.smartregister.eusm.util.AppUtils;
import org.smartregister.eusm.util.DefaultLocationUtils;
Expand Down Expand Up @@ -69,6 +70,7 @@
import org.smartregister.tasking.util.TaskingMapHelper;
import org.smartregister.util.AppExecutors;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -431,7 +433,7 @@ public String getProvinceFromTreeDialogValue(List<String> arrayList) {
@Override
public String getDistrictFromTreeDialogValue(List<String> arrayList) {
try {
return arrayList.get(2);
return arrayList.get(Integer.parseInt(AppConstants.LocationGeographicLevel.DISTRICT));
} catch (IndexOutOfBoundsException e) {
Timber.e(e);
}
Expand Down Expand Up @@ -514,15 +516,20 @@ public List<Location> getLocationsIdsForDownload(List<String> downloadedLocation
.findPlanDefinitionSearchByPlanStatus(PlanDefinition.PlanStatus.ACTIVE)
.stream().map(PlanDefinitionSearch::getJurisdictionId).collect(Collectors.toList());

// List districts instead of regions
List<String> districtIds = new ArrayList<>();
AppLocationRepository appLocationRepository = EusmApplication.getInstance().getAppLocationRepository();
for (String regionId : jurisdictionIds) {
Set<Location> districts = appLocationRepository.getDistrictIdsForRegionId(regionId);
for (Location district: districts) {
districtIds.add(district.getId());
}
}

if (downloadedLocations != null) {
jurisdictionIds.removeAll(downloadedLocations);
districtIds.removeAll(downloadedLocations);
}
// List<Location> locationList = EusmApplication.getInstance().getStructureRepository().getLocationByDistrictIds(jurisdictionIds);
//
// if (downloadedLocations != null) {
// locationList = locationList.stream().filter(location -> !downloadedLocations.contains(location.getId())).collect(Collectors.toList());
// }
return EusmApplication.getInstance().getLocationRepository().getLocationsByIds(jurisdictionIds);
return EusmApplication.getInstance().getLocationRepository().getLocationsByIds(districtIds);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.smartregister.eusm.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;

import androidx.annotation.NonNull;
import androidx.annotation.StringRes;

import com.google.android.material.snackbar.Snackbar;
import com.mapbox.geojson.FeatureCollection;

import org.json.JSONArray;
Expand All @@ -15,12 +17,13 @@
import org.smartregister.eusm.application.EusmApplication;
import org.smartregister.eusm.repository.AppStructureRepository;
import org.smartregister.tasking.fragment.AvailableOfflineMapsFragment;
import org.smartregister.tasking.model.OfflineMapModel;
import org.smartregister.tasking.presenter.AvailableOfflineMapsPresenter;
import org.smartregister.tasking.util.OfflineMapHelper;
import org.smartregister.tasking.util.TaskingConstants;
import org.smartregister.util.AppExecutors;
import org.smartregister.util.Utils;

import java.util.ArrayList;
import java.util.List;

import timber.log.Timber;
Expand All @@ -31,6 +34,12 @@ public class EusmAvailableOfflineMapsFragment extends AvailableOfflineMapsFragme

private AppExecutors appExecutors;

private List<OfflineMapModel> offlineMapModelList = new ArrayList<>();

private final List<Location> operationalAreasToDownload = new ArrayList<>();

private Snackbar displayBar = null;

public static EusmAvailableOfflineMapsFragment newInstance(Bundle bundle, @NonNull String mapStyleAssetPath) {
EusmAvailableOfflineMapsFragment fragment = new EusmAvailableOfflineMapsFragment();
if (bundle != null) {
Expand All @@ -43,40 +52,34 @@ public static EusmAvailableOfflineMapsFragment newInstance(Bundle bundle, @NonNu

@Override
protected void downloadLocation(@NonNull Location location) {
getAppExecutors().diskIO().execute(new Runnable() {
@Override
public void run() {
String name = location.getId();
List<Location> locationList = getAppStructureRepository().getStructuresByDistrictId(name);
if (locationList == null || locationList.isEmpty()) {
getAppExecutors().mainThread().execute(new Runnable() {
@Override
public void run() {
showToast(R.string.location_has_no_structures);
}
});
} else {
JSONObject featureCollection = new JSONObject();
try {
featureCollection.put(TaskingConstants.GeoJSON.TYPE, TaskingConstants.GeoJSON.FEATURE_COLLECTION);
featureCollection.put(TaskingConstants.GeoJSON.FEATURES, new JSONArray(gson.toJson(locationList)));
getAppExecutors().mainThread().execute(new Runnable() {
@Override
public void run() {
showToast(R.string.download_starting);
downloadMap(FeatureCollection.fromJson(featureCollection.toString()), name);
}
});
} catch (JSONException e) {
Timber.e(e);
}
}
}
});
getAppExecutors().diskIO().execute(() -> downloadDistrictMap(location.getId()));

}

protected void showToast(@StringRes int stringRes) {
Utils.showToast(getContext(), getString(stringRes));
protected void downloadDistrictMap(String districtId) {
List<Location> locationList = getAppStructureRepository().getStructuresByDistrictId(districtId);
if (locationList == null || locationList.isEmpty()) {
getAppExecutors().mainThread().execute(() -> displayToast(getString(R.string.location_has_no_structures)));
// Revert download map status
for (OfflineMapModel model : offlineMapModelList) {
if (model.getOfflineMapStatus() == OfflineMapModel.OfflineMapStatus.DOWNLOAD_STARTED) {
model.setOfflineMapStatus(OfflineMapModel.OfflineMapStatus.READY);
return;
}
}
} else {
JSONObject featureCollection = new JSONObject();
try {
featureCollection.put(TaskingConstants.GeoJSON.TYPE, TaskingConstants.GeoJSON.FEATURE_COLLECTION);
featureCollection.put(TaskingConstants.GeoJSON.FEATURES, new JSONArray(gson.toJson(locationList)));
getAppExecutors().mainThread().execute(() -> {
displayToast(getString(R.string.download_starting));
downloadMap(FeatureCollection.fromJson(featureCollection.toString()), districtId);
});
} catch (JSONException e) {
Timber.e(e);
}
}
}

protected void downloadMap(FeatureCollection operationalAreaFeature, String mapName) {
Expand All @@ -96,4 +99,90 @@ public AppExecutors getAppExecutors() {
}
return appExecutors;
}

@Override
public void moveDownloadedOAToDownloadedList(String operationalAreaId) {
List<Location> toRemoveFromDownloadList = new ArrayList<>();
for (OfflineMapModel offlineMapModel : offlineMapModelList) {
if (operationalAreaId.equals(offlineMapModel.getDownloadAreaId())) {
offlineMapModel.setOfflineMapStatus(OfflineMapModel.OfflineMapStatus.READY);
callback.onMapDownloaded(offlineMapModel);
toRemoveFromDownloadList.add(offlineMapModel.getLocation());
setOfflineMapModelList(offlineMapModelList);
break;
}
}
operationalAreasToDownload.removeAll(toRemoveFromDownloadList);
}

@Override
public void updateOperationalAreasToDownload(View view) {
CheckBox checkBox = (CheckBox) view;
OfflineMapModel offlineMapModel = (OfflineMapModel) view.getTag(R.id.offline_map_checkbox);

if (checkBox.isChecked()) {
offlineMapModel.setOfflineMapStatus(OfflineMapModel.OfflineMapStatus.SELECTED_FOR_DOWNLOAD);
operationalAreasToDownload.clear();
operationalAreasToDownload.add(offlineMapModel.getLocation());

for (OfflineMapModel model : offlineMapModelList) {
if (!model.getDownloadAreaId().equals(offlineMapModel.getDownloadAreaId())
&& model.getOfflineMapStatus() == OfflineMapModel.OfflineMapStatus.SELECTED_FOR_DOWNLOAD) {
model.setOfflineMapStatus(OfflineMapModel.OfflineMapStatus.READY);
}
}
setOfflineMapModelList(offlineMapModelList);
} else {
operationalAreasToDownload.remove(offlineMapModel.getLocation());
}
}

@Override
public void setOfflineMapModelList(List<OfflineMapModel> offlineMapModelList) {
this.offlineMapModelList = offlineMapModelList;
super.setOfflineMapModelList(offlineMapModelList);
}

@Override
public void initiateMapDownload() {
if (this.operationalAreasToDownload.isEmpty()) {
displayToast(getString(R.string.select_offline_map_to_download));
return;
}

for (OfflineMapModel model : offlineMapModelList) {
if (model.getOfflineMapStatus() == OfflineMapModel.OfflineMapStatus.DOWNLOAD_STARTED) {
displayToast(getString(R.string.another_map_in_download));
Timber.e("Error: A map download is already in progress");
return;
}
}

for (Location location : this.operationalAreasToDownload) {
downloadLocation(location);
}
}

@Override
public void displayToast(String message) {
if (displayBar == null) {
displayBar = Snackbar.make(requireView(), message, Snackbar.LENGTH_LONG);
displayBar.show();
} else {
displayBar.setText(message);
displayBar.setDuration(Snackbar.LENGTH_LONG);
displayBar.show();
}
}

@Override
public void displayError(int title, String message) {
displayToast(message);
}

@Override
protected void mapDeletedSuccessfully(String mapUniqueName) {
super.mapDeletedSuccessfully(mapUniqueName);
displayToast(getString(R.string.map_deleted, mapUniqueName));
}
}
Loading

0 comments on commit b87b648

Please sign in to comment.