Demo entry 3912555

SavedData

   

Submitted by SavedData on Mar 06, 2016 at 22:15
Language: Java. Code size: 4.9 kB.

package com.hfad.databasetest;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Calendar;

/**
 * Created by Stephen on 3/2/2016.
 */
public class SavedData {

    private static SavedData instance = null;
    private Context mainActivityContext;
    public static String LOG_TAG = "My log tag";

    public static SavedData getSavedData(Context inputMainActivityContext){
        if(instance == null){
            return instance = new SavedData(inputMainActivityContext);
        }
        return instance;
    }

    /**
     * My climbs is the most up to date list of entries.
     */
    private ArrayList<Entry> myClimbs = new ArrayList<Entry>();

    /**
     * Construcor for class SavedData
     * @param context is the context from the main Activity
     */
    public SavedData(Context context){
        Log.i(LOG_TAG, "Inside of constructor for SavedData");
        mainActivityContext = context;
        readData();
    }

    /**
     * ReadData retrieves an instance of class Climblist from shared preferences and saves it into
     * the field myJsonClimbList.
     *
     * ReadData then saves the ArrayList of climbs from myJsonClimbList into the field myClimbs
     */
    public void readData(){
        Log.i(LOG_TAG,"Inside of readData");

        //Creates a shared preferences instance from the Main Activities Context
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mainActivityContext);
        //Creates a gson object for json conversion and reads the json from sharedPreferences.
        Gson gson = new Gson();
        String json = settings.getString("MyClimbList", null);
        if(json == null){
            Log.i(LOG_TAG, "MyClimbList not found in shared preferences");
        }
        else{
            Log.i(LOG_TAG,"MyClimbList found: " + json);
            JsonClimbList sharedPrefData = gson.fromJson(json, JsonClimbList.class);
            myClimbs = sharedPrefData.getMyClimbs();
        }
    }

    /**
     * Write data updates myJsonClimbList with the most up to date ArrayList of entries
     * Write data then serializes myJsonClimbList into json and stores it with the tag MyClimbList
     */
    public void writeData(){
        Log.i(LOG_TAG,"Inside of writeData");
        //Creates a shared preferences instance fromm the Main Actives's context
        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mainActivityContext);
        SharedPreferences.Editor prefsEditor = settings.edit();
        //Creates a gson object to be used in conversion
        sortClimbsByDate();
        JsonClimbList myJsonClimbList = new JsonClimbList(myClimbs);

        Gson gson = new Gson();
        String json = gson.toJson(myJsonClimbList);

        Log.i(LOG_TAG,"About to write: " + json);
        prefsEditor.putString("MyClimbList", json);
        prefsEditor.commit();
    }

    /**
     * Climbing entry for a climb on the current day.
     * Takes the current day month and year.
     */
    public void addClimb(int grade, boolean rope){
        Calendar c = Calendar.getInstance();
        Entry inputEntry = new Entry(grade, rope, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
        myClimbs.add(inputEntry);
    }

    /**
     * Climbing entry with an option to put in a specfici year month and day
     */
    public void addClimb(int grade, boolean rope, int year, int month, int day){
        Entry inputEntry = new Entry(grade, rope, year, month, day);
        myClimbs.add(inputEntry);
    }

    public void sortClimbsByDate(){
        int j;
        boolean flag = true;
        int temp;
        while(flag){
            flag = false;
            for(j = 0; j < myClimbs.size() - 1; j++){
                if(myClimbs.get(j).getDay().compareTo(myClimbs.get(j+1).getDay()) == -1){
                    myClimbs.get(j).swapParams(myClimbs.get(j+1));
                    flag = true;
                }
            }
        }
    }

    public void sortClimbsByGrade(){
        int j;
        boolean flag = true;
        int temp;
        while(flag){
            flag = false;
            for(j = 0; j < myClimbs.size() - 1; j++){
                if(myClimbs.get(j).getRawGrade() < myClimbs.get(j+1).getRawGrade()){
                    myClimbs.get(j).swapParams(myClimbs.get(j+1));
                    flag = true;
                }
            }
        }
    }

    public ArrayList<Entry> getClimbs(){
        return myClimbs;
    }

    public void setClimbs(ArrayList<Entry> inputClimbs){
        myClimbs = inputClimbs;
    }

}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).