package team.tr.permitlog;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.auth.ErrorCodes;
import com.firebase.ui.auth.IdpResponse;
import com.firebase.ui.auth.ResultCodes;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

import java.util.Arrays;

public class MainActivity extends AppCompatActivity {
    // For logging
    private static final String TAG = "MainActivity";

    private FirebaseUser currentUser;

    // Sign in request code
    private static final int RC_SIGN_IN = 123;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the current user from Firebase.
        currentUser = FirebaseAuth.getInstance().getCurrentUser();
        // Log whether currentUser is null or not:
        Log.d(TAG, "Is the user not signed in? "+Boolean.toString(currentUser == null));
        // If no user is logged in, show the FirebaseUI login screen.
        if (currentUser == null) {
            startActivityForResult(
                    AuthUI.getInstance()
                        .createSignInIntentBuilder()
                        .setProviders(Arrays.asList(
                                new AuthUI.IdpConfig.Builder(AuthUI.GOOGLE_PROVIDER).build()
                        ))
                        .build(),
                    RC_SIGN_IN
            );
        }
    }

    //This is for when we start an activity and then want a result from it back:
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // If this is for the sign in activity:
        if (requestCode == RC_SIGN_IN) {
            IdpResponse response = IdpResponse.fromResultIntent(data);
            if (resultCode == ResultCodes.OK) {
                Log.d(TAG, "Login was successful");
            } else {
                // If there is not a success, try to figure out what went wrong:
                if (response == null) Log.e(TAG, "User pressed back button");
                else if (response.getErrorCode() == ErrorCodes.NO_NETWORK) Log.e(TAG, "Network connection error");
                else if (response.getErrorCode() == ErrorCodes.UNKNOWN_ERROR) Log.e(TAG, "Unknown error");
                else Log.e(TAG, "Unknown response");
            }
        }
    }
}

@@ -558,16 +558,75 @@
 vity%22;%0A%0A
+    // Firebase variables:%0A    private FirebaseAuth mAuth;%0A
     priv
@@ -655,16 +655,16 @@
 ntUser;%0A
-
 %0A    // 
@@ -951,27 +951,21 @@
         
-currentUser
+mAuth
  = Fireb
@@ -985,16 +985,45 @@
 stance()
+;%0A        currentUser = mAuth
 .getCurr
@@ -2162,24 +2162,149 @@
 ccessful%22);%0A
+                // Now that the user is signed in, update currentUser:%0A                currentUser = mAuth.getCurrentUser();%0A
             
@@ -2742,24 +2742,24 @@
 response%22);%0A
-
             
@@ -2756,24 +2756,157 @@
           %7D%0A
+            // Debug currentUser again:%0A            Log.d(TAG, %22Is the user not signed in? %22+Boolean.toString(currentUser == null));%0A
         %7D%0A  

Set currentUser after signin