use of com.google.firebase.database.ChildEventListener in project FindMyHome by DjangoBlockchained.
the class ShelterListActivity method showAllShelters.
private void showAllShelters() {
shelterRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Map string = (Map) dataSnapshot.getValue();
if (string != null) {
Object nameObj = string.get("name");
if (nameObj != null) {
names.add(nameObj.toString());
adapter.notifyDataSetChanged();
}
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
list.setAdapter(adapter);
list.setOnItemClickListener((parent, view, position, id) -> {
String selected = (String) list.getItemAtPosition(position);
Intent intent = new Intent(ShelterListActivity.this, ShelterDetailActivity.class);
intent.putExtra("username", username);
intent.putExtra("Shelter Name", selected);
startActivity(intent);
});
}
use of com.google.firebase.database.ChildEventListener in project FindMyHome by DjangoBlockchained.
the class LoginScreenActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_screen);
userName = findViewById(R.id.username);
password = findViewById(R.id.password);
Button login = findViewById(R.id.login);
login.setOnClickListener((View view) -> {
Editable usernameText = userName.getText();
String username = usernameText.toString();
Editable passwordText = password.getText();
String pass = passwordText.toString();
DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference().child("users");
dbRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.getKey().equals(username)) {
Object password = dataSnapshot.child("password").getValue();
if ((password != null) && password.equals(pass)) {
if ("administer".equals(dataSnapshot.child("type").getValue())) {
Intent intent = new Intent(LoginScreenActivity.this, AdminScreenActivity.class);
intent.putExtra("username", username);
startActivity(intent);
finish();
} else {
Intent intent = new Intent(LoginScreenActivity.this, ApplicationScreenActivity.class);
intent.putExtra("username", username);
startActivity(intent);
finish();
}
} else {
AlertDialog.Builder alert = new AlertDialog.Builder(LoginScreenActivity.this);
alert.setMessage("Your username and password do not match with a real user.");
alert.setTitle("Login Error");
alert.setPositiveButton("OK", null);
alert.create().show();
}
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
});
Button cancel = findViewById(R.id.cancel);
cancel.setOnClickListener((View view) -> finish());
}
use of com.google.firebase.database.ChildEventListener in project SEProject by NicholasBarreyre.
the class TeamActivity method populateTeamList.
/**
* Populates the list of teams associated with the user
*/
private void populateTeamList() {
// Get the reference to the UI contents
final ListView teamListView = findViewById(R.id.teamList);
// retrieve database reference to the teams
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference teamsReference = database.getReference("teams");
teamAdapter = new TeamsAdapter(this, new ArrayList<Team>());
teamsReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.getValue(Team.class).getTeamMembers().contains(user.getUsername())) {
if (teamAdapter.getPosition(dataSnapshot.getValue(Team.class)) == -1)
teamAdapter.add(dataSnapshot.getValue(Team.class));
}
if (dataSnapshot.getValue(Team.class).getOwner().equals(user.getUsername())) {
TeamActivity.this.handleJoinRequests(dataSnapshot.getValue(Team.class));
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.getValue(Team.class).getTeamMembers().contains(user.getUsername())) {
if (teamAdapter.getPosition(dataSnapshot.getValue(Team.class)) == -1) {
teamAdapter.add(dataSnapshot.getValue(Team.class));
} else {
teamAdapter.remove(teamAdapter.getItem(teamAdapter.getPosition(dataSnapshot.getValue(Team.class))));
teamAdapter.add(dataSnapshot.getValue(Team.class));
}
} else if (teamAdapter.getPosition(dataSnapshot.getValue(Team.class)) != -1) {
teamAdapter.remove(dataSnapshot.getValue(Team.class));
}
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
if (dataSnapshot.getValue(Team.class).getTeamMembers().contains(user.getUsername())) {
if (teamAdapter.getPosition(dataSnapshot.getValue(Team.class)) != -1)
teamAdapter.remove(dataSnapshot.getValue(Team.class));
}
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
Log.d("TeamList", dataSnapshot.getValue(Team.class).getName() + "has moved locations...");
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
teamListView.setAdapter(teamAdapter);
teamListView.setOnItemClickListener(new TeamListClickListener());
}
use of com.google.firebase.database.ChildEventListener in project UniPool by divya21raj.
the class MessageListActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message_list);
messagesLayout = findViewById(R.id.layout1);
sendButton = findViewById(R.id.sendButton);
messageArea = findViewById(R.id.message_edit_text);
scrollView = findViewById(R.id.scrollView);
personalMessageMap = new TreeMap<>();
messagesOnScreen = new ArrayList<>();
messageProgressDialog = new ProgressDialog(this);
messageProgressDialog.setCanceledOnTouchOutside(false);
messageProgressDialog.setMessage("Fetching your messages...");
messageProgressDialog.show();
setScrollViewToBottom();
DatabaseReference userMessageDatabaseReference = null;
try {
userMessageDatabaseReference = FirebaseDatabase.getInstance().getReference("messages/" + BaseActivity.getFinalCurrentUser().getUserId());
} catch (Exception e) {
// Could be NPE, fall back to locally stored USER_ID
userMessageDatabaseReference = FirebaseDatabase.getInstance().getReference("messages/" + Globals.USER_ID);
}
/*load from local*/
userMessageDatabaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Message message = dataSnapshot.getValue(Message.class);
if (message != null) {
UtilityMethods.putMessageInMap(BaseActivity.getMessages(), message);
if (!message.getMessageId().equals("def@ult") && (message.getSenderId().equals(chatUser.getUserId()) || message.getReceiverId().equals(chatUser.getUserId()))) {
personalMessageMap.containsKey(message.getCreatedAtTime());
if (!messagesOnScreen.contains(message.getMessageId())) {
showMessage(message);
}
} else if (message.getMessageId().equals("def@ult"))
messageProgressDialog.dismiss();
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Failed to read value
Log.w("Hello", "Failed to read value.", databaseError.toException());
Toast.makeText(getApplicationContext(), "Problems! Couldn't fetch messages...", Toast.LENGTH_LONG).show();
}
});
sendButton.setOnClickListener(view -> {
String typedMessage = messageArea.getText().toString();
if (!typedMessage.isEmpty()) {
Message message = new Message("", pairUp.getPairUpId(), typedMessage, BaseActivity.getFinalCurrentUser().getUserId(), chatUser.getUserId(), UtilityMethods.getCurrentTime());
// online update
UtilityMethods.putMessageOnDB(message, chatUser, BaseActivity.getFinalCurrentUser());
// local update
UtilityMethods.putMessageInMap(BaseActivity.getMessages(), message);
HashMap<String, String> notificationObject = new HashMap<>();
notificationObject.put("from", BaseActivity.getFinalCurrentUser().getUserId());
notificationObject.put("type", "chat");
Globals.notificationDatabaseReference.child(chatUser.getUserId()).push().setValue(notificationObject);
messageArea.setText("");
personalMessageMap.put(message.getCreatedAtTime(), message);
showMessage(message);
}
});
// detecting if keyboard on-screen
final View activityRootView = findViewById(R.id.activity_message_list);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
if (heightDiff > UtilityMethods.dpToPx(MessageListActivity.this, 200))
setScrollViewToBottom();
});
}
use of com.google.firebase.database.ChildEventListener in project FindMyHome by DjangoBlockchained.
the class ShelterListActivity method showSearchedShelter.
private void showSearchedShelter(String name, String gender, String age) {
shelterRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Map<String, Object> string = (Map<String, Object>) dataSnapshot.getValue();
String restriction = string.get("restrictions").toString();
String dbname = string.get("name").toString();
if (gender.equals("Any")) {
if (dbname.toLowerCase().contains(name.toLowerCase()) && (((!(restriction.contains("Men") || (restriction.contains("Women")))) || (restriction.contains("Anyone"))) && ((restriction.contains(age)) || (restriction.contains("Anyone"))))) {
names.add(string.get("name").toString());
adapter.notifyDataSetChanged();
}
} else if (!gender.equals("")) {
if (dbname.toLowerCase().contains(name.toLowerCase()) && (((!restriction.contains(gender)) || (restriction.contains("Anyone"))) && ((restriction.contains(age)) || (restriction.contains("Anyone"))))) {
names.add(string.get("name").toString());
adapter.notifyDataSetChanged();
}
} else {
if (dbname.toLowerCase().contains(name.toLowerCase()) && ((restriction.contains(age)) || (restriction.contains("Anyone")))) {
names.add(string.get("name").toString());
adapter.notifyDataSetChanged();
}
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
list.setAdapter(adapter);
list.setOnItemClickListener((parent, view, position, id) -> {
String selected = (String) list.getItemAtPosition(position);
Intent intent = new Intent(ShelterListActivity.this, ShelterDetailActivity.class);
intent.putExtra("username", username);
intent.putExtra("Shelter Name", selected);
startActivity(intent);
});
}
Aggregations