Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resurvey drop-down #1101 #2251

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public final class ValidatorRulesDatabase {
/**
* Table: rulesets (id INTEGER, name TEXT) Table: resurvey (ruleset INTEGER, key TEXT, value TEXT DEFAULT NULL, days
* INTEGER DEFAULT 365, FOREIGN KEY(ruleset) REFERENCES rulesets(id)) Table: check (ruleset INTEGER, key TEXT,
* INTEGER DEFAULT 365, selected INTEGER DEFAULT 1, FOREIGN KEY(ruleset) REFERENCES rulesets(id)) Table: check (ruleset INTEGER, key TEXT,
* optional INTEGER DEFAULT 0, FOREIGN KEY(ruleset) REFERENCES rulesets(id))
*/

Expand All @@ -36,11 +36,12 @@ public final class ValidatorRulesDatabase {
static final String RULESET_FIELD = "ruleset";
private static final String CHECK_TABLE = "checktags";
static final String OPTIONAL_FIELD = "optional";
static final String SELECTED_FIELD = "selected";

static final String QUERY_RESURVEY_DEFAULT = "SELECT resurveytags.rowid as _id, key, value, is_regexp, days FROM resurveytags WHERE ruleset = "
static final String QUERY_RESURVEY_DEFAULT = "SELECT resurveytags.rowid as _id, key, value, is_regexp, days, selected FROM resurveytags WHERE ruleset = "
+ DEFAULT_RULESET + " ORDER BY key, value";
static final String QUERY_RESURVEY_BY_ROWID = "SELECT key, value, is_regexp, days FROM resurveytags WHERE rowid=?";
static final String QUERY_RESURVEY_BY_NAME = "SELECT resurveytags.rowid as _id, key, value, is_regexp, days FROM resurveytags, rulesets WHERE ruleset = rulesets.id and rulesets.name = ? ORDER BY key, value";
static final String QUERY_RESURVEY_BY_ROWID = "SELECT key, value, is_regexp, days, selected FROM resurveytags WHERE rowid=?";
static final String QUERY_RESURVEY_BY_NAME = "SELECT resurveytags.rowid as _id, key, value, is_regexp, days, selected FROM resurveytags, rulesets WHERE ruleset = rulesets.id and rulesets.name = ? ORDER BY key, value";

static final String QUERY_CHECK_DEFAULT = "SELECT checktags.rowid as _id, key, optional FROM checktags WHERE ruleset = " + DEFAULT_RULESET
+ " ORDER BY key";
Expand Down Expand Up @@ -77,18 +78,20 @@ public static void addRuleset(SQLiteDatabase db, int id, String name) {
@Nullable
public static MultiHashMap<String, PatternAndAge> getDefaultResurvey(@NonNull SQLiteDatabase database) {
MultiHashMap<String, PatternAndAge> result = null;
Cursor dbresult = database.query(RESURVEY_TABLE, new String[] { KEY_FIELD, VALUE_FIELD, ISREGEXP_FIELD, DAYS_FIELD },
Cursor dbresult = database.query(RESURVEY_TABLE, new String[] { KEY_FIELD, VALUE_FIELD, ISREGEXP_FIELD, DAYS_FIELD, SELECTED_FIELD },
RULESET_FIELD + " = " + DEFAULT_RULESET, null, null, null, KEY_FIELD + "," + VALUE_FIELD);

if (dbresult.getCount() >= 1) {
result = new MultiHashMap<>();
boolean haveEntry = dbresult.moveToFirst();
while (haveEntry) {
PatternAndAge v = new PatternAndAge();
v.setValue(dbresult.getString(1));
v.setIsRegexp(dbresult.getInt(2) == 1);
v.setAge(dbresult.getLong(3) * 24 * 3600); // days -> secs
result.add(dbresult.getString(0), v);
if (dbresult.getInt(dbresult.getColumnIndexOrThrow(ValidatorRulesDatabase.SELECTED_FIELD)) == 1){
PatternAndAge v = new PatternAndAge();
v.setValue(dbresult.getString(1));
v.setIsRegexp(dbresult.getInt(2) == 1);
v.setAge(dbresult.getLong(3) * 24 * 3600); // days -> secs
result.add(dbresult.getString(0), v);
}
haveEntry = dbresult.moveToNext();
}
}
Expand Down Expand Up @@ -121,13 +124,14 @@ public static Cursor queryResurveyByName(@NonNull SQLiteDatabase database, @Null
* @param isRegexp if true the value is a regexp
* @param days how man days old the object should max be
*/
public static void addResurvey(@NonNull SQLiteDatabase db, int ruleSetId, @NonNull String key, @Nullable String value, boolean isRegexp, int days) {
public static void addResurvey(@NonNull SQLiteDatabase db, int ruleSetId, @NonNull String key, @Nullable String value, boolean isRegexp, int days, boolean selected) {
ContentValues values = new ContentValues();
values.put(RULESET_FIELD, ruleSetId);
values.put(KEY_FIELD, key);
values.put(VALUE_FIELD, value);
values.put(ISREGEXP_FIELD, isRegexp ? 1 : 0);
values.put(DAYS_FIELD, days);
values.put(SELECTED_FIELD, selected ? 1 : 0);
db.insert(RESURVEY_TABLE, null, values);
}

Expand Down Expand Up @@ -160,6 +164,19 @@ static void deleteResurvey(final SQLiteDatabase db, final int id) {
db.delete(RESURVEY_TABLE, "rowid=?", new String[] { Integer.toString(id) });
}

/**
* Change the selected row of the resurvey table.
*
* @param db writable database
* @param id rowid of the entry
* @param isChecked state of the checkbox
*/
public static void resurveySelected(SQLiteDatabase db, int id, boolean isChecked) {
ContentValues values = new ContentValues();
values.put(SELECTED_FIELD, isChecked ? 1 : 0);
db.update(RESURVEY_TABLE, values, "rowid=" + id, null);
}

/**
* Return the default resurvey entries if any
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ValidatorRulesDatabaseHelper extends SQLiteOpenHelper {
private static final String DEBUG_TAG = "ValidatorRulesDatab...";

private static final String DATABASE_NAME = "validator_rules";
private static final int DATABASE_VERSION = 3;
private static final int DATABASE_VERSION = 4;
static final int ONE_YEAR = 365;

/**
Expand All @@ -31,18 +31,18 @@ public void onCreate(SQLiteDatabase db) {
ValidatorRulesDatabase.addRuleset(db, ValidatorRulesDatabase.DEFAULT_RULESET, ValidatorRulesDatabase.DEFAULT_RULESET_NAME);

db.execSQL(
"CREATE TABLE resurveytags (ruleset INTEGER, key TEXT, value TEXT DEFAULT NULL, is_regexp INTEGER DEFAULT 0, days INTEGER DEFAULT 365, FOREIGN KEY(ruleset) REFERENCES rulesets(id))");
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_SHOP, null, false, ONE_YEAR);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_AMENITY, Tags.VALUE_RESTAURANT, false, ONE_YEAR);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_AMENITY, Tags.VALUE_FAST_FOOD, false, ONE_YEAR);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_AMENITY, Tags.VALUE_CAFE, false, ONE_YEAR);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_AMENITY, Tags.VALUE_PUB, false, ONE_YEAR);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_AMENITY, Tags.VALUE_BAR, false, ONE_YEAR);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_AMENITY, Tags.VALUE_TOILETS, false, ONE_YEAR);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_LANDUSE, Tags.VALUE_CONSTRUCTION, false, ONE_YEAR);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_HIGHWAY, Tags.VALUE_CONSTRUCTION, false, ONE_YEAR);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_RAILWAY, Tags.VALUE_CONSTRUCTION, false, ONE_YEAR);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_BUILDING, Tags.VALUE_CONSTRUCTION, false, ONE_YEAR);
"CREATE TABLE resurveytags (ruleset INTEGER, key TEXT, value TEXT DEFAULT NULL, is_regexp INTEGER DEFAULT 0, days INTEGER DEFAULT 365, selected INTEGER DEFAULT 1, FOREIGN KEY(ruleset) REFERENCES rulesets(id))");
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_SHOP, null, false, ONE_YEAR, true);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_AMENITY, Tags.VALUE_RESTAURANT, false, ONE_YEAR, true);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_AMENITY, Tags.VALUE_FAST_FOOD, false, ONE_YEAR, true);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_AMENITY, Tags.VALUE_CAFE, false, ONE_YEAR, true);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_AMENITY, Tags.VALUE_PUB, false, ONE_YEAR, true);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_AMENITY, Tags.VALUE_BAR, false, ONE_YEAR, true);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_AMENITY, Tags.VALUE_TOILETS, false, ONE_YEAR, true);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_LANDUSE, Tags.VALUE_CONSTRUCTION, false, ONE_YEAR, true);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_HIGHWAY, Tags.VALUE_CONSTRUCTION, false, ONE_YEAR, true);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_RAILWAY, Tags.VALUE_CONSTRUCTION, false, ONE_YEAR, true);
ValidatorRulesDatabase.addResurvey(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_BUILDING, Tags.VALUE_CONSTRUCTION, false, ONE_YEAR, true);

db.execSQL("CREATE TABLE checktags (ruleset INTEGER, key TEXT, optional INTEGER DEFAULT 0, FOREIGN KEY(ruleset) REFERENCES rulesets(id))");
ValidatorRulesDatabase.addCheck(db, ValidatorRulesDatabase.DEFAULT_RULESET, Tags.KEY_OPENING_HOURS, false);
Expand All @@ -59,6 +59,9 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion <= 1 && newVersion >= 2) {
db.execSQL("ALTER TABLE resurveytags ADD COLUMN is_regexp INTEGER DEFAULT 0");
}
if (oldVersion <= 3 && newVersion >= 4) {
db.execSQL("ALTER TABLE resurveytags ADD COLUMN selected INTEGER DEFAULT 1");
}
if (oldVersion <= 2 && newVersion >= 3) {
db.execSQL("UPDATE checktags SET key='name|ref' WHERE key='name'");
}
Expand Down
111 changes: 103 additions & 8 deletions src/main/java/de/blau/android/validation/ValidatorRulesUI.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.blau.android.validation;

import com.google.android.material.floatingactionbutton.FloatingActionButton;

import static de.blau.android.osm.Tags.IMPORTANT_TAGS;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
Expand All @@ -10,6 +10,9 @@
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
Expand All @@ -20,11 +23,16 @@
import androidx.appcompat.widget.PopupMenu;
import androidx.cursoradapter.widget.CursorAdapter;
import androidx.viewpager.widget.PagerTabStrip;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import ch.poole.android.numberpicker.library.NumberPicker;
import de.blau.android.App;
import de.blau.android.R;
import de.blau.android.dialogs.ViewPagerAdapter;
import de.blau.android.filter.Filter;
import de.blau.android.presets.Preset;
import de.blau.android.util.StringWithDescription;
import de.blau.android.util.ThemeUtils;
import de.blau.android.views.ExtendedViewPager;

Expand Down Expand Up @@ -67,6 +75,27 @@ public void manageRulesetContents(@NonNull final Context context) {
Cursor checkCursor = ValidatorRulesDatabase.queryCheckByName(writableDb, ValidatorRulesDatabase.DEFAULT_RULESET_NAME);
checkAdapter = new CheckAdapter(writableDb, context, checkCursor);
checkList.setAdapter(checkAdapter);
CheckBox headerSelected = (CheckBox) rulesetView.findViewById(R.id.header_selected);
headerSelected.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
boolean isChecked = ((CheckBox) v).isChecked();
Cursor newCursor = resurveyCursor;
newCursor.moveToFirst();
int i = 0;
do{
if (i < resurveyList.getChildCount()) {
View childView = resurveyList.getChildAt(i);
Log.d(DEBUG_TAG, "---bindView childView == " + i);
CheckBox checkBox = (CheckBox) childView.findViewById(R.id.resurvey_selected);
checkBox.setChecked(isChecked);
}
int id = newCursor.getInt(resurveyCursor.getColumnIndexOrThrow("_id"));
ValidatorRulesDatabase.resurveySelected(writableDb, id, isChecked);
Log.d(DEBUG_TAG, "---bindView id ==" + id);
i++;
}while (newCursor.moveToNext());
}
});
alertDialog.setNeutralButton(R.string.done, null);
alertDialog.setOnDismissListener(dialog -> {
resurveyCursor.close();
Expand Down Expand Up @@ -129,6 +158,17 @@ public void bindView(final View view, final Context context, Cursor cursor) {
keyView.setText(key);
TextView daysView = (TextView) view.findViewById(R.id.days);
daysView.setText(Integer.toString(days));
CheckBox selected = (CheckBox) view.findViewById(R.id.resurvey_selected);
Cursor newCursor = db.rawQuery(ValidatorRulesDatabase.QUERY_RESURVEY_BY_ROWID, new String[] { Integer.toString(id) });
newCursor.moveToFirst();
boolean isSelected = newCursor.getInt(newCursor.getColumnIndexOrThrow(ValidatorRulesDatabase.SELECTED_FIELD)) == 1;
selected.setChecked(isSelected);
newCursor.close();
selected.setOnClickListener((v) -> {
boolean isChecked = selected.isChecked();
ValidatorRulesDatabase.resurveySelected(db, id, isChecked);
selected.setChecked(isChecked);
});
view.setOnClickListener(v -> {
Integer tag = (Integer) view.getTag();
showResurveyDialog(context, db, true, tag != null ? tag : -1);
Expand Down Expand Up @@ -160,11 +200,12 @@ private void showResurveyDialog(@NonNull final Context context, @NonNull final S
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
View templateView = LayoutInflater.from(context).inflate(R.layout.validator_ruleset_resurvey_item, null);
alertDialog.setView(templateView);

final EditText keyEdit = (EditText) templateView.findViewById(R.id.resurvey_key);
final EditText valueEdit = (EditText) templateView.findViewById(R.id.resurvey_value);
final AutoCompleteTextView keyEdit = (AutoCompleteTextView) templateView.findViewById(R.id.resurvey_key);
final AutoCompleteTextView valueEdit = (AutoCompleteTextView) templateView.findViewById(R.id.resurvey_value);
final CheckBox regexpCheck = (CheckBox) templateView.findViewById(R.id.resurvey_is_regexp);
final NumberPicker daysPicker = (NumberPicker) templateView.findViewById(R.id.resurvey_days);
List<String> matchingKeys = new ArrayList<>();
List<String> matchingValues = new ArrayList<>();
if (existing) {
Cursor cursor = db.rawQuery(ValidatorRulesDatabase.QUERY_RESURVEY_BY_ROWID, new String[] { Integer.toString(id) });
if (cursor.moveToFirst()) {
Expand Down Expand Up @@ -192,14 +233,68 @@ private void showResurveyDialog(@NonNull final Context context, @NonNull final S
alertDialog.setTitle(R.string.add_resurvey_title);
daysPicker.setValue(ValidatorRulesDatabaseHelper.ONE_YEAR);
}
Preset[] presets = App.getCurrentPresets(context);
matchingKeys.addAll(IMPORTANT_TAGS); //object-keys
if (matchingKeys.contains(keyEdit)) {
matchingKeys.remove(keyEdit);
}
Collections.sort(matchingKeys);
ArrayAdapter<String> empty = new ArrayAdapter<>(context, R.layout.autocomplete_row, new String[0]);
keyEdit.setAdapter(empty);
valueEdit.setAdapter(empty);
View.OnClickListener autocompleteOnClick = v -> {
if (v.hasFocus()) {
((AutoCompleteTextView) v).showDropDown();
}
};
keyEdit.setOnClickListener(autocompleteOnClick);
valueEdit.setOnClickListener(autocompleteOnClick);
keyEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
keyEdit.setAdapter(new ArrayAdapter<>(context, R.layout.autocomplete_row, matchingKeys));
}
}
});
keyEdit.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedKey = parent.getItemAtPosition(position).toString();
keyEdit.setText(selectedKey);
}
});
valueEdit.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
Log.d(DEBUG_TAG, "onFocusChange value");
if (hasFocus) {
matchingValues.clear();
for (StringWithDescription s : Preset.getAutocompleteValues(presets, null, keyEdit.getText().toString().trim())) {
String values = s.getValue();
if (!matchingValues.contains(values)) {
matchingValues.add(values);
}
}
Collections.sort(matchingValues);
valueEdit.setAdapter(new ArrayAdapter<>(context, R.layout.autocomplete_row, matchingValues));
}
}
});
valueEdit.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedKey = parent.getItemAtPosition(position).toString();
valueEdit.setText(selectedKey);
}
});
alertDialog.setNegativeButton(R.string.cancel, null);

alertDialog.setPositiveButton(R.string.save, (dialog, which) -> {
if (!existing) {
ValidatorRulesDatabase.addResurvey(db, 0, keyEdit.getText().toString(), valueEdit.getText().toString(), regexpCheck.isChecked(),
daysPicker.getValue());
ValidatorRulesDatabase.addResurvey(db, 0, keyEdit.getText().toString().trim(), valueEdit.getText().toString().trim(), regexpCheck.isChecked(),
daysPicker.getValue(), true);
} else {
ValidatorRulesDatabase.updateResurvey(db, id, keyEdit.getText().toString(), valueEdit.getText().toString(), regexpCheck.isChecked(),
ValidatorRulesDatabase.updateResurvey(db, id, keyEdit.getText().toString().trim(), valueEdit.getText().toString().trim(), regexpCheck.isChecked(),
daysPicker.getValue());
}
newResurveyCursor(db);
Expand Down
Loading