Firebase Social Media App Complete Android Tutorials
Firebase Social Media App using Android Studio and Firebase
✓Registration using Email/Password
✓Login using Email/Password, Google, Phone
✓Recover password in case you forgot
✓Profile [Name, Email, Profile Pic, Phone, etc]
✓Update
✓Add Posts [Publisher name, email; Title, Image Description, Date & Time etc]
✓All Posts, User Specific Posts
✓Like Post
✓Comment Post
✓Delete, Edit your post
Firebase Social Media App Complete Android Tutorials |
Complete Videos Playlist
When presses like button, refreshes whole list - Fixed
package com.blogspot.atifsoftwares.firebaseapp.fragments; import android.content.Intent; import android.os.Bundle; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.fragment.app.Fragment; import androidx.core.view.MenuItemCompat; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import androidx.appcompat.widget.SearchView; import android.text.TextUtils; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; import com.blogspot.atifsoftwares.firebaseapp.AddPostActivity; import com.blogspot.atifsoftwares.firebaseapp.MainActivity; import com.blogspot.atifsoftwares.firebaseapp.R; import com.blogspot.atifsoftwares.firebaseapp.SettingsActivity; import com.blogspot.atifsoftwares.firebaseapp.adapters.AdapterPosts; import com.blogspot.atifsoftwares.firebaseapp.models.ModelPost; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseUser; import com.google.firebase.database.ChildEventListener; import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DatabaseError; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.database.ValueEventListener; import java.util.ArrayList; import java.util.List; /** * A simple {@link Fragment} subclass. */ public class HomeFragment extends Fragment { //firebase auth FirebaseAuth firebaseAuth; RecyclerView recyclerView; List<ModelPost> postList; AdapterPosts adapterPosts; public HomeFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_home, container, false); //init firebaseAuth = FirebaseAuth.getInstance(); //recycler view and its properties recyclerView = view.findViewById(R.id.postsRecyclerview); LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity()); //show newest post first, for this load from last layoutManager.setStackFromEnd(true); layoutManager.setReverseLayout(true); //set layout to recyclerview recyclerView.setLayoutManager(layoutManager); //init post list postList = new ArrayList<>(); loadPosts(); return view; } private void loadPosts() { postList = new ArrayList<>(); postList.clear(); adapterPosts = new AdapterPosts(getActivity(), postList); recyclerView.setAdapter(adapterPosts); //path of all posts DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Posts"); //get all data from this ref ref.addChildEventListener(new ChildEventListener() { @Override public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String previousChildName) { ModelPost modelPost = dataSnapshot.getValue(ModelPost.class); postList.add(modelPost); adapterPosts.notifyItemInserted(postList.size() - 1); } @Override public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String previousChildName) { Log.i("mytag", "child changed"); ModelPost modelPost = dataSnapshot.getValue(ModelPost.class); for (int i = 0; i < postList.size(); i++) { if (postList.get(i).getUid().equals(modelPost.getUid())) { postList.set(i, modelPost); Log.i("mytag", "changed child found at position: " + i); adapterPosts.notifyItemChanged(i); break; } } } @Override public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) { Log.i("mytag", "child removed"); ModelPost modelPost = dataSnapshot.getValue(ModelPost.class); for (int i = 0; i < postList.size(); i++) { if (postList.get(i).getUid().equals(modelPost.getUid())) { postList.remove(i); Log.i("mytag", "removed child found at position: " + i); adapterPosts.notifyItemRemoved(i); break; } } } @Override public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) { } @Override public void onCancelled(@NonNull DatabaseError error) { } }); } private void searchPosts(final String searchQuery){ postList = new ArrayList<>(); postList.clear(); adapterPosts = new AdapterPosts(getActivity(), postList); recyclerView.setAdapter(adapterPosts); //path of all posts DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Posts"); //get all data from this ref ref.addChildEventListener(new ChildEventListener() { @Override public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String previousChildName) { ModelPost modelPost = dataSnapshot.getValue(ModelPost.class); if (modelPost.getpTitle().toLowerCase().contains(searchQuery.toLowerCase()) || modelPost.getpDescr().toLowerCase().contains(searchQuery.toLowerCase())){ postList.add(modelPost); adapterPosts.notifyItemInserted(postList.size() - 1); } } @Override public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String previousChildName) { Log.i("mytag", "child changed"); ModelPost modelPost = dataSnapshot.getValue(ModelPost.class); for (int i = 0; i < postList.size(); i++) { if (postList.get(i).getUid().equals(modelPost.getUid())) { postList.set(i, modelPost); Log.i("mytag", "changed child found at position: " + i); adapterPosts.notifyItemChanged(i); break; } } } @Override public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) { Log.i("mytag", "child removed"); ModelPost modelPost = dataSnapshot.getValue(ModelPost.class); for (int i = 0; i < postList.size(); i++) { if (postList.get(i).getUid().equals(modelPost.getUid())) { postList.remove(i); Log.i("mytag", "removed child found at position: " + i); adapterPosts.notifyItemRemoved(i); break; } } } @Override public void onChildMoved(@NonNull DataSnapshot snapshot, @Nullable String previousChildName) { } @Override public void onCancelled(@NonNull DatabaseError error) { } }); } private void checkUserStatus(){ //get current user FirebaseUser user = firebaseAuth.getCurrentUser(); if (user != null){ //user is signed in stay here //set email of logged in user //mProfileTv.setText(user.getEmail()); } else { //user not signed in, go to main acitivity startActivity(new Intent(getActivity(), MainActivity.class)); getActivity().finish(); } } @Override public void onCreate(@Nullable Bundle savedInstanceState) { setHasOptionsMenu(true);//to show menu option in fragment super.onCreate(savedInstanceState); } /*inflate options menu*/ @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { //inflating menu inflater.inflate(R.menu.menu_main, menu); //hide some options menu.findItem(R.id.action_create_group).setVisible(false); menu.findItem(R.id.action_add_participant).setVisible(false); menu.findItem(R.id.action_groupinfo).setVisible(false); //searchview to search posts by post title/description MenuItem item = menu.findItem(R.id.action_search); SearchView searchView = (SearchView)MenuItemCompat.getActionView(item); //search listener searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String s) { //called when user press search button if (!TextUtils.isEmpty(s)){ searchPosts(s); } else { loadPosts(); } return false; } @Override public boolean onQueryTextChange(String s) { //called as and when user press any letter if (!TextUtils.isEmpty(s)){ searchPosts(s); } else { loadPosts(); } return false; } }); super.onCreateOptionsMenu(menu, inflater); } /*handle menu item clicks*/ @Override public boolean onOptionsItemSelected(MenuItem item) { //get item id int id = item.getItemId(); if (id == R.id.action_logout){ firebaseAuth.signOut(); checkUserStatus(); } else if (id == R.id.action_add_post){ startActivity(new Intent(getActivity(), AddPostActivity.class)); } else if (id==R.id.action_settings){ //go to settings activity startActivity(new Intent(getActivity(), SettingsActivity.class)); } return super.onOptionsItemSelected(item); } }
Can You Please Provide me the AdapterPost, ModelPost Home fragments and Chat Fragment code of this project please i am stuck on these pages some errors are occuring infinitely i solve one another starts. Its making a loop in these 4 pages of java class.
ReplyDeleteIt will be very Helpful for me as i am still a noob debloper