package org.starlo.climate;

import android.os.*;
import android.app.*;
import android.view.*;
import android.widget.*;

public class ZipcodeInputFragment extends Fragment
{
    private ListView mList = null;
    private EditableArrayAdapter mAdapter = null;
    private final String[] DEFAULT_ZIP_CODES = new String[]{"78757", "78758", "78759"};

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.zipcode_input, container, false);
        mList = (ListView)view.findViewById(R.id.list);
        mAdapter = new EditableArrayAdapter(getActivity(), 0, DEFAULT_ZIP_CODES);
        mAdapter.setOnRemoveItemClickListener(new RemoveFieldListener());
        mList.setAdapter(mAdapter);
        Button addButton = (Button)view.findViewById(R.id.add);
        addButton.setOnClickListener(new AddFieldListener());

        return view;
    }

    private class AddFieldListener implements View.OnClickListener
    {
        public void onClick(View view)
        {
            String[] newList = new String[mAdapter.getCount()+1];
            for(int i = 0; i < DEFAULT_ZIP_CODES.length; i++)
            {
                newList[i] = DEFAULT_ZIP_CODES[i];
            }
            mAdapter = new EditableArrayAdapter(getActivity(), 0, newList);
            mAdapter.setOnRemoveItemClickListener(new RemoveFieldListener());
            mList.setAdapter(mAdapter);
        }
    }

    private class RemoveFieldListener implements View.OnClickListener
    {
        public void onClick(View view)
        {
            Integer position = (Integer)((View)view.getParent()).getTag();
        }
    }
}

@@ -112,16 +112,63 @@
 idget.*;
+%0Aimport android.content.*;%0A%0Aimport java.util.*;
 %0A%0Apublic
@@ -241,32 +241,69 @@
 w mList = null;%0A
+    private Context mContext = null;%0A
     private Edit
@@ -364,44 +364,227 @@
 ring
-%5B%5D DEFAULT_ZIP_CODES = new String%5B%5D%7B
+ SAVED_ZIP_CODES_KEY = %22codes%22;%0A    private final String SAVED_ZIP_CODES_FILE = %22zipcodes%22;%0A    private final static Set%3CString%3E DEFAULT_ZIP_CODES =%0A        Collections.unmodifiableSet(new HashSet%3CString%3E(Arrays.asList(
 %22787
@@ -604,17 +604,19 @@
  %2278759%22
-%7D
+)))
 ;%0A%0A    @
@@ -728,24 +728,58 @@
 tate)%0A    %7B%0A
+        mContext = getActivity();%0A
         View
@@ -914,114 +914,87 @@
     
-mAdapter = new EditableArrayAdapter(getActivity(), 0, DEFAULT_ZIP_CODES);%0A        mAdapter.setOnRemoveItem
+Button addButton = (Button)view.findViewById(R.id.add);%0A        addButton.setOn
 Clic
@@ -999,38 +999,35 @@
 ickListener(new 
-Remove
+Add
 FieldListener())
@@ -1028,24 +1028,25 @@
 ner());%0A
+%0A
         
 mList.se
@@ -1041,123 +1041,337 @@
     
-mList.setAdapter(mAdapter);%0A        Button addButton = (Button)view.findViewById(R.id.add);%0A        addButton.setOn
+SharedPreferences preferences = mContext.getSharedPreferences(SAVED_ZIP_CODES_FILE, Context.MODE_PRIVATE);%0A        Set%3CString%3E data = preferences.getStringSet(SAVED_ZIP_CODES_KEY, DEFAULT_ZIP_CODES);%0A        mAdapter = new EditableArrayAdapter(mContext, 0, data.toArray(new String%5Bdata.size()%5D));%0A        mAdapter.setOnRemoveItem
 Clic
@@ -1376,35 +1376,38 @@
 ickListener(new 
-Add
+Remove
 FieldListener())
@@ -1399,32 +1399,68 @@
 ieldListener());
+%0A        mList.setAdapter(mAdapter);
 %0A%0A        return
@@ -1612,56 +1612,293 @@
    S
-tring%5B%5D newList = new String%5BmAdapter.getCount()
+haredPreferences preferences = mContext.getSharedPreferences(SAVED_ZIP_CODES_FILE, Context.MODE_PRIVATE);%0A            Set%3CString%3E data = preferences.getStringSet(SAVED_ZIP_CODES_KEY, DEFAULT_ZIP_CODES);%0A            int size = data.size();%0A            String%5B%5D newList = new String%5Bsize
 +1%5D;
@@ -1933,32 +1933,12 @@
 i %3C 
-DEFAULT_ZIP_CODES.length
+size
 ; i+
@@ -1974,56 +1974,424 @@
     
-newList%5Bi%5D = DEFAULT_ZIP_CODES%5Bi%5D;%0A            %7D
+View item = mList.getChildAt(i);%0A                newList%5Bi%5D = ((TextView)item.findViewById(R.id.text)).getText().toString();%0A            %7D%0A            newList%5Bsize%5D = %22%22;%0A            SharedPreferences.Editor editor = preferences.edit();%0A            HashSet%3CString%3E uniqueSet = new HashSet%3CString%3E(Arrays.asList(newList));%0A            editor.putStringSet(SAVED_ZIP_CODES_KEY, uniqueSet);%0A            editor.commit();%0A
 %0A   
@@ -2453,23 +2453,63 @@
 y(), 0, 
-newList
+uniqueSet.toArray(new String%5BuniqueSet.size()%5D)
 );%0A     

Add a backing data store for zipcodes