Skip to content

Commit

Permalink
Fix recitation starting from the beginning in the single verse mode
Browse files Browse the repository at this point in the history
  • Loading branch information
faisalcodes committed Jan 20, 2024
1 parent 642bc98 commit a9078bf
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,28 @@ public void onServiceConnected(ComponentName name, IBinder service) {
final int fromVerse;
final int toVerse;
Pair<Integer, Integer> verseRange = mReaderParams.verseRange;
final var isSingleVerse = QuranUtils.doesRangeDenoteSingle(verseRange);

if (QuranUtils.doesRangeDenoteSingle(verseRange)) {
if (isSingleVerse) {
fromVerse = 1;
toVerse = currChapter.getVerseCount();
} else {
fromVerse = verseRange.getFirst();
toVerse = verseRange.getSecond();
}

var playerCurrVerseNo = mPlayerService.getP().getCurrentVerseNo();

if (playerCurrVerseNo == -1) {
// get the first verse of the range (even if it's the single verse mode)
playerCurrVerseNo = verseRange.getFirst();
}

mPlayerService.onChapterChanged(
currChapter.getChapterNumber(),
fromVerse,
toVerse,
mPlayerService.getP().getCurrentVerseNo()
playerCurrVerseNo
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public void jumpToVerse(int chapterNo, int verseNo, boolean invokePlayer) {
}

switch (mReaderParams.readType) {
case READER_READ_TYPE_VERSES: {
case READER_READ_TYPE_VERSES -> {
Pair<Integer, Integer> range = mReaderParams.verseRange;
if (QuranUtils.doesRangeDenoteSingle(range)) {
if (range.getFirst() != verseNo) {
Expand All @@ -303,12 +303,9 @@ public void jumpToVerse(int chapterNo, int verseNo, boolean invokePlayer) {
}
}
}
break;
case READER_READ_TYPE_JUZ:
case READER_READ_TYPE_CHAPTER: {
case READER_READ_TYPE_JUZ, READER_READ_TYPE_CHAPTER -> {
scrollToVerse(chapterNo, verseNo, true);
}
break;
}

mActivity.updateVerseNumber(chapterNo, verseNo);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package com.quranapp.android.reader_managers;

import android.content.Context;

import androidx.annotation.IntDef;
import androidx.annotation.Nullable;

import static com.quranapp.android.reader_managers.ReaderParams.RecyclerItemViewType.BISMILLAH;
import static com.quranapp.android.reader_managers.ReaderParams.RecyclerItemViewType.CHAPTER_INFO;
import static com.quranapp.android.reader_managers.ReaderParams.RecyclerItemViewType.CHAPTER_TITLE;
Expand Down Expand Up @@ -84,16 +82,12 @@ public boolean isSingleVerse() {
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public boolean isVerseInValidRange(int chapterNo, int verseNo) {
QuranMeta quranMeta = mActivity.mQuranMetaRef.get();
switch (readType) {
case READER_READ_TYPE_VERSES:
case READER_READ_TYPE_CHAPTER:
return currChapter.getChapterNumber() == chapterNo && quranMeta.isVerseValid4Chapter(chapterNo,
verseNo);
case READER_READ_TYPE_JUZ:
return quranMeta.isVerseValid4Juz(currJuzNo, chapterNo, verseNo);
}

return false;
return switch (readType) {
case READER_READ_TYPE_VERSES, READER_READ_TYPE_CHAPTER ->
currChapter.getChapterNumber() == chapterNo && quranMeta.isVerseValid4Chapter(chapterNo, verseNo);
case READER_READ_TYPE_JUZ -> quranMeta.isVerseValid4Juz(currJuzNo, chapterNo, verseNo);
default -> false;
};
}

public boolean isPageReaderStyle() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,11 @@ class RecitationPlayerParams() : Parcelable {
}

/**
* @return Returns true if the player has previous verse within the current playable verse range.
* @return Returns true if the player has next verse within the current playable verse range.
*/
fun hasNextVerse(quranMeta: QuranMeta): Boolean {
val currentChapterNo = currentVerse.chapterNo
val currentVerseNo = currentVerse.verseNo
val lastChapterNo = lastVerse.chapterNo
val lastVerseNo = lastVerse.verseNo
val (currentChapterNo, currentVerseNo) = currentVerse
val (lastChapterNo, lastVerseNo) = lastVerse

if (
!QuranMeta.isChapterValid(currentChapterNo) ||
Expand All @@ -196,10 +194,8 @@ class RecitationPlayerParams() : Parcelable {
* @return Returns true if the player has previous verse within the current playable verse range.
*/
fun hasPreviousVerse(quranMeta: QuranMeta): Boolean {
val currentChapterNo = currentVerse.chapterNo
val currentVerseNo = currentVerse.verseNo
val firstChapterNo = firstVerse.chapterNo
val firstVerseNo = firstVerse.verseNo
val (currentChapterNo, currentVerseNo) = currentVerse
val (firstChapterNo, firstVerseNo) = firstVerse

if (
!QuranMeta.isChapterValid(currentChapterNo) ||
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.quranapp.android.utils.services

import android.Manifest.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK
import android.app.Notification
import android.app.PendingIntent
import android.app.Service
Expand All @@ -19,8 +18,21 @@ import androidx.core.app.ServiceCompat
import androidx.core.graphics.drawable.toBitmap
import androidx.core.net.toUri
import androidx.core.util.Pair
import com.google.android.exoplayer2.*
import com.google.android.exoplayer2.Player.*
import com.google.android.exoplayer2.C
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.MediaItem
import com.google.android.exoplayer2.PlaybackException
import com.google.android.exoplayer2.Player
import com.google.android.exoplayer2.Player.DISCONTINUITY_REASON_AUTO_TRANSITION
import com.google.android.exoplayer2.Player.DISCONTINUITY_REASON_SEEK
import com.google.android.exoplayer2.Player.Listener
import com.google.android.exoplayer2.Player.PositionInfo
import com.google.android.exoplayer2.Player.REPEAT_MODE_OFF
import com.google.android.exoplayer2.Player.REPEAT_MODE_ONE
import com.google.android.exoplayer2.Player.STATE_BUFFERING
import com.google.android.exoplayer2.Player.STATE_ENDED
import com.google.android.exoplayer2.Player.STATE_IDLE
import com.google.android.exoplayer2.Player.STATE_READY
import com.google.android.exoplayer2.audio.AudioAttributes
import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector
import com.google.android.exoplayer2.source.ConcatenatingMediaSource
Expand Down Expand Up @@ -315,6 +327,8 @@ class RecitationService : Service(), MediaDescriptionAdapter {

@Synchronized
fun onChapterChanged(chapterNo: Int, fromVerse: Int, toVerse: Int, currentVerse: Int) {
Log.d(chapterNo, fromVerse, toVerse, currentVerse)

recParams.currentVerse = ChapterVersePair(chapterNo, currentVerse)
recParams.firstVerse = ChapterVersePair(chapterNo, fromVerse)
recParams.lastVerse = ChapterVersePair(chapterNo, toVerse)
Expand Down Expand Up @@ -528,11 +542,14 @@ class RecitationService : Service(), MediaDescriptionAdapter {
if (quranMeta == null) return

if (player.duration > 0 && player.currentPosition < player.duration) {
Log.d("This 1")
if (isPlaying) pauseMedia()
else playMedia()
} else if (!recParams.hasNextVerse(quranMeta!!)) {
Log.d("This 2")
restartRange()
} else {
Log.d("This 3")
restartVerse()
}
}
Expand Down

0 comments on commit a9078bf

Please sign in to comment.