Skip to content

Commit a967ff0

Browse files
authored
Fix clearing history when switching between servers (#6357)
1 parent d8b93f0 commit a967ff0

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

app/src/main/kotlin/io/homeassistant/companion/android/webview/WebViewActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1509,12 +1509,12 @@ class WebViewActivity :
15091509
}
15101510
supportFragmentManager.clearFragmentResultListener(BlockInsecureFragment.RESULT_KEY)
15111511

1512-
clearHistory = !keepHistory
15131512
val oldUrl = loadedUrl
15141513
// It means that if we loaded an URL with a path previously and we try to load the same URL without
15151514
// a path we don't do anything.
15161515
val shouldLoadUrl = !url.hasSameOrigin(oldUrl) || url.hasNonRootPath()
15171516
if (shouldLoadUrl) {
1517+
clearHistory = !keepHistory
15181518
loadedUrl = url
15191519
webView.loadUrl(url.toString())
15201520
waitForConnection()

app/src/main/kotlin/io/homeassistant/companion/android/webview/WebViewPresenter.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ interface WebViewPresenter {
1616
lifecycle: Lifecycle,
1717
path: String? = null,
1818
isInternalOverride: ((ServerConnectionInfo) -> Boolean)? = null,
19+
isNewServer: Boolean? = null,
1920
)
2021

2122
fun getActiveServer(): Int

app/src/main/kotlin/io/homeassistant/companion/android/webview/WebViewPresenterImpl.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,11 @@ class WebViewPresenterImpl @Inject constructor(
107107
lifecycle: Lifecycle,
108108
path: String?,
109109
isInternalOverride: ((ServerConnectionInfo) -> Boolean)?,
110+
isNewServer: Boolean?,
110111
) {
111112
urlFlowJob?.cancel()
112113

113-
val isNewServer = ensureServerIsAvailable()
114+
val isNewServer = isNewServer ?: ensureServerIsAvailable()
114115
if (!isSessionConnected()) return
115116

116117
urlFlowJob = lifecycle.cancelOnLifecycle(Lifecycle.State.STARTED) {
@@ -260,11 +261,12 @@ class WebViewPresenterImpl @Inject constructor(
260261
}
261262

262263
override suspend fun switchActiveServer(lifecycle: Lifecycle, id: Int) {
264+
val isNewServer = serverId != id
263265
if (serverId != id && serverId != ServerManager.SERVER_ID_ACTIVE) {
264266
setAppActive(false) // 'Lock' old server
265267
}
266268
setActiveServer(id)
267-
load(lifecycle = lifecycle)
269+
load(lifecycle, isNewServer = isNewServer)
268270
view.unlockAppIfNeeded()
269271
}
270272

app/src/test/kotlin/io/homeassistant/companion/android/webview/WebViewPresenterImplTest.kt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,41 @@ class WebViewPresenterImplTest {
507507
assertEquals(42, presenter.getActiveServer())
508508
}
509509

510+
@Test
511+
fun `Given url to load when server is changed then history should not be kept`() = runTest {
512+
val server1 = mockk<Server>(relaxed = true)
513+
every { server1.id } returns 1
514+
coEvery { serverManager.getServer(1) } returns server1
515+
coEvery { serverManager.getServer(ServerManager.SERVER_ID_ACTIVE) } returns server1
516+
517+
coEvery { authenticationRepository.getSessionState() } returns SessionState.CONNECTED
518+
coEvery { connectionStateProvider.urlFlow(any()) } returns flowOf(UrlState.HasUrl(URL("https://example.com")))
519+
520+
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
521+
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START)
522+
523+
createPresenter()
524+
advanceUntilIdle()
525+
526+
// Simulate initial load
527+
presenter.load(lifecycle, path = null, isInternalOverride = null)
528+
advanceUntilIdle()
529+
530+
// Initial load - server didn't change so history should be kept
531+
verify(exactly = 1) { webView.loadUrl(url = any(), keepHistory = true, openInApp = any(), serverHandleInsets = any()) }
532+
533+
// Switch server
534+
val server2 = mockk<Server>(relaxed = true)
535+
every { server2.id } returns 2
536+
coEvery { serverManager.getServer(2) } returns server2
537+
538+
presenter.switchActiveServer(lifecycle, server2.id)
539+
advanceUntilIdle()
540+
541+
// Server change should trigger load - server changed so history should not be kept
542+
verify(exactly = 1) { webView.loadUrl(url = any(), keepHistory = false, openInApp = any(), serverHandleInsets = any()) }
543+
}
544+
510545
@Test
511546
fun `Given security level not set when load called then shows security level fragment`() = runTest(testDispatcher) {
512547
val server = mockk<Server>(relaxed = true)

0 commit comments

Comments
 (0)