Skip to content

Commit

Permalink
hook up ui to ViewModel
Browse files Browse the repository at this point in the history
I needed to add a setChecked function to the RadioListItem as it didn't exist and we need to be able to clear sections

I opted to clear everything when a selection is made to make things easier
  • Loading branch information
mikescamell committed Sep 12, 2024
1 parent db1555b commit cd4279d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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?) {
Expand All @@ -38,6 +49,7 @@ class ShowOnAppLaunchActivity : DuckDuckGoActivity() {
setupToolbar(binding.includeToolbar.toolbar)

configureUiEventHandlers()
observeViewModel()
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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")) }
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,8 @@ class RadioListItem @JvmOverloads constructor(
fun setTrailingIconClickListener(onClick: (View) -> Unit) {
trailingIconContainer.setOnClickListener { onClick(trailingIconContainer) }
}

fun setChecked(checked: Boolean) {
radioButton.isChecked = checked
}
}

0 comments on commit cd4279d

Please sign in to comment.