AzisMM

MenuJadwalSholatActivity.kt

Oct 17th, 2021 (edited)
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.81 KB | None | 0 0
  1. class MenuJadwalSholatActivity : AppCompatActivity() {
  2.     private lateinit var binding: ActivityMenuJadwalSholatBinding
  3.  
  4.     override fun onCreate(savedInstanceState: Bundle?) {
  5.         super.onCreate(savedInstanceState)
  6.         binding = ActivityMenuJadwalSholatBinding.inflate(layoutInflater)
  7.         val view = binding.root
  8.         setContentView(view)
  9.  
  10.         setSupportActionBar(binding.toolbarMenuJadwalSholat)
  11.         initView()
  12.     }
  13.  
  14.     private fun initView() {
  15.         val c: Date = Calendar.getInstance().time
  16.         val df = SimpleDateFormat("E, dd MMM", Locale.getDefault())
  17.         val formattedDate: String = df.format(c)
  18.  
  19.         binding.tvDatePray.text = formattedDate
  20.         initGetDataJadwalSholat(c, "jakarta")
  21.     }
  22.  
  23.     private fun initGetDataJadwalSholat(date: Date, city: String) {
  24.         val df = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
  25.         val formattedDate: String = df.format(date)
  26.  
  27.         val client = AsyncHttpClient()
  28.         val url = "https://api.pray.zone/v2/times/day.json?city=$city&date=$formattedDate"
  29.         client.get(url, object : AsyncHttpResponseHandler() {
  30.             override fun onSuccess(
  31.                 statusCode: Int,
  32.                 headers: Array<out Header>?,
  33.                 responseBody: ByteArray?
  34.             ) {
  35.                 binding.pbJadwalSholat.visibility = View.INVISIBLE
  36.                 val response = responseBody?.let { String(it) }
  37.  
  38.                 try {
  39.                     val responseObject = JSONObject(response)
  40.                     val dataResult = responseObject.getJSONObject("results")
  41.                     val dataDatetimeArray = dataResult.getJSONArray("datetime")
  42.                     val dataObjectDateTime = dataDatetimeArray.getJSONObject(0)
  43.                     val dataObjectTimes = dataObjectDateTime.getJSONObject("times")
  44.  
  45.                     binding.tvPrayTimeImsak.text = dataObjectTimes.getString("Imsak")
  46.                     binding.tvPrayTimeSubuh.text = dataObjectTimes.getString("Fajr")
  47.                     binding.tvPrayTimeSunrise.text = dataObjectTimes.getString("Sunrise")
  48.                     binding.tvPrayTimeDzuhur.text = dataObjectTimes.getString("Dhuhr")
  49.                     binding.tvPrayTimeAshar.text = dataObjectTimes.getString("Asr")
  50.                     binding.tvPrayTimeMaghrib.text = dataObjectTimes.getString("Maghrib")
  51.                     binding.tvPrayTimeIsya.text = dataObjectTimes.getString("Isha")
  52.  
  53.                     val dataObjectLocation = dataResult.getJSONObject("location")
  54.                     binding.tvLocation.text = dataObjectLocation.getString("city")
  55.                 } catch (e: Exception) {
  56.                     Toast.makeText(this@MenuJadwalSholatActivity, e.message, Toast.LENGTH_SHORT)
  57.                         .show()
  58.                     e.printStackTrace()
  59.                 }
  60.             }
  61.  
  62.             override fun onFailure(
  63.                 statusCode: Int,
  64.                 headers: Array<out Header>?,
  65.                 responseBody: ByteArray?,
  66.                 error: Throwable?
  67.             ) {
  68.                 binding.pbJadwalSholat.visibility = View.INVISIBLE
  69.                 val errorMessage = when (statusCode) {
  70.                     401 -> "$statusCode : Bad Request"
  71.                     403 -> "$statusCode : Forbidden"
  72.                     404 -> "$statusCode : Not Found"
  73.                     else -> "$statusCode : ${error?.message}"
  74.                 }
  75.                 Toast.makeText(this@MenuJadwalSholatActivity, errorMessage,
  76.                     Toast.LENGTH_SHORT).show()
  77.             }
  78.  
  79.         })
  80.     }
  81.  
  82.     override fun onOptionsItemSelected(item: MenuItem): Boolean {
  83.         when (item.itemId) {
  84.             android.R.id.home -> {
  85.                 finish()
  86.             }
  87.         }
  88.         return super.onOptionsItemSelected(item)
  89.     }
  90.  
  91. }
Add Comment
Please, Sign In to add comment