123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- package fr.forum_thalie.tsumugi.planning
-
- import android.content.Context
- import androidx.lifecycle.MutableLiveData
- import fr.forum_thalie.tsumugi.Async
- import fr.forum_thalie.tsumugi.R
- import fr.forum_thalie.tsumugi.noConnectionValue
- import fr.forum_thalie.tsumugi.weekdays
- import org.json.JSONObject
- import java.io.IOException
- import java.net.URL
- import java.util.*
- import kotlin.collections.ArrayList
-
- class Planning {
-
- val programmes: ArrayList<Programme> = ArrayList()
- private var regularProgramme: String? = null
- val currentProgramme: MutableLiveData<String> = MutableLiveData()
- val isDataFetched: MutableLiveData<Boolean> = MutableLiveData()
-
- var timeZone: TimeZone = Calendar.getInstance().timeZone
-
- private fun findCurrentProgramme(): String
- {
- programmes.forEach {
- if (it.isCurrent())
- return it.title
- }
- return regularProgramme ?: noConnectionValue
- }
-
- fun checkProgramme()
- {
- val newProgramme = findCurrentProgramme()
- if (currentProgramme.value != newProgramme)
- currentProgramme.value = newProgramme
- }
-
- fun parseUrl(url: String? = null, context: Context? = null)
- {
- val scrape : (Any?) -> String = {
- if (url.isNullOrEmpty() && context != null)
- {
- val json: String
- try {
- val inputStream = context.assets.open("planning_example.json")
- val size = inputStream.available()
- val buffer = ByteArray(size)
- inputStream.use { it.read(buffer) }
- json = String(buffer)
- json
- } catch (ioException: IOException) {
- ioException.printStackTrace()
- ""
- }
- }
- else
- URL(url).readText()
-
- }
- val post : (parameter: Any?) -> Unit = {
- val result = JSONObject(it as String)
- if (result.has("planning"))
- {
- val programList = result.getJSONArray("planning")
- programmes.clear()
- for (i in 0 until programList.length())
- {
- val item = programList[i] as JSONObject
- var periodicityDec = item.getInt("periodicity")
- var periodicity = 0b0000000
- var po = 1000000
- for (j in 0 until weekdays.size)
- {
- if (periodicityDec / (po) > 0)
- {
- periodicityDec -= po
- periodicity += 1 shl (weekdays.size-1 - j)
- }
- po /= 10
- }
- val hourBeginS = item.getString("hour_begin").split(":")
- val hourBegin = hourBeginS.first().toInt()*60 + hourBeginS.last().toInt()
- val hourEndS = item.getString("hour_end").split(":")
- val hourEnd = hourEndS.first().toInt()* 60 + hourEndS.last().toInt()
- val title = item.getString("title")
- programmes.add(Programme(title, periodicity, hourBegin, hourEnd))
- }
- }
- regularProgramme = if (result.has("regular_programme"))
- result.getString("regular_programme")
- else
- context?.getString(R.string.regular_programme)
-
- timeZone = if (result.has("timezone"))
- TimeZone.getTimeZone(result.getString("timezone"))
- else
- Calendar.getInstance().timeZone
-
- isDataFetched.value = true
- }
- Async(scrape, post)
- }
-
- companion object {
- val instance by lazy {
- Planning()
- }
- }
- }
|