gitoria

jukebox

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Branchrecords-day39d6b3e068|||PR Records: day39 live (PR #2), mission 011 PR step, statusmrerecords-day39/lib/remember.hl

3.3 KB

  1. // lib/remember.hl — WHAT THE LISTENER LAST CHOSE, kept in storage/ui.json: the sort of the Songs screen (artist, title, album, plays), and the player's Shuffle (on / off)
  2. // and Repeat (off / all / one), and where each list was scrolled to (per list key, never a row, in pixels from the top: scroll = { 'songs:artist' = 5600, ... }). The screen and the player open the way they were left. Its own file, apart from settings.json (that one holds the library paths and is
  3. // written whole by the settings call). Every write keeps the other keys of the file.
  4. import { readFile, writeFile, exists, mkDir } from 'hl:fs'
  5. import { floor } from 'hl:math'
  6. import { storageRoot, storageDir } from './util.hl'
  7. static uiFile = storageRoot + '/ui.json'
  8. static sorts = ['artist', 'title', 'album', 'plays']
  9. static repeats = ['off', 'all', 'one']
  10. static rowHeight = 56
  11. // everything the file holds ({} when there is no file or it says something else)
  12. static readUi = () => {
  13. if (!exists(uiFile)) { return {} }
  14. text = readFile(uiFile)
  15. if (text == null || !text.trim().startsWith('{')) { return {} }
  16. j = JSON.parse(text)
  17. if (j == null) { return {} }
  18. return j
  19. }
  20. // the whole file again, with the one key changed
  21. static writeUi = (key, value) => {
  22. j = readUi()
  23. j[key] = value
  24. mkDir(storageRoot)
  25. writeFile(uiFile, JSON.stringify(j), 420)
  26. }
  27. // the sort the Songs screen was left with ('artist' when nothing was saved or the file says something else)
  28. static lastSort = () => {
  29. j = readUi()
  30. if (j.sort == null || !sorts.includes(j.sort)) { return 'artist' }
  31. return j.sort
  32. }
  33. // the listener chose a sort: keep it for the next visit; a name that is no sort is ignored
  34. static rememberSort = (sort) => {
  35. if (sort == null || !sorts.includes(sort)) { return null }
  36. writeUi('sort', sort)
  37. return sort
  38. }
  39. // Shuffle as the player was left: 'on' or 'off' (off when nothing was saved)
  40. static lastShuffle = () => {
  41. j = readUi()
  42. return j.shuffle == 'on' ? 'on' : 'off'
  43. }
  44. // Repeat as the player was left: 'off', 'all' or 'one' (off when nothing was saved)
  45. static lastRepeat = () => {
  46. j = readUi()
  47. if (j.repeat == null || !repeats.includes(j.repeat)) { return 'off' }
  48. return j.repeat
  49. }
  50. // the listener pressed Shuffle or Repeat: keep both for the next visit; a value that is none of the names is ignored
  51. static rememberPlayer = (shuffle, repeat) => {
  52. if (shuffle == 'on' || shuffle == 'off') { writeUi('shuffle', shuffle) }
  53. if (repeat != null && repeats.includes(repeat)) { writeUi('repeat', repeat) }
  54. return { shuffle = lastShuffle() repeat = lastRepeat() }
  55. }
  56. // where the list `key` was scrolled to (pixels from its top); 0 when nothing was saved
  57. static lastScroll = (key) => {
  58. j = readUi()
  59. if (j.scroll == null || j.scroll[key] == null) { return 0 }
  60. top = j.scroll[key]
  61. return top > 0 ? top : 0
  62. }
  63. // the listener scrolled the list `key`: keep the offset for the next visit; no key or no number is ignored
  64. static rememberScroll = (key, top) => {
  65. if (key == null || key == '' || top == null || top < 0) { return null }
  66. j = readUi()
  67. scroll = j.scroll == null ? {} : j.scroll
  68. if (scroll[key] == top) { return top }
  69. scroll[key] = top
  70. writeUi('scroll', scroll)
  71. return top
  72. }
  73. // the first row to load so that the rows around a saved offset are in the first chunk (the list asks for more when it needs them)
  74. static startRowFor = (top) => {
  75. start = floor(top / rowHeight) - 20
  76. return start < 0 ? 0 : start
  77. }

Branches

Latest commits

  • d6b3e068|||PR Records: day39 live (PR #2), mission 011 PR step, statusmre
  • 5fa19e89Merge branch 'day39' into mainJukebox
  • cfe95440|||PR day39 re-vendor: Hybriel 51e921d7 (bin 6b481aa7), ids kebab-case since hybriel#256: quoted state rule and tests use jb-shuffle / jb-repeatmre
  • f3614c9bRe-vendor to Hybriel day39 (51e921d7, bin 6b481aa7)mre
  • 9f5c6532Merge branch 'status-22-pr' into mainJukebox
  • 5838d6c7|||PR STATUS: #22 pending decision, PR-only rulemre
  • 29d2029cRecords: PR-only rule (Kunibert)mre
  • adacb0abRecords: neutral ticket rule, #22 decision recordedmre
  • b2d6f121Records: #308 hold liftedmre
  • 7ab67533Records: day39 on hold (hybriel#308)mre
  • 22f21739Records: note rebase before mergemre
  • f7368ea3Records: mission 011 day39, waits for lisbethmre
  • 9515d2cdRecords: jb20 livemre
  • 019f6601ui gate: comment names the open bug components#27 (workaround, not fixed)mre
  • 277a2722README: provenance linemre
  • 4169ca6aREADME: sync pinmre
  • a01f3aa8ui gate: back to the top before the sort checks (the screen reopens where it was left)mre
  • f7936c20Lists keep their scroll position across a reload (jukebox#20): per-list offsets in storage/ui.json, gate jb20mre
  • d67906devirtual-list and icons from components.hybriel e5b98ec (centerId, scrollTop, onScroll)mre
  • df7920ceRecords: R3 startedmre