Planning.kt 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package fr.riff_app.riff.planning
  2. import android.content.Context
  3. import androidx.lifecycle.MutableLiveData
  4. import fr.riff_app.riff.Async
  5. import fr.riff_app.riff.R
  6. import fr.riff_app.riff.noConnectionValue
  7. import fr.riff_app.riff.weekdays
  8. import org.json.JSONObject
  9. import java.io.IOException
  10. import java.net.URL
  11. import java.util.*
  12. import kotlin.collections.ArrayList
  13. class Planning {
  14. val programmes: ArrayList<Programme> = ArrayList()
  15. private var regularProgramme: String? = null
  16. val currentProgramme: MutableLiveData<String> = MutableLiveData()
  17. val isDataFetched: MutableLiveData<Boolean> = MutableLiveData()
  18. var timeZone: TimeZone = Calendar.getInstance().timeZone
  19. private fun findCurrentProgramme(): String
  20. {
  21. programmes.forEach {
  22. if (it.isCurrent())
  23. return it.title
  24. }
  25. return regularProgramme ?: noConnectionValue
  26. }
  27. fun checkProgramme()
  28. {
  29. val newProgramme = findCurrentProgramme()
  30. if (currentProgramme.value != newProgramme)
  31. currentProgramme.value = newProgramme
  32. }
  33. fun parseUrl(url: String? = null, context: Context? = null)
  34. {
  35. val scrape : (Any?) -> String = {
  36. if (url.isNullOrEmpty() && context != null)
  37. {
  38. val json: String
  39. try {
  40. val inputStream = context.assets.open("planning_example.json")
  41. val size = inputStream.available()
  42. val buffer = ByteArray(size)
  43. inputStream.use { it.read(buffer) }
  44. json = String(buffer)
  45. json
  46. } catch (ioException: IOException) {
  47. ioException.printStackTrace()
  48. ""
  49. }
  50. }
  51. else
  52. URL(url).readText()
  53. }
  54. val post : (parameter: Any?) -> Unit = {
  55. val result = JSONObject(it as String)
  56. if (result.has("planning"))
  57. {
  58. val programList = result.getJSONArray("planning")
  59. programmes.clear()
  60. for (i in 0 until programList.length())
  61. {
  62. val item = programList[i] as JSONObject
  63. var periodicityDec = item.getInt("periodicity")
  64. var periodicity = 0b0000000
  65. var po = 1000000
  66. for (j in 0 until weekdays.size)
  67. {
  68. if (periodicityDec / (po) > 0)
  69. {
  70. periodicityDec -= po
  71. periodicity += 1 shl (weekdays.size-1 - j)
  72. }
  73. po /= 10
  74. }
  75. val hourBeginS = item.getString("hour_begin").split(":")
  76. val hourBegin = hourBeginS.first().toInt()*60 + hourBeginS.last().toInt()
  77. val hourEndS = item.getString("hour_end").split(":")
  78. val hourEnd = hourEndS.first().toInt()* 60 + hourEndS.last().toInt()
  79. val title = item.getString("title")
  80. programmes.add(Programme(title, periodicity, hourBegin, hourEnd))
  81. }
  82. }
  83. regularProgramme = if (result.has("regular_programme"))
  84. result.getString("regular_programme")
  85. else
  86. context?.getString(R.string.regular_programme)
  87. timeZone = if (result.has("timezone"))
  88. TimeZone.getTimeZone(result.getString("timezone"))
  89. else
  90. Calendar.getInstance().timeZone
  91. isDataFetched.value = true
  92. }
  93. Async(scrape, post)
  94. }
  95. companion object {
  96. val instance by lazy {
  97. Planning()
  98. }
  99. }
  100. }