Song.kt 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package fr.forum_thalie.tsumugi.playerstore
  2. import androidx.lifecycle.MutableLiveData
  3. import fr.forum_thalie.tsumugi.noConnectionValue
  4. class Song(artistTitle: String = "", _id : Int = 0) {
  5. // TODO : remove MutableLiveData, use a MutableLiveData Boolean on PlayerStore instead
  6. val title: MutableLiveData<String> = MutableLiveData()
  7. val artist: MutableLiveData<String> = MutableLiveData()
  8. val type: MutableLiveData<Int> = MutableLiveData()
  9. val startTime: MutableLiveData<Long> = MutableLiveData()
  10. val stopTime: MutableLiveData<Long> = MutableLiveData()
  11. var id: Int? = _id
  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. // else : do nothing
  37. }
  38. }
  39. override fun equals(other: Any?) : Boolean
  40. {
  41. val song: Song = other as Song
  42. return this.title.value == song.title.value && this.artist.value == song.artist.value
  43. }
  44. fun copy(song: Song) {
  45. this.title.value = song.title.value
  46. this.artist.value = song.artist.value
  47. this.startTime.value = song.startTime.value
  48. this.stopTime.value = song.stopTime.value
  49. this.type.value = song.type.value
  50. }
  51. override fun hashCode(): Int {
  52. var result = title.hashCode()
  53. result = 31 * result + artist.hashCode()
  54. result = 31 * result + type.hashCode()
  55. result = 31 * result + startTime.hashCode()
  56. result = 31 * result + stopTime.hashCode()
  57. result = 31 * result + (id ?: 0)
  58. return result
  59. }
  60. }