Search in sources :

Example 1 with UsersAdapter

use of com.tutuanle.chatapp.adapters.UsersAdapter in project Chat-app by TuTuanLe.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ActivityMainBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());
    dialog = new ProgressDialog(this);
    dialog.setMessage("upLoading Image ...");
    dialog.setCancelable(false);
    database = FirebaseDatabase.getInstance();
    users = new ArrayList<User>();
    usersAdapter = new UsersAdapter(this, users);
    userStatuses = new ArrayList<>();
    database.getReference().child("users").child(FirebaseAuth.getInstance().getUid()).addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            user = snapshot.getValue(User.class);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
        }
    });
    statusAdapter = new TopStatusAdapter(this, userStatuses);
    // binding.statusList.setLayoutManager((new LinearLayoutManager(this)));
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    layoutManager.setOrientation(RecyclerView.HORIZONTAL);
    binding.statusList.setLayoutManager(layoutManager);
    // binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
    binding.statusList.setAdapter(statusAdapter);
    binding.recyclerView.setAdapter(usersAdapter);
    database.getReference().child("users").addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            users.clear();
            for (DataSnapshot snapshot1 : snapshot.getChildren()) {
                User user = snapshot1.getValue(User.class);
                Log.d("log", user.toString());
                users.add(user);
            }
            usersAdapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
        }
    });
    database.getReference().child("stories").addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            if (snapshot.exists()) {
                userStatuses.clear();
                for (DataSnapshot storySnapshot : snapshot.getChildren()) {
                    UserStatus status = new UserStatus();
                    status.setName(storySnapshot.child("name").getValue(String.class));
                    status.setProfileImage(storySnapshot.child("profileImage").getValue(String.class));
                    status.setLastUpdated(storySnapshot.child("lastUpdate").getValue(Long.class));
                    ArrayList<Status> statuses = new ArrayList<>();
                    for (DataSnapshot statusSnapshot : storySnapshot.child("statuses").getChildren()) {
                        Status sampleStatus = statusSnapshot.getValue(Status.class);
                        statuses.add(sampleStatus);
                    }
                    status.setStatuses(statuses);
                    userStatuses.add(status);
                }
                statusAdapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
        }
    });
    binding.BottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch(item.getItemId()) {
                case R.id.status:
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(intent, 75);
                    break;
            }
            ;
            return false;
        }
    });
}
Also used : UserStatus(com.tutuanle.chatapp.models.UserStatus) Status(com.tutuanle.chatapp.models.Status) User(com.tutuanle.chatapp.models.User) ArrayList(java.util.ArrayList) MenuItem(android.view.MenuItem) Intent(android.content.Intent) ProgressDialog(android.app.ProgressDialog) DataSnapshot(com.google.firebase.database.DataSnapshot) TopStatusAdapter(com.tutuanle.chatapp.adapters.TopStatusAdapter) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) UserStatus(com.tutuanle.chatapp.models.UserStatus) UsersAdapter(com.tutuanle.chatapp.adapters.UsersAdapter) DatabaseError(com.google.firebase.database.DatabaseError) BottomNavigationView(com.google.android.material.bottomnavigation.BottomNavigationView) ValueEventListener(com.google.firebase.database.ValueEventListener)

Aggregations

ProgressDialog (android.app.ProgressDialog)1 Intent (android.content.Intent)1 MenuItem (android.view.MenuItem)1 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)1 BottomNavigationView (com.google.android.material.bottomnavigation.BottomNavigationView)1 DataSnapshot (com.google.firebase.database.DataSnapshot)1 DatabaseError (com.google.firebase.database.DatabaseError)1 ValueEventListener (com.google.firebase.database.ValueEventListener)1 TopStatusAdapter (com.tutuanle.chatapp.adapters.TopStatusAdapter)1 UsersAdapter (com.tutuanle.chatapp.adapters.UsersAdapter)1 Status (com.tutuanle.chatapp.models.Status)1 User (com.tutuanle.chatapp.models.User)1 UserStatus (com.tutuanle.chatapp.models.UserStatus)1 ArrayList (java.util.ArrayList)1