diff --git a/app/src/main/java/com/duckduckgo/app/generalsettings/showonapplaunch/ShowOnAppLaunchActivity.kt b/app/src/main/java/com/duckduckgo/app/generalsettings/showonapplaunch/ShowOnAppLaunchActivity.kt index 5d77e490cabd..e16968f9370b 100644 --- a/app/src/main/java/com/duckduckgo/app/generalsettings/showonapplaunch/ShowOnAppLaunchActivity.kt +++ b/app/src/main/java/com/duckduckgo/app/generalsettings/showonapplaunch/ShowOnAppLaunchActivity.kt @@ -18,17 +18,28 @@ package com.duckduckgo.app.generalsettings.showonapplaunch import android.os.Bundle import android.view.MenuItem +import androidx.core.view.isGone +import androidx.core.view.isVisible +import androidx.lifecycle.Lifecycle +import androidx.lifecycle.flowWithLifecycle +import androidx.lifecycle.lifecycleScope import com.duckduckgo.anvil.annotations.ContributeToActivityStarter import com.duckduckgo.anvil.annotations.InjectWith import com.duckduckgo.app.browser.databinding.ActivityShowOnAppLaunchSettingBinding +import com.duckduckgo.app.generalsettings.showonapplaunch.ShowOnAppLaunchViewModel.ShowOnAppLaunchOption.LastOpenedTab +import com.duckduckgo.app.generalsettings.showonapplaunch.ShowOnAppLaunchViewModel.ShowOnAppLaunchOption.NewTabPage +import com.duckduckgo.app.generalsettings.showonapplaunch.ShowOnAppLaunchViewModel.ShowOnAppLaunchOption.SpecificPage import com.duckduckgo.common.ui.DuckDuckGoActivity import com.duckduckgo.common.ui.viewbinding.viewBinding import com.duckduckgo.di.scopes.ActivityScope +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach @InjectWith(ActivityScope::class) @ContributeToActivityStarter(ShowOnAppLaunchScreenNoParams::class) class ShowOnAppLaunchActivity : DuckDuckGoActivity() { + private val viewModel: ShowOnAppLaunchViewModel by bindViewModel() private val binding: ActivityShowOnAppLaunchSettingBinding by viewBinding() override fun onCreate(savedInstanceState: Bundle?) { @@ -38,6 +49,7 @@ class ShowOnAppLaunchActivity : DuckDuckGoActivity() { setupToolbar(binding.includeToolbar.toolbar) configureUiEventHandlers() + observeViewModel() } override fun onOptionsItemSelected(item: MenuItem): Boolean { @@ -53,15 +65,47 @@ class ShowOnAppLaunchActivity : DuckDuckGoActivity() { private fun configureUiEventHandlers() { binding.lastOpenedTabCheckListItem.setOnClickListener { - // TODO: Implement this + viewModel.onShowOnAppLaunchOptionChanged(LastOpenedTab) } binding.newTabCheckListItem.setOnClickListener { - // TODO: Implement this + viewModel.onShowOnAppLaunchOptionChanged(NewTabPage) } binding.specificPageCheckListItem.setOnClickListener { - // TODO: Implement this + viewModel.onShowOnAppLaunchOptionChanged(SpecificPage(binding.specificPageUrlInput.text)) } } + + private fun observeViewModel() { + viewModel.viewState + .flowWithLifecycle(lifecycle, Lifecycle.State.RESUMED) + .onEach { viewState -> + clearSelections() + + when (viewState.selectedOption) { + LastOpenedTab -> { + binding.lastOpenedTabCheckListItem.setChecked(true) + } + NewTabPage -> { + binding.newTabCheckListItem.setChecked(true) + } + is SpecificPage -> { + binding.specificPageCheckListItem.setChecked(true) + with(binding.specificPageUrlInput) { + isVisible = true + text = viewState.selectedOption.url + } + } + } + } + .launchIn(lifecycleScope) + } + + private fun clearSelections() { + binding.lastOpenedTabCheckListItem.setChecked(false) + binding.newTabCheckListItem.setChecked(false) + binding.specificPageCheckListItem.setChecked(false) + binding.specificPageUrlInput.isGone = true + } } diff --git a/app/src/main/java/com/duckduckgo/app/generalsettings/showonapplaunch/ShowOnAppLaunchViewModel.kt b/app/src/main/java/com/duckduckgo/app/generalsettings/showonapplaunch/ShowOnAppLaunchViewModel.kt index 588c38b7796e..2a7d6aac3a04 100644 --- a/app/src/main/java/com/duckduckgo/app/generalsettings/showonapplaunch/ShowOnAppLaunchViewModel.kt +++ b/app/src/main/java/com/duckduckgo/app/generalsettings/showonapplaunch/ShowOnAppLaunchViewModel.kt @@ -20,13 +20,17 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.duckduckgo.anvil.annotations.ContributesViewModel import com.duckduckgo.app.generalsettings.showonapplaunch.ShowOnAppLaunchViewModel.ShowOnAppLaunchOption.LastOpenedTab +import com.duckduckgo.app.generalsettings.showonapplaunch.ShowOnAppLaunchViewModel.ShowOnAppLaunchOption.NewTabPage +import com.duckduckgo.app.generalsettings.showonapplaunch.ShowOnAppLaunchViewModel.ShowOnAppLaunchOption.SpecificPage import com.duckduckgo.common.utils.DispatcherProvider import com.duckduckgo.di.scopes.ActivityScope import javax.inject.Inject import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.filterNotNull +import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch +import timber.log.Timber @ContributesViewModel(ActivityScope::class) class ShowOnAppLaunchViewModel @Inject constructor( @@ -56,4 +60,16 @@ class ShowOnAppLaunchViewModel @Inject constructor( ) } } + + fun onShowOnAppLaunchOptionChanged(option: ShowOnAppLaunchOption) { + Timber.i("User changed show on app launch option to $option") + when (option) { + LastOpenedTab -> _viewState.update { it?.copy(selectedOption = option) } + NewTabPage -> _viewState.update { it?.copy(selectedOption = option) } + is SpecificPage -> { + // TODO get the last set page if we have one and populate the url + _viewState.update { it?.copy(selectedOption = SpecificPage("duckduckgo.com")) } + } + } + } } diff --git a/common/common-ui/src/main/java/com/duckduckgo/common/ui/view/listitem/RadioListItem.kt b/common/common-ui/src/main/java/com/duckduckgo/common/ui/view/listitem/RadioListItem.kt index bdd782189052..9cd6a7bd49c3 100644 --- a/common/common-ui/src/main/java/com/duckduckgo/common/ui/view/listitem/RadioListItem.kt +++ b/common/common-ui/src/main/java/com/duckduckgo/common/ui/view/listitem/RadioListItem.kt @@ -124,4 +124,8 @@ class RadioListItem @JvmOverloads constructor( fun setTrailingIconClickListener(onClick: (View) -> Unit) { trailingIconContainer.setOnClickListener { onClick(trailingIconContainer) } } + + fun setChecked(checked: Boolean) { + radioButton.isChecked = checked + } }