package org.wordpress.persistentedittext;

import android.content.Context;
import android.view.View;
import android.widget.EditText;

public class PersistentEditTextHelper {
    private String mUniqueId;
    private PersistentEditTextDatabase mPersistentEditTextDatabase;

    public PersistentEditTextHelper(Context context) {
        mPersistentEditTextDatabase = new PersistentEditTextDatabase(context);
    }

    public void setUniqueId(String uniqueId) {
        mUniqueId = uniqueId;
    }

    public String getUniqueId() {
        return mUniqueId;
    }

    private static String getViewPathId(View view) {
        StringBuilder sb = new StringBuilder();
        for (View currentView = view; currentView != null && currentView.getParent() != null
                && currentView.getParent() instanceof View; currentView = (View) currentView.getParent()) {
            sb.append(currentView.getId());
        }
        return sb.toString();
    }

    public void clearSavedText(View view) {
        clearSavedText(view, mUniqueId);
    }

    public void loadString(EditText editText) {
        String text = mPersistentEditTextDatabase.get(getViewPathId(editText) + mUniqueId, "");
        if (!text.isEmpty()) {
            editText.setText(text);
            editText.setSelection(text.length());
        }
    }

    public void saveString(EditText editText) {
        if (editText.getText() == null) {
            return;
        }
        mPersistentEditTextDatabase.put(getViewPathId(editText) + mUniqueId, editText.getText().toString());
    }

    public void clearSavedText(View view, String uniqueId) {
        mPersistentEditTextDatabase.remove(getViewPathId(view) + uniqueId);
    }
}

@@ -415,510 +415,608 @@
     
-public void setUniqueId(String uniqueId) %7B%0A        mUniqueId = uniqueId;%0A    %7D%0A%0A    public String getUniqueId() %7B%0A        return mUniqueId;%0A    %7D%0A%0A    private static String getViewPathId(View view) %7B%0A        StringBuilder sb = new StringBuilder();%0A        for (View currentView = view; currentView != null && currentView.getParent() != null%0A                && currentView.getParent() instanceof View; currentView = (View) currentView.getParent()) %7B%0A            sb.append(currentView.getId())
+/**%0A     * Set a unique id. This is only needed to identify a text edit from another if they share the same layout%0A     * path and id.%0A     *%0A     * For instance if you want to use the same fragment with one text field to display different content and want%0A     * the saved text in the EditField to be differentiated, you'll have to set that id. Another example: if you have%0A     * a listview with an EditText in each rows, you might want to set a different id for each row.%0A     */%0A    public void setUniqueId(String uniqueId) %7B%0A        mUniqueId = uniqueId
 ;%0A    
+%7D%0A%0A
     
-%7D
+public String getUniqueId() %7B
 %0A   
@@ -1031,21 +1031,17 @@
 urn 
-sb.toString()
+mUniqueId
 ;%0A  
@@ -1793,11 +1793,406 @@
 );%0A    %7D
+%0A%0A    protected static String getViewPathId(View view) %7B%0A        StringBuilder sb = new StringBuilder();%0A        for (View currentView = view; currentView != null && currentView.getParent() != null%0A                && currentView.getParent() instanceof View; currentView = (View) currentView.getParent()) %7B%0A            sb.append(currentView.getId());%0A        %7D%0A        return sb.toString();%0A    %7D
 %0A%7D%0A

add a comment to explain setUniqueId method