package com.jamesdube.laravelnewsapp;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.jamesdube.laravelnewsapp.util.Themes;

import io.realm.Realm;
import io.realm.RealmConfiguration;

/**
 * Created by rick on 1/6/17.
 */

public class App extends Application {
    private static App Instance;
    private static Gson gson;
    public static String Tag = "LaravelNewsApp";
    private static Realm realm;

    @Override
    public void onCreate() {
        super.onCreate();
        Instance = this;
    }

    public static App getInstance() {
        if(Instance == null) {
            Log.d(Tag,"app init");
            Instance = new App();
        }
        return Instance;
    }

    public static Context getAppContext() {
        return getInstance().getApplicationContext();
    }

    //Gson Singleton
    public static Gson Gson(){
        if(gson == null){
            gson = new Gson();
        }
        return gson;
    }

    public static Realm Realm() {
        if(realm == null){
            // Create a RealmConfiguration that saves the Realm file in the app's "files" directory.
            RealmConfiguration realmConfig = new RealmConfiguration.Builder(getInstance())
                    .deleteRealmIfMigrationNeeded()
                    .build();
            Realm.setDefaultConfiguration(realmConfig);

            // Get a Realm instance for this thread
            realm = Realm.getDefaultInstance();
        }

        return realm;
    }
}

@@ -224,16 +224,53 @@
 .Realm;%0A
+import io.realm.RealmChangeListener;%0A
 import i
@@ -320,23 +320,13 @@
  by 
-rick on 1/6/17.
+james
 %0A */
@@ -605,16 +605,33 @@
 = this;%0A
+        Realm();%0A
     %7D%0A%0A 
@@ -1223,16 +1223,57 @@
 ectory.%0A
+            Realm.init(getAppContext());%0A
         
@@ -1340,29 +1340,16 @@
 Builder(
-getInstance()
 )%0A      
@@ -1576,24 +1576,270 @@
 tInstance();
+%0A%0A            realm.addChangeListener(new RealmChangeListener%3CRealm%3E() %7B%0A                @Override%0A                public void onChange(Realm element) %7B%0A                    Log.d(Tag,%22app realmOnChangeListener%22);%0A                %7D%0A            %7D);
 %0A        %7D%0A%0A

update the realm config to match realm upgrade