WallPostActivity.kt 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package com.ethosa.ktc.ui.activities
  2. import android.animation.ObjectAnimator
  3. import android.os.Bundle
  4. import android.view.MenuItem
  5. import androidx.appcompat.app.AppCompatActivity
  6. import androidx.core.text.HtmlCompat
  7. import com.bumptech.glide.Glide
  8. import com.ethosa.ktc.R
  9. import com.ethosa.ktc.college.CollegeApi
  10. import com.ethosa.ktc.college.CollegeCallback
  11. import com.ethosa.ktc.college.news.News
  12. import com.ethosa.ktc.databinding.ActivityWallPostBinding
  13. import com.ethosa.ktc.glide.transformation.CenterInsideBlur
  14. import com.ethosa.ktc.utils.AppDynamicTheme
  15. import com.ethosa.ktc.utils.HtmlImageGetter
  16. import com.google.gson.Gson
  17. import okhttp3.Call
  18. import okhttp3.Response
  19. /**
  20. * Provides Wall post behavior
  21. */
  22. class WallPostActivity : AppCompatActivity(), CollegeCallback {
  23. private lateinit var binding: ActivityWallPostBinding
  24. private val college: CollegeApi = CollegeApi()
  25. override fun onCreate(savedInstanceState: Bundle?) {
  26. super.onCreate(savedInstanceState)
  27. // Setup view binding
  28. binding = ActivityWallPostBinding.inflate(layoutInflater)
  29. setContentView(binding.root)
  30. setSupportActionBar(binding.toolbar)
  31. supportActionBar?.setDisplayHomeAsUpEnabled(true)
  32. AppDynamicTheme(this).loadTheme()
  33. // Loads intent data to toolbarLayout
  34. setSupportActionBar(findViewById(R.id.toolbar))
  35. binding.toolbarLayout.title = intent.getStringExtra("title")
  36. Glide.with(binding.root)
  37. .asBitmap()
  38. .load(intent.getStringExtra("image"))
  39. .transform(CenterInsideBlur(40, 5))
  40. .into(binding.toolbarImage)
  41. // Fetches post data.
  42. college.fetchNewById(intent.getStringExtra("id")!!.toInt(), this)
  43. }
  44. override fun onOptionsItemSelected(item: MenuItem): Boolean {
  45. finish()
  46. return super.onOptionsItemSelected(item)
  47. }
  48. /**
  49. * Fetches a news data
  50. */
  51. override fun onResponse(call: Call, response: Response) {
  52. // Parse JSON
  53. val body = response.body?.string()
  54. val new = Gson().fromJson(body, News::class.java)
  55. // Create animation object
  56. val animate = ObjectAnimator.ofFloat(
  57. binding.content.progressBar, "alpha", 1f, 0f
  58. ).setDuration(500)
  59. // Fix <img/> tag
  60. new.body = new.body.replace(
  61. "src=\"/", "src=\"http://www.kansk-tc.ru/"
  62. )
  63. runOnUiThread {
  64. // Render HTML tags
  65. binding.content.body.text = HtmlCompat.fromHtml(
  66. new.body,
  67. HtmlCompat.FROM_HTML_MODE_LEGACY,
  68. HtmlImageGetter(resources, binding.content.body),
  69. null
  70. )
  71. // cancel progress
  72. animate.start()
  73. }
  74. }
  75. }