Search in sources :

Example 1 with TeamsAdapter

use of ca.dal.cs.athletemonitor.athletemonitor.adapters.TeamsAdapter 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 2 with TeamsAdapter

use of ca.dal.cs.athletemonitor.athletemonitor.adapters.TeamsAdapter in project SEProject by NicholasBarreyre.

the class SearchResultsActivity method populateListWithSearchResults.

private void populateListWithSearchResults(final String query) {
    // 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) {
            // TODO: Don't add teams already a member of
            if (dataSnapshot.getValue(Team.class).getName().toUpperCase().contains(query.toUpperCase())) {
                teamAdapter.add(dataSnapshot.getValue(Team.class));
                teamAdapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            // TODO: Don't add teams already a member of
            if (dataSnapshot.getValue(Team.class).getName().toUpperCase().contains(query.toUpperCase())) {
                teamAdapter.add(dataSnapshot.getValue(Team.class));
            }
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
            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 AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            final Team team = teamAdapter.getItem(position);
            AlertDialog.Builder builder = new AlertDialog.Builder(SearchResultsActivity.this);
            builder.setNegativeButton("Close", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int id) {
                    dialog.dismiss();
                }
            });
            builder.setPositiveButton("Apply", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    final AlertDialog.Builder confirmDialog = new AlertDialog.Builder(SearchResultsActivity.this);
                    confirmDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    confirmDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            TeamManager.requestToJoinTeam(team, user);
                            dialog.dismiss();
                            finish();
                        }
                    });
                    confirmDialog.setTitle("Confirm Application").setMessage("Are you sure you want to apply to " + team.getName() + "?").show();
                }
            });
            builder.setTitle("Apply To Team").setMessage("\nDo you want to apply to " + team.getName()).show();
        }
    });
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) DatabaseReference(com.google.firebase.database.DatabaseReference) DialogInterface(android.content.DialogInterface) ArrayList(java.util.ArrayList) DataSnapshot(com.google.firebase.database.DataSnapshot) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView) TeamsAdapter(ca.dal.cs.athletemonitor.athletemonitor.adapters.TeamsAdapter) ListView(android.widget.ListView) DatabaseError(com.google.firebase.database.DatabaseError) AdapterView(android.widget.AdapterView) ChildEventListener(com.google.firebase.database.ChildEventListener)

Aggregations

ListView (android.widget.ListView)2 TeamsAdapter (ca.dal.cs.athletemonitor.athletemonitor.adapters.TeamsAdapter)2 ChildEventListener (com.google.firebase.database.ChildEventListener)2 DataSnapshot (com.google.firebase.database.DataSnapshot)2 DatabaseError (com.google.firebase.database.DatabaseError)2 DatabaseReference (com.google.firebase.database.DatabaseReference)2 FirebaseDatabase (com.google.firebase.database.FirebaseDatabase)2 ArrayList (java.util.ArrayList)2 DialogInterface (android.content.DialogInterface)1 AlertDialog (android.support.v7.app.AlertDialog)1 View (android.view.View)1 AdapterView (android.widget.AdapterView)1