package com.miguelgaeta.bootstrap.mg_preference;

import android.content.Context;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import rx.Scheduler;
import rx.schedulers.Schedulers;
import rx.subjects.BehaviorSubject;
import rx.subjects.SerializedSubject;

/**
 * Created by mrkcsc on 12/5/14.
 */
@SuppressWarnings("unchecked, unused") @RequiredArgsConstructor
public class MGPreference<T> {

    @Getter(value = AccessLevel.PACKAGE, lazy = true)
    private static final MGPreferenceDataStore dataStore = new MGPreferenceDataStore();

    @Getter(value = AccessLevel.PACKAGE, lazy = true)
    private static final Scheduler scheduler = Schedulers.computation();

    @Getter(value = AccessLevel.PACKAGE, lazy = true)
    private static final SerializedSubject<Boolean, Boolean> initialized = new SerializedSubject<>(BehaviorSubject.create());

    @Getter(value = AccessLevel.PACKAGE)
    private final MGPreferenceData<T> metaData;

    public static void init(Context context) {

        getInitialized().onNext(true);
    }

    public static <T> MGPreference<T> create(@NonNull String key, T defaultValue, int serializationDelay) {

        return new MGPreference<>(MGPreferenceData.create(key, defaultValue, serializationDelay));
    }

    public static <T> MGPreference<T> create(@NonNull String key, T defaultValue) {

        return create(key, defaultValue, 100);
    }

    public static <T> MGPreference<T> create(@NonNull String key) {

        return create(key, null);
    }

    /**
     * Resets the entire cache for versioned data.
     */
    public static void reset() {

        getDataStore().reset();
    }

    /**
     * Public preference setter, abstracts away the
     * native commit type and allows for persisting
     * more complex types such as dictionaries.
     */
    public void set(T value) {

        metaData.set(value);
    }

    /**
     * Public preference getter.  Supports expanded commit types
     * backed by the native commit types supported by android.
     */
    public T get() {

        return metaData.get();
    }

    /**
     * Clear out the preference value from local
     * and native stores.
     */
    public void clear() {

        metaData.clear();
    }
}

@@ -76,16 +76,104 @@
 ntext;%0A%0A
+import java.lang.ref.WeakReference;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 import l
@@ -191,16 +191,16 @@
 sLevel;%0A
-
 import l
@@ -989,24 +989,171 @@
 create());%0A%0A
+    @Getter(value = AccessLevel.PACKAGE, lazy = true)%0A    private static final List%3CWeakReference%3CMGPreference%3E%3E preferences = new ArrayList%3C%3E();%0A%0A
     @Getter(
@@ -1441,22 +1441,50 @@
         
-return
+final MGPreference%3CT%3E preference =
  new MGP
@@ -1559,16 +1559,108 @@
 Delay));
+%0A%0A        getPreferences().add(new WeakReference%3C%3E(preference));%0A%0A        return preference;
 %0A    %7D%0A%0A
@@ -2001,32 +2001,331 @@
 void reset() %7B%0A%0A
+        // Reset any preferences in memory.%0A        for (WeakReference%3CMGPreference%3E weakPreferenceReference : getPreferences()) %7B%0A%0A            if (weakPreferenceReference.get() != null) %7B%0A                weakPreferenceReference.get().clear();%0A            %7D%0A        %7D%0A%0A        // Hard delete files.%0A
         getDataS

Use a weak reference static list to allow reset to also clear our all in-memory preference values as well.