package com.averi.worldscribe.activities;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.widget.EditText;

import com.averi.worldscribe.Category;
import com.averi.worldscribe.R;
import com.averi.worldscribe.utilities.AppPreferences;
import com.averi.worldscribe.utilities.ExternalReader;

public class SnippetActivity extends AppCompatActivity {

    /**
     * The text field for editing the Snippet's content.
     */
    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_snippet);

        editText = (EditText) findViewById(R.id.editText);

        Intent intent = getIntent();
        String snippetName = intent.getStringExtra(AppPreferences.SNIPPET_NAME);
        setAppBarTitle(snippetName);
        loadSnippetContent(intent.getStringExtra(AppPreferences.WORLD_NAME),
                (Category) intent.getSerializableExtra(AppPreferences.CATEGORY),
                intent.getStringExtra(AppPreferences.ARTICLE_NAME),
                snippetName);
    }

    /**
     * Setup the app bar and set its title to be the Snippet's name.
     * @param snippetName The name of the Snippet displayed in this Activity.
     */
    private void setAppBarTitle(String snippetName) {
        Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(myToolbar);
        assert getSupportActionBar() != null;
        getSupportActionBar().setTitle(snippetName);
    }

    /**
     * Display the contents of the Snippet stored on file.
     */
    private void loadSnippetContent(String worldName, Category category, String articleName,
                                    String snippetName) {
        String snippetContent = ExternalReader.getSnippetText(this, worldName, category,
                articleName, snippetName);
        editText.setText(snippetContent);
    }
}

@@ -184,16 +184,79 @@
 oolbar;%0A
+import android.text.Editable;%0Aimport android.text.TextWatcher;%0A
 import a
@@ -460,16 +460,71 @@
 lReader;
+%0Aimport com.averi.worldscribe.utilities.ExternalWriter;
 %0A%0Apublic
@@ -680,16 +680,608 @@
 tText;%0A%0A
+    /**%0A     * Set to true if the Snippet was edited since the last time it was saved.%0A     */%0A    private boolean editedSinceLastSave = false;%0A%0A    /**%0A     * The name of the World that the Article possessing the Snippet belongs to.%0A     */%0A    private String worldName;%0A%0A    /**%0A     * The Category of the Article possessing the Snippet.%0A     */%0A    private Category category;%0A%0A    /**%0A     * The name of the Article possessing the Snippet.%0A     */%0A    private String articleName;%0A%0A    /**%0A     * The name of the Snippet displayed in this Activity.%0A     */%0A    private String snippetName;%0A%0A
     @Ove
@@ -1536,38 +1536,259 @@
 tent();%0A        
-String
+worldName = intent.getStringExtra(AppPreferences.WORLD_NAME);%0A        category = (Category) intent.getSerializableExtra(AppPreferences.CATEGORY);%0A        articleName = intent.getStringExtra(AppPreferences.ARTICLE_NAME);%0A       
  snippetName = i
@@ -1830,32 +1830,33 @@
 .SNIPPET_NAME);%0A
+%0A
         setAppBa
@@ -2123,32 +2123,561 @@
    snippetName);
+%0A%0A        editText.addTextChangedListener(new TextWatcher() %7B%0A            @Override%0A            public void beforeTextChanged(CharSequence s, int start, int count, int after) %7B %7D%0A%0A            @Override%0A            public void onTextChanged(CharSequence s, int start, int before, int count) %7B %7D%0A%0A            @Override%0A            public void afterTextChanged(Editable s) %7B editedSinceLastSave = true; %7D%0A        %7D);%0A    %7D%0A%0A    @Override%0A    protected void onPause() %7B%0A        super.onPause();%0A%0A        saveSnippetContentIfEdited();
 %0A    %7D%0A%0A    /**%0A
@@ -3505,11 +3505,459 @@
 );%0A    %7D
+%0A%0A    /**%0A     * Saves any edits made to the Snippet's content to the corresponding text file.%0A     */%0A    private void saveSnippetContentIfEdited() %7B%0A        if (editedSinceLastSave) %7B%0A            String snippetContent = editText.getText().toString();%0A            ExternalWriter.writeSnippetContents(this, worldName, category, articleName,%0A                    snippetName, snippetContent);%0A            editedSinceLastSave = false;%0A        %7D%0A%0A    %7D
 %0A%7D%0A

Save Snippet edit to file