Search in sources :

Example 11 with CollectionReference

use of com.google.firebase.firestore.CollectionReference in project snippets-android by firebase.

the class DocSnippets method exampleDataCollectionGroup.

public void exampleDataCollectionGroup() {
    // [START fs_collection_group_query_data_setup]
    CollectionReference citiesRef = db.collection("cities");
    Map<String, Object> ggbData = new HashMap<>();
    ggbData.put("name", "Golden Gate Bridge");
    ggbData.put("type", "bridge");
    citiesRef.document("SF").collection("landmarks").add(ggbData);
    Map<String, Object> lohData = new HashMap<>();
    lohData.put("name", "Legion of Honor");
    lohData.put("type", "museum");
    citiesRef.document("SF").collection("landmarks").add(lohData);
    Map<String, Object> gpData = new HashMap<>();
    gpData.put("name", "Griffith Park");
    gpData.put("type", "park");
    citiesRef.document("LA").collection("landmarks").add(gpData);
    Map<String, Object> tgData = new HashMap<>();
    tgData.put("name", "The Getty");
    tgData.put("type", "museum");
    citiesRef.document("LA").collection("landmarks").add(tgData);
    Map<String, Object> lmData = new HashMap<>();
    lmData.put("name", "Lincoln Memorial");
    lmData.put("type", "memorial");
    citiesRef.document("DC").collection("landmarks").add(lmData);
    Map<String, Object> nasaData = new HashMap<>();
    nasaData.put("name", "National Air and Space Museum");
    nasaData.put("type", "museum");
    citiesRef.document("DC").collection("landmarks").add(nasaData);
    Map<String, Object> upData = new HashMap<>();
    upData.put("name", "Ueno Park");
    upData.put("type", "park");
    citiesRef.document("TOK").collection("landmarks").add(upData);
    Map<String, Object> nmData = new HashMap<>();
    nmData.put("name", "National Museum of Nature and Science");
    nmData.put("type", "museum");
    citiesRef.document("TOK").collection("landmarks").add(nmData);
    Map<String, Object> jpData = new HashMap<>();
    jpData.put("name", "Jingshan Park");
    jpData.put("type", "park");
    citiesRef.document("BJ").collection("landmarks").add(jpData);
    Map<String, Object> baoData = new HashMap<>();
    baoData.put("name", "Beijing Ancient Observatory");
    baoData.put("type", "museum");
    citiesRef.document("BJ").collection("landmarks").add(baoData);
// [END fs_collection_group_query_data_setup]
}
Also used : HashMap(java.util.HashMap) CollectionReference(com.google.firebase.firestore.CollectionReference)

Example 12 with CollectionReference

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

the class TakePhoto method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_take_photo);
    // This button is just here for testing, it should later be moved to User Menu
    Button scanButton = (Button) findViewById(R.id.scan);
    // moved it here
    scanButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            startActivity(new Intent(getApplicationContext(), ScannerView.class));
        }
    });
    // Once clicked, the button will update the database
    addPlayerButton = findViewById(R.id.addPlayer);
    addUserNameText = findViewById(R.id.addUserName);
    addScore = findViewById(R.id.score);
    playerList = findViewById(R.id.player_list);
    playerDataList = new ArrayList<>();
    playerAdapter = new CustomList(this, playerDataList);
    playerList.setAdapter(playerAdapter);
    // Access a Cloud FireStore instance from Activity
    db = FirebaseFirestore.getInstance();
    final CollectionReference collectionReference = db.collection("Players");
    addPlayerButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            final String userName = addUserNameText.getText().toString();
            final String score = addScore.getText().toString();
            HashMap<String, String> data = new HashMap<>();
            HashMap<String, Object> jsonData = new HashMap<>();
            data.put("score", score);
            // collectionReference.document(userName).update("scannedcodes", FieldValue.arrayUnion(score));
            collectionReference.document(userName).set(data).addOnSuccessListener(new OnSuccessListener<Void>() {

                @Override
                public void onSuccess(Void unused) {
                    Log.d(TAG, "Data has been added successfully");
                }
            }).addOnFailureListener(new OnFailureListener() {

                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.d(TAG, "Data could not be added!" + e.toString());
                }
            });
            // add scannedcodes: ["array", "nano"]
            collectionReference.document(userName).update("scannedcodes", FieldValue.arrayUnion("array"));
            collectionReference.document(userName).update("scannedcodes", FieldValue.arrayUnion("nano"));
            addUserNameText.setText("");
            addScore.setText("");
        }
    });
// collectionReference.addSnapshotListener(new EventListener<QuerySnapshot>() {
// @Override
// public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException error) {
// //Clear the old list
// playerDataList.clear();
// for (QueryDocumentSnapshot doc: queryDocumentSnapshots){
// Log.d(TAG, String.valueOf(doc.getData().get("score")));
// String user = doc.getId();
// String score = (String) doc.getData().get("score");
// playerDataList.add(new Player(user,score));
// // ACCESS the "scannedcoes" array element
// List<String> scannedCOdes = (List<String>) (doc.getData().get("scannedcodes"));
// for (int i = 0; i < scannedCOdes.size(); i++) {
// Log.d(TAG, scannedCOdes.get(i));
// }
// }
// playerAdapter.notifyDataSetChanged();
// }
// });
}
Also used : HashMap(java.util.HashMap) Intent(android.content.Intent) TextView(android.widget.TextView) View(android.view.View) ListView(android.widget.ListView) CollectionReference(com.google.firebase.firestore.CollectionReference) FirebaseFirestoreException(com.google.firebase.firestore.FirebaseFirestoreException) Button(android.widget.Button) NonNull(androidx.annotation.NonNull) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

Example 13 with CollectionReference

use of com.google.firebase.firestore.CollectionReference in project firebase-android-sdk by firebase.

the class ConformanceRuntime method setup.

/**
 * Initializes the database with the initial data.
 */
public void setup() throws InterruptedException, TimeoutException {
    WriteBatch batch = firestore.batch();
    for (TestCollection collection : initialData) {
        CollectionReference ref = firestore.document(testDocument).collection(collection.path());
        for (TestDocument document : collection.documents()) {
            DocumentReference docRef = ref.document(document.id());
            batch.set(docRef, document.fields());
            logger.fine("Initializing document: " + docRef.getPath());
        }
    }
    waitForBatch(batch);
}
Also used : WriteBatch(com.google.firebase.firestore.WriteBatch) CollectionReference(com.google.firebase.firestore.CollectionReference) DocumentReference(com.google.firebase.firestore.DocumentReference)

Example 14 with CollectionReference

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

the class GameMap method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game_map);
    Toast.makeText(GameMap.this, "lat " + singletonPlayer.lat + " lon " + singletonPlayer.lon, Toast.LENGTH_SHORT).show();
    items = new ArrayList<>();
    // fetch all players --------------------
    // TODO: snapshot listener might cause issues when players get updated real time. whil
    // TODO: tryna cmput coordinates.
    allPlayers = new ArrayList<>();
    db = FirebaseFirestore.getInstance();
    final CollectionReference collectionReference = db.collection("Players");
    Configuration.getInstance().load(getApplicationContext(), PreferenceManager.getDefaultSharedPreferences(getApplicationContext()));
    map = findViewById(R.id.map);
    // render
    map.setTileSource(TileSourceFactory.MAPNIK);
    // zoomable
    map.setBuiltInZoomControls(true);
    // change start point
    GeoPoint startPoint = new GeoPoint(53.52289, -113.52503);
    // if user saved his current location
    if (singletonPlayer.lat != -1 && singletonPlayer.lon != -1) {
        startPoint = new GeoPoint(singletonPlayer.lat, singletonPlayer.lon);
    }
    IMapController mapController = map.getController();
    mapController.setCenter(startPoint);
    mapController.setZoom(18.0);
    String go = "53.607426, -113.529866";
    // items = new ArrayList<>();
    OverlayItem home = new OverlayItem("Em's test office", "my test office", new GeoPoint(53.600044, -113.530837));
    Drawable m = home.getMarker(0);
    items.add(home);
    populateMap();
    ItemizedOverlayWithFocus<OverlayItem> mOverlay = new ItemizedOverlayWithFocus<OverlayItem>(getApplicationContext(), items, new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {

        @Override
        public boolean onItemSingleTapUp(int index, OverlayItem item) {
            return true;
        }

        @Override
        public boolean onItemLongPress(int index, OverlayItem item) {
            return false;
        }
    });
    mOverlay.setFocusItemsOnTap(true);
    map.getOverlays().add(mOverlay);
}
Also used : OverlayItem(org.osmdroid.views.overlay.OverlayItem) Drawable(android.graphics.drawable.Drawable) CollectionReference(com.google.firebase.firestore.CollectionReference) GeoPoint(org.osmdroid.util.GeoPoint) GeoPoint(org.osmdroid.util.GeoPoint) ItemizedOverlayWithFocus(org.osmdroid.views.overlay.ItemizedOverlayWithFocus) IMapController(org.osmdroid.api.IMapController) ItemizedIconOverlay(org.osmdroid.views.overlay.ItemizedIconOverlay)

Example 15 with CollectionReference

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

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Check if the preference has already been created
    // The concept of how to stay logged in was learned and obtained from:
    // Video By: Stevdza-San
    // Date: June 10, 2019
    // URL: https://youtu.be/8pTcATGRDGM
    SharedPreferences preferences = getSharedPreferences("rememberMeBox", MODE_PRIVATE);
    String rememberMeCheckbox = preferences.getString("remember", "");
    if (!rememberMeCheckbox.equals("")) {
        singletonPlayer.player.setUsername(rememberMeCheckbox);
        db = FirebaseFirestore.getInstance();
        final CollectionReference collectionReference = db.collection("Players");
        final CollectionReference collectionReferenceQR = db.collection("QRCodes");
        DocumentReference playerDocRef = db.collection("Players").document(rememberMeCheckbox);
        playerDocRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {

            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if (task.isSuccessful()) {
                    DocumentSnapshot documentSnapshot = task.getResult();
                    if (documentSnapshot.exists()) {
                        singletonPlayer.player = documentSnapshot.toObject(Player.class);
                        Log.d("Success", "12");
                    }
                }
            }
        });
        Intent intent = new Intent(getApplicationContext(), UserMenu.class);
        intent.putExtra("userMenu_act", rememberMeCheckbox);
        startActivity(intent);
    }
}
Also used : QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot) DocumentSnapshot(com.google.firebase.firestore.DocumentSnapshot) SharedPreferences(android.content.SharedPreferences) Intent(android.content.Intent) CollectionReference(com.google.firebase.firestore.CollectionReference) DocumentReference(com.google.firebase.firestore.DocumentReference)

Aggregations

CollectionReference (com.google.firebase.firestore.CollectionReference)43 QuerySnapshot (com.google.firebase.firestore.QuerySnapshot)15 Intent (android.content.Intent)14 QueryDocumentSnapshot (com.google.firebase.firestore.QueryDocumentSnapshot)14 View (android.view.View)13 DocumentSnapshot (com.google.firebase.firestore.DocumentSnapshot)13 NonNull (androidx.annotation.NonNull)10 OnFailureListener (com.google.android.gms.tasks.OnFailureListener)10 DocumentReference (com.google.firebase.firestore.DocumentReference)10 FirebaseFirestoreException (com.google.firebase.firestore.FirebaseFirestoreException)10 ArrayList (java.util.ArrayList)8 Bundle (android.os.Bundle)7 ListView (android.widget.ListView)7 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)7 AdapterView (android.widget.AdapterView)6 TextView (android.widget.TextView)6 OnCompleteListener (com.google.android.gms.tasks.OnCompleteListener)5 Task (com.google.android.gms.tasks.Task)5 List (java.util.List)5 RecyclerView (androidx.recyclerview.widget.RecyclerView)4