package com.alwaysallthetime.adnlib.resourceStream;

import android.content.SharedPreferences;

import com.alwaysallthetime.adnlib.data.IPageableAppDotNetObject;
import com.alwaysallthetime.adnlib.gson.AppDotNetGson;
import com.google.gson.Gson;

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

public class ResourceStreamSerializer {
    public static void serialize(SharedPreferences preferences, String key, ResourceStream resourceStream) {
        String resourceStreamJson = AppDotNetGson.getInstance().toJson(resourceStream);
        String objectsJson = AppDotNetGson.getPersistenceInstance().toJson(resourceStream.getObjects());
        SharedPreferences.Editor edit = preferences.edit();
        edit.putString(key, resourceStreamJson);
        edit.putString(key + "_objects", objectsJson);
        edit.commit();
    }

    public static ResourceStream deserializeStream(SharedPreferences preferences, String key, Class resourceStreamType, Class objectType) {
        String json = preferences.getString(key, null);
        if(json != null) {
            ResourceStream stream = (ResourceStream) AppDotNetGson.getInstance().fromJson(json, resourceStreamType);

            Gson gson = AppDotNetGson.getPersistenceInstance();
            String objectsJson = preferences.getString(key + "_objects", null);
            ArrayList<IPageableAppDotNetObject> objects = (ArrayList<IPageableAppDotNetObject>) gson.fromJson(objectsJson, objectType);
            if(objects != null) {
                stream.setObjects(objects);
            }
            return stream;
        }
        return null;
    }
}

@@ -271,31 +271,8 @@
 ist;
-%0Aimport java.util.List;
 %0A%0Apu
@@ -805,16 +805,246 @@
 %0A    %7D%0A%0A
+    public static void remove(SharedPreferences preferences, String key) %7B%0A        SharedPreferences.Editor edit = preferences.edit();%0A        edit.remove(key);%0A        edit.remove(key + %22_objects%22);%0A        edit.commit();%0A    %7D%0A%0A
     publ

add remove method for clearing a serialized resource stream