PageViewFragment: restore back button function

This commit is contained in:
dece 2022-01-10 09:34:09 +01:00
parent 09e19a1a76
commit 4aaa0fbcdb

View file

@ -14,6 +14,8 @@ import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodManager
import android.widget.EditText import android.widget.EditText
import android.widget.FrameLayout import android.widget.FrameLayout
import android.widget.TextView
import androidx.activity.addCallback
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.core.view.setMargins import androidx.core.view.setMargins
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
@ -51,36 +53,36 @@ class PageViewFragment : Fragment(), ContentAdapter.ContentAdapterListener {
binding.contentRecycler.layoutManager = LinearLayoutManager(requireContext()) binding.contentRecycler.layoutManager = LinearLayoutManager(requireContext())
binding.contentRecycler.adapter = adapter binding.contentRecycler.adapter = adapter
binding.addressBar.setOnEditorActionListener { editText, actionId, _ -> binding.addressBar.setOnEditorActionListener { v, id, _ -> onAddressBarAction(v, id) }
if (actionId == EditorInfo.IME_ACTION_DONE) {
openUrl(editText.text.toString())
activity?.run {
val imm = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(editText.windowToken, 0)
}
editText.clearFocus()
true
} else {
false
}
}
binding.contentSwipeLayout.setOnRefreshListener { openUrl(currentUrl) } binding.contentSwipeLayout.setOnRefreshListener { openUrl(currentUrl) }
pageViewModel.state.observe(viewLifecycleOwner, { updateState(it) }) pageViewModel.state.observe(viewLifecycleOwner, { updateState(it) })
pageViewModel.lines.observe(viewLifecycleOwner, { updateLines(it) }) pageViewModel.lines.observe(viewLifecycleOwner, { updateLines(it) })
pageViewModel.event.observe(viewLifecycleOwner, { handleEvent(it) }) pageViewModel.event.observe(viewLifecycleOwner, { handleEvent(it) })
activity?.onBackPressedDispatcher?.addCallback(viewLifecycleOwner) { onBackPressed() }
} }
override fun onLinkClick(url: String) { override fun onLinkClick(url: String) {
openUrl(url, base = if (currentUrl.isNotEmpty()) currentUrl else null) openUrl(url, base = if (currentUrl.isNotEmpty()) currentUrl else null)
} }
fun onBackPressed(): Boolean { private fun onBackPressed() {
visitedUrls.removeLastOrNull() // Always remove current page first. if (visitedUrls.size >= 2) {
val previousUrl = visitedUrls.removeLastOrNull() visitedUrls.removeLastOrNull() // Always remove current page first.
if (previousUrl != null) { openUrl(visitedUrls.removeLastOrNull()!!)
openUrl(previousUrl) }
}
private fun onAddressBarAction(addressBar: TextView, actionId: Int): Boolean {
if (actionId == EditorInfo.IME_ACTION_DONE) {
openUrl(addressBar.text.toString())
activity?.run {
val imm = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(addressBar.windowToken, 0)
}
addressBar.clearFocus()
return true return true
} }
return false return false