From e0e0be4220210885f10d222f31342e973eb31dcc Mon Sep 17 00:00:00 2001 From: Mike Scamell Date: Tue, 10 Sep 2024 08:48:02 +0200 Subject: [PATCH] open show on app launch setting when set The launchNewSearchOrQuery seemed to be the best place to add check if we should launch the show on app setting. It does a lot of checking based on what's passed into the intent as far as I can see. If there is no specialised launch happening then we check the show on app launch setting 1. If LastOpenedTab is set, we do nothing 2. If NewTabPage is set then we open a new tab 3. If SpecificPage is set we compare the url to the current selected tab, if it's not the same we open a new tab otherwise we do nothing I had to expose selectedTab from the TabsDao so I could the tab in a synchronous fashion to check. I would have made the tabsDao.selectedTab() suspending but that would have meant changes elsewhere. --- .../duckduckgo/app/browser/BrowserActivity.kt | 2 ++ .../app/browser/BrowserViewModel.kt | 22 +++++++++++++++++++ .../app/tabs/model/TabDataRepository.kt | 3 +++ .../app/tabs/model/TabRepository.kt | 2 ++ 4 files changed, 29 insertions(+) diff --git a/app/src/main/java/com/duckduckgo/app/browser/BrowserActivity.kt b/app/src/main/java/com/duckduckgo/app/browser/BrowserActivity.kt index aa8fd23901fc..fd85ef28f075 100644 --- a/app/src/main/java/com/duckduckgo/app/browser/BrowserActivity.kt +++ b/app/src/main/java/com/duckduckgo/app/browser/BrowserActivity.kt @@ -364,6 +364,8 @@ open class BrowserActivity : DuckDuckGoActivity() { return } } + + viewModel.handleShowOnAppLaunchOption() } private fun configureObservers() { diff --git a/app/src/main/java/com/duckduckgo/app/browser/BrowserViewModel.kt b/app/src/main/java/com/duckduckgo/app/browser/BrowserViewModel.kt index b9359b5f3586..7452b7b362f6 100644 --- a/app/src/main/java/com/duckduckgo/app/browser/BrowserViewModel.kt +++ b/app/src/main/java/com/duckduckgo/app/browser/BrowserViewModel.kt @@ -20,11 +20,16 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.Observer import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope import com.duckduckgo.anvil.annotations.ContributesRemoteFeature import com.duckduckgo.anvil.annotations.ContributesViewModel import com.duckduckgo.app.browser.defaultbrowsing.DefaultBrowserDetector import com.duckduckgo.app.browser.omnibar.OmnibarEntryConverter import com.duckduckgo.app.fire.DataClearer +import com.duckduckgo.app.generalsettings.showonapplaunch.model.ShowOnAppLaunchOption.LastOpenedTab +import com.duckduckgo.app.generalsettings.showonapplaunch.model.ShowOnAppLaunchOption.NewTabPage +import com.duckduckgo.app.generalsettings.showonapplaunch.model.ShowOnAppLaunchOption.SpecificPage +import com.duckduckgo.app.generalsettings.showonapplaunch.store.ShowOnAppLaunchOptionDataStore import com.duckduckgo.app.global.ApplicationClearDataState import com.duckduckgo.app.global.rating.AppEnjoymentPromptEmitter import com.duckduckgo.app.global.rating.AppEnjoymentPromptOptions @@ -55,6 +60,7 @@ import com.duckduckgo.feature.toggles.api.Toggle import javax.inject.Inject import kotlin.coroutines.CoroutineContext import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.flow.first import kotlinx.coroutines.launch import timber.log.Timber @@ -69,6 +75,7 @@ class BrowserViewModel @Inject constructor( private val dispatchers: DispatcherProvider, private val pixel: Pixel, private val skipUrlConversionOnNewTabFeature: SkipUrlConversionOnNewTabFeature, + private val showOnAppLaunchOptionDataStore: ShowOnAppLaunchOptionDataStore, ) : ViewModel(), CoroutineScope { @@ -284,6 +291,21 @@ class BrowserViewModel @Inject constructor( fun onBookmarksActivityResult(url: String) { command.value = Command.OpenSavedSite(url) } + + fun handleShowOnAppLaunchOption() { + viewModelScope.launch { + when (val option = showOnAppLaunchOptionDataStore.optionFlow.first()) { + LastOpenedTab -> Unit + NewTabPage -> onNewTabRequested() + is SpecificPage -> { + val liveSelectedTabUrl = tabRepository.getSelectedTab()?.url + if (liveSelectedTabUrl != option.url) { + onOpenInNewTabRequested(option.url) + } + } + } + } + } } /** diff --git a/app/src/main/java/com/duckduckgo/app/tabs/model/TabDataRepository.kt b/app/src/main/java/com/duckduckgo/app/tabs/model/TabDataRepository.kt index 70f08a2dff60..f34550357e0f 100644 --- a/app/src/main/java/com/duckduckgo/app/tabs/model/TabDataRepository.kt +++ b/app/src/main/java/com/duckduckgo/app/tabs/model/TabDataRepository.kt @@ -298,6 +298,9 @@ class TabDataRepository @Inject constructor( siteData.clear() } + override suspend fun getSelectedTab(): TabEntity? = + withContext(dispatchers.io()) { tabsDao.selectedTab() } + override suspend fun select(tabId: String) { databaseExecutor().scheduleDirect { val selection = TabSelectionEntity(tabId = tabId) diff --git a/browser-api/src/main/java/com/duckduckgo/app/tabs/model/TabRepository.kt b/browser-api/src/main/java/com/duckduckgo/app/tabs/model/TabRepository.kt index a96eb9f44eb5..d9adb1780081 100644 --- a/browser-api/src/main/java/com/duckduckgo/app/tabs/model/TabRepository.kt +++ b/browser-api/src/main/java/com/duckduckgo/app/tabs/model/TabRepository.kt @@ -93,6 +93,8 @@ interface TabRepository { suspend fun deleteAll() + suspend fun getSelectedTab(): TabEntity? + suspend fun select(tabId: String) fun updateTabPreviewImage(