Created
November 1, 2016 11:37
-
-
Save yooouuri/42633348b0b6e2c16ef23a593e93769b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.nielyouri.pluff.ui.activities; | |
import android.app.ProgressDialog; | |
import android.content.Intent; | |
import android.support.annotation.NonNull; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.text.TextUtils; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import com.google.android.gms.tasks.OnCompleteListener; | |
import com.google.android.gms.tasks.Task; | |
import com.google.firebase.auth.AuthResult; | |
import com.google.firebase.auth.FirebaseAuth; | |
import com.google.firebase.database.DataSnapshot; | |
import com.google.firebase.database.DatabaseError; | |
import com.google.firebase.database.DatabaseReference; | |
import com.google.firebase.database.FirebaseDatabase; | |
import com.google.firebase.database.ValueEventListener; | |
import com.nielyouri.pluff.R; | |
import com.nielyouri.pluff.model.Course; | |
import com.nielyouri.pluff.model.Day; | |
import java.io.Serializable; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class SignInActivity | |
extends AppCompatActivity | |
implements View.OnClickListener { | |
private static final String TAG = "SignInActivity"; | |
private FirebaseDatabase mDatabase = FirebaseDatabase.getInstance(); | |
private List<Day> mDays = new ArrayList<Day>(); | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_sign_in); | |
Button button = (Button) findViewById(R.id.signin_button); | |
button.setOnClickListener(this); | |
DatabaseReference reference = mDatabase.getReference(); | |
reference.addValueEventListener(new ValueEventListener() { | |
@Override | |
public void onDataChange(DataSnapshot dataSnapshot) { | |
mDays.clear(); | |
for (DataSnapshot day : dataSnapshot.getChildren()) { | |
Day day1 = day.getValue(Day.class); | |
for (DataSnapshot course : day.child("courses").getChildren()) { | |
day1.addCourse(course.getValue(Course.class)); | |
} | |
mDays.add(day1); | |
} | |
Log.d(TAG, String.format("Amount of days: %d", mDays.size())); | |
} | |
@Override | |
public void onCancelled(DatabaseError databaseError) { | |
Log.w(TAG, "Stop fetching data"); | |
} | |
}); | |
} | |
@Override | |
public void onClick(View v) { | |
int id = v.getId(); | |
if (id == R.id.signin_button) { | |
signIn(); | |
} | |
} | |
private void signIn() { | |
final ProgressDialog progressDialog = new ProgressDialog(SignInActivity.this, | |
R.style.Dialog); | |
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(); | |
TextView email = (TextView) findViewById(R.id.signin_email); | |
TextView password = (TextView) findViewById(R.id.signin_password); | |
// Check if the two fields are not empty | |
if (TextUtils.isEmpty(email.getText()) | |
|| TextUtils.isEmpty(password.getText())) | |
{ | |
Toast.makeText(SignInActivity.this, getString(R.string.signin_empty_fields), Toast.LENGTH_SHORT).show(); | |
return; | |
} | |
progressDialog.setIndeterminate(true); | |
progressDialog.setMessage(getString(R.string.signin_dialog)); | |
firebaseAuth.signInWithEmailAndPassword(email.getText().toString(), password.getText().toString()) | |
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { | |
@Override | |
public void onComplete(@NonNull Task<AuthResult> task) { | |
if (task.isSuccessful()) { | |
progressDialog.show(); | |
Intent intent = new Intent(SignInActivity.this, MainActivity.class); | |
intent.putExtra("days", (Serializable) mDays); | |
// Don't start the activity until we have days | |
if (mDays.size() > 0) { | |
startActivity(intent); | |
} | |
new android.os.Handler().postDelayed( | |
new Runnable() { | |
@Override | |
public void run() { | |
finish(); | |
progressDialog.dismiss(); | |
} | |
}, 3000); | |
} else { | |
Toast.makeText(SignInActivity.this, getString(R.string.signin_error), Toast.LENGTH_SHORT).show(); | |
} | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment