Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mdp.ac.id.pelancong;
- import android.content.Intent;
- import android.drm.DrmManagerClient;
- import android.support.annotation.NonNull;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.support.v7.view.menu.ListMenuItemView;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.GridView;
- import android.widget.ProgressBar;
- import android.widget.TextView;
- import com.google.firebase.auth.FirebaseAuth;
- import com.google.firebase.auth.FirebaseUser;
- import com.google.firebase.database.ChildEventListener;
- 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 java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- public class MainActivity extends AppCompatActivity {
- FirebaseAuth mAuth;
- DatabaseReference mRoot, mRef;
- private FirebaseAuth.AuthStateListener mAuthListener;
- String TAG = "MainActivity";
- GridView gvLocation;
- TextView tvNoLocation;
- ProgressBar progressBar;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mRoot = FirebaseDatabase.getInstance().getReference();
- mRef = mRoot.child("locations");
- progressBar = (ProgressBar) findViewById(R.id.progressBar);
- gvLocation = (GridView) findViewById(R.id.gvLocation);
- tvNoLocation = (TextView) findViewById(R.id.tvNoLocation);
- mAuth = FirebaseAuth.getInstance();
- mAuthListener = new FirebaseAuth.AuthStateListener() {
- @Override
- public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
- FirebaseUser user = firebaseAuth.getCurrentUser();
- if (user != null) {
- // User is signed in
- Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
- } else {
- // User is signed out
- Log.d(TAG, "onAuthStateChanged:signed_out");
- startActivity(new Intent(MainActivity.this, SignInActivity.class));
- finish();
- }
- // ...
- }
- };
- progressBar.setVisibility(View.VISIBLE);
- fetchLocation();
- }
- private void fetchLocation() {
- mRef.addValueEventListener(new ValueEventListener() {
- List<Location> locationData = new ArrayList<>();
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- Iterator<DataSnapshot> locationSnapshot = dataSnapshot.getChildren().iterator();
- do {
- HashMap<String, String> locationMap = new HashMap<String, String>();
- Location location = new Location();
- String locId = locationSnapshot.next().getKey();
- Log.d(TAG, "locId: " + locId);
- location = dataSnapshot.child(locId).getValue(Location.class);
- Log.d(TAG, "Location name: " + location.getLocName());
- locationData.add(location);
- } while (locationSnapshot.hasNext());
- progressBar.setVisibility(View.GONE);
- setUI(locationData);
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- }
- });
- }
- private void setUI(List<Location> locations) {
- GridAdapter adapter = new GridAdapter(getApplicationContext(), locations);
- gvLocation.setAdapter(adapter);
- }
- @Override
- protected void onStart() {
- super.onStart();
- mAuth.addAuthStateListener(mAuthListener);
- }
- @Override
- protected void onStop() {
- super.onStop();
- if (mAuthListener != null) {
- mAuth.removeAuthStateListener(mAuthListener);
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- int id = item.getItemId();
- if (id == R.id.action_sign_out) {
- mAuth.signOut();
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement