MainActivity: fix issue with going back

This commit is contained in:
dece 2021-12-13 15:06:40 +01:00
parent 6a65df0f4e
commit 9743fea7d5

View file

@ -35,9 +35,6 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
/** A non-saved list of visited URLs. Not an history, just used for going back. */ /** A non-saved list of visited URLs. Not an history, just used for going back. */
private val visitedUrls = mutableListOf<String>() private val visitedUrls = mutableListOf<String>()
/** Are we going back? */
private var goingBack = false
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -71,14 +68,12 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
} }
override fun onBackPressed() { override fun onBackPressed() {
if (visitedUrls.isNotEmpty()) { visitedUrls.removeLastOrNull() // Always remove current page first.
if (visitedUrls.size > 1) val previousUrl = visitedUrls.removeLastOrNull()
visitedUrls.removeLast() if (previousUrl != null)
goingBack = true openUrl(previousUrl)
openUrl(visitedUrls.removeLast()) else
} else {
super.onBackPressed() super.onBackPressed()
}
} }
override fun onLinkClick(url: String) { override fun onLinkClick(url: String) {
@ -126,10 +121,7 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
if (!event.handled) { if (!event.handled) {
when (event) { when (event) {
is PageViewModel.SuccessEvent -> { is PageViewModel.SuccessEvent -> {
if (goingBack) visitedUrls.add(event.uri)
goingBack = false
else
visitedUrls.add(event.uri)
} }
is PageViewModel.FailureEvent -> { is PageViewModel.FailureEvent -> {
alert(event.message) alert(event.message)
@ -243,6 +235,8 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
} }
lines.postValue(linesList) lines.postValue(linesList)
// We record the history entry here: it's nice because we have the main title available
// and we're already in a coroutine for database access.
History.record(uri.toString(), mainTitle) History.record(uri.toString(), mainTitle)
event.postValue(SuccessEvent(uri.toString())) event.postValue(SuccessEvent(uri.toString()))
state.postValue(State.IDLE) state.postValue(State.IDLE)