Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.google.android.material.button.MaterialButton;
import com.google.common.util.concurrent.MoreExecutors;

import java.util.ArrayList;
import java.util.List;


Expand Down Expand Up @@ -293,18 +294,18 @@ private void displaySyncedLyrics() {

if (lines == null || lines.isEmpty()) return;

for (Line line : lines) {
lyricsBuilder.append(line.getValue().trim()).append("\n");
}

Line toHighlight = lines.stream().filter(line -> line != null && line.getStart() != null && line.getStart() < timestamp).reduce((first, second) -> second).orElse(null);
List<Line> curLines = getCurerntLyricsLine(lines, lyricsBuilder, timestamp);

if (toHighlight != null) {
if (!curLines.isEmpty()) {
Line toHighlight = curLines.get(0);
String lyrics = lyricsBuilder.toString();
Spannable spannableString = new SpannableString(lyrics);

int startingPosition = getStartPosition(lines, toHighlight);
int endingPosition = startingPosition + toHighlight.getValue().length();
if (curLines.size() >1){
endingPosition = curLines.stream().mapToInt(line -> line.getValue().length() + 1).sum() + startingPosition -1;
}

spannableString.setSpan(new ForegroundColorSpan(requireContext().getResources().getColor(R.color.shadowsLyricsTextColor, null)), 0, lyrics.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(new ForegroundColorSpan(requireContext().getResources().getColor(R.color.lyricsTextColor, null)), startingPosition, endingPosition, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Expand All @@ -318,6 +319,38 @@ private void displaySyncedLyrics() {
}
}

@NonNull
private List<Line> getCurerntLyricsLine(List<Line> lines, StringBuilder lyricsBuilder, int timestamp){
lyricsBuilder.setLength(0);
int currentLineStart = 0;
int firstIndex = 0, lastIndex = 0;
for (int i = 0; i < lines.size(); i++) {
Line line = lines.get(i);
lyricsBuilder.append(line.getValue().trim()).append("\n");

if (line.getStart() == null || lastIndex > 0){
continue;
}

if (line.getStart() < timestamp) {
if (currentLineStart < line.getStart()){
currentLineStart = line.getStart();
firstIndex = i;
}
}else{
lastIndex = i;
}
}
if (lastIndex == 0){
if (firstIndex > 0){
lastIndex = lines.size();
}else{
return new ArrayList<>(0);
}
}
return lines.subList(firstIndex, lastIndex);
}

private int getStartPosition(List<Line> lines, Line toHighlight) {
int start = 0;

Expand Down Expand Up @@ -362,4 +395,4 @@ private int getScroll(List<Line> lines, Line toHighlight) {

return Math.max(scroll, 0);
}
}
}