Skip to content

Commit

Permalink
Improve highlighting behavior in translation view (#1489)
Browse files Browse the repository at this point in the history
* Fix several crashes

* Improve highlighting behavior in translation view

This patch:
- treats audio highlights differently than selection highlights (though
  both aren't enabled at the same time yet). This allows audio
  highlighting to not show a menu, and clicking an ayah to not highlight
  that ayah unless we're already in selection mode.
- clear selection mode when audio highlight overwrites selection
  highlight, and when long pressing an already selected ayah.
  • Loading branch information
ahmedre authored Nov 6, 2020
1 parent cdae0e5 commit 86a3168
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ public AyahTranslationTrackerItem(int page,
@Override
boolean onHighlightAyah(int page, int sura, int ayah, HighlightType type, boolean scrollToAyah) {
if (this.page == page) {
ayahView.highlightAyah(new SuraAyah(sura, ayah), quranInfo.getAyahId(sura, ayah));
ayahView.highlightAyah(new SuraAyah(sura, ayah), quranInfo.getAyahId(sura, ayah), type);
return true;
}
ayahView.unhighlightAyat();
ayahView.unhighlightAyah(type);
return false;
}

@Override
void onUnHighlightAyah(int page, int sura, int ayah, HighlightType type) {
if (this.page == page) {
ayahView.unhighlightAyat();
ayahView.unhighlightAyah(type);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,9 @@ public class TranslationFragment extends Fragment implements
TranslationPresenter.TranslationScreen, PageController {
private static final String PAGE_NUMBER_EXTRA = "pageNumber";

private static final String SI_PAGE_NUMBER = "SI_PAGE_NUMBER";
private static final String SI_HIGHLIGHTED_AYAH = "SI_HIGHLIGHTED_AYAH";
private static final String SI_SCROLL_POSITION = "SI_SCROLL_POSITION";

private int pageNumber;
private int highlightedAyah;
private int scrollPosition;

private TranslationView translationView;
Expand All @@ -71,14 +68,6 @@ public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (savedInstanceState != null) {
int page = savedInstanceState.getInt(SI_PAGE_NUMBER, -1);
if (page == pageNumber) {
int highlightedAyah =
savedInstanceState.getInt(SI_HIGHLIGHTED_AYAH, -1);
if (highlightedAyah > 0) {
this.highlightedAyah = highlightedAyah;
}
}
scrollPosition = savedInstanceState.getInt(SI_SCROLL_POSITION);
}
setHasOptionsMenu(true);
Expand Down Expand Up @@ -156,10 +145,6 @@ public void setVerses(int page,
@NonNull LocalTranslation[] translations,
@NonNull List<QuranAyahInfo> verses) {
translationView.setVerses(quranDisplayData, translations, verses);
if (highlightedAyah > 0) {
translationView.highlightAyah(quranInfo.getSuraAyahFromAyahId(highlightedAyah),
highlightedAyah);
}
}

@Override
Expand All @@ -173,9 +158,6 @@ public void refresh() {

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
if (highlightedAyah > 0) {
outState.putInt(SI_HIGHLIGHTED_AYAH, highlightedAyah);
}
scrollPosition = translationView.findFirstCompletelyVisibleItemPosition();
outState.putInt(SI_SCROLL_POSITION, scrollPosition);
super.onSaveInstanceState(outState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.quran.labs.androidquran.common.QuranAyahInfo
import com.quran.data.model.SuraAyah
import com.quran.labs.androidquran.model.translation.ArabicDatabaseUtils
import com.quran.labs.androidquran.ui.helpers.ExpandTafseerSpan
import com.quran.labs.androidquran.ui.helpers.HighlightType
import com.quran.labs.androidquran.ui.helpers.UthmaniSpan
import com.quran.labs.androidquran.ui.util.TypefaceManager
import com.quran.labs.androidquran.util.QuranSettings
Expand All @@ -46,6 +47,7 @@ internal class TranslationAdapter(private val context: Context,
private var highlightedAyah: Int = 0
private var highlightedRowCount: Int = 0
private var highlightedStartPosition: Int = 0
private var highlightType: HighlightType? = null

private val expandedTafaseerAyahs = mutableSetOf<Pair<Int, Int>>()
private val expandedHyperlinks = mutableSetOf<Pair<Int, Int>>()
Expand Down Expand Up @@ -84,19 +86,19 @@ internal class TranslationAdapter(private val context: Context,
expandedTafaseerAyahs.clear();
this.data.addAll(data)
if (highlightedAyah > 0) {
highlightAyah(highlightedAyah, false)
highlightAyah(highlightedAyah, false, highlightType ?: HighlightType.SELECTION)
}
}

fun setHighlightedAyah(ayahId: Int) {
highlightAyah(ayahId, true)
fun setHighlightedAyah(ayahId: Int, highlightType: HighlightType) {
highlightAyah(ayahId, true, highlightType)
}

fun highlightedAyahInfo(): QuranAyahInfo? {
return data.firstOrNull { it.ayahInfo.ayahId == highlightedAyah }?.ayahInfo
}

private fun highlightAyah(ayahId: Int, notify: Boolean) {
private fun highlightAyah(ayahId: Int, notify: Boolean, highlightedType: HighlightType) {
if (ayahId != highlightedAyah) {
val matches = data.withIndex().filter { it.value.ayahInfo.ayahId == ayahId }
val (startPosition, count) = (matches.firstOrNull()?.index ?: -1) to matches.size
Expand Down Expand Up @@ -130,8 +132,8 @@ internal class TranslationAdapter(private val context: Context,
recyclerView.handler.post {
notifyItemRangeChanged(startChangeRange, startChangeCount, HIGHLIGHT_CHANGE)
val layoutManager = recyclerView.layoutManager
if (layoutManager is LinearLayoutManager) {
layoutManager.scrollToPositionWithOffset(startPosition, 52)
if (highlightedType == HighlightType.AUDIO && layoutManager is LinearLayoutManager) {
layoutManager.scrollToPositionWithOffset(startPosition, 64)
} else {
recyclerView.smoothScrollToPosition(startPosition)
}
Expand All @@ -141,6 +143,7 @@ internal class TranslationAdapter(private val context: Context,
highlightedAyah = ayahId
highlightedStartPosition = startPosition
highlightedRowCount = count
highlightType = highlightedType
}
}

Expand Down Expand Up @@ -184,7 +187,7 @@ internal class TranslationAdapter(private val context: Context,
val position = recyclerView.getChildAdapterPosition(view)
if (highlightedAyah != 0 && position != RecyclerView.NO_POSITION) {
val ayahInfo = data[position].ayahInfo
if (ayahInfo.ayahId != highlightedAyah) {
if (ayahInfo.ayahId != highlightedAyah && highlightType == HighlightType.SELECTION) {
onVerseSelectedListener.onVerseSelected(ayahInfo)
return
}
Expand All @@ -196,7 +199,7 @@ internal class TranslationAdapter(private val context: Context,
val position = recyclerView.getChildAdapterPosition(view)
if (position != RecyclerView.NO_POSITION) {
val ayahInfo = data[position].ayahInfo
highlightAyah(ayahInfo.ayahId, true)
highlightAyah(ayahInfo.ayahId, true, HighlightType.SELECTION)
onVerseSelectedListener.onVerseSelected(ayahInfo)
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.quran.labs.androidquran.common.QuranAyahInfo;
import com.quran.labs.androidquran.common.TranslationMetadata;
import com.quran.labs.androidquran.data.QuranDisplayData;
import com.quran.labs.androidquran.ui.helpers.HighlightType;
import com.quran.labs.androidquran.ui.util.PageController;
import com.quran.labs.androidquran.util.QuranSettings;

Expand Down Expand Up @@ -165,15 +166,26 @@ public void setTranslationClickedListener(OnClickListener listener) {
onClickListener = listener;
}

public void highlightAyah(SuraAyah suraAyah, int ayahId) {
selectedAyah = suraAyah;
translationAdapter.setHighlightedAyah(ayahId);
public void highlightAyah(SuraAyah suraAyah, int ayahId, HighlightType highlightType) {
if (highlightType == HighlightType.SELECTION) {
selectedAyah = suraAyah;
} else if (selectedAyah != null) {
hideMenu();
}
translationAdapter.setHighlightedAyah(ayahId, highlightType);
}

private void hideMenu() {
pageController.endAyahMode();
}

public void unhighlightAyah(HighlightType highlightType) {
if (highlightType == HighlightType.SELECTION) {
selectedAyah = null;
}
translationAdapter.unhighlight();
}

public void unhighlightAyat() {
if (selectedAyah != null) {
selectedAyah = null;
Expand Down Expand Up @@ -248,9 +260,14 @@ private void updateAyahToolBarPosition() {
@Override
public void onVerseSelected(@NonNull QuranAyahInfo ayahInfo) {
final SuraAyah suraAyah = new SuraAyah(ayahInfo.sura, ayahInfo.ayah);
if (selectedAyah != null && !selectedAyah.equals(suraAyah)) {
if (selectedAyah != null) {
final boolean isUnselectingSelectedVerse = selectedAyah.equals(suraAyah);
hideMenu();
if (isUnselectingSelectedVerse) {
return;
}
}

pageController.handleLongPress(suraAyah);
pageController.requestMenuPositionUpdate();
}
Expand Down

0 comments on commit 86a3168

Please sign in to comment.