/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2017 Abhijit Parida <abhijitparida.me@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package app.abhijit.iter.data;

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

import com.google.gson.Gson;

import app.abhijit.iter.models.Student;

/**
 * This class handles local storage and retrieval of Student objects.
 */
public class Cache {

    private SharedPreferences sharedPreferences;
    private Gson gson;

    public Cache(Context context) {
        this.sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        this.gson = new Gson();
    }

    public Student getStudent() {
        try {
            return gson.fromJson(sharedPreferences.getString("student", null), Student.class);
        } catch (Exception e) {
            return null;
        }
    }

    public void setStudent(Student student) {
        sharedPreferences.edit().putString("student", gson.toJson(student)).apply();
    }
}

@@ -1280,81 +1280,87 @@
 es;%0A
+%0A
 import 
-android.preference.PreferenceManager;%0A%0Aimport com.google.gson.Gson
+com.google.gson.Gson;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Map
 ;%0A%0Ai
@@ -1645,61 +1645,69 @@
 s = 
-PreferenceManager.getDefaultSharedPreferences(context
+context.getSharedPreferences(%22students%22, Context.MODE_PRIVATE
 );%0A 
@@ -1774,16 +1774,31 @@
 Student(
+String username
 ) %7B%0A    
@@ -1868,25 +1868,24 @@
 tString(
-%22student%22
+username
 , null),
@@ -1990,24 +1990,416 @@
 lic 
-void setStudent(
+ArrayList%3CStudent%3E getStudents() %7B%0A        ArrayList%3CStudent%3E students = new ArrayList%3C%3E();%0A        for (Map.Entry%3CString, ?%3E entry : sharedPreferences.getAll().entrySet()) %7B%0A            Student student = getStudent(entry.getKey());%0A            if (student != null) %7B%0A                students.add(student);%0A            %7D%0A        %7D%0A%0A        return students;%0A    %7D%0A%0A    public void setStudent(String username, 
 Stud
@@ -2460,17 +2460,16 @@
 ing(
-%22student%22
+username
 , gs

Refactor Cache to handle multiple students