use of com.google.firebase.database.Query in project OnlineCanteen by josephgunawan97.
the class EditProductActivity method submitData.
// To submit data
private void submitData() {
Log.i(TAG, "TEXT2 MASUK");
if (validateRegisterInfo()) {
Log.i(TAG, "TEXT2 MASUK LAGI");
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
final DatabaseReference reference = firebaseDatabase.getReference();
Query query = reference.child("products").orderByChild("tokoId").equalTo(merchant.getUid());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
DataSnapshot nodeDataSnapshot = dataSnapshot.getChildren().iterator().next();
Product product = nodeDataSnapshot.getValue(Product.class);
Log.i(TAG, "TEXT2 test " + nodeDataSnapshot.getValue().toString());
String key = nodeDataSnapshot.getKey();
String path = "/" + dataSnapshot.getKey() + "/" + key;
HashMap<String, Object> result = new HashMap<>();
// result.put("imageUrl", );
result.put("name", productName.getText().toString());
// HashMap<Integer, Object> result2 = new HashMap<>();
result.put("price", Integer.parseInt(productPrice.getText().toString()));
result.put("stock", Integer.parseInt(productQty.getText().toString()));
result.put("imageUrl", profPicUrl);
StorageReference photoRef = FirebaseStorage.getInstance().getReferenceFromUrl(product.getImageUrl());
photoRef.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
// File deleted successfully
Log.d(TAG, "onSuccess: deleted file");
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Uh-oh, an error occurred!
Log.d(TAG, "onFailure: did not delete file");
}
});
// if(!product.getImageUrl().equals())
reference.child(path).updateChildren(result);
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Logger.error(TAG, ">>> Error:" + "find onCancelled:" + databaseError);
}
});
backToScreen();
}
}
use of com.google.firebase.database.Query in project OnlineCanteen by josephgunawan97.
the class EditProductActivity method submitDatawithoutImage.
private void submitDatawithoutImage() {
Log.i(TAG, "TEXT2 MASUK");
if (validateRegisterInfo()) {
Log.i(TAG, "TEXT2 MASUK LAGI");
FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
final DatabaseReference reference = firebaseDatabase.getReference();
Query query = reference.child("products").orderByChild("tokoId").equalTo(merchant.getUid());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
DataSnapshot nodeDataSnapshot = dataSnapshot.getChildren().iterator().next();
Product product = nodeDataSnapshot.getValue(Product.class);
Log.i(TAG, "TEXT2 test " + nodeDataSnapshot.getValue().toString());
String key = nodeDataSnapshot.getKey();
String path = "/" + dataSnapshot.getKey() + "/" + key;
HashMap<String, Object> result = new HashMap<>();
// result.put("imageUrl", );
result.put("name", productName.getText().toString());
// HashMap<Integer, Object> result2 = new HashMap<>();
result.put("price", Integer.parseInt(productPrice.getText().toString()));
result.put("stock", Integer.parseInt(productQty.getText().toString()));
// if(!product.getImageUrl().equals())
reference.child(path).updateChildren(result);
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Logger.error(TAG, ">>> Error:" + "find onCancelled:" + databaseError);
}
});
backToScreen();
}
}
use of com.google.firebase.database.Query in project OnlineCanteen by josephgunawan97.
the class MerchantOrderDetailActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_merchant_order_detail);
firebaseDatabase = FirebaseDatabase.getInstance();
reference = firebaseDatabase.getReference("transactions");
intent = getIntent();
pos = intent.getIntExtra("Position", 0);
transactionHistory = (ArrayList<Transaction>) intent.getSerializableExtra("Transaction");
transaction = transactionHistory.get(pos);
mAuth = FirebaseAuth.getInstance();
// Initialize views
transactiondate = findViewById(R.id.transaction_date);
username = findViewById(R.id.username);
location = findViewById(R.id.user_location);
grandTotal = findViewById(R.id.transaction_detail_amount);
orderStatus = findViewById(R.id.order_status);
acceptButton = findViewById(R.id.acceptOrder);
declineButton = findViewById(R.id.declineOrder);
scanQR = findViewById(R.id.scan_qr);
if (transaction.getDeliveryStatus() == 0) {
acceptButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
WalletUtil walletUtil = new WalletUtil();
walletUtil.debitAmount(mAuth.getCurrentUser().getUid(), transaction.getTotalPrice());
Toast.makeText(getApplicationContext(), "Order accepted", Toast.LENGTH_LONG).show();
Query query = reference.orderByChild("purchaseDate").equalTo(transaction.getPurchaseDate());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot child : snapshot.getChildren()) {
child.getRef().child("deliveryStatus").setValue(1);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Intent intent = new Intent(MerchantOrderDetailActivity.this, MainActivityMerchant.class);
startActivity(intent);
finish();
}
});
declineButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Order declined", Toast.LENGTH_LONG).show();
Query query = reference.orderByChild("purchaseDate").equalTo(transaction.getPurchaseDate());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot child : snapshot.getChildren()) {
child.getRef().child("deliveryStatus").setValue(4);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Intent intent = new Intent(MerchantOrderDetailActivity.this, MainActivityMerchant.class);
startActivity(intent);
finish();
}
});
} else {
acceptButton.setClickable(false);
acceptButton.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
declineButton.setClickable(false);
declineButton.getBackground().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
}
scanQR.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), QrActivity.class);
i.putExtra("Location", "order");
i.putExtra("Transaction", transactionHistory);
i.putExtra("Position", pos);
startActivityForResult(i, SECOND_ACTIVITY_REQUEST_CODE);
}
});
// Set views
transactiondate.setText(Transaction.getPurchasedDateString(transaction.getPurchaseDate()));
username.setText(transaction.getName());
location.setText(transaction.getLocation());
grandTotal.setText("Rp " + String.valueOf(transaction.getTotalPrice()));
// TO BE CHANGED LATER
orderStatus.setText(statusString(transaction.getDeliveryStatus()));
// Adapter for order items list
detailAdapter = new OrderDetailAdapter(transaction.getItems());
itemsRecyclerView = findViewById(R.id.transaction_detail_items);
layoutManager = new LinearLayoutManager(getApplicationContext());
itemsRecyclerView.setLayoutManager(layoutManager);
itemsRecyclerView.setAdapter(detailAdapter);
}
use of com.google.firebase.database.Query in project geofire-java by firebase.
the class GeoQuery method setupQueries.
private void setupQueries() {
Set<GeoHashQuery> oldQueries = (this.queries == null) ? new HashSet<GeoHashQuery>() : this.queries;
Set<GeoHashQuery> newQueries = GeoHashQuery.queriesAtLocation(center, radius);
this.queries = newQueries;
for (GeoHashQuery query : oldQueries) {
if (!newQueries.contains(query)) {
firebaseQueries.get(query).removeEventListener(this.childEventLister);
firebaseQueries.remove(query);
outstandingQueries.remove(query);
}
}
for (final GeoHashQuery query : newQueries) {
if (!oldQueries.contains(query)) {
outstandingQueries.add(query);
DatabaseReference databaseReference = this.geoFire.getDatabaseReference();
Query firebaseQuery = databaseReference.orderByChild("g").startAt(query.getStartValue()).endAt(query.getEndValue());
firebaseQuery.addChildEventListener(this.childEventLister);
addValueToReadyListener(firebaseQuery, query);
firebaseQueries.put(query, firebaseQuery);
}
}
for (Map.Entry<String, LocationInfo> info : this.locationInfos.entrySet()) {
LocationInfo oldLocationInfo = info.getValue();
if (oldLocationInfo != null) {
updateLocationInfo(oldLocationInfo.dataSnapshot, oldLocationInfo.location);
}
}
// remove locations that are not part of the geo query anymore
Iterator<Map.Entry<String, LocationInfo>> it = this.locationInfos.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, LocationInfo> entry = it.next();
if (!this.geoHashQueriesContainGeoHash(entry.getValue().geoHash)) {
it.remove();
}
}
checkAndFireReady();
}
use of com.google.firebase.database.Query in project MadMax by deviz92.
the class User method addFriend.
public void addFriend(final String friendID) {
final DatabaseReference databaseReference = FirebaseUtils.getDatabaseReference();
final DatabaseReference usersRef = databaseReference.child("users");
final DatabaseReference groupsRef = databaseReference.child("groups");
final String currentUID = this.getID();
// getting friend data from db
DatabaseReference friendRef = usersRef.child(friendID);
User friend = new User(friendID, friendRef.child("username").toString(), friendRef.child("name").toString(), friendRef.child("surname").toString(), friendRef.child("email").toString(), friendRef.child("profileImage").toString());
// Add friendID to friend list of currentUID
// add friend to current user local HashMap
this.userFriends.put(friendID, friend);
usersRef.child(currentUID).child("friends").push();
usersRef.child(currentUID).child("friends").child(friendID).child("deleted").setValue(false);
// Add currentUID to friend list of friendID
usersRef.child(friendID).child("friends").push();
usersRef.child(friendID).child("friends").child(currentUID).child("deleted").setValue(false);
// Read groups currentUID belongs to
Query query = databaseReference.child("users").child(currentUID).child("groups");
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final ArrayList<String> u1Groups = new ArrayList<String>();
for (DataSnapshot groupSnapshot : dataSnapshot.getChildren()) {
Log.d(TAG, groupSnapshot.getKey() + " : " + groupSnapshot.getValue());
if (groupSnapshot.getValue(Boolean.class))
u1Groups.add(groupSnapshot.getKey());
}
Query query = databaseReference.child("users").child(friendID).child("groups");
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ArrayList<String> sharedGroups = new ArrayList<String>();
for (DataSnapshot groupSnapshot : dataSnapshot.getChildren()) {
if (u1Groups.contains(groupSnapshot.getKey()) && groupSnapshot.getValue(Boolean.class))
sharedGroups.add(groupSnapshot.getKey());
}
final HashMap<String, Group> groups = new HashMap<>();
// ora in sharedGroups ci sono solo i gruppi di cui fanno parte entrambi gli utenti
for (String groupID : sharedGroups) {
// getting group data from db
DatabaseReference groupRef = groupsRef.child(groupID);
Group group = new Group(groupID, groupRef.child("name").toString(), groupRef.child("image").toString(), groupRef.child("description").toString(), Integer.parseInt(groupRef.child("numberMembers").toString()));
groups.put(friendID, group);
usersRef.child(currentUID).child("friends").child(friendID).child(groupID).setValue(true);
usersRef.child(friendID).child("friends").child(currentUID).child(groupID).setValue(true);
}
// add shared groups to local sharedGroupPerFriend HashMap
sharedGroupPerFriend.put(friendID, groups);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w("addFriendFirebase", databaseError.getMessage());
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w("addFriendFirebase", databaseError.getMessage());
}
});
}
Aggregations