Skip to content

Commit

Permalink
Merge pull request #118 from AmericanRedCross/select-multiple-tag-value
Browse files Browse the repository at this point in the history
Select Multiple Tag Value Feature
  • Loading branch information
hallahan committed Jan 6, 2016
2 parents 99809ff + f280d73 commit 85a37db
Show file tree
Hide file tree
Showing 8 changed files with 374 additions and 23 deletions.
9 changes: 5 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "org.redcross.openmapkit"
minSdkVersion 16
targetSdkVersion 21
versionCode 14
versionName "0.14"
versionCode 15
versionName "0.15"
}
buildTypes {
release {
Expand All @@ -25,8 +25,9 @@ repositories {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:design:22.2.1'
compile group: 'com.google.guava', name: 'guava', version: '18.0'
compile project(':MapboxAndroidSDK')
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:design:22.2.1'
compile 'com.android.support:support-v4:21.0.3'
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package org.redcross.openmapkit.odkcollect.tag;

import android.app.Activity;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
Expand All @@ -17,7 +20,8 @@ public class ODKTag {
private String key;
private String label;
private LinkedHashMap<String, ODKTagItem> items = new LinkedHashMap<>();
private Map<Integer, ODKTagItem> radioButtonIdToODKTagItemHash = new HashMap<>();
private Map<Integer, ODKTagItem> buttonIdToODKTagItemHash = new HashMap<>();
private List<CheckBox> checkBoxes = new ArrayList<>();

public String getKey() {
return key;
Expand Down Expand Up @@ -47,12 +51,12 @@ public void addItem(ODKTagItem item) {
items.put(item.getValue(), item);
}

public void putRadioButtonIdToTagItemHash(Integer id, ODKTagItem tagItem) {
radioButtonIdToODKTagItemHash.put(id, tagItem);
public void putButtonIdToTagItemHash(Integer id, ODKTagItem tagItem) {
buttonIdToODKTagItemHash.put(id, tagItem);
}

public String getTagItemValueFromRadioButtonId(Integer id) {
ODKTagItem item = radioButtonIdToODKTagItemHash.get(id);
public String getTagItemValueFromButtonId(Integer id) {
ODKTagItem item = buttonIdToODKTagItemHash.get(id);
if (item != null) {
return item.getValue();
}
Expand All @@ -76,4 +80,47 @@ public TextView createTagValueTextView(Activity activity, String initialTagVal)
}
return et;
}

public void addCheckbox(CheckBox cb) {
checkBoxes.add(cb);
}

public boolean hasCheckedTagValues() {
for (CheckBox cb : checkBoxes) {
if (cb.isChecked()) {
return true;
}
}
return false;
}

public String getSemiColonDelimitedTagValues(String customValues) {
String values = null;
boolean firstVal = true;
for (CheckBox cb : checkBoxes) {
if (cb.isChecked()) {
int id = cb.getId();
ODKTagItem item = buttonIdToODKTagItemHash.get(id);
if (item != null) {
if (firstVal) {
firstVal = false;
values = item.getValue();
} else {
values += ';' + item.getValue();
}
}
}
}
if (customValues != null) {
customValues = customValues.trim();
if (customValues.length() > 0) {
if (firstVal) {
values = customValues;
} else {
values += ';' + customValues;
}
}
}
return values;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
package org.redcross.openmapkit.tagswipe;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.redcross.openmapkit.R;
import org.redcross.openmapkit.odkcollect.tag.ODKTag;
import org.redcross.openmapkit.odkcollect.tag.ODKTagItem;

import java.util.Collection;

/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link SelectMultipleTagValueFragment.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link SelectMultipleTagValueFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class SelectMultipleTagValueFragment extends Fragment {

private static final String IDX = "IDX";

private TagEdit tagEdit;
private View rootView;

private OnFragmentInteractionListener mListener;

public SelectMultipleTagValueFragment() {
// Required empty public constructor
}


public static SelectMultipleTagValueFragment newInstance(int idx) {
SelectMultipleTagValueFragment fragment = new SelectMultipleTagValueFragment();
Bundle args = new Bundle();
args.putInt(IDX, idx);
fragment.setArguments(args);
return fragment;
}

private void setupWidgets() {
TextView tagKeyLabelTextView = (TextView) rootView.findViewById(R.id.tagKeyLabelTextView);
TextView tagKeyTextView = (TextView) rootView.findViewById(R.id.tagKeyTextView);

String keyLabel = tagEdit.getTagKeyLabel();
String key = tagEdit.getTagKey();

if (keyLabel != null) {
tagKeyLabelTextView.setText(keyLabel);
tagKeyTextView.setText(key);
} else {
tagKeyLabelTextView.setText(key);
tagKeyTextView.setText("");
}

setupCheckBoxes();
}

@SuppressWarnings("ResourceType")
private void setupCheckBoxes() {
final LinearLayout checkboxLinearLayout = (LinearLayout)rootView.findViewById(R.id.checkboxLinearLayout);
final Activity activity = getActivity();
ODKTag odkTag = tagEdit.getODKTag();
if (odkTag == null) return;

/**
* Setting up buttons with prescribed choice values.
*/
String prevTagVal = tagEdit.getTagVal();
boolean prevTagValInTagItems = false;
Collection<ODKTagItem> odkTagItems = odkTag.getItems();
int id = 1;
for (ODKTagItem item : odkTagItems) {
String label = item.getLabel();
String value = item.getValue();
if (value.equals(prevTagVal)) {
prevTagValInTagItems = true;
}
CheckBox checkBox = new CheckBox(activity);
checkBox.setTextSize(18);
TextView textView = new TextView(activity);
textView.setPadding(66, 0, 0, 25);
textView.setOnClickListener(new TextViewOnClickListener(checkBox));
if (label != null) {
checkBox.setText(label);
textView.setText(value);
} else {
checkBox.setText(value);
textView.setText("");
}
checkboxLinearLayout.addView(checkBox);
if (prevTagVal != null && value.equals(prevTagVal)) {
checkBox.toggle();
}
checkBox.setId(id);
odkTag.putButtonIdToTagItemHash(id++, item);
odkTag.addCheckbox(checkBox);
checkboxLinearLayout.addView(textView);
}

final CheckBox editTextCheckBox = new CheckBox(activity);
final EditText editText = new EditText(activity);
editText.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
if (!prevTagValInTagItems && prevTagVal != null) {
editText.setText(prevTagVal);
editTextCheckBox.setChecked(true);
}
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}

@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.length() > 0) {
editTextCheckBox.setChecked(true);
} else {
editTextCheckBox.setChecked(false);
}
}

@Override
public void afterTextChanged(Editable editable) {
}
});
editTextCheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (editTextCheckBox.isChecked()) {
editText.setFocusableInTouchMode(true);
editText.requestFocus();
final InputMethodManager inputMethodManager = (InputMethodManager) activity
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
}
});
tagEdit.setupEditCheckbox(editTextCheckBox, editText);

LinearLayout customLinearLayout = new LinearLayout(activity);
customLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
customLinearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
customLinearLayout.setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS);
customLinearLayout.setFocusableInTouchMode(true);
customLinearLayout.addView(editTextCheckBox);
customLinearLayout.addView(editText);
checkboxLinearLayout.addView(customLinearLayout);

}

/**
* Allows us to pass a CheckBox as a parameter to onClick
*/
private class TextViewOnClickListener implements View.OnClickListener {
CheckBox checkBox;

public TextViewOnClickListener(CheckBox cb) {
checkBox = cb;
}

@Override
public void onClick(View v) {
checkBox.toggle();
}
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
int idx = getArguments().getInt(IDX);
tagEdit = TagEdit.getTag(idx);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_select_multiple_tag_value, container, false);
setupWidgets();
return rootView;
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}

@Override
public void onDetach() {
super.onDetach();
mListener = null;
}

/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.redcross.openmapkit.tagswipe;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
Expand All @@ -11,7 +10,6 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
Expand Down Expand Up @@ -40,9 +38,6 @@ public class SelectOneTagValueFragment extends Fragment {
private TagEdit tagEdit;
private View rootView;

private TextView tagKeyLabelTextView;
private TextView tagKeyTextView;

private OnFragmentInteractionListener mListener;


Expand All @@ -55,8 +50,8 @@ public static SelectOneTagValueFragment newInstance(int idx) {
}

private void setupWidgets() {
tagKeyLabelTextView = (TextView)rootView.findViewById(R.id.tagKeyLabelTextView);
tagKeyTextView = (TextView)rootView.findViewById(R.id.tagKeyTextView);
TextView tagKeyLabelTextView = (TextView) rootView.findViewById(R.id.tagKeyLabelTextView);
TextView tagKeyTextView = (TextView) rootView.findViewById(R.id.tagKeyTextView);

String keyLabel = tagEdit.getTagKeyLabel();
String key = tagEdit.getTagKey();
Expand Down Expand Up @@ -152,8 +147,8 @@ public void onClick(View view) {
if (prevTagVal != null && value.equals(prevTagVal)) {
button.toggle();
}
int buttonId = button.getId();
odkTag.putRadioButtonIdToTagItemHash(buttonId, item);
int id = button.getId();
odkTag.putButtonIdToTagItemHash(id, item);
tagValueRadioGroup.addView(textView);
}
if (!prevTagValInTagItems) {
Expand Down
Loading

0 comments on commit 85a37db

Please sign in to comment.