Song.kt 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package fr.riff_app.riff.playerstore
  2. import androidx.core.text.HtmlCompat
  3. import androidx.lifecycle.MutableLiveData
  4. import fr.riff_app.riff.noConnectionValue
  5. class Song(artistTitle: String = "", _id : Int = 0) {
  6. // TODO : remove MutableLiveData, use a MutableLiveData Boolean on PlayerStore instead
  7. val title: MutableLiveData<String> = MutableLiveData()
  8. val artist: MutableLiveData<String> = MutableLiveData()
  9. val type: MutableLiveData<Int> = MutableLiveData()
  10. val startTime: MutableLiveData<Long> = MutableLiveData()
  11. val stopTime: MutableLiveData<Long> = MutableLiveData()
  12. var id: Int? = _id
  13. init {
  14. setTitleArtist(artistTitle)
  15. type.value = 0
  16. startTime.value = System.currentTimeMillis()
  17. stopTime.value = System.currentTimeMillis() + 1000
  18. }
  19. override fun toString() : String {
  20. return "id=$id | ${artist.value} - ${title.value} | type=${type.value} | times ${startTime.value} - ${stopTime.value}\n"
  21. }
  22. fun setTitleArtist(dataHtml: String)
  23. {
  24. val data = HtmlCompat.fromHtml(dataHtml, HtmlCompat.FROM_HTML_MODE_LEGACY).toString()
  25. val hyphenPos = data.indexOf(" - ")
  26. try {
  27. if (hyphenPos < 0)
  28. throw ArrayIndexOutOfBoundsException()
  29. if (artist.value != data.substring(0, hyphenPos))
  30. artist.value = data.substring(0, hyphenPos)
  31. if (title.value != data.substring(hyphenPos + 3))
  32. title.value = data.substring(hyphenPos + 3)
  33. } catch (e: Exception) {
  34. if (artist.value != "")
  35. artist.value = ""
  36. if (title.value != data)
  37. title.value = data
  38. // else : do nothing
  39. }
  40. }
  41. override fun equals(other: Any?) : Boolean
  42. {
  43. val song: Song = other as Song
  44. return this.title.value === song.title.value && this.artist.value === song.artist.value
  45. }
  46. fun copy(song: Song) {
  47. this.artist.value = song.artist.value
  48. this.title. value = song.title.value
  49. this.startTime.value = song.startTime.value
  50. this.stopTime.value = song.stopTime.value
  51. this.type.value = song.type.value
  52. }
  53. override fun hashCode(): Int {
  54. var result = title.hashCode()
  55. result = 31 * result + artist.hashCode()
  56. result = 31 * result + type.hashCode()
  57. result = 31 * result + startTime.hashCode()
  58. result = 31 * result + stopTime.hashCode()
  59. result = 31 * result + (id ?: 0)
  60. return result
  61. }
  62. }