Search in sources :

Example 6 with User

use of com.tutuanle.chatapp.models.User in project Chat-app by TuTuanLe.

the class SearchActivity method searchFilter.

@SuppressLint("SetTextI18n")
private void searchFilter(String name) {
    List<User> temp = new ArrayList<>();
    if (name.length() == 0) {
        temp.addAll(users);
    } else {
        for (int i = 0; i < users.size(); i++) {
            if (users.get(i).getName().toLowerCase().contains(name.toLowerCase())) {
                temp.add(users.get(i));
            }
        }
    }
    if (temp.size() == 0) {
        binding.layoutNotFound.setVisibility(View.VISIBLE);
        binding.txtNotFound.setText("No matches were found for " + name + " .Try checking for typos or using complete words. ");
    } else {
        binding.layoutNotFound.setVisibility(View.GONE);
    }
    searchAdapter = new SearchAdapter(temp, SearchActivity.this);
    binding.userRecyclerView.setAdapter(searchAdapter);
    binding.userRecyclerView.setVisibility(View.VISIBLE);
}
Also used : User(com.tutuanle.chatapp.models.User) SearchAdapter(com.tutuanle.chatapp.adapters.SearchAdapter) ArrayList(java.util.ArrayList) SuppressLint(android.annotation.SuppressLint) SuppressLint(android.annotation.SuppressLint)

Example 7 with User

use of com.tutuanle.chatapp.models.User in project Chat-app by TuTuanLe.

the class SearchActivity method getUSer.

private void getUSer() {
    loading(true);
    FirebaseFirestore database = FirebaseFirestore.getInstance();
    database.collection(Constants.KEY_COLLECTION_USERS).get().addOnCompleteListener(task -> {
        loading(false);
        String currentUserId = preferenceManager.getString(Constants.KEY_USER_ID);
        if (task.isSuccessful() && task.getResult() != null) {
            users = new ArrayList<>();
            for (QueryDocumentSnapshot queryDocumentSnapshot : task.getResult()) {
                if (currentUserId.equals(queryDocumentSnapshot.getId())) {
                    continue;
                }
                User user = new User();
                user.setName(queryDocumentSnapshot.getString(Constants.KEY_NAME));
                user.setEmail(queryDocumentSnapshot.getString(Constants.KEY_EMAIL));
                user.setProfileImage(queryDocumentSnapshot.getString(Constants.KEY_IMAGE));
                user.setToken(queryDocumentSnapshot.getString(Constants.KEY_FCM_TOKEN));
                user.setUid(queryDocumentSnapshot.getId());
                user.setAvailability(queryDocumentSnapshot.getLong(Constants.KEY_AVAILABILITY));
                users.add(user);
            }
            if (users.size() > 0) {
                searchAdapter = new SearchAdapter(users, SearchActivity.this);
                binding.userRecyclerView.setAdapter(searchAdapter);
                binding.userRecyclerView.setVisibility(View.VISIBLE);
            } else {
                showToast("Error");
            }
        }
    });
}
Also used : FirebaseFirestore(com.google.firebase.firestore.FirebaseFirestore) User(com.tutuanle.chatapp.models.User) SearchAdapter(com.tutuanle.chatapp.adapters.SearchAdapter) QueryDocumentSnapshot(com.google.firebase.firestore.QueryDocumentSnapshot)

Example 8 with User

use of com.tutuanle.chatapp.models.User in project Chat-app by TuTuanLe.

the class SetupProfileActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ActivitySetupProfileBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());
    dialog = new ProgressDialog(this);
    dialog.setMessage("Updating profile ...");
    dialog.setCancelable(false);
    getSupportActionBar().hide();
    database = FirebaseDatabase.getInstance();
    storage = FirebaseStorage.getInstance();
    auth = FirebaseAuth.getInstance();
    binding.imageView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(intent, 45);
        }
    });
    binding.continueBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            String name = binding.nameBox.getText().toString();
            if (name.isEmpty()) {
                binding.nameBox.setError("Please type a name");
                return;
            }
            dialog.show();
            if (selectedImage != null) {
                StorageReference reference = storage.getReference().child("Profile").child(auth.getUid());
                reference.putFile(selectedImage).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {

                    @Override
                    public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                        if (task.isSuccessful()) {
                            reference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {

                                @Override
                                public void onSuccess(Uri uri) {
                                    String imageUrl = uri.toString();
                                    String uid = auth.getUid();
                                    String phone = auth.getCurrentUser().getPhoneNumber();
                                    String name = binding.nameBox.getText().toString();
                                    User user = new User(uid, name, phone, imageUrl);
                                    database.getReference().child("users").child(uid).setValue(user).addOnSuccessListener(new OnSuccessListener<Void>() {

                                        @Override
                                        public void onSuccess(Void unused) {
                                            dialog.dismiss();
                                            Intent intent = new Intent(SetupProfileActivity.this, MainActivity.class);
                                            startActivity(intent);
                                            finish();
                                        }
                                    });
                                }
                            });
                        }
                    }
                });
            }
        }
    });
}
Also used : Task(com.google.android.gms.tasks.Task) UploadTask(com.google.firebase.storage.UploadTask) User(com.tutuanle.chatapp.models.User) StorageReference(com.google.firebase.storage.StorageReference) Intent(android.content.Intent) ProgressDialog(android.app.ProgressDialog) View(android.view.View) Uri(android.net.Uri) OnCompleteListener(com.google.android.gms.tasks.OnCompleteListener) UploadTask(com.google.firebase.storage.UploadTask) NonNull(androidx.annotation.NonNull) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener)

Example 9 with User

use of com.tutuanle.chatapp.models.User in project Chat-app by TuTuanLe.

the class UsersAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(@NonNull UsersViewHolder holder, int position) {
    User user = users.get(position);
    String senderId = FirebaseAuth.getInstance().getUid();
    String senderRoom = senderId + user.getUid();
    FirebaseDatabase.getInstance().getReference().child("Chats").child(senderRoom).addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            if (snapshot.exists()) {
                String lastMsg = snapshot.child("lastMsg").getValue(String.class);
                long time = snapshot.child("lastMsgTime").getValue(Long.class);
                SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm a");
                holder.binding.msgTime.setText(dateFormat.format(new Date(time)));
                holder.binding.lastMsg.setText(lastMsg);
            } else {
                holder.binding.lastMsg.setText("Tap to Chat");
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
        }
    });
    holder.binding.username.setText(user.getName());
    Glide.with(context).load(user.getProfileImage()).placeholder(R.drawable.avatar).into(holder.binding.profile);
    holder.itemView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Intent intent = new Intent(context, ChatActivity.class);
            intent.putExtra("name", user.getName());
            intent.putExtra("uid", user.getUid());
            context.startActivity(intent);
        }
    });
}
Also used : User(com.tutuanle.chatapp.models.User) ChatActivity(com.tutuanle.chatapp.activities.ChatActivity) Intent(android.content.Intent) DataSnapshot(com.google.firebase.database.DataSnapshot) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) Date(java.util.Date) DatabaseError(com.google.firebase.database.DatabaseError) ValueEventListener(com.google.firebase.database.ValueEventListener) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

User (com.tutuanle.chatapp.models.User)9 Intent (android.content.Intent)5 View (android.view.View)3 FirebaseFirestore (com.google.firebase.firestore.FirebaseFirestore)3 ArrayList (java.util.ArrayList)3 SuppressLint (android.annotation.SuppressLint)2 ProgressDialog (android.app.ProgressDialog)2 NonNull (androidx.annotation.NonNull)2 RecyclerView (androidx.recyclerview.widget.RecyclerView)2 OnCompleteListener (com.google.android.gms.tasks.OnCompleteListener)2 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)2 DataSnapshot (com.google.firebase.database.DataSnapshot)2 DatabaseError (com.google.firebase.database.DatabaseError)2 ValueEventListener (com.google.firebase.database.ValueEventListener)2 QueryDocumentSnapshot (com.google.firebase.firestore.QueryDocumentSnapshot)2 SearchAdapter (com.tutuanle.chatapp.adapters.SearchAdapter)2 NotificationChannel (android.app.NotificationChannel)1 NotificationManager (android.app.NotificationManager)1 PendingIntent (android.app.PendingIntent)1 Uri (android.net.Uri)1