Skip to content
This repository has been archived by the owner on Jul 29, 2022. It is now read-only.

Commit

Permalink
Merge pull request #111 from readium/fixes/seach-locators
Browse files Browse the repository at this point in the history
fixes current locator issue after search
  • Loading branch information
aferditamuriqi authored Jan 16, 2020
2 parents 7766cab + 70c9120 commit 6096daf
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import androidx.viewpager.widget.ViewPager
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import org.readium.r2.navigator.*
Expand Down Expand Up @@ -67,13 +68,24 @@ open class R2EpubActivity : AppCompatActivity(), IR2Activity, IR2Selectable, IR2
if (resourcePager.currentItem == resource.first) {
// reload webview if it has an anchor
val currentFragent = ((resourcePager.adapter as R2PagerAdapter).mFragments.get((resourcePager.adapter as R2PagerAdapter).getItemId(resourcePager.currentItem))) as? R2EpubPageFragment
locator.locations?.fragment?.let {
var anchor = it
if (!anchor.startsWith("#")) {
anchor = "#$anchor"
locator.locations?.fragment?.let { fragment ->

val fragments = JSONArray(fragment).getString(0).split(",").associate {
val (left, right) = it.split("=")
left to right.toInt()
}
// val id = fragments.getValue("id")
if (fragments.isEmpty()) {
var anchor = fragment
if (!anchor.startsWith("#")) {
anchor = "#$anchor"
}
val goto = resource.second + anchor
currentFragent?.webView?.loadUrl(goto)
} else {
currentFragent?.webView?.loadUrl(resource.second)
}
val goto = resource.second + anchor
currentFragent?.webView?.loadUrl(goto)

} ?: run {
currentFragent?.webView?.loadUrl(resource.second)
}
Expand Down Expand Up @@ -402,13 +414,24 @@ open class R2EpubActivity : AppCompatActivity(), IR2Activity, IR2Selectable, IR2
if (resourcePager.currentItem == resource.first) {
// reload webview if it has an anchor
val currentFragent = ((resourcePager.adapter as R2PagerAdapter).mFragments.get((resourcePager.adapter as R2PagerAdapter).getItemId(resourcePager.currentItem))) as? R2EpubPageFragment
locator.locations?.fragment?.let {
var anchor = it
if (!anchor.startsWith("#")) {
anchor = "#$anchor"
locator.locations?.fragment?.let { fragment ->

val fragments = JSONArray(fragment).getString(0).split(",").associate {
val (left, right) = it.split("=")
left to right.toInt()
}
val goto = resource.second + anchor
currentFragent?.webView?.loadUrl(goto)
// val id = fragments.getValue("id")
if (fragments.isEmpty()) {
var anchor = fragment
if (!anchor.startsWith("#")) {
anchor = "#$anchor"
}
val goto = resource.second + anchor
currentFragent?.webView?.loadUrl(goto)
} else {
currentFragent?.webView?.loadUrl(resource.second)
}

} ?: run {
currentFragent?.webView?.loadUrl(resource.second)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.webkit.WebViewClientCompat
import org.json.JSONArray
import org.readium.r2.navigator.*
import org.readium.r2.shared.APPEARANCE_REF
import org.readium.r2.shared.Locations
Expand Down Expand Up @@ -228,13 +229,24 @@ class R2EpubPageFragment : Fragment() {

val locations = webView.navigator.currentLocation?.locations

locations?.fragment?.let {
var anchor = it
if (!anchor.startsWith("#")) {
anchor = "#$anchor"

locations?.fragment?.let { fragment ->

val fragments = JSONArray(fragment).getString(0).split(",").associate {
val (left, right) = it.split("=")
left to right.toInt()
}
// val id = fragments.getValue("id")
if (fragments.isEmpty()) {
var anchor = fragment
if (!anchor.startsWith("#")) {
anchor = "#$anchor"
}
val href = resourceUrl + anchor
webView.loadUrl(href)
} else {
webView.loadUrl(resourceUrl)
}
val href = resourceUrl + anchor
webView.loadUrl(href)
} ?: run {
webView.loadUrl(resourceUrl)
}
Expand Down

0 comments on commit 6096daf

Please sign in to comment.