Song.kt 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package io.r_a_d.radio2.playerstore
  2. import androidx.lifecycle.MutableLiveData
  3. class Song(artistTitle: String = "", _id : Int = 0, _isRequestable : Boolean = false) {
  4. // TODO : remove MutableLiveData, use a MutableLiveData Boolean on PlayerStore instead
  5. val title: MutableLiveData<String> = MutableLiveData()
  6. val artist: MutableLiveData<String> = MutableLiveData()
  7. val type: MutableLiveData<Int> = MutableLiveData()
  8. val startTime: MutableLiveData<Long> = MutableLiveData()
  9. val stopTime: MutableLiveData<Long> = MutableLiveData()
  10. var id: Int? = _id
  11. var isRequestable : Boolean = _isRequestable
  12. init {
  13. setTitleArtist(artistTitle)
  14. type.value = 0
  15. startTime.value = System.currentTimeMillis()
  16. stopTime.value = System.currentTimeMillis() + 1000
  17. }
  18. override fun toString() : String {
  19. return "id=$id | ${artist.value} - ${title.value} | type=${type.value} | times ${startTime.value} - ${stopTime.value}\n"
  20. }
  21. fun setTitleArtist(data: String)
  22. {
  23. val hyphenPos = data.indexOf(" - ")
  24. try {
  25. if (hyphenPos < 0)
  26. throw ArrayIndexOutOfBoundsException()
  27. if (artist.value != data.substring(0, hyphenPos))
  28. artist.value = data.substring(0, hyphenPos)
  29. if (title.value != data.substring(hyphenPos + 3))
  30. title.value = data.substring(hyphenPos + 3)
  31. } catch (e: Exception) {
  32. if (artist.value != "")
  33. artist.value = ""
  34. if (title.value != data)
  35. title.value = data
  36. }
  37. }
  38. override fun equals(other: Any?) : Boolean
  39. {
  40. val song: Song = other as Song
  41. return this.title.value == song.title.value && this.artist.value == song.artist.value
  42. }
  43. fun copy(song: Song) {
  44. this.title.value = song.title.value
  45. this.artist.value = song.artist.value
  46. this.startTime.value = song.startTime.value
  47. this.stopTime.value = song.stopTime.value
  48. this.type.value = song.type.value
  49. }
  50. }