Skip to content

Commit

Permalink
open show on app launch setting when set
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mikescamell committed Sep 19, 2024
1 parent 8949392 commit e0e0be4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ open class BrowserActivity : DuckDuckGoActivity() {
return
}
}

viewModel.handleShowOnAppLaunchOption()
}

private fun configureObservers() {
Expand Down
22 changes: 22 additions & 0 deletions app/src/main/java/com/duckduckgo/app/browser/BrowserViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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 {

Expand Down Expand Up @@ -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)
}
}
}
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ interface TabRepository {

suspend fun deleteAll()

suspend fun getSelectedTab(): TabEntity?

suspend fun select(tabId: String)

fun updateTabPreviewImage(
Expand Down

0 comments on commit e0e0be4

Please sign in to comment.