ipdan4ik

[lb9] RightFragment.kt

Apr 22nd, 2021
598
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.lb9
  2.  
  3. import android.content.Context
  4. import android.graphics.Bitmap
  5. import android.graphics.BitmapFactory
  6. import android.os.Bundle
  7. import android.view.LayoutInflater
  8. import android.view.View
  9. import android.view.ViewGroup
  10. import android.widget.ImageView
  11. import android.widget.TextView
  12. import androidx.core.net.toUri
  13. import androidx.fragment.app.Fragment
  14. import kotlinx.coroutines.GlobalScope
  15. import kotlinx.coroutines.MainScope
  16. import kotlinx.coroutines.launch
  17. import java.io.BufferedInputStream
  18. import java.io.IOException
  19. import java.net.URL
  20. import java.net.URLConnection
  21.  
  22.  
  23. class RightFragment(): Fragment() {
  24.     override fun onCreateView(
  25.         inflater: LayoutInflater,
  26.         container: ViewGroup?,
  27.         savedInstanceState: Bundle?
  28.     ): View? {
  29.         val view = inflater.inflate(
  30.                 R.layout.fragment_right,
  31.                 container, false
  32.         )
  33.         if (arguments != null) {
  34.             val full_name = arguments?.getString("full_name")
  35.             var v = view.findViewById<TextView>(R.id.textView4)
  36.             v.text = "Репозиторий: ${full_name}"
  37.             val description = arguments?.getString("description")
  38.             v = view.findViewById<TextView>(R.id.textView7)
  39.             v.text = description
  40.             val p_url = arguments?.getString("p_url")
  41.             v = view.findViewById<TextView>(R.id.textView8)
  42.             v.text = "Ссылка: ${p_url}"
  43.             val lang = arguments?.getString("lang")
  44.             v = view.findViewById<TextView>(R.id.textView5)
  45.             v.text = "ЯП: ${lang}"
  46.             val avatar = arguments?.getString("avatar")
  47.             val v2 = view.findViewById<ImageView>(R.id.imageView2)
  48.             GlobalScope.launch {
  49.                 val avatar_url = URL(avatar)
  50.                 val btm = getRemoteImage(avatar_url)
  51.                 MainScope().launch {
  52.                     v2.setImageBitmap(btm)
  53.                 }
  54.             }
  55.  
  56.         } else {
  57.             var v = view.findViewById<TextView>(R.id.textView4)
  58.             v.text = "Репозиторий не выбран"
  59.         }
  60.         return view
  61.     }
  62.  
  63.     private fun getRemoteImage(aURL: URL): Bitmap? {
  64.         try {
  65.             val conn: URLConnection = aURL.openConnection()
  66.             conn.connect()
  67.             val bis = BufferedInputStream(conn.getInputStream())
  68.             val bm = BitmapFactory.decodeStream(bis)
  69.             bis.close()
  70.             return bm
  71.         } catch (e: IOException) {
  72.         }
  73.         return null
  74.     }
  75.  
  76.     override fun onAttach(context: Context) {
  77.         super.onAttach(context)
  78.         mainContext = context
  79.     }
  80.     private lateinit var mainContext: Context
  81.  
  82.  
  83. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×