Ver código fonte

fix(card): tighten music card lyric semantics

onecold 4 meses atrás
pai
commit
6d114f34e1

+ 54 - 86
entry/src/main/ets/common/player/MusicCardSnapshot.ets

@@ -33,55 +33,21 @@ export interface MusicCardLyricLines {
 }
 
 export class MusicCardBindingData {
-  title: string
-  artist: string
-  coverPath: string
-  hasCoverImage: boolean
-  hasSong: boolean
-  isPlaying: boolean
-  currentPositionMs: number
-  durationMs: number
-  currentTimeText: string
-  durationTimeText: string
-  lyricLine1: string
-  lyricLine2: string
-  hasLyric: boolean
-  filePath: string
-  updatedAtMs: number
-
-  constructor(
-    title: string,
-    artist: string,
-    coverPath: string,
-    hasCoverImage: boolean,
-    hasSong: boolean,
-    isPlaying: boolean,
-    currentPositionMs: number,
-    durationMs: number,
-    currentTimeText: string,
-    durationTimeText: string,
-    lyricLine1: string,
-    lyricLine2: string,
-    hasLyric: boolean,
-    filePath: string,
-    updatedAtMs: number
-  ) {
-    this.title = title
-    this.artist = artist
-    this.coverPath = coverPath
-    this.hasCoverImage = hasCoverImage
-    this.hasSong = hasSong
-    this.isPlaying = isPlaying
-    this.currentPositionMs = currentPositionMs
-    this.durationMs = durationMs
-    this.currentTimeText = currentTimeText
-    this.durationTimeText = durationTimeText
-    this.lyricLine1 = lyricLine1
-    this.lyricLine2 = lyricLine2
-    this.hasLyric = hasLyric
-    this.filePath = filePath
-    this.updatedAtMs = updatedAtMs
-  }
+  title: string = MUSIC_CARD_EMPTY_TITLE
+  artist: string = MUSIC_CARD_EMPTY_ARTIST
+  coverPath: string = ''
+  hasCoverImage: boolean = false
+  hasSong: boolean = false
+  isPlaying: boolean = false
+  currentPositionMs: number = 0
+  durationMs: number = 0
+  currentTimeText: string = DEFAULT_TIME_TEXT
+  durationTimeText: string = DEFAULT_TIME_TEXT
+  lyricLine1: string = MUSIC_CARD_EMPTY_TITLE
+  lyricLine2: string = MUSIC_CARD_EMPTY_ARTIST
+  hasLyric: boolean = false
+  filePath: string = ''
+  updatedAtMs: number = 0
 }
 
 export function createEmptyMusicCardSnapshot(): MusicCardSnapshot {
@@ -179,45 +145,47 @@ export function resolveMusicCardLyricLines(
 export function buildMusicCardBindingData(snapshot: MusicCardSnapshot): MusicCardBindingData {
   const source = snapshot || createEmptyMusicCardSnapshot()
   const lyricLines = resolveMusicCardLyricLines(source.lyricText, source.currentPositionMs)
-  const hasSong = Boolean(source.hasSong)
-  const title =
-    hasSong && isNonEmpty(source.title) ? source.title : MUSIC_CARD_EMPTY_TITLE
-  const artist =
-    hasSong && isNonEmpty(source.artist) ? source.artist : MUSIC_CARD_EMPTY_ARTIST
-  const lyricLine1 =
-    lyricLines.hasLyric && isNonEmpty(lyricLines.line1) ? lyricLines.line1 : title
-  const lyricLine2 =
-    lyricLines.hasLyric && isNonEmpty(lyricLines.line2) ? lyricLines.line2 : artist
-  const hasLyric = lyricLines.hasLyric || Boolean(source.hasLyric)
-  const currentTimeText = isNonEmpty(source.currentTimeText)
+  const data = new MusicCardBindingData()
+
+  data.hasSong = Boolean(source.hasSong)
+  data.title =
+    data.hasSong && isNonEmpty(source.title) ? source.title : MUSIC_CARD_EMPTY_TITLE
+  data.artist =
+    data.hasSong && isNonEmpty(source.artist) ? source.artist : MUSIC_CARD_EMPTY_ARTIST
+  data.coverPath = source.coverPath ?? ''
+  data.hasCoverImage = Boolean(source.hasCoverImage)
+  data.isPlaying = Boolean(source.isPlaying)
+  data.currentPositionMs = source.currentPositionMs ?? 0
+  data.durationMs = source.durationMs ?? 0
+  data.currentTimeText = isNonEmpty(source.currentTimeText)
     ? source.currentTimeText
     : DEFAULT_TIME_TEXT
-  const durationTimeText = isNonEmpty(source.durationTimeText)
+  data.durationTimeText = isNonEmpty(source.durationTimeText)
     ? source.durationTimeText
     : DEFAULT_TIME_TEXT
-  const coverPath = source.coverPath ?? ''
-  const hasCoverImage = Boolean(source.hasCoverImage)
-  const isPlaying = Boolean(source.isPlaying)
-  const currentPositionMs = source.currentPositionMs ?? 0
-  const durationMs = source.durationMs ?? 0
-  const filePath = source.filePath ?? ''
-  const updatedAtMs = source.updatedAtMs ?? 0
-
-  return new MusicCardBindingData(
-    title,
-    artist,
-    coverPath,
-    hasCoverImage,
-    hasSong,
-    isPlaying,
-    currentPositionMs,
-    durationMs,
-    currentTimeText,
-    durationTimeText,
-    lyricLine1,
-    lyricLine2,
-    hasLyric,
-    filePath,
-    updatedAtMs
-  )
+  data.filePath = source.filePath ?? ''
+  data.updatedAtMs = source.updatedAtMs ?? 0
+
+  let lyricLine1 = data.title
+  let lyricLine2 = data.artist
+  let hasLyric = false
+
+  if (lyricLines.hasLyric) {
+    lyricLine1 = lyricLines.line1
+    lyricLine2 = lyricLines.line2
+    hasLyric = true
+  } else if (
+    source.hasLyric &&
+    (isNonEmpty(source.lyricLine1) || isNonEmpty(source.lyricLine2))
+  ) {
+    lyricLine1 = isNonEmpty(source.lyricLine1) ? source.lyricLine1 : data.title
+    lyricLine2 = isNonEmpty(source.lyricLine2) ? source.lyricLine2 : data.artist
+    hasLyric = true
+  }
+
+  data.lyricLine1 = lyricLine1
+  data.lyricLine2 = lyricLine2
+  data.hasLyric = hasLyric
+
+  return data
 }

+ 53 - 0
entry/src/ohosTest/ets/test/MusicCardSnapshot.test.ets

@@ -25,5 +25,58 @@ export default function musicCardSnapshotTest() {
       expect(binding.lyricLine1).assertEqual('未在播放')
       expect(binding.lyricLine2).assertEqual('点击打开播放器')
     })
+
+    it('buildMusicCardBindingDataMapsMediaFields', 0, () => {
+      const snapshot = createEmptyMusicCardSnapshot()
+      snapshot.hasSong = true
+      snapshot.title = 'Song Title'
+      snapshot.artist = 'Song Artist'
+      snapshot.coverPath = '/cover.png'
+      snapshot.hasCoverImage = true
+      snapshot.isPlaying = true
+      snapshot.currentTimeText = '00:12'
+      snapshot.durationTimeText = '03:45'
+      snapshot.filePath = '/storage/song.mp3'
+      snapshot.updatedAtMs = 123456
+
+      const binding = buildMusicCardBindingData(snapshot)
+
+      expect(binding.coverPath).assertEqual('/cover.png')
+      expect(binding.hasCoverImage).assertEqual(true)
+      expect(binding.isPlaying).assertEqual(true)
+      expect(binding.currentTimeText).assertEqual('00:12')
+      expect(binding.durationTimeText).assertEqual('03:45')
+      expect(binding.filePath).assertEqual('/storage/song.mp3')
+      expect(binding.updatedAtMs).assertEqual(123456)
+      expect(binding.hasSong).assertEqual(true)
+      expect(binding.title).assertEqual('Song Title')
+      expect(binding.artist).assertEqual('Song Artist')
+    })
+
+    it('buildMusicCardBindingDataKeepsSourceLyricsWhenAvailable', 0, () => {
+      const snapshot = createEmptyMusicCardSnapshot()
+      snapshot.hasLyric = true
+      snapshot.lyricLine1 = 'Backline 1'
+      snapshot.lyricLine2 = 'Backline 2'
+
+      const binding = buildMusicCardBindingData(snapshot)
+
+      expect(binding.hasLyric).assertEqual(true)
+      expect(binding.lyricLine1).assertEqual('Backline 1')
+      expect(binding.lyricLine2).assertEqual('Backline 2')
+    })
+
+    it('buildMusicCardBindingDataFallsBackWhenLyricLinesEmpty', 0, () => {
+      const snapshot = createEmptyMusicCardSnapshot()
+      snapshot.hasLyric = true
+      snapshot.lyricLine1 = ''
+      snapshot.lyricLine2 = ''
+
+      const binding = buildMusicCardBindingData(snapshot)
+
+      expect(binding.hasLyric).assertEqual(false)
+      expect(binding.lyricLine1).assertEqual('未在播放')
+      expect(binding.lyricLine2).assertEqual('点击打开播放器')
+    })
   })
 }