Skip to content

Commit

Permalink
Merge pull request #1901 from quran/fix_jump_to_translation
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedre authored Mar 2, 2022
2 parents 6882d7f + 056a68b commit 3705345
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class AyahTranslationTrackerItem(
}

override fun onUnHighlightAyahType(type: HighlightType) {
ayahView.unhighlightAyat()
ayahView.unhighlightAyat(type)
}

override fun getToolBarPosition(page: Int, sura: Int, ayah: Int): SelectionIndicator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.quran.data.core.QuranInfo;
import com.quran.data.model.SuraAyah;
import com.quran.data.model.selection.AyahSelection;
import com.quran.data.model.selection.SelectionIndicator;
import com.quran.labs.androidquran.common.LocalTranslation;
import com.quran.labs.androidquran.common.QuranAyahInfo;
import com.quran.labs.androidquran.data.QuranDisplayData;
Expand All @@ -28,15 +30,12 @@
import com.quran.labs.androidquran.ui.util.PageController;
import com.quran.labs.androidquran.util.QuranSettings;
import com.quran.labs.androidquran.view.QuranTranslationPageLayout;

import com.quran.reading.common.ReadingEventPresenter;

import java.util.List;

import javax.inject.Inject;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

public class TranslationFragment extends Fragment implements
AyahTrackerPresenter.AyahInteractionHandler, QuranPage,
TranslationPresenter.TranslationScreen, PageController {
Expand Down Expand Up @@ -115,11 +114,13 @@ public void updateView() {
}
}

@NonNull
@Override
public AyahTracker getAyahTracker() {
return ayahTrackerPresenter;
}

@NonNull
@Override
public AyahTrackerItem[] getAyahTrackerItems() {
if (ayahTrackerItems == null) {
Expand Down Expand Up @@ -168,8 +169,9 @@ public void onSaveInstanceState(@NonNull Bundle outState) {
}

@Override
public boolean handleTouchEvent(MotionEvent event,
AyahSelectedListener.EventType eventType, int page) {
public boolean handleTouchEvent(@NonNull MotionEvent event,
@NonNull AyahSelectedListener.EventType eventType,
int page) {
return false;
}

Expand All @@ -194,7 +196,7 @@ public void onScrollChanged(float y) {
}

@Override
public void handleLongPress(SuraAyah suraAyah) {
public void handleLongPress(@NonNull SuraAyah suraAyah) {
if (isVisible()) {
readingEventPresenter.onAyahSelection(
new AyahSelection.Ayah(suraAyah, translationView.getToolbarPosition(suraAyah.sura, suraAyah.ayah))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ object HighlightTypes {

@JvmField
val SELECTION = HighlightType(1, R.color.selection_highlight, HIGHLIGHT, isSingle = true)
@JvmField
val AUDIO = HighlightType(2, R.color.audio_highlight, HIGHLIGHT, isSingle = true, isTransitionAnimated = true)
val NOTE = HighlightType(3, R.color.note_highlight, HIGHLIGHT)
@JvmField
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ internal class TranslationAdapter(
expandedTafseerAyahs.clear()
this.data.addAll(data)
if (highlightedAyah > 0) {
highlightAyah(highlightedAyah, false, highlightType ?: HighlightTypes.SELECTION)
highlightAyah(highlightedAyah, true, highlightType ?: HighlightTypes.SELECTION, true)
}
}

Expand All @@ -123,8 +123,8 @@ internal class TranslationAdapter(
return (matches.firstOrNull()?.index ?: -1) to matches.size
}

private fun highlightAyah(ayahId: Int, notify: Boolean, highlightedType: HighlightType) {
if (ayahId != highlightedAyah) {
private fun highlightAyah(ayahId: Int, notify: Boolean, highlightedType: HighlightType, force: Boolean = false) {
if (ayahId != highlightedAyah || force) {
val matches = data.withIndex().filter { it.value.ayahInfo.ayahId == ayahId }
val (startPosition, count) = (matches.firstOrNull()?.index ?: -1) to matches.size

Expand Down Expand Up @@ -157,7 +157,7 @@ internal class TranslationAdapter(
recyclerView.handler.post {
notifyItemRangeChanged(startChangeRange, startChangeCount, HIGHLIGHT_CHANGE)
val layoutManager = recyclerView.layoutManager
if (highlightedType == HighlightTypes.AUDIO && layoutManager is LinearLayoutManager) {
if ((force || highlightedType == HighlightTypes.AUDIO) && layoutManager is LinearLayoutManager) {
layoutManager.scrollToPositionWithOffset(startPosition, 64)
} else {
recyclerView.smoothScrollToPosition(startPosition)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class TranslationView extends FrameLayout implements View.OnClickListener
private final TranslationAdapter translationAdapter;

private SuraAyah selectedAyah;
private int selectedAyahId = -1;
private OnClickListener onClickListener;
private final LinearLayoutManager layoutManager;
private PageController pageController;
Expand Down Expand Up @@ -165,10 +166,18 @@ public void setTranslationClickedListener(OnClickListener listener) {
public void highlightAyah(SuraAyah suraAyah, int ayahId, HighlightType highlightType) {
if (highlightType == HighlightTypes.SELECTION) {
selectedAyah = suraAyah;
selectedAyahId = ayahId;
} else if (selectedAyah != null) {
hideMenu();
}
translationAdapter.setHighlightedAyah(ayahId, highlightType);

if (shouldHandleHighlightType(highlightType)) {
translationAdapter.setHighlightedAyah(ayahId, highlightType);
}
}

private boolean shouldHandleHighlightType(HighlightType highlightType) {
return highlightType == HighlightTypes.AUDIO || highlightType == HighlightTypes.SELECTION;
}

private void hideMenu() {
Expand All @@ -178,22 +187,39 @@ private void hideMenu() {
public void unhighlightAyah(HighlightType highlightType) {
if (highlightType == HighlightTypes.SELECTION) {
selectedAyah = null;
selectedAyahId = -1;
}

if (shouldHandleHighlightType(highlightType)) {
translationAdapter.unhighlight();
if (selectedAyah != null) {
// imples that it's not selection, so let's reselect the selected ayah
translationAdapter.setHighlightedAyah(selectedAyahId, HighlightTypes.SELECTION);
}
}
translationAdapter.unhighlight();
}

public void unhighlightAyat() {
if (selectedAyah != null) {
public void unhighlightAyat(HighlightType highlightType) {
if (selectedAyah != null && highlightType == HighlightTypes.SELECTION) {
selectedAyah = null;
selectedAyahId = -1;
}

if (shouldHandleHighlightType(highlightType)) {
translationAdapter.unhighlight();
if (selectedAyah != null) {
// imples that it's not selection, so let's reselect the selected ayah
translationAdapter.setHighlightedAyah(selectedAyahId, HighlightTypes.SELECTION);
}
}
translationAdapter.unhighlight();
}

@Override
public void onClick(View v) {
if (selectedAyah != null) {
hideMenu();
selectedAyah = null;
selectedAyahId = -1;
return;
}

Expand Down

0 comments on commit 3705345

Please sign in to comment.