package com.averi.worldscribe.utilities;

import android.content.Context;
import android.os.Environment;

import com.averi.worldscribe.Category;

import java.io.File;

/**
 * Created by mark on 08/06/16.
 */
public final class ExternalWriter {
//    public static boolean createAppDirectory() {
//        boolean directoryExists = true;
//
//        File appDirectory = new File(Environment.getExternalStorageDirectory(), APP_DIRECTORY_NAME);
//        Log.d("WorldScribe", appDirectory.getAbsolutePath());
//
//        if (externalStorageIsWritable()) {
//            if (!(appDirectory.exists())) {
//                directoryExists = appDirectory.mkdirs();
//            }
//        }
//
//        return directoryExists;
//    }

    private static boolean externalStorageIsWritable() {
        String state = Environment.getExternalStorageState();
        return (Environment.MEDIA_MOUNTED.equals(state));
    }

    public static boolean createWorldDirectory(Context context, String worldName) {
        boolean directoryWasCreated = true;

        File worldDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
                + "/" + FileRetriever.APP_DIRECTORY_NAME + "/", worldName);

        directoryWasCreated = worldDirectory.mkdirs();

        if (directoryWasCreated) {
            directoryWasCreated = createArticleTypeDirectories(context, worldDirectory);

            // If one or more subfolders couldn't be created, delete the World folder so that
            // subsequent attempts can start building the World folder again from scratch.
            if (!(directoryWasCreated)) {
                // Delete the World directory.
            }
        }

        return directoryWasCreated;
    }

    private static boolean createArticleTypeDirectories(Context context, File worldDirectory) {
        boolean directoriesWereCreated = true;

        for (Category category : Category.values()) {
            File articleFolder = new File(worldDirectory.getAbsolutePath(), category.pluralName(context));

            if (!(articleFolder.mkdirs())) {
                directoriesWereCreated = false;
            }
        }

        return directoriesWereCreated;
    }
}

@@ -159,16 +159,134 @@
 io.File;
+%0Aimport java.io.FileNotFoundException;%0Aimport java.io.IOError;%0Aimport java.io.IOException;%0Aimport java.io.PrintWriter;
 %0A%0A/**%0A *
@@ -2325,11 +2325,1351 @@
 d;%0A    %7D
+%0A%0A    /**%0A     * Saves a String to a text file within an Article's directory.%0A     * @param context The Context calling this method.%0A     * @param worldName The name of the world the Article belongs to.%0A     * @param category The name of the Category the Article belongs to.%0A     * @param articleName The name of the Article whose directory will possess the text file.%0A     * @param fileName The name of the text file that will be written to, with the file extension%0A     *                 omitted.%0A     * @param contents The String that will be saved in a text file.%0A     * @return True if the String was saved successfully; false if an I/O error occurs.%0A     */%0A    public static boolean writeStringToArticleFile(Context context, String worldName,%0A                                                   Category category, String articleName,%0A                                                   String fileName, String contents) %7B%0A        Boolean result = true;%0A%0A        try %7B%0A            PrintWriter writer = new PrintWriter(FileRetriever.getArticleFile(context, worldName,%0A                    category, articleName, fileName + ExternalReader.TEXT_FIELD_FILE_EXTENSION));%0A            writer.println(contents);%0A            writer.close();%0A        %7D catch (IOException error) %7B%0A            result = false;%0A        %7D%0A%0A        return result;%0A    %7D
 %0A%7D%0A

Add writeStringToArticleFile to ExternalWriter