Search in sources :

Example 1 with UserStatus

use of com.tutuanle.chatapp.models.UserStatus 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)

Example 2 with UserStatus

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

the class MainActivity method onActivityResult.

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (data != null) {
        if (data.getData() != null) {
            dialog.show();
            FirebaseStorage storage = FirebaseStorage.getInstance();
            Date date = new Date();
            StorageReference reference = storage.getReference().child("status").child(date.getTime() + "");
            reference.putFile(data.getData()).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) {
                                UserStatus userStatus = new UserStatus();
                                userStatus.setName(user.getName());
                                userStatus.setProfileImage(user.getProfileImage());
                                userStatus.setLastUpdated(date.getTime());
                                userStatuses.add(userStatus);
                                HashMap<String, Object> obj = new HashMap<>();
                                obj.put("name", userStatus.getName());
                                obj.put("profileImage", userStatus.getProfileImage());
                                obj.put("lastUpdate", userStatus.getLastUpdated());
                                String imageUrl = uri.toString();
                                Status status = new Status(imageUrl, userStatus.getLastUpdated());
                                database.getReference().child("stories").child(FirebaseAuth.getInstance().getUid()).updateChildren(obj);
                                database.getReference().child("stories").child(FirebaseAuth.getInstance().getUid()).child("statuses").push().setValue(status);
                                dialog.dismiss();
                            }
                        });
                    }
                }
            });
        }
    }
}
Also used : UserStatus(com.tutuanle.chatapp.models.UserStatus) Status(com.tutuanle.chatapp.models.Status) StorageReference(com.google.firebase.storage.StorageReference) HashMap(java.util.HashMap) Uri(android.net.Uri) UserStatus(com.tutuanle.chatapp.models.UserStatus) Date(java.util.Date) FirebaseStorage(com.google.firebase.storage.FirebaseStorage) OnSuccessListener(com.google.android.gms.tasks.OnSuccessListener)

Example 3 with UserStatus

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

the class HomeFragment method getUserStatus.

@SuppressLint("NotifyDataSetChanged")
private void getUserStatus() {
    userStatuses = new ArrayList<>();
    statusAdapter = new TopStatusAdapter(mainScreenActivity, userStatuses);
    RecyclerView temp = view.findViewById(R.id.statusList);
    temp.setAdapter(statusAdapter);
    ArrayList<Status> statuses = new ArrayList<>();
    statuses.add(new Status("https://firebasestorage.googleapis.com/v0/b/chatsapp-4b8d6.appspot.com/o/status%2F1648451488181?alt=media&token=9e1f4eb7-8825-43c2-8220-106c92409620", 164845148));
    statuses.add(new Status("https://firebasestorage.googleapis.com/v0/b/chatsapp-4b8d6.appspot.com/o/status%2F1648451488181?alt=media&token=9e1f4eb7-8825-43c2-8220-106c92409620", 164853774));
    ArrayList<Status> statuses1 = new ArrayList<>();
    statuses1.add(new Status("hhttps://images.unsplash.com/photo-1453728013993-6d66e9c9123a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8Mnx8dmlld3xlbnwwfHwwfHw%3D&w=1000&q=80", 164845148));
    statuses1.add(new Status("https://www.w3schools.com/w3images/fjords.jpg", 164853774));
    statuses1.add(new Status("https://st.depositphotos.com/1006706/2671/i/600/depositphotos_26715369-stock-photo-which-way-to-choose-3d.jpg", 164853774));
    ArrayList<Status> statuses2 = new ArrayList<>();
    statuses2.add(new Status("https://www.perma-horti.com/wp-content/uploads/2019/02/image-2.jpg", 164845148));
    userStatuses.add(new UserStatus("Tuan Anh", "https://firebasestorage.googleapis.com/v0/b/chatsapp-4b8d6.appspot.com/o/status%2F1648537741702?alt=media&token=685fb7d8-a05d-429d-9834-1db74b41ff0e", 164853810, statuses));
    userStatuses.add(new UserStatus("TuTuanLe", "https://www.w3schools.com/w3images/fjords.jpg", 174853810, statuses1));
    userStatuses.add(new UserStatus("TAnh", "https://images.unsplash.com/photo-1453728013993-6d66e9c9123a?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxzZWFyY2h8Mnx8dmlld3xlbnwwfHwwfHw%3D&w=1000&q=80", 184853810, statuses2));
    statusAdapter.notifyDataSetChanged();
}
Also used : UserStatus(com.tutuanle.chatapp.models.UserStatus) Status(com.tutuanle.chatapp.models.Status) ArrayList(java.util.ArrayList) RecyclerView(androidx.recyclerview.widget.RecyclerView) TopStatusAdapter(com.tutuanle.chatapp.adapters.TopStatusAdapter) UserStatus(com.tutuanle.chatapp.models.UserStatus) SuppressLint(android.annotation.SuppressLint)

Example 4 with UserStatus

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

the class TopStatusAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(@NonNull TopStatusViewHolder holder, int position) {
    UserStatus userStatus = userStatuses.get(position);
    Status lastStatus = userStatus.getStatuses().get(userStatus.getStatuses().size() - 1);
    Glide.with(context).load(lastStatus.getImageUrl()).into(holder.binding.image);
    Glide.with(context).load(lastStatus.getImageUrl()).into(holder.binding.imageUserStatus);
    holder.binding.circularStatusView.setPortionsCount(userStatus.getStatuses().size());
    holder.binding.circularStatusView.setOnClickListener(view -> {
        ArrayList<MyStory> myStories = new ArrayList<>();
        for (Status status : userStatus.getStatuses()) {
            myStories.add(new MyStory(status.getImageUrl()));
        }
        new StoryView.Builder(((MainScreenActivity) context).getSupportFragmentManager()).setStoriesList(// Required
        myStories).setStoryDuration(// Default is 2000 Millis (2 Seconds)
        5000).setTitleText(// Default is Hidden
        userStatus.getName()).setSubtitleText(// Default is Hidden
        "").setTitleLogoUrl(// Default is Hidden
        userStatus.getProfileImage()).setStoryClickListeners(new StoryClickListeners() {

            @Override
            public void onDescriptionClickListener(int position1) {
            // your action
            }

            @Override
            public void onTitleIconClickListener(int position1) {
            // your action
            }
        }).build().show();
    });
}
Also used : UserStatus(com.tutuanle.chatapp.models.UserStatus) Status(com.tutuanle.chatapp.models.Status) StoryClickListeners(omari.hamza.storyview.callback.StoryClickListeners) ArrayList(java.util.ArrayList) MyStory(omari.hamza.storyview.model.MyStory) UserStatus(com.tutuanle.chatapp.models.UserStatus)

Example 5 with UserStatus

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

the class StoryFragment method onActivityResult.

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 2 && resultCode == -1 && data != null) {
        dialog.show();
        FirebaseStorage storage = FirebaseStorage.getInstance();
        Date date = new Date();
        StorageReference reference = storage.getReference().child("status").child(date.getTime() + "");
        reference.putFile(data.getData()).addOnCompleteListener(task -> {
            if (task.isSuccessful()) {
                reference.getDownloadUrl().addOnSuccessListener(uri -> {
                    UserStatus userStatus = new UserStatus();
                    userStatus.setName(preferenceManager.getString(Constants.KEY_NAME));
                    userStatus.setProfileImage(preferenceManager.getString(Constants.KEY_IMAGE));
                    userStatus.setLastUpdated(date.getTime());
                    userStatuses.add(userStatus);
                    HashMap<String, Object> obj = new HashMap<>();
                    obj.put("name", userStatus.getName());
                    obj.put("profileImage", userStatus.getProfileImage());
                    obj.put("lastUpdate", userStatus.getLastUpdated());
                    String imageUrl = uri.toString();
                    Status status = new Status(imageUrl, userStatus.getLastUpdated());
                    database.collection(Constants.KEY_COLLECTION_STORIES).document(preferenceManager.getString(Constants.KEY_USER_ID)).collection(Constants.KEY_COLLECTION_STATUSES).add(status);
                    database.collection(Constants.KEY_COLLECTION_STORIES).document(preferenceManager.getString(Constants.KEY_USER_ID)).set(obj);
                    ;
                    dialog.dismiss();
                });
            }
        });
    }
}
Also used : UserStatus(com.tutuanle.chatapp.models.UserStatus) Status(com.tutuanle.chatapp.models.Status) StorageReference(com.google.firebase.storage.StorageReference) HashMap(java.util.HashMap) FirebaseStorage(com.google.firebase.storage.FirebaseStorage) UserStatus(com.tutuanle.chatapp.models.UserStatus) Date(java.util.Date)

Aggregations

Status (com.tutuanle.chatapp.models.Status)6 UserStatus (com.tutuanle.chatapp.models.UserStatus)6 ArrayList (java.util.ArrayList)4 SuppressLint (android.annotation.SuppressLint)2 FirebaseStorage (com.google.firebase.storage.FirebaseStorage)2 StorageReference (com.google.firebase.storage.StorageReference)2 TopStatusAdapter (com.tutuanle.chatapp.adapters.TopStatusAdapter)2 Date (java.util.Date)2 HashMap (java.util.HashMap)2 StoryClickListeners (omari.hamza.storyview.callback.StoryClickListeners)2 MyStory (omari.hamza.storyview.model.MyStory)2 ProgressDialog (android.app.ProgressDialog)1 Intent (android.content.Intent)1 Uri (android.net.Uri)1 MenuItem (android.view.MenuItem)1 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 OnSuccessListener (com.google.android.gms.tasks.OnSuccessListener)1 BottomNavigationView (com.google.android.material.bottomnavigation.BottomNavigationView)1 DataSnapshot (com.google.firebase.database.DataSnapshot)1