Guest User

main screen

a guest
Jul 16th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 8.17 KB | None | 0 0
  1. class Home : Fragment() {
  2.     lateinit var auth: FirebaseAuth
  3.     lateinit var db:FirebaseFirestore
  4.     lateinit var itemsCells: ArrayList<post_data?>
  5.     lateinit var loadMoreItemsCells: ArrayList<post_data>
  6.     lateinit var adapterLinear: Items_LinearRVAdapter
  7.     lateinit var scrollListener: RecyclerViewLoadMoreScroll
  8.     lateinit var mLayoutManager: RecyclerView.LayoutManager
  9.     lateinit var root:View
  10.     lateinit var lastVisable:DocumentSnapshot
  11.     override fun onCreateView(
  12.         inflater: LayoutInflater, container: ViewGroup?,
  13.         savedInstanceState: Bundle?
  14.     ): View? {
  15.         // Inflate the layout for this fragment
  16.          root= inflater.inflate(R.layout.fragment_home, container, false)
  17.         db=Firebase.firestore
  18.  
  19.         //** Set the data for our ArrayList
  20.         setItemsData()
  21.  
  22.         //** Set the adapterLinear of the RecyclerView
  23.        setAdapter()
  24.  
  25.         //** Set the Layout Manager of the RecyclerView
  26.         setRVLayoutManager()
  27.  
  28.         //** Set the scrollListerner of the RecyclerView
  29.         setRVScrollListener()
  30.  
  31.  
  32.         return root
  33.     }
  34.  
  35.  
  36.  
  37.  
  38.     private fun setItemsData() {
  39.         itemsCells = ArrayList()
  40.         loadingData(10,itemsCells)
  41.  
  42.     }
  43.  
  44.     private fun setAdapter() {
  45.         adapterLinear = Items_LinearRVAdapter(itemsCells)
  46.         root.rc_posts.adapter = adapterLinear
  47.         adapterLinear.notifyDataSetChanged()
  48.  
  49.  
  50.  
  51.     }
  52.  
  53.     private fun setRVLayoutManager() {
  54.         mLayoutManager = LinearLayoutManager(activity!!)
  55.         root.rc_posts.layoutManager = mLayoutManager
  56.         root.rc_posts.setHasFixedSize(true)
  57.  
  58.     }
  59.  
  60.     private fun setRVScrollListener() {
  61.         mLayoutManager = LinearLayoutManager(activity!!)
  62.         scrollListener = RecyclerViewLoadMoreScroll(mLayoutManager as LinearLayoutManager)
  63.         scrollListener.setOnLoadMoreListener(object :
  64.             OnLoadMoreListener {
  65.             override fun onLoadMore() {
  66.                 LoadMoreData()
  67.             }
  68.         })
  69.         root.rc_posts.addOnScrollListener(scrollListener)
  70.     }
  71.  
  72.     private fun LoadMoreData() {
  73.         //Add the Loading View
  74.         adapterLinear.addLoadingView()
  75.         //Create the loadMoreItemsCells Arraylist
  76.         loadMoreItemsCells=ArrayList()
  77.         //Get the number of the current Items of the main Arraylist
  78.        // val start = adapterLinear.itemCount.toLong()
  79.         Log.e("arr","item_count $start")
  80.         //Load 16 more items
  81.         //val end:Long= start + 5
  82.         //Use Handler if the items are loading too fast.
  83.         //If you remove it, the data will load so fast that you can't even see the LoadingView
  84.  
  85.             auth=Firebase.auth
  86.  
  87.             db.collection("Users").limit(10).get().addOnSuccessListener {querySnapshot ->
  88.  
  89.                 for (uid in querySnapshot){
  90.                     val uid2=uid.getString("UserId").toString()
  91.  
  92.                     db.collection("Users").document(uid2).get().addOnSuccessListener { documentSnapshot ->
  93.                         val username=documentSnapshot.getString("UserName").toString()
  94.                         val profile_img=documentSnapshot.getString("Image_Url").toString()
  95.                         Log.e("arr","p1")
  96.  
  97.  
  98.                     db.collection("Users").document(uid2).collection("MyPosts").orderBy("post_time",Query.Direction.DESCENDING).startAfter(lastVisable).limit(10).get().addOnSuccessListener { querySnapshot ->
  99.                         if (!querySnapshot.isEmpty){
  100.                             lastVisable = querySnapshot.documents[querySnapshot.size() - 1]
  101.  
  102.                         }
  103.  
  104.                         for (doc in querySnapshot){
  105.                             val time= doc.get("post_time").toString()
  106.                             val description=doc.getString("post_details").toString()
  107.                             val like_count=doc.get("like_count").toString()
  108.                             val commint_count=doc.get("comment_count").toString()
  109.  
  110.  
  111.                             val video:String
  112.                             if (doc?.getString("post_video") == null) {
  113.                                 video = ""
  114.                             } else {
  115.                                 video = doc.getString("post_video").toString()
  116.                             }
  117.  
  118.  
  119.  
  120.                             val img:ArrayList<String>
  121.                             if (doc.get("post_img")!=null){
  122.                                 img=doc.get("post_img") as ArrayList<String>
  123.                             }else{
  124.                                 img=ArrayList()
  125.                             }
  126.  
  127.                             val url=ArrayList<SlideModel>()
  128.                             url.add(SlideModel("https://www.w3schools.com/w3css/img_lights.jpg"))
  129.                             url.add(SlideModel("https://www.talkwalker.com/images/2020/blog-headers/image-analysis.png"))
  130.                             Log.e("arr","p2")
  131.                             loadMoreItemsCells.add(post_data(uid2,username,profile_img,time,description,
  132.                                 img,video,like_count,commint_count))
  133.  
  134.                         }
  135.                         adapterLinear.removeLoadingView()
  136.                         //We adding the data to our main ArrayList
  137.                         adapterLinear.addData(loadMoreItemsCells)
  138.                         //Change the boolean isLoading to false
  139.                         scrollListener.setLoaded()
  140.                         //Update the recyclerView in the main thread
  141.                         root.rc_posts.post {
  142.                             adapterLinear.notifyDataSetChanged()
  143.                         }
  144.                     }
  145.  
  146.                     }.addOnFailureListener {
  147.                         Log.e("error",it.message.toString())
  148.                     }
  149.                 }
  150.             }
  151.  
  152.     }
  153.  
  154.  
  155.  
  156.  
  157.  
  158.     private fun loadingData(limit: Long,array: ArrayList<post_data?>){
  159.     auth=Firebase.auth
  160.     val user_array=ArrayList<String>()
  161.     var user_id=""
  162.     db.collection("Users").limit(limit).get().addOnSuccessListener {querySnapshot ->
  163.         try {
  164.             for (user in querySnapshot){
  165.                 user_id=user.getString("UserId").toString()
  166.                 user_array.add(user_id.toString())
  167.             }
  168.             for (uid in user_array){
  169.                 db.collection("Users").document(uid).collection("MyPosts").orderBy("post_time",Query.Direction.DESCENDING).limit(limit).get().addOnSuccessListener {querySnapshot ->
  170.                    lastVisable = querySnapshot.documents[querySnapshot.size() - 1]
  171.                     for (doc in querySnapshot){
  172.                         val time= doc.get("post_time").toString()
  173.                         val description=doc.getString("post_details").toString()
  174.                         val like_count=doc.get("like_count").toString()
  175.                         val commint_count=doc.get("comment_count").toString()
  176.  
  177.  
  178.                         val video:String
  179.                         if (doc?.getString("post_video") == null) {
  180.                             video = ""
  181.                         } else {
  182.                             video = doc.getString("post_video").toString()
  183.                         }
  184.  
  185.  
  186.                         val img:ArrayList<String>
  187.                         if (doc.get("post_img")!=null){
  188.                             img=doc.get("post_img") as ArrayList<String>
  189.                         }else{
  190.                             img=ArrayList()
  191.                         }
  192.                        
  193.  
  194.  
  195.                         db.collection("Users").document(uid).get().addOnSuccessListener { documentSnapshot ->
  196.                             val username=documentSnapshot.getString("UserName").toString()
  197.                             val profile_img=documentSnapshot.getString("Image_Url").toString()
  198.                             array.add(post_data(user_id,username,profile_img,time,description,img,video,like_count,commint_count))
  199.                             adapterLinear.notifyDataSetChanged()
  200.                         }
  201.                     }
  202.                 }.addOnFailureListener {
  203.                     Log.e("error",it.message.toString())
  204.                 }
  205.  
  206.             }
  207.         }catch (r:RuntimeException){}
  208.  
  209.     }
  210.  
  211.  
  212.  
  213. }
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220. }
Add Comment
Please, Sign In to add comment