package com.example.todocloud.app;

import android.app.Application;
import android.content.Context;
import android.graphics.Color;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.support.v7.view.ActionMode;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

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

public class AppController extends Application {

  public static final String TAG = AppController.class.getSimpleName();

  private RequestQueue requestQueue;

  private static AppController instance;

  private static boolean actionModeEnabled;
  private static ActionMode actionMode;
  private static Context applicationContext;

  @Override
  public void onCreate() {
    super.onCreate();
    instance = this;
    applicationContext = getApplicationContext();
  }

  public static synchronized AppController getInstance() {
    return instance;
  }

  public static synchronized Context getAppContext() {
    return applicationContext;
  }

  public static boolean isActionModeEnabled() {
    return actionModeEnabled;
  }

  public static void setActionModeEnabled(boolean actionModeEnabled) {
    AppController.actionModeEnabled = actionModeEnabled;
  }

  public static void setActionMode(ActionMode actionMode) {
    AppController.actionMode = actionMode;
  }

  public static boolean isActionMode() {
    return actionMode != null;
  }

  public RequestQueue getRequestQueue() {
    if (requestQueue == null) {
      requestQueue = Volley.newRequestQueue(applicationContext);
    }
    return requestQueue;
  }

  public <T> void addToRequestQueue(Request<T> request, String tag) {
    request.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
    getRequestQueue().add(request);
  }

  public <T> void addToRequestQueue(Request<T> request) {
    request.setTag(TAG);
    getRequestQueue().add(request);
  }

  public void cancelPendingRequest(Object tag) {
    if (requestQueue != null) {
      requestQueue.cancelAll(tag);
    }
  }

  public static void showWhiteTextSnackbar(Snackbar snackbar) {
    View snackbarView = snackbar.getView();
    TextView snackbarText = (TextView)
        snackbarView.findViewById(android.support.design.R.id.snackbar_text);
    snackbarText.setTextColor(Color.WHITE);
    snackbar.show();
  }

  /**
   * Fix unnecessary TextInputEditText animation on set text.
   * @param text The text to set.
   * @param textInputEditText TextInputEditText to set text.
   * @param relatedTextInputLayout The TextInputLayout related to the textInputEditText.
   */
  public static void setText(
      String text, TextInputEditText textInputEditText, TextInputLayout relatedTextInputLayout
  ) {
    // Disable animation before set text. The unnecessary animation won't play in this case.
    relatedTextInputLayout.setHintAnimationEnabled(false);
    textInputEditText.setText(text);

    // Enable animation, because it should work on user interaction.
    relatedTextInputLayout.setHintAnimationEnabled(true);
  }

}

@@ -359,34 +359,8 @@
 ls;%0A
-import android.view.View;%0A
 impo
@@ -839,16 +839,62 @@
 ntext;%0A%0A
+  private static Snackbar lastShownSnackbar;%0A%0A
   @Overr
@@ -2260,16 +2260,20 @@
 ) %7B%0A    
+Text
 View sna
@@ -2273,24 +2273,28 @@
 iew snackbar
+Text
 View = snack
@@ -2310,143 +2310,697 @@
 ew()
-;%0A    TextView snackbarText = (TextView)%0A        snackbarView.findViewById(android.support.design.R.id.snackbar_text);%0A    snackbarText
+%0A        .findViewById(android.support.design.R.id.snackbar_text);%0A    TextView lastShownSnackbarTextView;%0A%0A    CharSequence snackbarText = snackbarTextView.getText();%0A    CharSequence lastShownSnackbarText = %22%22;%0A%0A    if (lastShownSnackbar != null) %7B%0A      lastShownSnackbarTextView = lastShownSnackbar.getView()%0A          .findViewById(android.support.design.R.id.snackbar_text);%0A      lastShownSnackbarText = lastShownSnackbarTextView.getText();%0A    %7D%0A%0A    boolean shouldShowSnackbar =%0A        !(lastShownSnackbar != null%0A            && lastShownSnackbar.isShown()%0A            && snackbarText.equals(lastShownSnackbarText)%0A        );%0A%0A    if (shouldShowSnackbar) %7B%0A      snackbarTextView
 .set
@@ -3023,24 +3023,26 @@
 WHITE);%0A    
+  
 snackbar.sho
@@ -3045,16 +3045,59 @@
 .show();
+%0A%0A      lastShownSnackbar = snackbar;%0A    %7D
 %0A  %7D%0A%0A  

Fix Snackbar being shown with same text