laminaung

Untitled

Jun 27th, 2020
372
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package mm.basiceducation.beprimary.ui
  2.  
  3. import android.content.Intent
  4. import android.os.Bundle
  5. import android.util.Log
  6. import android.view.LayoutInflater
  7. import android.view.View
  8. import android.view.ViewGroup
  9. import android.widget.TextView
  10. import androidx.fragment.app.Fragment
  11. import androidx.recyclerview.widget.RecyclerView
  12. import com.google.android.gms.ads.AdListener
  13. import com.google.android.gms.ads.AdRequest
  14. import com.google.android.gms.ads.AdView
  15. import com.google.android.gms.ads.MobileAds
  16. import com.google.android.gms.ads.initialization.OnInitializationCompleteListener
  17. import com.google.firebase.firestore.FirebaseFirestore
  18. import mm.basiceducation.beprimary.R
  19. import mm.basiceducation.beprimary.activity.PlayChannel
  20. import mm.basiceducation.beprimary.adapter.ChannelAdapter
  21. import mm.basiceducation.beprimary.dto.Channel
  22. import mm.basiceducation.beprimary.listener.ClickListener
  23.  
  24. class ChannelFragment : Fragment() {
  25.  
  26.     lateinit var mAdView: AdView
  27.  
  28.     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
  29.                               savedInstanceState: Bundle?): View? {
  30.         // Inflate the layout for this fragment
  31.         val view = inflater.inflate(R.layout.fragment_channel, container, false)
  32.  
  33.         val db = FirebaseFirestore.getInstance()
  34.         val data = mutableListOf<Channel>()
  35.         val text = view.findViewById<TextView>(R.id.channel_text)
  36.         val recyclerView = view.findViewById<RecyclerView>(R.id.channel_recyclerview)
  37.        
  38.         // banner ads
  39.         MobileAds.initialize(context)
  40.         mAdView = view.findViewById(R.id.channel_adview)
  41.         val adRequest = AdRequest.Builder().build()
  42.         mAdView.loadAd(adRequest)
  43.  
  44.         db.collection("channel")
  45.                 .get()
  46.                 .addOnCompleteListener { task ->
  47.                     if (task.isSuccessful) {
  48.                         for (document in task.result!!) {
  49.                             data.add(document.toObject(Channel::class.java))
  50.  
  51.                             Log.d("channel", document.id + " => " + document.data);
  52.                         }
  53.                         if (data.size < 1) {
  54.                             text.visibility = View.VISIBLE
  55.                         }
  56.                         data.sort()
  57.                         val adapter = ChannelAdapter(data, requireContext())
  58.                         recyclerView.adapter = adapter
  59.                         adapter.setDetailListner(ClickListener { _, positon ->
  60. //
  61.                             val intent = Intent(requireActivity(), PlayChannel::class.java)
  62.                             intent.putExtra("LINK", data[positon].link)
  63.                             startActivity(intent)
  64. //                            Toast.makeText(requireActivity().applicationContext, "" + data[positon].link, Toast.LENGTH_SHORT).show()
  65.                         })
  66.                     } else {
  67.                         //                            Log.w(TAG, "Error getting documents.", task.getException());
  68.                     }
  69.                 }
  70.  
  71.         return view
  72.     }
  73.  
  74. }
RAW Paste Data