uscode

Untitled

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