package mprog.nl.mars_weather_explorer;

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

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Nadeche
 */
public class SharedPreferencesManager {

    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor sharedPreferencesEditor;
    private boolean celsiusUnit;
    private String camera;
    private int latestSol;
    private String imageFilePath;
    private List<BaseFragmentSuper> baseFragments = new ArrayList<>();

    private static SharedPreferencesManager preferencesManager = null;

    private SharedPreferencesManager(Context context){
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        sharedPreferencesEditor = sharedPreferences.edit();
        celsiusUnit = sharedPreferences.getBoolean("celsiusUnit", true);
        camera = sharedPreferences.getString("camera", "Front Hazard Avoidance Camera");
        latestSol = sharedPreferences.getInt("latestSol", 1363);
        imageFilePath = sharedPreferences.getString("imageFilePath", "");
    }

    public static SharedPreferencesManager getInstance(Context context){
        if(preferencesManager == null){
            preferencesManager = new SharedPreferencesManager(context);
        }
        return preferencesManager;
    }

    public String getCamera() {
        return camera;
    }

    public void setCamera(String camera) {
        this.camera = camera;
        sharedPreferencesEditor.putString("camera", camera);
        sharedPreferencesEditor.commit();
    }

    public boolean isCelsiusUnit() {
        return celsiusUnit;
    }

    public void setCelsiusUnit(boolean celsiusUnit) {
        this.celsiusUnit = celsiusUnit;
        sharedPreferencesEditor.putBoolean("celsiusUnit", celsiusUnit);
        sharedPreferencesEditor.commit();

        for (BaseFragmentSuper baseFragment:baseFragments){
            baseFragment.onTemperatureUnitChanged();
        }
    }

    public int getLatestSol() {
        return latestSol;
    }

    public void setLatestSol(int latestSol) {
        this.latestSol = latestSol;
        sharedPreferencesEditor.putInt("latestSol", latestSol);
        sharedPreferencesEditor.commit();
    }

    public String getImageFilePath() {
        return imageFilePath;
    }

    public void setImageFilePath(String imageFilePath) {
        this.imageFilePath = imageFilePath;
        sharedPreferencesEditor.putString("imageFilePath", imageFilePath);
        sharedPreferencesEditor.commit();
    }

    public void registerBaseFragmentSuper(BaseFragmentSuper baseFragment){
        boolean classExists = false;
        for (BaseFragmentSuper fragment: baseFragments){
            if (fragment.getClass() == baseFragment.getClass()){
                classExists = true;
                break;
            }
        }
        if (!classExists){
            baseFragments.add(baseFragment);
        }
    }
}

@@ -30,24 +30,99 @@
 _explorer;%0A%0A
+/**%0A * SharedPreferencesManager.java%0A *%0A * Created by Nadeche Studer%0A * */%0A
 import andro
@@ -292,103 +292,196 @@
 %0A * 
-Created by Nadeche%0A */%0Apublic class SharedPreferencesManager %7B%0A%0A    private SharedPreferences s
+This class is a singleton that updates the SharedPreferences whenever they change.%0A * It provides information to the app and to the widget about the users preferences.%0A * */%0Apublic class S
 hare
@@ -492,17 +492,25 @@
 ferences
-;
+Manager%7B%0A
 %0A    pri
@@ -567,129 +567,581 @@
 tor;
-%0A    private boolean celsiusUnit;%0A    private String camera;%0A    private int latestSol;%0A    private String imageFilePath;
+           // can make alterations to the saved preferences%0A    private boolean celsiusUnit;                                        // true if the user prefers to use the Celsius scale%0A    private String camera;                                              // the name of the camera the user has last loaded a photo from%0A    private int latestSol;                                              // the last Martian solar day there is data about%0A    private String imageFilePath;                                       // the file path where the last downloaded photo is stored
 %0A   
@@ -1207,16 +1207,63 @@
 ist%3C%3E();
+  // a list of all fragments in the swipe views
 %0A%0A    pr
@@ -1383,32 +1383,50 @@
 ntext)%7B%0A        
+SharedPreferences 
 sharedPreference
@@ -1769,12 +1769,9 @@
 l%22, 
-1363
+0
 );%0A 
@@ -2396,32 +2396,193 @@
 iusUnit;%0A    %7D%0A%0A
+    /** This method loops through all fragments when the temperature unit changes%0A     * to let them know the unit is changed and they should act accordingly */%0A
     public void 
@@ -3457,32 +3457,131 @@
 ommit();%0A    %7D%0A%0A
+    /** This method checks the list of fragments and adds the fragment if it is not in the list */%0A
     public void 

Refactor and comments added to SharedPreferencesManager.