Advertisement
ipdan4ik

[lb9] LeftFragment.kt

Apr 22nd, 2021
941
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.09 KB | None | 0 0
  1. package com.example.lb9
  2.  
  3. import android.annotation.SuppressLint
  4. import android.content.Context
  5. import android.os.Bundle
  6. import android.util.Log
  7. import android.view.LayoutInflater
  8. import android.view.View
  9. import android.view.ViewGroup
  10. import android.widget.*
  11. import androidx.fragment.app.Fragment
  12. import kotlinx.coroutines.GlobalScope
  13. import kotlinx.coroutines.MainScope
  14. import kotlinx.coroutines.launch
  15. import org.json.JSONObject
  16. import org.w3c.dom.Text
  17. import java.lang.Exception
  18. import java.net.URL
  19. import kotlin.math.ceil
  20.  
  21.  
  22. class LeftFragment() : Fragment() {
  23.     override fun onCreateView(
  24.         inflater: LayoutInflater,
  25.         container: ViewGroup?,
  26.         savedInstanceState: Bundle?
  27.     ): View? {
  28.         val view = inflater.inflate(R.layout.fragment_left_n, container, false)
  29.         val btn = view.findViewById<Button>(R.id.search_button)
  30.         btn.setOnClickListener{
  31.  
  32.             val text = view.findViewById<TextView>(R.id.search_field).text ?: "example"
  33.             GlobalScope.launch {
  34.                 val tUrl = URL("https://api.github.com/search/repositories?q=${text}")
  35.                 try {
  36.                     val t = tUrl.readText()
  37.                     val json = JSONObject(t)
  38.                     val count = json.getInt("total_count")
  39.                     val pages: Int = ceil(count.toDouble() / 100.0).toInt()
  40.  
  41.                     MainScope().launch {
  42.                         var page: Int = 1
  43.                         displayPage(view, page, text, pages)
  44.                         val next = view.findViewById<Button>(R.id.next)
  45.                         next.setOnClickListener {
  46.                             if (page < pages) {
  47.                                 page += 1
  48.                                 displayPage(view, page, text, pages)
  49.                             }
  50.                         }
  51.                         val prev = view.findViewById<Button>(R.id.prev)
  52.                         prev.setOnClickListener {
  53.                             if (page > 1) {
  54.                                 page -= 1
  55.                                 displayPage(view, page, text, pages)
  56.                             }
  57.                         }
  58.                     }
  59.                 } catch (e: Exception) { Log.i("info_3", "$e") }
  60.             }
  61.         }
  62.         return view
  63.     }
  64.  
  65.     private fun displayPage(view: View, page: Int, url: CharSequence, pages: Int) {
  66.         GlobalScope.launch {
  67.             val tUrl = URL("https://api.github.com/search/repositories?q=${url}&page=${page}")
  68.             Log.i("info_1", "$tUrl")
  69.                 val t = tUrl.readText()
  70.             Log.i("info_2", "$t")
  71.             val json = JSONObject(t)
  72.             val jArray = json.getJSONArray("items")
  73.             val array = MutableList(0) { " " }
  74.             for (i in 0 until jArray.length()) {
  75.                 val o = jArray.getJSONObject(i)
  76.                 val s = o.getString("name")
  77.                 array.add(s)
  78.             }
  79.             MainScope().launch {
  80.                 val pageView = view.findViewById<TextView>(R.id.current_page)
  81.                 pageView.text = "$page / $pages"
  82.                 val listOptions = view.findViewById<ListView>(R.id.list_elements)
  83.                 listOptions.adapter = ArrayAdapter<String>(context!!,
  84.                     R.layout.elements_list_item,array)
  85.                 listOptions.setOnItemClickListener { parent, view, position, id ->
  86.                     val obj = jArray.getJSONObject(position)
  87.                     val full_name = obj.getString("full_name")
  88.                     val description = obj.getString("description")
  89.                     val p_url = obj.getString("html_url")
  90.                     val lang = obj.getString("language")
  91.                     val avatar = obj.getJSONObject("owner").getString("avatar_url")
  92.                     (mainContext as OnDataListener).onData(full_name, description, p_url, lang, avatar)
  93.                 }
  94.             }
  95.         }
  96.     }
  97.  
  98.     override fun onAttach(context: Context) {
  99.         super.onAttach(context)
  100.         mainContext = context
  101.     }
  102.     private lateinit var mainContext: Context
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement