Skip to content

Commit

Permalink
Merge pull request #23 from EyeSeeTea/feature/bring_from_widp_add_fil…
Browse files Browse the repository at this point in the history
…ter_by_text_value

Feature/bring from widp add filter by text value
  • Loading branch information
ifoche authored Jul 17, 2020
2 parents d9cfeee + 6e7030d commit ecdbdd0
Show file tree
Hide file tree
Showing 19 changed files with 435 additions and 12 deletions.
2 changes: 1 addition & 1 deletion app/src/main/assets/paperwork.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"buildTime":"2020-07-17 17:34","gitSha":"46259d5e6"}
{"buildTime":"2020-07-17 17:34","gitSha":"46259d5e6"}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.reactivex.disposables.CompositeDisposable
import io.reactivex.processors.PublishProcessor
import org.dhis2.data.prefs.PreferenceProvider
import org.dhis2.data.schedulers.SchedulerProvider
import org.dhis2.data.tuples.Pair
import org.dhis2.utils.Constants.PROGRAM_THEME
import org.dhis2.utils.filters.FilterManager
import timber.log.Timber
Expand All @@ -19,6 +20,8 @@ class ProgramPresenter internal constructor(
var disposable: CompositeDisposable = CompositeDisposable()

fun init() {
FilterManager.getInstance().clearTextValues()

val applyFiler = PublishProcessor.create<FilterManager>()

disposable.add(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import org.hisp.dhis.android.core.category.CategoryCombo;
import org.hisp.dhis.android.core.category.CategoryOptionCombo;
import org.hisp.dhis.android.core.common.FeatureType;
import org.hisp.dhis.android.core.dataelement.DataElement;
import org.hisp.dhis.android.core.program.Program;

import java.lang.reflect.Field;
Expand Down Expand Up @@ -322,6 +323,11 @@ public void setCatOptionComboFilter(Pair<CategoryCombo, List<CategoryOptionCombo
filtersAdapter.addCatOptCombFilter(categoryOptionCombos);
}

@Override
public void setTextTypeDataElementsFilter(List<DataElement> textTypeDataElementsFilter) {
filtersAdapter.addTextValueFilter(textTypeDataElementsFilter);
}

@Override
public void showPeriodRequest(FilterManager.PeriodRequest periodRequest) {
if (periodRequest == FilterManager.PeriodRequest.FROM_TO) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.dhis2.usescases.programEventDetail;

import androidx.lifecycle.LiveData;
import androidx.paging.PagedList;

import com.mapbox.geojson.BoundingBox;
import com.mapbox.geojson.FeatureCollection;
import com.mapbox.mapboxsdk.geometry.LatLng;
Expand All @@ -13,11 +10,13 @@
import org.hisp.dhis.android.core.category.CategoryCombo;
import org.hisp.dhis.android.core.category.CategoryOptionCombo;
import org.hisp.dhis.android.core.common.FeatureType;
import org.hisp.dhis.android.core.period.DatePeriod;
import org.hisp.dhis.android.core.dataelement.DataElement;
import org.hisp.dhis.android.core.program.Program;

import java.util.List;

import androidx.lifecycle.LiveData;
import androidx.paging.PagedList;
import io.reactivex.functions.Consumer;

/**
Expand All @@ -44,6 +43,8 @@ public interface View extends AbstractActivityContracts.View {

void setCatOptionComboFilter(Pair<CategoryCombo, List<CategoryOptionCombo>> categoryOptionCombos);

void setTextTypeDataElementsFilter(List<DataElement> textTypeDataElementsFilter);

void openOrgUnitTreeSelector();

void setMap(FeatureCollection featureCollection, BoundingBox boundingBox);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ public void init() {
)
);

compositeDisposable.add(
eventRepository.textTypeDataElements()
.subscribeOn(schedulerProvider.io())
.observeOn(schedulerProvider.ui())
.subscribe(view::setTextTypeDataElementsFilter,
Timber::e
)
);

compositeDisposable.add(
filterManager.asFlowable()
.startWith(filterManager)
Expand All @@ -101,7 +110,8 @@ public void init() {
filterManager.getCatOptComboFilters(),
filterManager.getEventStatusFilters(),
filterManager.getStateFilters(),
filterManager.getAssignedFilter()
filterManager.getAssignedFilter(),
filterManager.getTexValueFilter()
))
.subscribeOn(schedulerProvider.io())
.observeOn(schedulerProvider.ui())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.hisp.dhis.android.core.category.CategoryOptionCombo;
import org.hisp.dhis.android.core.common.FeatureType;
import org.hisp.dhis.android.core.common.State;
import org.hisp.dhis.android.core.dataelement.DataElement;
import org.hisp.dhis.android.core.event.EventStatus;
import org.hisp.dhis.android.core.period.DatePeriod;
import org.hisp.dhis.android.core.program.Program;
Expand All @@ -25,7 +26,7 @@
public interface ProgramEventDetailRepository {

@NonNull
LiveData<PagedList<ProgramEventViewModel>> filteredProgramEvents(List<DatePeriod> dateFilter, List<String> orgUnitFilter, List<CategoryOptionCombo> catOptionComboUid, List<EventStatus> eventStatus, List<State> states, boolean assignedToUser);
LiveData<PagedList<ProgramEventViewModel>> filteredProgramEvents(List<DatePeriod> dateFilter, List<String> orgUnitFilter, List<CategoryOptionCombo> catOptionComboUid, List<EventStatus> eventStatus, List<State> states, boolean assignedToUser,Pair<String, String> valueFilter);

@NonNull
Flowable<kotlin.Pair<FeatureCollection, BoundingBox>> filteredEventsForMap(List<DatePeriod> dateFilter, List<String> orgUnitFilter, List<CategoryOptionCombo> catOptionComboUid, List<EventStatus> eventStatus, List<State> states, boolean assignedToUser);
Expand All @@ -37,6 +38,9 @@ public interface ProgramEventDetailRepository {

Single<Pair<CategoryCombo, List<CategoryOptionCombo>>> catOptionCombos();

@NonNull
Observable<List<DataElement>> textTypeDataElements();

Single<Boolean> hasAccessToAllCatOptions();

Flowable<ProgramEventViewModel> getInfoForEvent(String eventUid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.mapbox.geojson.BoundingBox;
import com.mapbox.geojson.FeatureCollection;
import android.database.Cursor;

import org.dhis2.data.tuples.Pair;
import org.dhis2.utils.maps.GeometryUtils;
Expand All @@ -20,16 +21,25 @@
import org.hisp.dhis.android.core.category.CategoryOptionCombo;
import org.hisp.dhis.android.core.common.FeatureType;
import org.hisp.dhis.android.core.common.State;
import org.hisp.dhis.android.core.common.ValueType;
import org.hisp.dhis.android.core.dataelement.DataElement;
import org.hisp.dhis.android.core.event.Event;
import org.hisp.dhis.android.core.event.EventCollectionRepository;
import org.hisp.dhis.android.core.event.EventStatus;
import org.hisp.dhis.android.core.period.DatePeriod;
import org.hisp.dhis.android.core.program.Program;
import org.hisp.dhis.android.core.program.ProgramStageDataElement;
import org.hisp.dhis.android.core.program.ProgramStageSection;
import org.hisp.dhis.android.core.trackedentity.TrackedEntityDataValue;
import org.jetbrains.annotations.NotNull;

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

import io.reactivex.Flowable;
import io.reactivex.Observable;
import io.reactivex.Single;
import timber.log.Timber;


public class ProgramEventDetailRepositoryImpl implements ProgramEventDetailRepository {
Expand All @@ -47,8 +57,9 @@ public class ProgramEventDetailRepositoryImpl implements ProgramEventDetailRepos
@NonNull
@Override
public LiveData<PagedList<ProgramEventViewModel>> filteredProgramEvents(List<DatePeriod> dateFilter, List<String> orgUnitFilter, List<CategoryOptionCombo> catOptCombList,
List<EventStatus> eventStatus, List<State> states, boolean assignedToUser) {
EventCollectionRepository eventRepo = d2.eventModule().events().byProgramUid().eq(programUid).byDeleted().isFalse();
List<EventStatus> eventStatus, List<State> states, boolean assignedToUser,Pair<String, String> valueFilter) {
EventCollectionRepository eventRepo = d2.eventModule().events().byProgramUid().eq(programUid);

if (!dateFilter.isEmpty())
eventRepo = eventRepo.byEventDate().inDatePeriods(dateFilter);
if (!orgUnitFilter.isEmpty())
Expand All @@ -61,6 +72,10 @@ public LiveData<PagedList<ProgramEventViewModel>> filteredProgramEvents(List<Dat
eventRepo = eventRepo.byState().in(states);
if (assignedToUser)
eventRepo = eventRepo.byAssignedUser().eq(getCurrentUser());
if (valueFilter != null && !valueFilter.val0().isEmpty() && !valueFilter.val1().isEmpty()) {
List<String> uIds = getEventUIdsFilteredByValue(valueFilter);
eventRepo = eventRepo.byUid().in(uIds);
}

DataSource dataSource = eventRepo.orderByEventDate(RepositoryScope.OrderByDirection.DESC).withTrackedEntityDataValues().getDataSource().map(event -> mapper.eventToProgramEvent(event));

Expand All @@ -72,6 +87,33 @@ public DataSource create() {
}, 20).build();
}

@NotNull
private List<String> getEventUIdsFilteredByValue(
Pair<String, String> valueFilter) {

List<String> uids = new ArrayList<>();

String QUERY = "SELECT Event.uid FROM Event " +
"LEFT OUTER JOIN TrackedEntityDataValue AS Value ON Value.event = Event.uid " +
"WHERE Value.dataElement = '" + valueFilter.val0() + "' AND Value.value like '%" +
valueFilter.val1()+ "%'";

try (Cursor uIdsCursor = d2.databaseAdapter().rawQuery(QUERY)) {
if (uIdsCursor != null) {
uIdsCursor.moveToFirst();
for (int i = 0; i < uIdsCursor.getCount(); i++) {
uids.add(uIdsCursor.getString(0));
uIdsCursor.moveToNext();
}
}
} catch (Exception e) {
Timber.e(e);
}

return uids;
}


@NonNull
@Override
public Flowable<kotlin.Pair<FeatureCollection, BoundingBox>> filteredEventsForMap(
Expand Down Expand Up @@ -160,6 +202,34 @@ public Single<Pair<CategoryCombo, List<CategoryOptionCombo>>> catOptionCombos()
));
}

@NonNull
@Override
public Observable<List<DataElement>> textTypeDataElements() {
List<String> programStageUIds =
d2.programModule().programs().uid(programUid).get()
.flatMap(program -> d2.programModule().programStages().byProgramUid()
.eq(program.uid()).get())
.toObservable().flatMap(programStages ->
Observable.fromIterable(programStages)
.map(item -> item.uid())
.toList().toObservable()).blockingFirst();

List<String> programStagesDataElementsUIds =
d2.programModule().programStageDataElements().byProgramStage().in(programStageUIds).get()
.toObservable().flatMap(programStageDataElements ->
Observable.fromIterable(programStageDataElements)
.map(item -> item.dataElement().uid())
.toList().toObservable()).blockingFirst();


return d2.dataElementModule().dataElements()
.byValueType().eq(ValueType.TEXT)
.byUid().in(programStagesDataElementsUIds)
.byOptionSetUid().isNull()
.orderByDisplayName(RepositoryScope.OrderByDirection.ASC)
.get().toObservable();
}

@Override
public Single<Boolean> hasAccessToAllCatOptions() {
return d2.programModule().programs().uid(programUid).get()
Expand Down
61 changes: 61 additions & 0 deletions app/src/main/java/org/dhis2/utils/DataElementsAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.dhis2.utils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import org.dhis2.databinding.SpinnerDataelementLayoutBinding;
import org.hisp.dhis.android.core.dataelement.DataElement;

import java.util.List;

import androidx.annotation.ColorRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

public class DataElementsAdapter extends ArrayAdapter<DataElement> {

private final List<DataElement> dataElements;
private @ColorRes
int textColor;

public DataElementsAdapter(@NonNull Context context, int resource, int textViewResourceId,
@NonNull List<DataElement> objects, @ColorRes int textColor) {
super(context, resource, textViewResourceId, objects);
this.dataElements = objects;
this.textColor = textColor;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

LayoutInflater inflater = LayoutInflater.from(parent.getContext());
SpinnerDataelementLayoutBinding binding = SpinnerDataelementLayoutBinding.inflate(inflater,
parent, false);
binding.setDataElement(dataElements.get(position));
binding.spinnerText.setTextColor(
ContextCompat.getColor(binding.spinnerText.getContext(), textColor));
return binding.getRoot();

}

@Override
public View getDropDownView(int position, @Nullable View convertView,
@NonNull ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
SpinnerDataelementLayoutBinding binding = SpinnerDataelementLayoutBinding.inflate(inflater,
parent, false);
binding.setDataElement(dataElements.get(position));

return binding.getRoot();
}

@Override
public int getCount() {
return super.getCount();
}
}
Loading

0 comments on commit ecdbdd0

Please sign in to comment.