LyricsDialog displays lyrics without timestamps
Refactoring
This commit is contained in:
parent
3122923d5f
commit
c047ea96a7
7 changed files with 142 additions and 112 deletions
|
|
@ -2,29 +2,16 @@ package com.kabouzeid.gramophone.model.lyrics;
|
|||
|
||||
import android.util.SparseArray;
|
||||
|
||||
public abstract class AbsSynchronizedLyrics {
|
||||
import com.kabouzeid.gramophone.model.Song;
|
||||
|
||||
public abstract class AbsSynchronizedLyrics extends Lyrics {
|
||||
private static final int TIME_OFFSET_MS = 500; // time adjustment to display line before it actually starts
|
||||
|
||||
public final SparseArray<String> lines = new SparseArray<>();
|
||||
public boolean isValid = false;
|
||||
public int offset = 0;
|
||||
|
||||
/**
|
||||
* @param data Lyrics string
|
||||
* @param justCheck Set isValid = true and stop parsing if lyrics appears to be valid
|
||||
* and has at least 1 line
|
||||
*/
|
||||
public static AbsSynchronizedLyrics parse(String data, boolean justCheck) {
|
||||
return new SynchronizedLyricsLRC(data, justCheck); // no another formats at the moment
|
||||
}
|
||||
|
||||
public static AbsSynchronizedLyrics parse(String data) {
|
||||
return parse(data, false);
|
||||
}
|
||||
|
||||
public static boolean isSynchronized(String data) {
|
||||
AbsSynchronizedLyrics lyrics = parse(data, true);
|
||||
return lyrics.isValid;
|
||||
AbsSynchronizedLyrics(Song song, String data) {
|
||||
super(song, data);
|
||||
}
|
||||
|
||||
public String getLine(int time) {
|
||||
|
|
@ -44,4 +31,31 @@ public abstract class AbsSynchronizedLyrics {
|
|||
|
||||
return lines.get(lastLineTime);
|
||||
}
|
||||
|
||||
public boolean isSynchronized() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
this.parse(true);
|
||||
return this.valid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getText() {
|
||||
if (isValid()) {
|
||||
parse(false);
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for (int i = 0; i < lines.size(); i++) {
|
||||
String line = lines.valueAt(i);
|
||||
sb.append(line).append('\n');
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
return super.getText();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,49 @@
|
|||
package com.kabouzeid.gramophone.model.lyrics;
|
||||
|
||||
import com.kabouzeid.gramophone.model.Song;
|
||||
|
||||
public class Lyrics {
|
||||
public Song song;
|
||||
public String data;
|
||||
|
||||
boolean parsed = false;
|
||||
boolean valid = false;
|
||||
|
||||
public Lyrics(Song song, String data) {
|
||||
this.song = song;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public static Lyrics parse(Song song, String data) {
|
||||
Lyrics lyrics = new SynchronizedLyricsLRC(song, data);
|
||||
if (lyrics.isValid()) {
|
||||
return lyrics.parse(false);
|
||||
} else {
|
||||
return new Lyrics(song, data).parse(false);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isSynchronized(String data) {
|
||||
Lyrics lyrics = new SynchronizedLyricsLRC(null, data);
|
||||
return lyrics.isValid();
|
||||
}
|
||||
|
||||
public Lyrics parse(boolean check) {
|
||||
this.valid = true;
|
||||
this.parsed = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isSynchronized() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
this.parse(true);
|
||||
return this.valid;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return this.data;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +1,11 @@
|
|||
package com.kabouzeid.gramophone.model.lyrics;
|
||||
|
||||
import com.kabouzeid.gramophone.model.Song;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class SynchronizedLyricsLRC extends AbsSynchronizedLyrics {
|
||||
class SynchronizedLyricsLRC extends AbsSynchronizedLyrics {
|
||||
private static final Pattern LRC_LINE_PATTERN = Pattern.compile("((?:\\[.*?\\])+)(.*)");
|
||||
private static final Pattern LRC_TIME_PATTERN = Pattern.compile("\\[(\\d+):(\\d{2}(?:\\.\\d+)?)\\]");
|
||||
private static final Pattern LRC_ATTRIBUTE_PATTERN = Pattern.compile("\\[(\\D+):(.+)\\]");
|
||||
|
|
@ -11,17 +13,17 @@ public class SynchronizedLyricsLRC extends AbsSynchronizedLyrics {
|
|||
private static final float LRC_SECONDS_TO_MS_MULTIPLIER = 1000f;
|
||||
private static final int LRC_MINUTES_TO_MS_MULTIPLIER = 60000;
|
||||
|
||||
/**
|
||||
* @param data Lyrics string
|
||||
* @param justCheck Set isValid = true and stop parsing if lyrics appears to be valid
|
||||
* and has at least 1 line
|
||||
*/
|
||||
public SynchronizedLyricsLRC(String data, boolean justCheck) {
|
||||
if (data == null || data.isEmpty()) {
|
||||
return;
|
||||
SynchronizedLyricsLRC(Song song, String data) {
|
||||
super(song, data);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SynchronizedLyricsLRC parse(boolean check) {
|
||||
if (this.parsed || this.data == null || this.data.isEmpty()) {
|
||||
return this;
|
||||
}
|
||||
|
||||
String[] lines = data.split("\r?\n");
|
||||
String[] lines = this.data.split("\r?\n");
|
||||
|
||||
for (String line : lines) {
|
||||
line = line.trim();
|
||||
|
|
@ -60,14 +62,17 @@ public class SynchronizedLyricsLRC extends AbsSynchronizedLyrics {
|
|||
}
|
||||
int ms = (int) (s * LRC_SECONDS_TO_MS_MULTIPLIER) + m * LRC_MINUTES_TO_MS_MULTIPLIER;
|
||||
|
||||
this.isValid = true;
|
||||
if (justCheck) return;
|
||||
this.valid = true;
|
||||
if (check) return this;
|
||||
|
||||
this.lines.append(ms, text);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.parsed = true;
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue