package vn.efode.vts.application;

import android.app.Application;
import android.text.TextUtils;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.Volley;

/**
 * Created by Tuan on 04/04/2017.
 */

public class ApplicationController extends Application {
    public static final String TAG = "VolleyPatterns";//Log or request TAG
    private RequestQueue mRequestQueue;//Global request queue for Volley
    private static ApplicationController sInstance;//A singleton instance of the application class for easy access in other places

    @Override
    public void onCreate() {
        super.onCreate();

        // initialize the singleton
        sInstance = this;
    }

    /**
     * @return co.pixelmatter.meme.ApplicationController singleton instance
     */
    public static synchronized ApplicationController getInstance() {
        return sInstance;
    }

    /**
     * @return The Volley Request queue, the queue will be created if it is null
     */
    public RequestQueue getRequestQueue() {
        // lazy initialize the request queue, the queue instance will be
        // created when it is accessed for the first time
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }

        return mRequestQueue;
    }

    /**
     * Adds the specified request to the global queue, if tag is specified
     * then it is used else Default TAG is used.
     *
     * @param req
     * @param tag
     */
    public <T> void addToRequestQueue(Request<T> req, String tag) {
        // set the default tag if tag is empty
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);

        VolleyLog.d("Adding request to queue: %s", req.getUrl());

        getRequestQueue().add(req);
    }

    /**
     * Adds the specified request to the global queue using the Default TAG.
     *
     * @param req
     */
    public <T> void addToRequestQueue(Request<T> req) {
        // set the default tag if tag is empty
        req.setTag(TAG);

        getRequestQueue().add(req);
    }

    /**
     * Cancels all pending requests by the specified TAG, it is important
     * to specify a TAG so that the pending/ongoing requests can be cancelled.
     *
     * @param tag
     */
    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }
}

@@ -60,16 +60,58 @@
 cation;%0A
+import android.content.SharedPreferences;%0A
 import a
@@ -287,16 +287,156 @@
 .Volley;
+%0Aimport com.google.gson.FieldNamingPolicy;%0Aimport com.google.gson.Gson;%0Aimport com.google.gson.GsonBuilder;%0A%0Aimport vn.efode.vts.model.User;
 %0A%0A/**%0A *
@@ -812,75 +812,387 @@
 ces%0A
-%0A    @Override%0A    public void onCreate() %7B%0A        super.onCreate(
+    public static final String USER_SESSION = %22user_session%22;%0A%0A    /**%0A     * Common shared preference.%0A     */%0A    public static SharedPreferences sharedPreferences;%0A%0A    @Override%0A    public void onCreate() %7B%0A        super.onCreate();%0A        // initialize sharedPreferences instance%0A        sharedPreferences = getSharedPreferences( getPackageName() + %22_storage%22, MODE_PRIVATE
 );%0A%0A
@@ -2983,11 +2983,550 @@
  %7D%0A    %7D
+%0A%0A    /**%0A     * Get current user.%0A     * @return current user.%0A     */%0A    public static User getCurrentUser() %7B%0A        String userJson = ApplicationController.sharedPreferences.getString(USER_SESSION, null);%0A        User user = null;%0A        if (userJson != null) %7B%0A            Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)%0A                    .setDateFormat(%22yyyy-MM-dd HH:mm:ss%22).create();%0A            user = gson.fromJson(userJson, User.class);%0A        %7D%0A        return user;%0A    %7D
 %0A%7D%0A

Add static sharedPreference instance #31 Add getCurrentUser method #13