Search in sources :

Example 71 with QueryDocumentSnapshot

use of com.google.firebase.firestore.QueryDocumentSnapshot in project QR-Game by CMPUT301W22T15.

the class ExistingUser method onCreate.

/**
 * This method creates the initial interface and obtains the necessary permissions.
 * @param savedInstanceState
 * Expects a Bundle object.
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_existing_user);
    scanButton = findViewById(R.id.scan_button);
    rememberMe = (CheckBox) findViewById(R.id.login_checkbox_existing);
    db = FirebaseFirestore.getInstance();
    CollectionReference collectionReference = db.collection("Players");
    // Obtain list of players
    collectionReference.addSnapshotListener(new EventListener<QuerySnapshot>() {

        @Override
        public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException error) {
            allPlayers.clear();
            for (QueryDocumentSnapshot doc : queryDocumentSnapshots) {
                Player p = doc.toObject(Player.class);
                allPlayers.add(p);
            }
        }
    });
    // Open ScannerView2 to scan a new QRcode
    scanButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Intent intent = new Intent(ExistingUser.this, ScannerView2.class);
            startActivityForResult(intent, 1);
        }
    });
}
Also used : QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) FirebaseFirestoreException(com.google.firebase.firestore.FirebaseFirestoreException) Intent(android.content.Intent) View(android.view.View) CollectionReference(com.google.firebase.firestore.CollectionReference) QuerySnapshot(com.google.firebase.firestore.QuerySnapshot)

Example 72 with QueryDocumentSnapshot

use of com.google.firebase.firestore.QueryDocumentSnapshot in project QR-Game by CMPUT301W22T15.

the class OtherPlayers method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_other_players);
    // fetch all the document to display them on listview
    db = FirebaseFirestore.getInstance();
    final CollectionReference collectionReference = db.collection("Players");
    // Prepare ListView and Adapter
    playerList = findViewById(R.id.otherPlayerListview);
    allPlayers = new ArrayList<>();
    playerAdapter = new OtherPlayerListAdapter(this, R.layout.other_player_listview_item, allPlayers);
    playerList.setAdapter(playerAdapter);
    collectionReference.addSnapshotListener(new EventListener<QuerySnapshot>() {

        @Override
        public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException error) {
            allPlayers.clear();
            for (QueryDocumentSnapshot doc : queryDocumentSnapshots) {
                Player p = doc.toObject(Player.class);
                if (!p.getPlayerHash().equals(singletonPlayer.player.getPlayerHash()) && !p.getOwner()) {
                    allPlayers.add(p);
                }
            }
            playerAdapter.notifyDataSetChanged();
        }
    });
    // Be able to view a profile if we click a user
    playerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            Player clickedPLayer = allPlayers.get(position);
            String playerUserName = clickedPLayer.getUsername();
            String playerHash = clickedPLayer.getPlayerHash();
            Intent intent = new Intent(OtherPlayers.this, OtherPlayerProfile.class);
            intent.putExtra("playerUserName", playerUserName);
            intent.putExtra("playerHash", playerHash);
            startActivity(intent);
        }
    });
    // Be able to search for a player
    Button searchConfirmButton = findViewById(R.id.searchConfirmButton);
    searchConfirmButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            // TODO: could cause error when playerlist updates in DB while trying to do this.
            EditText searchbox = findViewById(R.id.searchPlayerEditText);
            String searchedPlayerName = searchbox.getText().toString();
            // TRIM WHITESPACES, IMPORTANT
            searchedPlayerName = searchedPlayerName.trim();
            boolean exist = false;
            // check if entered user exist
            for (int i = 0; i < allPlayers.size(); i++) {
                String thisplayerUsername = allPlayers.get(i).getUsername();
                if (thisplayerUsername.trim().equals(searchedPlayerName)) {
                    // case: if found
                    // go to activity
                    exist = true;
                    searchbox.setText("");
                    String playerHash = allPlayers.get(i).getPlayerHash();
                    Intent intent = new Intent(OtherPlayers.this, OtherPlayerProfile.class);
                    intent.putExtra("playerUserName", thisplayerUsername);
                    intent.putExtra("playerHash", playerHash);
                    startActivity(intent);
                // since startActivity is asynchronous call, i needed to use "exist" loic to toast
                }
            }
            // case: invalid userName
            if (!exist) {
                Toast.makeText(OtherPlayers.this, "Username Don't exist", Toast.LENGTH_SHORT).show();
            }
            searchbox.setText("");
        }
    });
    // Be able to scan a button to search for a user
    Button scanCodeButton = findViewById(R.id.scan_player_code);
    scanCodeButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Intent intent = new Intent(OtherPlayers.this, ScannerView2.class);
            intent.putExtra("scanProfileCode", true);
            startActivity(intent);
        }
    });
}
Also used : EditText(android.widget.EditText) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) FirebaseFirestoreException(com.google.firebase.firestore.FirebaseFirestoreException) Intent(android.content.Intent) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView) CollectionReference(com.google.firebase.firestore.CollectionReference) QuerySnapshot(com.google.firebase.firestore.QuerySnapshot) Button(android.widget.Button) AdapterView(android.widget.AdapterView)

Example 73 with QueryDocumentSnapshot

use of com.google.firebase.firestore.QueryDocumentSnapshot in project QR-Game by CMPUT301W22T15.

the class UserMenu method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_menu);
    // Initialize fusedLocationProviderClient--------------------------------------------------
    fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
    // TODO: set up location request
    locationRequest = LocationRequest.create().setPriority(// 
    LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationCallback = new LocationCallback() {

        @Override
        public void onLocationResult(LocationResult locationResult) {
            // here is the location
            Log.i("TAG", "incallback");
            if (locationResult == null) {
                Toast.makeText(UserMenu.this, "saved current location", Toast.LENGTH_SHORT).show();
                // deactivate callback so it doesn't loop.
                fusedLocationProviderClient.removeLocationUpdates(locationCallback);
                Toast.makeText(UserMenu.this, "null location", Toast.LENGTH_SHORT).show();
                return;
            }
            // got location.
            Location lastLocation = locationResult.getLastLocation();
            double lat = lastLocation.getLatitude();
            double lon = lastLocation.getLongitude();
            singletonPlayer.lat = lastLocation.getLatitude();
            singletonPlayer.lon = lastLocation.getLongitude();
            Toast.makeText(UserMenu.this, "lat " + lat + " lon " + lon, Toast.LENGTH_SHORT).show();
            fusedLocationProviderClient.removeLocationUpdates(locationCallback);
        }
    };
    db = FirebaseFirestore.getInstance();
    final CollectionReference collectionReference = db.collection("Players");
    menuList = findViewById(R.id.userMenu_list);
    String userName = singletonPlayer.player.getUsername();
    String[] dataList = new String[] { userName, "Scan New Code", "My Scans", "Ranking", "Codes Near Me", "Other Player", "set map spawn point here" };
    menuAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dataList);
    menuList.setAdapter(menuAdapter);
    menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            if (position == 0) {
                Intent intent = new Intent(getApplicationContext(), PlayerProfile.class);
                startActivity(intent);
            } else if (position == 1) {
                Intent intent = new Intent(getApplicationContext(), ScannerView.class);
                startActivity(intent);
            } else if (position == 2) {
                Intent intent = new Intent(getApplicationContext(), MyScans.class);
                // intent.putExtra("scan_new_code", (String) null);
                startActivity(intent);
            } else if (position == 3) {
                Intent intent = new Intent(getApplicationContext(), PlayerRanking.class);
                startActivity(intent);
            } else if (position == 4) {
                // TODO: fetch player here?
                // EL-start
                Intent intent = new Intent(getApplicationContext(), GameMap.class);
                // intent.putExtra("Codes_Near_Me", (String) null);
                startActivity(intent);
            // EL-end
            } else if (position == 5) {
                Intent intent = new Intent(getApplicationContext(), OtherPlayers.class);
                startActivity(intent);
            } else if (position == 6) {
                // save player location
                Log.i("TAG0", "in 153");
                if (ActivityCompat.checkSelfPermission(UserMenu.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(UserMenu.this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
                    Log.i("TAG1", "in 7");
                    getLocation1();
                } else {
                    ActivityCompat.requestPermissions(UserMenu.this, new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, 44);
                }
            }
        }
    });
    // FOR the sore purpose to have all the players before going to Gamemap activity.
    collectionReference.addSnapshotListener(new EventListener<QuerySnapshot>() {

        @Override
        public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException error) {
            globalAllPlayers.allPlayers.clear();
            for (QueryDocumentSnapshot doc : queryDocumentSnapshots) {
                Player p = doc.toObject(Player.class);
                globalAllPlayers.allPlayers.add(p);
            }
        }
    });
}
Also used : QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) LocationCallback(com.google.android.gms.location.LocationCallback) Intent(android.content.Intent) FirebaseFirestoreException(com.google.firebase.firestore.FirebaseFirestoreException) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView) CollectionReference(com.google.firebase.firestore.CollectionReference) SuppressLint(android.annotation.SuppressLint) QuerySnapshot(com.google.firebase.firestore.QuerySnapshot) LocationResult(com.google.android.gms.location.LocationResult) AdapterView(android.widget.AdapterView) Location(android.location.Location)

Aggregations

QueryDocumentSnapshot (com.google.firebase.firestore.QueryDocumentSnapshot)73 QuerySnapshot (com.google.firebase.firestore.QuerySnapshot)33 ArrayList (java.util.ArrayList)30 View (android.view.View)22 NonNull (androidx.annotation.NonNull)19 Task (com.google.android.gms.tasks.Task)18 OnCompleteListener (com.google.android.gms.tasks.OnCompleteListener)17 Intent (android.content.Intent)16 CollectionReference (com.google.firebase.firestore.CollectionReference)13 AdapterView (android.widget.AdapterView)12 FirebaseFirestore (com.google.firebase.firestore.FirebaseFirestore)12 TextView (android.widget.TextView)10 Bundle (android.os.Bundle)9 ListView (android.widget.ListView)9 DocumentSnapshot (com.google.firebase.firestore.DocumentSnapshot)9 FirebaseFirestoreException (com.google.firebase.firestore.FirebaseFirestoreException)8 UsersDataModel (com.example.first_responder_app.dataModels.UsersDataModel)7 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)6 FirebaseUser (com.google.firebase.auth.FirebaseUser)6 DocumentReference (com.google.firebase.firestore.DocumentReference)6