Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.lb9
- import android.annotation.SuppressLint
- import android.content.Context
- import android.os.Bundle
- import android.util.Log
- import android.view.LayoutInflater
- import android.view.View
- import android.view.ViewGroup
- import android.widget.*
- import androidx.fragment.app.Fragment
- import kotlinx.coroutines.GlobalScope
- import kotlinx.coroutines.MainScope
- import kotlinx.coroutines.launch
- import org.json.JSONObject
- import org.w3c.dom.Text
- import java.lang.Exception
- import java.net.URL
- import kotlin.math.ceil
- class LeftFragment() : Fragment() {
- override fun onCreateView(
- inflater: LayoutInflater,
- container: ViewGroup?,
- savedInstanceState: Bundle?
- ): View? {
- val view = inflater.inflate(R.layout.fragment_left_n, container, false)
- val btn = view.findViewById<Button>(R.id.search_button)
- btn.setOnClickListener{
- val text = view.findViewById<TextView>(R.id.search_field).text ?: "example"
- GlobalScope.launch {
- val tUrl = URL("https://api.github.com/search/repositories?q=${text}")
- try {
- val t = tUrl.readText()
- val json = JSONObject(t)
- val count = json.getInt("total_count")
- val pages: Int = ceil(count.toDouble() / 100.0).toInt()
- MainScope().launch {
- var page: Int = 1
- displayPage(view, page, text, pages)
- val next = view.findViewById<Button>(R.id.next)
- next.setOnClickListener {
- if (page < pages) {
- page += 1
- displayPage(view, page, text, pages)
- }
- }
- val prev = view.findViewById<Button>(R.id.prev)
- prev.setOnClickListener {
- if (page > 1) {
- page -= 1
- displayPage(view, page, text, pages)
- }
- }
- }
- } catch (e: Exception) { Log.i("info_3", "$e") }
- }
- }
- return view
- }
- private fun displayPage(view: View, page: Int, url: CharSequence, pages: Int) {
- GlobalScope.launch {
- val tUrl = URL("https://api.github.com/search/repositories?q=${url}&page=${page}")
- Log.i("info_1", "$tUrl")
- val t = tUrl.readText()
- Log.i("info_2", "$t")
- val json = JSONObject(t)
- val jArray = json.getJSONArray("items")
- val array = MutableList(0) { " " }
- for (i in 0 until jArray.length()) {
- val o = jArray.getJSONObject(i)
- val s = o.getString("name")
- array.add(s)
- }
- MainScope().launch {
- val pageView = view.findViewById<TextView>(R.id.current_page)
- pageView.text = "$page / $pages"
- val listOptions = view.findViewById<ListView>(R.id.list_elements)
- listOptions.adapter = ArrayAdapter<String>(context!!,
- R.layout.elements_list_item,array)
- listOptions.setOnItemClickListener { parent, view, position, id ->
- val obj = jArray.getJSONObject(position)
- val full_name = obj.getString("full_name")
- val description = obj.getString("description")
- val p_url = obj.getString("html_url")
- val lang = obj.getString("language")
- val avatar = obj.getJSONObject("owner").getString("avatar_url")
- (mainContext as OnDataListener).onData(full_name, description, p_url, lang, avatar)
- }
- }
- }
- }
- override fun onAttach(context: Context) {
- super.onAttach(context)
- mainContext = context
- }
- private lateinit var mainContext: Context
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement