Skip to content

Commit

Permalink
Issue #3, #11: Se realiza un filtrado de senderos en el último paso d…
Browse files Browse the repository at this point in the history
…e el cuestionario de afinidad de senderos. Se puede observar en el QuestionRecommenderFactory cómo se asocia un filtro a una de las preguntas del cuestionario.
  • Loading branch information
jelcaf committed Apr 21, 2015
1 parent 3359505 commit bf519dd
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 7 deletions.
5 changes: 4 additions & 1 deletion android/PateaLaPalma/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ dependencies {

compile "com.android.support:support-v4:+"
compile 'com.squareup.picasso:picasso:2.3.2'
//

// https://github.com/nispok/snackbar
compile 'com.nispok:snackbar:2.7.5'

compile 'com.daimajia.slider:library:1.1.2@aar'
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.jelcaf.pacomf.patealapalma.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
Expand All @@ -10,9 +10,18 @@
import android.view.MenuItem;
import android.view.View;

import com.activeandroid.ActiveAndroid;
import com.activeandroid.query.Select;
import com.gc.materialdesign.views.ButtonRectangle;
import com.jelcaf.pacomf.patealapalma.R;
import com.jelcaf.pacomf.patealapalma.adapter.RecomenderQuestionsPagerAdapter;
import com.jelcaf.pacomf.patealapalma.binding.dao.Sendero;
import com.jelcaf.pacomf.patealapalma.recommender.RecommenderBaseQuestion;
import com.nispok.snackbar.Snackbar;
import com.nispok.snackbar.enums.SnackbarType;

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

import me.relex.circleindicator.CircleIndicator;

Expand All @@ -34,6 +43,8 @@ protected void onCreate(Bundle savedInstanceState) {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

ActiveAndroid.initialize(getApplicationContext());

// Show the Up button in the action bar.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Expand Down Expand Up @@ -76,6 +87,9 @@ public void onClick(View v) {
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mNextButton.getText().equals(getResources().getString(R.string.done_text))) {
validateMandatoryQuestions();
}
mViewPager.setCurrentItem(getItem(+1), true);
}
});
Expand Down Expand Up @@ -124,4 +138,62 @@ public void onPageSelected(int position) {
}
}

public String validateForm () {
List<RecommenderBaseQuestion> mForm = mPagerAdapter.form;
for (RecommenderBaseQuestion question : mForm) {
if (question.isMandatory() && question.getResponse() == null) {
return "Debe contestar la pregunta " + question.getResumeQuestion();
}
}
return "";
}

private void validateMandatoryQuestions() {
String result = validateForm();
if (!result.isEmpty()) {
Snackbar.with(getApplicationContext().getApplicationContext()) // context
.text(result) // text to display
.actionLabel("Close") // action button label
.actionColor(Color.parseColor("#FF8A80"))
.type(SnackbarType.MULTI_LINE)
.duration(Snackbar.SnackbarDuration.LENGTH_LONG)
.show(this); // activity where it is displayed
} else {
// TODO: El formulario es válido debemos filtrar los senderos y establecerlos en una
// preferencia
filtrarSenderos();
}
}

private void filtrarSenderos() {
List<Sendero> listaSenderos = getAllSenderos();
int total = listaSenderos.size();

// Para cada una de las preguntas le aplicamos el filtro
for (RecommenderBaseQuestion question : mPagerAdapter.form) {
List<Sendero> filterList = new ArrayList<Sendero>();
for (Sendero sendero: listaSenderos) {
if (question.checkSendero(sendero)) {
filterList.add(sendero);
}
}
listaSenderos = filterList;
}

Snackbar.with(getApplicationContext().getApplicationContext()) // context
.text(listaSenderos.size() + " de " + total + " senderos han pasado el filtro" )
.actionLabel("Close") // action button label
.actionColor(Color.parseColor("#FF8A80"))
.type(SnackbarType.MULTI_LINE)
.duration(Snackbar.SnackbarDuration.LENGTH_LONG)
.show(this); // activity where it is displayed

}

private List<Sendero> getAllSenderos() {
return new Select()
.from(Sendero.class)
.execute();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class RecomenderQuestionsPagerAdapter extends FragmentPagerAdapter {

private static int NUM_EXTRA_PAGES = 1;

private List<RecommenderBaseQuestion> form;
public List<RecommenderBaseQuestion> form;
private RecomenderResumeFragment recomenderResume;

private Random random = new Random();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.jelcaf.pacomf.patealapalma.factories;

import com.jelcaf.pacomf.patealapalma.binding.dao.Sendero;
import com.jelcaf.pacomf.patealapalma.recommender.ISenderoFilter;
import com.jelcaf.pacomf.patealapalma.recommender.RecommenderBaseQuestion;
import com.jelcaf.pacomf.patealapalma.recommender.RecommenderCalendarQuestion;
import com.jelcaf.pacomf.patealapalma.recommender.RecommenderOptionResponse;
Expand Down Expand Up @@ -37,7 +39,7 @@ public List<RecommenderBaseQuestion> obtainQuestionForm() {
strResponses.add("Menos de 5");
strResponses.add("Entre 5 y 10");
strResponses.add("Más de 10");
questionForm.add(createSingleChoiceQuestion(strQuestion, strResponses));
questionForm.add(createSingleChoiceQuestion(strQuestion, strResponses, true));

strQuestion = "¿Irán niños al sendero?";
strResponses.clear();
Expand All @@ -59,7 +61,18 @@ public List<RecommenderBaseQuestion> obtainQuestionForm() {
strResponses.add("Media");
strResponses.add("Alta");
strResponses.add("Extrema");
questionForm.add(createSingleChoiceQuestion(strQuestion, strResponses));
final RecommenderSingleChoiceQuestion difficultyQuestion = createSingleChoiceQuestion(strQuestion, strResponses);
difficultyQuestion.addSenderoFilter(new ISenderoFilter() {
@Override
public boolean filterSendero(Sendero sendero) {
if (sendero.getDifficulty() == null || sendero.getDifficulty().equals
(difficultyQuestion.getResponse())) {
return true;
}
return false;
}
});
questionForm.add(difficultyQuestion);

strQuestion = "¿El sendero es Circular?";
strResponses.clear();
Expand Down Expand Up @@ -88,6 +101,11 @@ public List<RecommenderBaseQuestion> obtainQuestionForm() {
}

private RecommenderSingleChoiceQuestion createSingleChoiceQuestion(String strQuestion, List<String> strResponses) {
return createSingleChoiceQuestion(strQuestion, strResponses, false);
}

private RecommenderSingleChoiceQuestion createSingleChoiceQuestion(String strQuestion,
List<String> strResponses, Boolean mandatory) {
List<RecommenderOptionResponse> responses = new ArrayList<RecommenderOptionResponse>();
Integer i = START_VALUE;
for (String strResponse : strResponses) {
Expand All @@ -97,6 +115,7 @@ private RecommenderSingleChoiceQuestion createSingleChoiceQuestion(String strQue
i++;
}
RecommenderSingleChoiceQuestion question = new RecommenderSingleChoiceQuestion(strQuestion, responses);
question.setMandatory(mandatory);
return question;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.jelcaf.pacomf.patealapalma.recommender;

import com.jelcaf.pacomf.patealapalma.binding.dao.Sendero;

import java.util.List;

/**
* @author Jorge Carballo
* 27/03/15
Expand All @@ -13,5 +17,6 @@ public interface IRecommenderQuestion {
public String getStrResponse();
public Object getResponseType();
public boolean isMandatory();

boolean checkSendero(Sendero sendero);
void addSenderoFilter(ISenderoFilter senderoFilter);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.jelcaf.pacomf.patealapalma.recommender;

import com.jelcaf.pacomf.patealapalma.binding.dao.Sendero;

/**
* @author Jorge Carballo
* 21/04/15
*/
public interface ISenderoFilter {

public boolean filterSendero(Sendero sendero);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.jelcaf.pacomf.patealapalma.recommender;

import com.jelcaf.pacomf.patealapalma.binding.dao.Sendero;

import java.io.Serializable;
import java.util.List;

/**
* @author Jorge Carballo
Expand All @@ -9,6 +12,8 @@
public class RecommenderBaseQuestion implements IRecommenderQuestion, Serializable {

private String question;
protected boolean mandatory = false;
private ISenderoFilter mSenderoFilter = null;

protected RecommenderBaseQuestion(String question) {
this.question = question;
Expand Down Expand Up @@ -49,8 +54,20 @@ public Object getResponseType() {

@Override
public boolean isMandatory() {
return false;
return mandatory;
}

@Override
public boolean checkSendero(Sendero sendero) {
if (mSenderoFilter == null) {
return true;
}
return mSenderoFilter.filterSendero(sendero);
}

@Override
public void addSenderoFilter(ISenderoFilter senderoFilter) {
mSenderoFilter = senderoFilter;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.jelcaf.pacomf.patealapalma.recommender;

import com.jelcaf.pacomf.patealapalma.binding.dao.Sendero;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
Expand Down Expand Up @@ -58,5 +61,9 @@ public void setSelectedResponse(Integer selectedResponse) {
this.selectedResponse = selectedResponse;
}

public void setMandatory(Boolean mandatory) {
this.mandatory = mandatory;
}


}

0 comments on commit bf519dd

Please sign in to comment.