package org.mti.hip.utils;

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

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.commons.io.FileUtils;
import org.mti.hip.model.Tally;
import org.mti.hip.model.Visit;

import java.io.File;
import java.io.IOException;

/**
 * Created by r624513 on 11/4/15.
 */
public class StorageManager {

    public static final String TALLY_FILENAME = "tally_file";
    public static final String LOG_TAG = "MTI-HIP";

    private Tally tally;

    public StorageManager() {
        tally = new Tally();
    }

    public Tally getTally() {
        return tally;
    }

    public Visit newVisit() {
        Visit visit = new Visit();
        tally.add(visit);

        return tally.get(tally.size() -1);
    }

    public Visit currentVisit() {
        return tally.get(tally.size() -1);
    }

}

@@ -114,120 +114,8 @@
 g;%0A%0A
-import com.fasterxml.jackson.core.JsonProcessingException;%0Aimport com.fasterxml.jackson.databind.ObjectMapper;%0A%0A
 impo
@@ -150,16 +150,16 @@
 eUtils;%0A
+
 import o
@@ -772,32 +772,32 @@
 urrentVisit() %7B%0A
-
         return t
@@ -830,10 +830,1895 @@
 %0A    %7D%0A%0A
+    /**%0A     * Reads the persisted file from disk and returns is as a String.%0A     * @param context%0A     * @return%0A     */%0A    public String readFileToString(Context context) %7B%0A        if (isExternalStorageReadable()) %7B%0A            File file = new File(context.getExternalFilesDir(null), TALLY_FILENAME);%0A            try %7B%0A                return FileUtils.readFileToString(file);%0A            %7D catch (IOException e) %7B%0A                e.printStackTrace();%0A            %7D%0A        %7D%0A        else %7B%0A            Log.w(LOG_TAG, %22External storage is not readable%22);%0A        %7D%0A        return null;%0A    %7D%0A%0A    /**%0A     * Persists the JSON string representation of the Tally object to a file on disk%0A     * @param jsonString%0A     * @param context%0A     */%0A    public void writeStringToFile(String jsonString, Context context) %7B%0A        if (isExternalStorageWritable()) %7B%0A            File file = new File(context.getExternalFilesDir(null), TALLY_FILENAME);%0A            try %7B%0A                FileUtils.writeStringToFile(file, jsonString);%0A            %7D catch (IOException e) %7B%0A                e.printStackTrace();%0A            %7D%0A        %7D%0A        else %7B%0A            Log.w(LOG_TAG, %22External storage is not writable%22);%0A        %7D%0A    %7D%0A%0A    /* Checks if external storage is available for read and write */%0A    public boolean isExternalStorageWritable() %7B%0A        String state = Environment.getExternalStorageState();%0A        if (Environment.MEDIA_MOUNTED.equals(state)) %7B%0A            return true;%0A        %7D%0A        return false;%0A    %7D%0A%0A    /* Checks if external storage is available to at least read */%0A    public boolean isExternalStorageReadable() %7B%0A        String state = Environment.getExternalStorageState();%0A        if (Environment.MEDIA_MOUNTED.equals(state) %7C%7C%0A                Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) %7B%0A            return true;%0A        %7D%0A        return false;%0A    %7D%0A%0A
 %7D%0A

Save JSON string to disk, read JSON string from disk