Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7f791335 authored by Marten Gajda's avatar Marten Gajda
Browse files

update checklist editor to use new check list adapter

parent 0ff0bfa4
Loading
Loading
Loading
Loading
+0 −10
Original line number Diff line number Diff line
@@ -19,16 +19,6 @@
            android:singleLine="true" />
    </LinearLayout>

    <EditText
        android:id="@android:id/text1"
        style="@style/field_editor_text_value"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:singleLine="false"
        android:textAllCaps="false" >
    </EditText>

    <LinearLayout
        android:id="@+id/checklist"
        android:layout_width="match_parent"
+68 −103
Original line number Diff line number Diff line
@@ -17,22 +17,25 @@

package org.dmfs.tasks.widget;

import java.util.List;

import org.dmfs.tasks.R;
import org.dmfs.tasks.model.CheckListItem;
import org.dmfs.tasks.model.ContentSet;
import org.dmfs.tasks.model.FieldDescriptor;
import org.dmfs.tasks.model.adapters.StringFieldAdapter;
import org.dmfs.tasks.model.adapters.ChecklistFieldAdapter;
import org.dmfs.tasks.model.layout.LayoutOptions;

import android.content.Context;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
@@ -47,11 +50,11 @@ import android.widget.EditText;
 */
public class CheckListFieldEditor extends AbstractFieldEditor implements OnCheckedChangeListener, OnFocusChangeListener
{
	private StringFieldAdapter mAdapter;
	private ChecklistFieldAdapter mAdapter;
	private ViewGroup mContainer;
	private EditText mText;

	private String mCurrentValue;
	private List<CheckListItem> mCurrentValue;
	private LayoutInflater mInflater;

	private boolean mBuilding = false;
@@ -101,7 +104,7 @@ public class CheckListFieldEditor extends AbstractFieldEditor implements OnCheck
	public void setFieldDescription(FieldDescriptor descriptor, LayoutOptions layoutOptions)
	{
		super.setFieldDescription(descriptor, layoutOptions);
		mAdapter = (StringFieldAdapter) descriptor.getFieldAdapter();
		mAdapter = (ChecklistFieldAdapter) descriptor.getFieldAdapter();
	}


@@ -110,7 +113,22 @@ public class CheckListFieldEditor extends AbstractFieldEditor implements OnCheck
	{
		if (!mBuilding && mValues != null)
		{
			updateValues();
			if (mCurrentValue == null || mBuilding)
			{
				return;
			}

			ViewParent parent = buttonView.getParent();
			CheckItemTag tag = (CheckItemTag) ((View) parent).getTag();
			if (tag != null && tag.index < mCurrentValue.size())
			{
				mCurrentValue.get(tag.index).checked = isChecked;
				if (mValues != null)
				{
					mAdapter.validateAndSet(mValues, mCurrentValue);
				}
				return;
			}
		}
	}

@@ -120,17 +138,26 @@ public class CheckListFieldEditor extends AbstractFieldEditor implements OnCheck
	{
		if (!hasFocus /* update only when loosing the focus */&& !mBuilding && mValues != null)
		{
			updateValues();
			ViewParent parent = v.getParent();
			CheckItemTag tag = (CheckItemTag) ((View) parent).getTag();
			if (tag != null && tag.index < mCurrentValue.size())
			{
				if (mCurrentValue.get(tag.index).text.length() == 0)
				{
					mCurrentValue.remove(tag.index);
					buildCheckList(mCurrentValue);
				}
			}
			updateValues();
		}
	}


	@Override
	public void onContentLoaded(ContentSet contentSet)
	{
		super.onContentLoaded(contentSet);
		String newValue = mAdapter.get(contentSet);
		List<CheckListItem> newValue = mCurrentValue = mAdapter.get(contentSet);
		buildCheckList(newValue);
	}

@@ -140,8 +167,8 @@ public class CheckListFieldEditor extends AbstractFieldEditor implements OnCheck
	{
		if (mValues != null)
		{
			String newValue = mAdapter.get(mValues);
			if (!TextUtils.equals(mCurrentValue, newValue)) // don't trigger unnecessary updates
			List<CheckListItem> newValue = mAdapter.get(mValues);
			if (newValue != null && !newValue.equals(mCurrentValue)) // don't trigger unnecessary updates
			{
				buildCheckList(newValue);
				mCurrentValue = newValue;
@@ -153,97 +180,31 @@ public class CheckListFieldEditor extends AbstractFieldEditor implements OnCheck
	@Override
	public void updateValues()
	{
		final String newText = getCheckListDescription();
		final String oldText = mAdapter.get(mValues);

		if (!TextUtils.equals(newText, oldText)) // don't trigger unnecessary updates
		{
			mAdapter.validateAndSet(mValues, newText);
		mAdapter.validateAndSet(mValues, mCurrentValue);
	}
	}


	private String getCheckListDescription()
	{
		StringBuilder builder = new StringBuilder(4 * 1024);
		String descriptionText = mText.getText().toString();
		builder.append(descriptionText);

		int count = mContainer.getChildCount();

		boolean first = descriptionText.length() == 0;
		for (int i = 0; i < count; ++i)
		{
			CheckItemTag tag = (CheckItemTag) mContainer.getChildAt(i).getTag();
			String text = tag.editText.getText().toString();
			if (text.length() > 0)
			{
				if (first)
	private void buildCheckList(List<CheckListItem> list)
	{
					first = false;
				}
				else
		Context context = getContext();
		Integer customBackgroud = getCustomBackgroundColor();
		if (customBackgroud != null)
		{
					builder.append("\n");
			mText.setTextColor(getTextColorFromBackground(customBackgroud));
		}

				builder.append(tag.checkbox.isChecked() ? "[x] " : "[ ] ");
				builder.append(text);
			}
		}
		return builder.toString();
		if (list == null || list.size() == 0)
		{
			setVisibility(GONE);
			return;
		}
		setVisibility(VISIBLE);


	private void buildCheckList(String text)
	{
		mBuilding = true;

		String[] items;
		if (text != null && text.length() > 0)
		{
			items = text.split("\n");
		}
		else
		{
			items = new String[0];
		}

		int count = 0;
		boolean inCheckListMode = false;
		int checkListStart = 0;

		for (int i = 0; i < items.length; ++i)
		{
			String item = items[i];
			boolean checked = false;
			if (item.startsWith("[x]") || item.startsWith("[X]"))
			{
				checked = true;
				item = item.substring(3).trim();
				inCheckListMode = true;
			}
			else if (item.startsWith("[ ]"))
			{
				item = item.substring(3).trim();
				inCheckListMode = true;
			}
			else if (!inCheckListMode)
			{
				checkListStart += item.length();
				if (i < items.length - 1)
				{
					// account for the removed new line character
					++checkListStart;
				}
				continue;
			}

			if (item.length() == 0)
		for (CheckListItem item : list)
		{
				continue;
			}

			ViewGroup vg = (ViewGroup) mContainer.getChildAt(count);
			CheckItemTag tag;
			if (vg != null)
@@ -252,21 +213,26 @@ public class CheckListFieldEditor extends AbstractFieldEditor implements OnCheck
				if (tag == null)
				{
					// this might happen for the initial element
					tag = new CheckItemTag(vg);
					tag = new CheckItemTag(vg, count);
				}
			}
			else
			{
				vg = (ViewGroup) mInflater.inflate(R.layout.checklist_field_editor_element, mContainer, false);
				tag = new CheckItemTag(vg);
				tag = new CheckItemTag(vg, count);
				mContainer.addView(vg);
			}

			tag.setItem(checked, item, false);
			tag.setItem(item.checked, item.text, false);

			++count;
		}

		while (mContainer.getChildCount() > count)
		{
			mContainer.removeViewAt(count);
		}

		// add one empty element
		ViewGroup vg = (ViewGroup) mContainer.getChildAt(count);
		CheckItemTag tag;
@@ -276,26 +242,18 @@ public class CheckListFieldEditor extends AbstractFieldEditor implements OnCheck
			if (tag == null)
			{
				// this might happen for the initial element
				tag = new CheckItemTag(vg);
				tag = new CheckItemTag(vg, count);
			}
		}
		else
		{
			vg = (ViewGroup) mInflater.inflate(R.layout.checklist_field_editor_element, mContainer, false);
			tag = new CheckItemTag(vg);
			tag = new CheckItemTag(vg, count);
			mContainer.addView(vg);
		}

		tag.setItem(false, "", true);

		++count;

		while (mContainer.getChildCount() > count)
		{
			mContainer.removeViewAt(count);
		}

		mText.setText(text != null ? text.substring(0, checkListStart).trim() : null);
		mBuilding = false;
	}

@@ -303,14 +261,16 @@ public class CheckListFieldEditor extends AbstractFieldEditor implements OnCheck
	{
		public final CheckBox checkbox;
		public final EditText editText;
		public final int index;
		private boolean mIsLast;


		public CheckItemTag(ViewGroup viewGroup)
		public CheckItemTag(ViewGroup viewGroup, int index)
		{
			checkbox = (CheckBox) viewGroup.findViewById(android.R.id.checkbox);
			editText = (EditText) viewGroup.findViewById(android.R.id.text1);
			viewGroup.setTag(this);
			this.index = index;

			int inputType = editText.getInputType();
			editText.setInputType(inputType | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
@@ -338,9 +298,14 @@ public class CheckListFieldEditor extends AbstractFieldEditor implements OnCheck
				{
					if (mIsLast && !mBuilding && s.length() > 0)
					{
						mCurrentValue.add(new CheckListItem(checkbox.isChecked(), s.toString()));
						updateValues();
						buildCheckList(mCurrentValue);
					}
					else if (!mBuilding)
					{
						mCurrentValue.get(CheckItemTag.this.index).text = s.toString();
					}
				}
			});
			editText.setOnFocusChangeListener(CheckListFieldEditor.this);