@@ -11,6 +11,8 @@ import io.github.kdroidfilter.composewebview.wry.WryWebViewPanel
1111@Stable
1212class WebViewState (initialUrl : String ) {
1313 internal var panel: WryWebViewPanel ? = null
14+ private val history = mutableListOf<String >()
15+ private var historyIndex: Int = - 1
1416
1517 /* *
1618 * The target URL to navigate to. Changing this will trigger navigation.
@@ -80,7 +82,8 @@ class WebViewState(initialUrl: String) {
8082 if (p.isReady()) {
8183 // Get current URL from native state
8284 p.getCurrentUrl()?.let { newUrl ->
83- if (newUrl.isNotEmpty()) {
85+ if (newUrl.isNotEmpty() && newUrl != currentUrl) {
86+ updateHistory(newUrl)
8487 currentUrl = newUrl
8588 }
8689 }
@@ -89,6 +92,30 @@ class WebViewState(initialUrl: String) {
8992 }
9093 }
9194 }
95+
96+ private fun updateHistory (newUrl : String ) {
97+ if (historyIndex >= 0 ) {
98+ val backUrl = history.getOrNull(historyIndex - 1 )
99+ val forwardUrl = history.getOrNull(historyIndex + 1 )
100+ when (newUrl) {
101+ backUrl -> historyIndex - = 1
102+ forwardUrl -> historyIndex + = 1
103+ else -> {
104+ if (historyIndex < history.size - 1 ) {
105+ history.subList(historyIndex + 1 , history.size).clear()
106+ }
107+ history.add(newUrl)
108+ historyIndex = history.lastIndex
109+ }
110+ }
111+ } else {
112+ history.add(newUrl)
113+ historyIndex = 0
114+ }
115+
116+ canGoBack = historyIndex > 0
117+ canGoForward = historyIndex >= 0 && historyIndex < history.size - 1
118+ }
92119}
93120
94121/* *
0 commit comments