uscode

Untitled

Nov 30th, 2020
705
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     package com.resmana.githubuser
  2.  
  3. import android.net.Uri
  4. import androidx.appcompat.app.AppCompatActivity
  5. import android.os.Bundle
  6. import android.util.Log
  7. import android.view.View
  8. import android.widget.Toast
  9. import androidx.lifecycle.Observer
  10. import androidx.lifecycle.ViewModelProviders
  11. import com.bumptech.glide.Glide
  12. import com.bumptech.glide.request.RequestOptions
  13. import com.loopj.android.http.AsyncHttpClient
  14. import com.loopj.android.http.AsyncHttpResponseHandler
  15. import cz.msebera.android.httpclient.Header
  16. import com.resmana.githubuser.db.FavoriteHelper
  17. import com.resmana.githubuser.entity.Users
  18. import com.resmana.githubuser.viewmodel.FavoriteViewModel
  19. import kotlinx.android.synthetic.main.activity_detail.*
  20. import kotlinx.android.synthetic.main.item_user.*
  21. import org.json.JSONObject
  22.  
  23. class DetailActivity : AppCompatActivity() {
  24.  
  25.     private var isFavorite =  false
  26.     private var position: Int = 0
  27.     private lateinit var favoriteHelper: FavoriteHelper
  28.     private lateinit var uriWithId: Uri
  29.     private var users: Users? = null
  30.     private lateinit var favorite: Users
  31.  
  32.     companion object {
  33.         private val TAG = DetailActivity::class.java.simpleName
  34.         const val EXTRA_STATE = "extra_state"
  35.         const val EXTRA_USER = "extra_user"
  36.         const val EXTRA_FAVORITE = "extra_favorite"
  37.         const val EXTRA_POSITION = "extra_position"
  38.         const val REQUEST_INTENT = 100
  39.         const val REQUEST_ADD = 200
  40.         const val REQUEST_DELETE = 300
  41.     }
  42.  
  43.     private val viewModel by lazy{
  44.         ViewModelProviders.of(this).get(FavoriteViewModel::class.java)
  45.     }
  46.  
  47.     override fun onCreate(savedInstanceState: Bundle?) {
  48.         super.onCreate(savedInstanceState)
  49.         setContentView(R.layout.activity_detail)
  50.         title = "Detail User"
  51.  
  52.  
  53.         setStatusFavorite(isFavorite)
  54.         users = intent.getParcelableExtra(EXTRA_USER)
  55.         if (users != null) {
  56.             position =  intent.getIntExtra(EXTRA_FAVORITE, 0)
  57.             viewModel.getFavoriteById(position)
  58.             getDetailUser(users!!.username)
  59.             } else {
  60.             users = Users()
  61.             }
  62.  
  63.         if (isFavorite) {
  64.             val observer = Observer {it : Users ->
  65.                     idGit.text = it.id.toString()
  66.                     Glide.with(this@DetailActivity).load(it.avatar)
  67.                     nameDetail.text = it.name
  68.                     tv_location.text = it.location
  69.                     tv_company.text = it.company
  70.                     users = it
  71.                 }
  72.                 viewModel.favorite.observe(this, observer)
  73.                 setStatusFavorite(true)
  74.             } else {
  75.                 setStatusFavorite(false)
  76.             }
  77.  
  78.         fab_favorite.setOnClickListener {
  79.             isFavorite = !isFavorite
  80.            val favorite = Users(
  81.             null,
  82.             idGit.text.toString(),
  83.             users!!.avatar.toString(),
  84.             userName.text.toString(),
  85.             nameDetail.text.toString(),
  86.             tv_location.text.toString(),
  87.             tv_company.text.toString(),
  88.             followersUsers.text.toString(),
  89.             followingUsers.text.toString()
  90.            )
  91.             viewModel.insertFavorite(favorite)
  92.             Toast.makeText(this, "Add Favorite User", Toast.LENGTH_SHORT).show()
  93.             finish()
  94.             setStatusFavorite(isFavorite)
  95.            }
  96.  
  97.         val sectionsPagerAdapter = SectionsPagerAdapter(this, supportFragmentManager)
  98.         sectionsPagerAdapter.userName = users!!.username
  99.         view_pager.adapter = sectionsPagerAdapter
  100.         tabs.setupWithViewPager(view_pager)
  101.         supportActionBar?.elevation = 0f
  102.     }
  103.  
  104.     private fun setStatusFavorite(isFavorite: Boolean) {
  105.         if (isFavorite) {
  106.             fab_favorite.setImageResource(R.drawable.ic_favorite)
  107.         } else {
  108.             fab_favorite.setImageResource(R.drawable.ic_favorite_border)
  109.         }
  110.     }
  111.  
  112.     private fun getDetailUser(userGit:String?) {
  113.         progressBarDetail.visibility = View.VISIBLE
  114.         val url = "https://api.github.com/users/${userGit}"
  115.         val client = AsyncHttpClient()
  116.         client.addHeader("Authorization","token 66aee8cabb1e59a238deddb718f1a4e742defb20")
  117.         client.addHeader("User-Agent", "request")
  118.         client.get(url, object : AsyncHttpResponseHandler(){
  119.             override fun onSuccess(statusCode: Int, headers: Array<Header>, responseBody: ByteArray){
  120.                 progressBarDetail.visibility = View.INVISIBLE
  121.                val result = String(responseBody)
  122.                 Log.d(TAG, result)
  123.                 try{
  124.                     val responseObject = JSONObject(result)
  125.                     val id = responseObject.getInt("id")
  126.                     val avatarUser = responseObject.getString("avatar_url")
  127.                     val username = responseObject.getString("login")
  128.                     val name = responseObject.getString("name")
  129.                     val company = responseObject.getString("company")
  130.                     val locations = responseObject.getString("location")
  131.                     val followers = responseObject.getInt("followers")
  132.                     val following = responseObject.getInt("following")
  133.  
  134.                     Glide.with(this@DetailActivity)
  135.                         .load(avatarUser)
  136.                         .apply(RequestOptions().override(55, 55))
  137.                         .into(avatar_img)
  138.                     idGit.text = id.toString()
  139.                     userName.text = username
  140.                     nameDetail.text = name
  141.                     tv_company.text = company
  142.                     tv_location.text = locations
  143.                     followersUsers.text = followers.toString()
  144.                     followingUsers.text = following.toString()
  145.                 } catch (e: Exception) {
  146.                     Log.d(TAG, e.message.toString())
  147.                     e.printStackTrace()
  148.                 }
  149.             }
  150.  
  151.             override fun onFailure(statusCode: Int, headers: Array<Header>, responseBody: ByteArray,error: Throwable) {
  152.                 progressBarDetail.visibility = View.INVISIBLE
  153.                 val errorMessage = when(statusCode){
  154.                     401 -> "$statusCode : Bad Request"
  155.                     403 -> "$statusCode : Forbidden"
  156.                     404 -> "$statusCode : Not Found"
  157.                     else -> "$statusCode : ${error.message}"
  158.                 }
  159.                 Toast.makeText(this@DetailActivity, errorMessage, Toast.LENGTH_SHORT).show()
  160.             }
  161.         })
  162.     }
  163. }
RAW Paste Data