Search in sources :

Example 16 with ChildEventListener

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);
    });
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) Intent(android.content.Intent) DataSnapshot(com.google.firebase.database.DataSnapshot) Map(java.util.Map) ChildEventListener(com.google.firebase.database.ChildEventListener)

Example 17 with ChildEventListener

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());
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) DatabaseReference(com.google.firebase.database.DatabaseReference) Intent(android.content.Intent) DataSnapshot(com.google.firebase.database.DataSnapshot) View(android.view.View) DatabaseError(com.google.firebase.database.DatabaseError) Button(android.widget.Button) Editable(android.text.Editable) ChildEventListener(com.google.firebase.database.ChildEventListener)

Example 18 with ChildEventListener

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());
}
Also used : TeamsAdapter(ca.dal.cs.athletemonitor.athletemonitor.adapters.TeamsAdapter) FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) ListView(android.widget.ListView) DatabaseError(com.google.firebase.database.DatabaseError) DatabaseReference(com.google.firebase.database.DatabaseReference) ArrayList(java.util.ArrayList) DataSnapshot(com.google.firebase.database.DataSnapshot) ChildEventListener(com.google.firebase.database.ChildEventListener)

Example 19 with ChildEventListener

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();
    });
}
Also used : Message(garbagecollectors.com.unipool.models.Message) DatabaseReference(com.google.firebase.database.DatabaseReference) HashMap(java.util.HashMap) ProgressDialog(android.app.ProgressDialog) DataSnapshot(com.google.firebase.database.DataSnapshot) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) ScrollView(android.widget.ScrollView) DatabaseError(com.google.firebase.database.DatabaseError) ChildEventListener(com.google.firebase.database.ChildEventListener)

Example 20 with ChildEventListener

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);
    });
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) Intent(android.content.Intent) DataSnapshot(com.google.firebase.database.DataSnapshot) Map(java.util.Map) ChildEventListener(com.google.firebase.database.ChildEventListener)

Aggregations

ChildEventListener (com.google.firebase.database.ChildEventListener)38 DataSnapshot (com.google.firebase.database.DataSnapshot)38 DatabaseError (com.google.firebase.database.DatabaseError)38 DatabaseReference (com.google.firebase.database.DatabaseReference)11 Intent (android.content.Intent)7 View (android.view.View)5 Product (com.example.asus.onlinecanteen.model.Product)5 Map (java.util.Map)5 ListView (android.widget.ListView)4 FirebaseDatabase (com.google.firebase.database.FirebaseDatabase)4 BrixxEventModel (com.manan.dev.ec2018app.Models.BrixxEventModel)4 TextView (android.widget.TextView)3 Transaction (com.example.asus.onlinecanteen.model.Transaction)3 LatLng (com.google.android.gms.maps.model.LatLng)3 Query (com.google.firebase.database.Query)3 HashMap (java.util.HashMap)3 AlertDialog (android.support.v7.app.AlertDialog)2 Button (android.widget.Button)2 ImageView (android.widget.ImageView)2 RadioButton (android.widget.RadioButton)2