Advertisement
rifki_cs29

viewmodel

Apr 5th, 2021
1,082
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.76 KB | None | 0 0
  1. package com.rifki.kotlin.mygithubapi.viewModel
  2.  
  3. import android.util.Log
  4. import androidx.lifecycle.LiveData
  5. import androidx.lifecycle.MutableLiveData
  6. import androidx.lifecycle.ViewModel
  7. import com.loopj.android.http.AsyncHttpClient
  8. import com.loopj.android.http.AsyncHttpResponseHandler
  9. import com.rifki.kotlin.mygithubapi.BuildConfig
  10. import com.rifki.kotlin.mygithubapi.model.GithubUser
  11. import cz.msebera.android.httpclient.Header
  12. import org.json.JSONObject
  13.  
  14. class MainViewModel : ViewModel() {
  15.     private val listGithubUsers = MutableLiveData<ArrayList<GithubUser>>()
  16.     val errorMessage = MutableLiveData<Event<String>>()
  17.  
  18.     fun setSearchGithubUsers(username: String) {
  19.         val listUsers = ArrayList<GithubUser>()
  20.         val client = AsyncHttpClient()
  21.         client.addHeader("Authorization", BuildConfig.GITHUB_API_KEY)
  22.         client.addHeader("User-Agent", "request")
  23.         val url = "https://api.github.com/search/users?q=$username"
  24.         client.get(url, object : AsyncHttpResponseHandler() {
  25.             override fun onSuccess(statusCode: Int, headers: Array<Header>?, responseBody: ByteArray) {
  26.                 val result = String(responseBody)
  27.                 try {
  28.                     val responseObject = JSONObject(result)
  29.                     val jsonArray = responseObject.getJSONArray("items")
  30.                     if (jsonArray.length() == 0){
  31.                         errorMessage.value = Event("Data Not Found")
  32.                     }
  33.                     for (i in 0 until jsonArray.length()) {
  34.                         val user = jsonArray.getJSONObject(i)
  35.                         val githubUser = GithubUser()
  36.                         githubUser.username = user.getString("login")
  37.                         githubUser.avatar = user.getString("avatar_url")
  38.                         githubUser.url = user.getString("html_url")
  39.                         listUsers.add(githubUser)
  40.                     }
  41.                     listGithubUsers.postValue(listUsers)
  42.                 } catch (e: Exception) {
  43.                     e.printStackTrace()
  44.                 }
  45.             }
  46.  
  47.             override fun onFailure(statusCode: Int, headers: Array<Header>?, responseBody: ByteArray?, error: Throwable) {
  48.                 Log.d("onFailure", error.message.toString())
  49.  
  50.                 val message = when (statusCode) {
  51.                     401 -> "$statusCode : Bad Request"
  52.                     403 -> "$statusCode : Forbidden"
  53.                     404 -> "$statusCode : Not Found"
  54.                     else -> "$statusCode : Check Your Connectivity"
  55.                 }
  56.                 errorMessage.value = Event(message)
  57.             }
  58.         })
  59.     }
  60.  
  61.     fun getSearchGithubUsers(): LiveData<ArrayList<GithubUser>> {
  62.         return listGithubUsers
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement