Skip to content

Commit

Permalink
Fix: Scroll state in retained in the partial translation mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ikerfah committed Mar 25, 2024
1 parent 149bdbb commit 2208af5
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ class InlineTranslationPresenter @Inject constructor(
.launchIn(scope)
}

suspend fun refresh(verseRange: VerseRange) {
suspend fun refresh(verseRange: VerseRange, ayahHasBeenChanged: Boolean) {
val result = withContext(Dispatchers.IO) {
getVerses(false, getTranslations(quranSettings), verseRange)
}
translationScreen?.setVerses(result.translations, result.ayahInformation)
translationScreen?.setVerses(result.translations, result.ayahInformation, ayahHasBeenChanged)
}

override fun bind(what: TranslationScreen) {
Expand All @@ -52,7 +52,7 @@ class InlineTranslationPresenter @Inject constructor(
}

interface TranslationScreen {
fun setVerses(translations: Array<LocalTranslation>, verses: List<QuranAyahInfo>)
fun setVerses(translations: Array<LocalTranslation>, verses: List<QuranAyahInfo>, ayahHasBeenChanged: Boolean)
fun onTranslationsUpdated(translations: List<LocalTranslation>)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ abstract class AyahActionFragment : Fragment() {
}

if (previousStart != start || previousEnd != end) {
refreshView()
refreshView(ayahHasBeenChanged = true)
}
}
.launchIn(scope)
Expand All @@ -65,5 +65,5 @@ abstract class AyahActionFragment : Fragment() {

open fun onToggleDetailsPanel(isVisible: Boolean) { }

protected abstract fun refreshView()
protected abstract fun refreshView(ayahHasBeenChanged: Boolean)
}
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,11 @@ class AyahPlaybackFragment : AyahActionFragment() {
override fun onToggleDetailsPanel(isVisible: Boolean) {
isOpen = isVisible
if (!isOpen) {
refreshView()
refreshView(ayahHasBeenChanged = true)
}
}

override fun refreshView() {
override fun refreshView(ayahHasBeenChanged: Boolean) {
val context: Context? = activity
val selectionEnd = end
val selectionStart = start
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import com.quran.labs.androidquran.R
import com.quran.labs.androidquran.common.QuranAyahInfo
import com.quran.labs.androidquran.presenter.translation.InlineTranslationPresenter
import com.quran.labs.androidquran.presenter.translation.InlineTranslationPresenter.TranslationScreen
import com.quran.labs.androidquran.presenter.translationlist.TranslationListPresenter
import com.quran.labs.androidquran.ui.PagerActivity
import com.quran.labs.androidquran.ui.helpers.SlidingPagerAdapter
import com.quran.labs.androidquran.ui.util.TranslationsSpinnerAdapter
Expand All @@ -25,8 +24,6 @@ import com.quran.mobile.di.AyahActionFragmentProvider
import com.quran.mobile.translation.model.LocalTranslation
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import javax.inject.Inject
import kotlin.math.abs
Expand Down Expand Up @@ -133,7 +130,7 @@ class AyahTranslationFragment : AyahActionFragment(), TranslationScreen {
) { selectedItems: Set<String?>? ->
quranSettings.activeTranslations = selectedItems
// this is the refresh for when a translation is selected from the spinner
refreshView()
refreshView(ayahHasBeenChanged = false)
}
translator.adapter = translationAdapter
} else {
Expand All @@ -143,11 +140,11 @@ class AyahTranslationFragment : AyahActionFragment(), TranslationScreen {
activeTranslationsFilesNames
)
}
refreshView()
refreshView(ayahHasBeenChanged = false)
}
}

public override fun refreshView() {
public override fun refreshView(ayahHasBeenChanged: Boolean) {
val start = start
val end = end
if (start == null || end == null) {
Expand All @@ -159,18 +156,21 @@ class AyahTranslationFragment : AyahActionFragment(), TranslationScreen {
)
val verseRange = VerseRange(start.sura, start.ayah, end.sura, end.ayah, verses)
scope.launch {
translationPresenter.refresh(verseRange)
translationPresenter.refresh(verseRange, ayahHasBeenChanged)
}
}

override fun setVerses(translations: Array<LocalTranslation>, verses: List<QuranAyahInfo>) {
override fun setVerses(translations: Array<LocalTranslation>, verses: List<QuranAyahInfo>, ayahHasBeenChanged: Boolean) {
progressBar.visibility = View.GONE
if (verses.isNotEmpty()) {
emptyState.visibility = View.GONE
translationControls.visibility = View.VISIBLE
translator.visibility = View.VISIBLE
translationView.visibility = View.VISIBLE
translationView.setAyahs(translations, verses)
if (ayahHasBeenChanged) {
translationView.resetScroll()
}
} else {
emptyState.visibility = View.VISIBLE
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,13 @@ class InlineTranslationView @JvmOverloads constructor(
i++
}
addFooterSpacer()
scrollTo(0, 0)
}
}

fun resetScroll() {
scrollTo(0, 0)
}

private fun addFooterSpacer() {
val params = LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, footerSpacerHeight
Expand Down

0 comments on commit 2208af5

Please sign in to comment.