package com.averi.worldscribe;

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

import com.averi.worldscribe.utilities.ExternalReader;

/**
 * Created by mark on 19/07/16.
 * An editable text field for describing some quality about a certain Article.
 */
public class ArticleTextField {
    /**
     * The name of this text field.
     */
    private String name;

    /**
     * The EditText that will allow the user to edit this text field.
     */
    private EditText editText;

    /**
     * The context this text field belongs to.
     */
    private Context context;

    /**
     * The name of the world of the Article this text field belongs to.
     */
    private String worldName;

    /**
     * The Category of the Article this text field belongs to.
     */
    private Category category;

    /**
     * The name of the Article this text field belongs to.
     */
    private String articleName;

    /**
     * Instantiates a new ArticleTextField.
     * @param name The name of this text field.
     * @param editText The EditText that will allow the user to edit this text field.
     * @param context The context this text field belongs to.
     * @param worldName The name of the world of the Article this text field belongs to.
     * @param category The Category of the Article this text field belongs to.
     * @param articleName The name of the Article this text field belongs to.
     */
    public ArticleTextField(String name, EditText editText, Context context, String worldName,
                            Category category, String articleName) {
        this.name = name;
        this.editText = editText;
        this.context = context;
        this.worldName = worldName;
        this.category = category;
        this.articleName = articleName;
    }

    /**
     * Loads data from the text file associated with this text field and display it in the EditText.
     * Displays empty contents instead if the text file doesn't exist or couldn't be read.
     */
    public void loadData() {
        editText.setText(ExternalReader.getArticleTextFieldData(context, worldName, category,
                articleName, name));
    }
}

@@ -57,16 +57,79 @@
 ontext;%0A
+import android.text.Editable;%0Aimport android.text.TextWatcher;%0A
 import a
@@ -207,16 +207,71 @@
 lReader;
+%0Aimport com.averi.worldscribe.utilities.ExternalWriter;
 %0A%0A/**%0A *
@@ -1038,24 +1038,235 @@
 ticleName;%0A%0A
+    /**%0A     * Set to True if the contents of this text field were edited since the last time contents%0A     * were saved to the corresponding text file.%0A     */%0A    private Boolean editedSinceLastSave = false;%0A%0A
     /**%0A    
@@ -2125,381 +2125,1281 @@
 me;%0A
-    %7D%0A%0A    /**%0A     * Loads data from the text file associated with this text field and display it in the EditText.%0A     * Displays empty contents instead if the text file doesn't exist or couldn't be read.%0A     */%0A    public void loadData() %7B%0A        editText.setText(ExternalReader.getArticleTextFieldData(context, worldName, category,%0A                articleName, name))
+%0A        this.editText.addTextChangedListener(new TextWatcher() %7B%0A            @Override%0A            public void beforeTextChanged(CharSequence s, int start, int count, int after) %7B%0A%0A            %7D%0A%0A            @Override%0A            public void onTextChanged(CharSequence s, int start, int before, int count) %7B%0A%0A            %7D%0A%0A            @Override%0A            public void afterTextChanged(Editable s) %7B%0A                editedSinceLastSave = true;%0A            %7D%0A        %7D);%0A    %7D%0A%0A    /**%0A     * Loads data from the text file associated with this text field and display it in the EditText.%0A     * Displays empty contents instead if the text file doesn't exist or couldn't be read.%0A     */%0A    public void loadData() %7B%0A        editText.setText(ExternalReader.getArticleTextFieldData(context, worldName, category,%0A                articleName, name));%0A    %7D%0A%0A    /**%0A     * If the EditText has been edited since the last save, write its contents to this text field's%0A     * corresponding text file.%0A     */%0A    public void saveDataIfEdited() %7B%0A        if (editedSinceLastSave) %7B%0A            ExternalWriter.writeStringToArticleFile(context, worldName, category, articleName,%0A                    name, editText.getText().toString());%0A        %7D%0A%0A        editedSinceLastSave = false
 ;%0A  

Add saving of contents in ArticleTextField