Untitled
unknown
kotlin
4 years ago
7.4 kB
16
Indexable
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.Settings
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Toast
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.loopj.android.http.AsyncHttpClient
import com.loopj.android.http.AsyncHttpResponseHandler
import cz.msebera.android.httpclient.Header
import kotlinx.android.synthetic.main.activity_main.*
import org.json.JSONArray
import org.json.JSONObject
class MainActivity : AppCompatActivity() {
private var listData: ArrayList<User> = ArrayList()
private lateinit var adapter: UserAdapter
companion object {
private val TAG = MainActivity::class.java.simpleName
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
adapter = UserAdapter(listData)
setupRecyclerView()
getUser()
}
private fun getUser() {
progressBar.visibility = View.VISIBLE
val client = AsyncHttpClient()
client.addHeader("User-Agent", "request")
client.addHeader("Authorization", "token GANTI_DENGAN_API_KEY")
val url = "https://api.github.com/users"
client.get(url, object : AsyncHttpResponseHandler() {
override fun onSuccess(
statusCode: Int,
headers: Array<Header>,
responseBody: ByteArray
) {
progressBar.visibility = View.INVISIBLE
val result = String(responseBody)
Log.d(TAG, result)
try {
val jsonArray = JSONArray(result)
for (i in 0 until jsonArray.length()) {
val jsonObject = jsonArray.getJSONObject(i)
val username: String = jsonObject.getString("login")
// Melakukan pemanggilan API sekali lagi untuk mendapatkan detail user
getUserDetail(username)
}
} catch (e: Exception) {
Toast.makeText(this@MainActivity, e.message, Toast.LENGTH_SHORT)
.show()
e.printStackTrace()
}
}
override fun onFailure(
statusCode: Int,
headers: Array<Header>,
responseBody: ByteArray,
error: Throwable
) {
progressBar.visibility = View.INVISIBLE
val errorMessage = when (statusCode) {
401 -> "$statusCode : Bad Request"
403 -> "$statusCode : Forbidden"
404 -> "$statusCode : Not Found"
else -> "$statusCode : ${error.message}"
}
Toast.makeText(this@MainActivity, errorMessage, Toast.LENGTH_LONG)
.show()
}
})
}
private fun getUserDetail(username: String) {
progressBar.visibility = View.VISIBLE
val client = AsyncHttpClient()
client.addHeader("User-Agent", "request")
client.addHeader("Authorization", "token GANTI_DENGAN_API_KEY")
val url = "https://api.github.com/users/$username"
client.get(url, object : AsyncHttpResponseHandler() {
override fun onSuccess(
statusCode: Int,
headers: Array<Header>,
responseBody: ByteArray
) {
progressBar.visibility = View.INVISIBLE
val result = String(responseBody)
try {
val jsonObject = JSONObject(result)
val username: String? = jsonObject.getString("login").toString()
val name: String? = jsonObject.getString("name").toString()
val avatar: String? = jsonObject.getString("avatar_url").toString()
val company: String? = jsonObject.getString("company").toString()
val location: String? = jsonObject.getString("location").toString()
val repository: String? = jsonObject.getString("public_repos")
val followers: String? = jsonObject.getString("followers")
val following: String? = jsonObject.getString("following")
listData.add(
User(
username,
name,
avatar,
company,
location,
repository,
followers,
following
)
)
showRecyclerView()
} catch (e: Exception) {
Toast.makeText(this@MainActivity, e.message, Toast.LENGTH_SHORT)
.show()
e.printStackTrace()
}
}
override fun onFailure(
statusCode: Int,
headers: Array<Header>,
responseBody: ByteArray,
error: Throwable
) {
progressBar.visibility = View.INVISIBLE
val errorMessage = when (statusCode) {
401 -> "$statusCode : Bad Request"
403 -> "$statusCode : Forbidden"
404 -> "$statusCode : Not Found"
else -> "$statusCode : ${error.message}"
}
Toast.makeText(this@MainActivity, errorMessage, Toast.LENGTH_LONG)
.show()
}
})
}
private fun setupRecyclerView() {
recycleView.layoutManager = LinearLayoutManager(recycleView.context)
recycleView.setHasFixedSize(true)
recycleView.addItemDecoration(
DividerItemDecoration(
recycleView.context,
DividerItemDecoration.VERTICAL
)
)
}
private fun showRecyclerView() {
recycleView.layoutManager = LinearLayoutManager(this)
val listDataAdapter = UserAdapter(userFilterList)
recycleView.adapter = adapter
listDataAdapter.setOnItemClickCallback(object : UserAdapter.OnItemClickCallback {
override fun onItemClicked(user: User) {
showSelectedUser(user)
}
})
}
private fun showSelectedUser(user: User) {
UserData(
user.username,
user.name,
user.avatar,
user.company,
user.location,
user.repository,
user.followers,
user.following
)
val intent = Intent(this@MainActivity, UserDetail::class.java)
intent.putExtra(UserDetail.EXTRA_DATA, user)
this@MainActivity.startActivity(intent)
}
}Editor is loading...