Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Home : Fragment() {
- lateinit var auth: FirebaseAuth
- lateinit var db:FirebaseFirestore
- lateinit var itemsCells: ArrayList<post_data?>
- lateinit var loadMoreItemsCells: ArrayList<post_data>
- lateinit var adapterLinear: Items_LinearRVAdapter
- lateinit var scrollListener: RecyclerViewLoadMoreScroll
- lateinit var mLayoutManager: RecyclerView.LayoutManager
- lateinit var root:View
- lateinit var lastVisable:DocumentSnapshot
- override fun onCreateView(
- inflater: LayoutInflater, container: ViewGroup?,
- savedInstanceState: Bundle?
- ): View? {
- // Inflate the layout for this fragment
- root= inflater.inflate(R.layout.fragment_home, container, false)
- db=Firebase.firestore
- //** Set the data for our ArrayList
- setItemsData()
- //** Set the adapterLinear of the RecyclerView
- setAdapter()
- //** Set the Layout Manager of the RecyclerView
- setRVLayoutManager()
- //** Set the scrollListerner of the RecyclerView
- setRVScrollListener()
- return root
- }
- private fun setItemsData() {
- itemsCells = ArrayList()
- loadingData(10,itemsCells)
- }
- private fun setAdapter() {
- adapterLinear = Items_LinearRVAdapter(itemsCells)
- root.rc_posts.adapter = adapterLinear
- adapterLinear.notifyDataSetChanged()
- }
- private fun setRVLayoutManager() {
- mLayoutManager = LinearLayoutManager(activity!!)
- root.rc_posts.layoutManager = mLayoutManager
- root.rc_posts.setHasFixedSize(true)
- }
- private fun setRVScrollListener() {
- mLayoutManager = LinearLayoutManager(activity!!)
- scrollListener = RecyclerViewLoadMoreScroll(mLayoutManager as LinearLayoutManager)
- scrollListener.setOnLoadMoreListener(object :
- OnLoadMoreListener {
- override fun onLoadMore() {
- LoadMoreData()
- }
- })
- root.rc_posts.addOnScrollListener(scrollListener)
- }
- private fun LoadMoreData() {
- //Add the Loading View
- adapterLinear.addLoadingView()
- //Create the loadMoreItemsCells Arraylist
- loadMoreItemsCells=ArrayList()
- //Get the number of the current Items of the main Arraylist
- // val start = adapterLinear.itemCount.toLong()
- Log.e("arr","item_count $start")
- //Load 16 more items
- //val end:Long= start + 5
- //Use Handler if the items are loading too fast.
- //If you remove it, the data will load so fast that you can't even see the LoadingView
- auth=Firebase.auth
- db.collection("Users").limit(10).get().addOnSuccessListener {querySnapshot ->
- for (uid in querySnapshot){
- val uid2=uid.getString("UserId").toString()
- db.collection("Users").document(uid2).get().addOnSuccessListener { documentSnapshot ->
- val username=documentSnapshot.getString("UserName").toString()
- val profile_img=documentSnapshot.getString("Image_Url").toString()
- Log.e("arr","p1")
- db.collection("Users").document(uid2).collection("MyPosts").orderBy("post_time",Query.Direction.DESCENDING).startAfter(lastVisable).limit(10).get().addOnSuccessListener { querySnapshot ->
- if (!querySnapshot.isEmpty){
- lastVisable = querySnapshot.documents[querySnapshot.size() - 1]
- }
- for (doc in querySnapshot){
- val time= doc.get("post_time").toString()
- val description=doc.getString("post_details").toString()
- val like_count=doc.get("like_count").toString()
- val commint_count=doc.get("comment_count").toString()
- val video:String
- if (doc?.getString("post_video") == null) {
- video = ""
- } else {
- video = doc.getString("post_video").toString()
- }
- val img:ArrayList<String>
- if (doc.get("post_img")!=null){
- img=doc.get("post_img") as ArrayList<String>
- }else{
- img=ArrayList()
- }
- val url=ArrayList<SlideModel>()
- url.add(SlideModel("https://www.w3schools.com/w3css/img_lights.jpg"))
- url.add(SlideModel("https://www.talkwalker.com/images/2020/blog-headers/image-analysis.png"))
- Log.e("arr","p2")
- loadMoreItemsCells.add(post_data(uid2,username,profile_img,time,description,
- img,video,like_count,commint_count))
- }
- adapterLinear.removeLoadingView()
- //We adding the data to our main ArrayList
- adapterLinear.addData(loadMoreItemsCells)
- //Change the boolean isLoading to false
- scrollListener.setLoaded()
- //Update the recyclerView in the main thread
- root.rc_posts.post {
- adapterLinear.notifyDataSetChanged()
- }
- }
- }.addOnFailureListener {
- Log.e("error",it.message.toString())
- }
- }
- }
- }
- private fun loadingData(limit: Long,array: ArrayList<post_data?>){
- auth=Firebase.auth
- val user_array=ArrayList<String>()
- var user_id=""
- db.collection("Users").limit(limit).get().addOnSuccessListener {querySnapshot ->
- try {
- for (user in querySnapshot){
- user_id=user.getString("UserId").toString()
- user_array.add(user_id.toString())
- }
- for (uid in user_array){
- db.collection("Users").document(uid).collection("MyPosts").orderBy("post_time",Query.Direction.DESCENDING).limit(limit).get().addOnSuccessListener {querySnapshot ->
- lastVisable = querySnapshot.documents[querySnapshot.size() - 1]
- for (doc in querySnapshot){
- val time= doc.get("post_time").toString()
- val description=doc.getString("post_details").toString()
- val like_count=doc.get("like_count").toString()
- val commint_count=doc.get("comment_count").toString()
- val video:String
- if (doc?.getString("post_video") == null) {
- video = ""
- } else {
- video = doc.getString("post_video").toString()
- }
- val img:ArrayList<String>
- if (doc.get("post_img")!=null){
- img=doc.get("post_img") as ArrayList<String>
- }else{
- img=ArrayList()
- }
- db.collection("Users").document(uid).get().addOnSuccessListener { documentSnapshot ->
- val username=documentSnapshot.getString("UserName").toString()
- val profile_img=documentSnapshot.getString("Image_Url").toString()
- array.add(post_data(user_id,username,profile_img,time,description,img,video,like_count,commint_count))
- adapterLinear.notifyDataSetChanged()
- }
- }
- }.addOnFailureListener {
- Log.e("error",it.message.toString())
- }
- }
- }catch (r:RuntimeException){}
- }
- }
- }
Add Comment
Please, Sign In to add comment