Skip to content

Commit

Permalink
more work
Browse files Browse the repository at this point in the history
  • Loading branch information
malmstein committed Apr 26, 2024
1 parent e147486 commit 031ee35
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 39 deletions.
63 changes: 43 additions & 20 deletions app/src/main/java/com/duckduckgo/app/browser/BrowserTabFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -613,13 +613,15 @@ class BrowserTabFragment :

private val omnibarInputTextWatcher = object : TextChangedWatcher() {
override fun afterTextChanged(editable: Editable) {
Timber.d("New Tab: triggerAutocomplete afterTextChanged")
viewModel.onOmnibarInputStateChanged(omnibar.omnibarTextInput.text.toString(), omnibar.omnibarTextInput.hasFocus(), true)
viewModel.triggerAutocomplete(omnibar.omnibarTextInput.text.toString(), omnibar.omnibarTextInput.hasFocus(), true)
}
}

private val showSuggestionsListener = object : ShowSuggestionsListener {
override fun showSuggestions() {
Timber.d("New Tab: triggerAutocomplete showSuggestions")
viewModel.triggerAutocomplete(omnibar.omnibarTextInput.text.toString(), omnibar.omnibarTextInput.hasFocus(), true)
}
}
Expand Down Expand Up @@ -763,8 +765,6 @@ class BrowserTabFragment :
}
}

private val homeBackgroundLogo by lazy { HomeBackgroundLogo(newBrowserTab.ddgLogo) }

private val ctaViewStateObserver = Observer<CtaViewState> {
it?.let { renderer.renderCtaViewState(it) }
}
Expand Down Expand Up @@ -1248,6 +1248,10 @@ class BrowserTabFragment :
}
}

fun hideFocusedView() {
showHome()
}

fun submitQuery(query: String) {
viewModel.onUserSubmittedQuery(query)
}
Expand Down Expand Up @@ -2156,6 +2160,7 @@ class BrowserTabFragment :
private fun configureOmnibarTextInput() {
omnibar.omnibarTextInput.onFocusChangeListener =
OnFocusChangeListener { _, hasFocus: Boolean ->
Timber.d("New Tab: triggerAutocomplete OnFocusChangeListener")
viewModel.onOmnibarInputStateChanged(omnibar.omnibarTextInput.text.toString(), hasFocus, false)
viewModel.triggerAutocomplete(omnibar.omnibarTextInput.text.toString(), hasFocus, false)
if (hasFocus) {
Expand Down Expand Up @@ -2780,7 +2785,6 @@ class BrowserTabFragment :
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)

newBrowserTab.ddgLogo.setImageResource(com.duckduckgo.mobile.android.R.drawable.logo_full)
if (newBrowserTab.ctaContainer.isNotEmpty()) {
renderer.renderHomeCta()
}
Expand Down Expand Up @@ -3421,21 +3425,44 @@ class BrowserTabFragment :
fun renderAutocomplete(viewState: AutoCompleteViewState) {
renderIfChanged(viewState, lastSeenAutoCompleteViewState) {
lastSeenAutoCompleteViewState = viewState

if (viewState.showSuggestions || viewState.showFavorites) {
if (viewState.favorites.isNotEmpty() && viewState.showFavorites) {
binding.autoCompleteSuggestionsList.gone()
focusedView.rootFocusedView.show()
omnibarQuickAccessAdapter.submitList(viewState.favorites)
Timber.d(
"New Tab: showSuggestions ${viewState.showSuggestions} showFavorites ${viewState.showFavorites} empty favourites ${viewState.favorites.isEmpty()}",
)
if (viewState.showSuggestions) {
binding.autoCompleteSuggestionsList.show()
focusedView.rootFocusedView.gone()
autoCompleteSuggestionsAdapter.updateData(viewState.searchResults.query, viewState.searchResults.suggestions)
} else if (viewState.showFavorites) {
focusedView.rootFocusedView.show()
// consume clicks so the view is not hidden after losing focus
focusedView.rootFocusedView.setOnClickListener { }
if (viewState.favorites.isEmpty()) {
focusedView.quickAccessRecyclerViewEmpty.show()
focusedView.quickAccessSuggestionsRecyclerView.gone()
} else {
binding.autoCompleteSuggestionsList.show()
focusedView.rootFocusedView.gone()
autoCompleteSuggestionsAdapter.updateData(viewState.searchResults.query, viewState.searchResults.suggestions)
focusedView.quickAccessRecyclerViewEmpty.gone()
focusedView.quickAccessSuggestionsRecyclerView.show()
omnibarQuickAccessAdapter.submitList(viewState.favorites)
}
} else {
binding.autoCompleteSuggestionsList.gone()
focusedView.rootFocusedView.gone()
}
// if (viewState.showSuggestions || viewState.showFavorites) {
// if (viewState.showFavorites) {
// binding.autoCompleteSuggestionsList.gone()
// focusedView.rootFocusedView.show()
// omnibarQuickAccessAdapter.submitList(viewState.favorites)
// } else {
// binding.autoCompleteSuggestionsList.show()
// focusedView.rootFocusedView.gone()
// autoCompleteSuggestionsAdapter.updateData(viewState.searchResults.query, viewState.searchResults.suggestions)
// }
// } else {
//
// binding.autoCompleteSuggestionsList.gone()
// focusedView.rootFocusedView.gone()
// }
}
}

Expand Down Expand Up @@ -3693,7 +3720,7 @@ class BrowserTabFragment :

viewState.message != null -> {
showRemoteMessage(viewState.message, newMessage)
showHomeBackground(viewState.favorites, hideLogo = true)
showHomeBackground(viewState.favorites)
hideHomeCta()
}

Expand Down Expand Up @@ -3861,16 +3888,13 @@ class BrowserTabFragment :
viewModel.onCtaShown()
}

private fun showHomeBackground(
favorites: List<QuickAccessFavorite>,
hideLogo: Boolean = false,
) {
private fun showHomeBackground(favorites: List<QuickAccessFavorite>) {
if (favorites.isEmpty()) {
if (hideLogo) homeBackgroundLogo.hideLogo() else homeBackgroundLogo.showLogo()
quickAccessItems.quickAccessRecyclerView.gone()
quickAccessItems.quickAccessRecyclerViewEmpty.show()
} else {
homeBackgroundLogo.hideLogo()
quickAccessAdapter.submitList(favorites)
quickAccessItems.quickAccessRecyclerViewEmpty.gone()
quickAccessItems.quickAccessRecyclerView.show()
with(quickAccessItems.newTabShortcutBookmarks) {
setClickListener {
Expand All @@ -3884,7 +3908,6 @@ class BrowserTabFragment :
}

private fun hideHomeBackground() {
homeBackgroundLogo.hideLogo()
newBrowserTab.newTabQuickAccessItemsLayout.gone()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ class BrowserTabViewModel @Inject constructor(
.flowOn(dispatchers.io())
.onEach { filteredFavourites ->
withContext(dispatchers.main()) {
Timber.d("New Tab: filteredFavourites")
val favorites = filteredFavourites.map { FavoritesQuickAccessAdapter.QuickAccessFavorite(it) }
ctaViewState.value = currentCtaViewState().copy(favorites = favorites)
autoCompleteViewState.value = currentAutoCompleteViewState().copy(favorites = favorites)
Expand Down Expand Up @@ -606,6 +607,7 @@ class BrowserTabViewModel @Inject constructor(

private fun onAutoCompleteResultReceived(result: AutoCompleteResult) {
val currentViewState = currentAutoCompleteViewState()
Timber.d("New Tab: onAutoCompleteResultReceived")
autoCompleteViewState.value = currentViewState.copy(searchResults = AutoCompleteResult(result.query, result.suggestions))
}

Expand Down Expand Up @@ -806,6 +808,7 @@ class BrowserTabViewModel @Inject constructor(
browserError = OMITTED,
sslError = NONE,
)
Timber.d("New Tab: onUserSubmittedQuery")
autoCompleteViewState.value =
currentAutoCompleteViewState().copy(showSuggestions = false, showFavorites = false, searchResults = AutoCompleteResult("", emptyList()))
}
Expand Down Expand Up @@ -963,6 +966,7 @@ class BrowserTabViewModel @Inject constructor(
* @return true if navigation handled, otherwise false
*/
fun onUserPressedBack(): Boolean {
Timber.d("New Tab: onUserPressedBack")
navigationAwareLoginDetector.onEvent(NavigationEvent.UserAction.NavigateBack)
val navigation = webNavigationState ?: return false
val hasSourceTab = tabRepository.liveSelectedTab.value?.sourceTabId != null
Expand All @@ -978,10 +982,18 @@ class BrowserTabViewModel @Inject constructor(
}

if (!currentBrowserViewState().browserShowing) {
Timber.d("New Tab: onUserPressedBack browser not showing")
return false
}

if (currentAutoCompleteViewState().showSuggestions) {
Timber.d("New Tab: onUserPressedBack focused view showing")
autoCompleteViewState.value = currentAutoCompleteViewState().copy(showFavorites = false, showSuggestions = false)
return true
}

if (navigation.canGoBack) {
Timber.d("New Tab: onUserPressedBack navigation can go back")
command.value = NavigationCommand.NavigateBack(navigation.stepsToPreviousPage)
return true
} else if (hasSourceTab) {
Expand All @@ -990,6 +1002,7 @@ class BrowserTabViewModel @Inject constructor(
}
return true
} else if (!skipHome) {
Timber.d("New Tab: onUserPressedBack navigate home")
navigateHome()
command.value = ShowKeyboard
return true
Expand Down Expand Up @@ -1799,6 +1812,10 @@ class BrowserTabViewModel @Inject constructor(
hasFocus: Boolean,
hasQueryChanged: Boolean,
) {
// business logic
// if omnibar focused and query is not blank and suggestions enabled -> show autocomplete suggestions
//

// determine if empty list to be shown, or existing search results
val autoCompleteSearchResults = if (query.isBlank() || !hasFocus) {
AutoCompleteResult(query, emptyList())
Expand All @@ -1811,20 +1828,21 @@ class BrowserTabViewModel @Inject constructor(
val showFavoritesAsSuggestions = if (!showAutoCompleteSuggestions) {
val urlFocused = hasFocus && query.isNotBlank() && !hasQueryChanged && UriString.isWebUrl(query)
val emptyQueryBrowsing = query.isBlank() && currentBrowserViewState().browserShowing
val favoritesAvailable = currentAutoCompleteViewState().favorites.isNotEmpty()
hasFocus && (urlFocused || emptyQueryBrowsing) && favoritesAvailable
// val favoritesAvailable = currentAutoCompleteViewState().favorites.isNotEmpty()
hasFocus && (urlFocused || emptyQueryBrowsing)
} else {
false
}

Timber.d("New Tab: triggerAutocomplete hasFocus $hasFocus")
autoCompleteViewState.value = currentAutoCompleteViewState()
.copy(
showSuggestions = showAutoCompleteSuggestions,
showFavorites = showFavoritesAsSuggestions,
searchResults = autoCompleteSearchResults,
)

if (hasFocus && autoCompleteSuggestionsEnabled) {
if (showAutoCompleteSuggestions) {
autoCompletePublishSubject.accept(query.trim())
}
}
Expand Down Expand Up @@ -2258,6 +2276,7 @@ class BrowserTabViewModel @Inject constructor(
}

private fun initializeDefaultViewStates() {
Timber.d("New Tab: initialize default values")
globalLayoutState.value = Browser()
browserViewState.value = BrowserViewState()
loadingViewState.value = LoadingViewState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ data class BrowserViewState(
val browserError: WebViewErrorResponse = WebViewErrorResponse.OMITTED,
val sslError: SSLErrorType = SSLErrorType.NONE,
val privacyProtectionsPopupViewState: PrivacyProtectionsPopupViewState = PrivacyProtectionsPopupViewState.Gone,
val showFocusedView: Boolean = false,
)

sealed class HighlightableButton {
Expand Down
39 changes: 35 additions & 4 deletions app/src/main/res/layout/include_focused_quick_access_items.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rootFocusedView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_height="match_parent"
android:background="?attr/daxColorSurface"
android:elevation="2dp"
android:orientation="vertical"
android:visibility="gone"
android:fillViewport="true"
android:clipChildren="false"
tools:visibility="visible"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:paddingTop="15dp">
Expand All @@ -45,15 +47,44 @@
android:id="@+id/quickAccessSuggestionsRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/keyline_2"
android:layout_marginEnd="@dimen/keyline_2"
android:clipToPadding="false"
android:overScrollMode="never"
tools:itemCount="8"
tools:listItem="@layout/view_quick_access_item"
tools:showIn="@layout/activity_system_search"
tools:spanCount="4" />

<LinearLayout
android:id="@+id/quickAccessRecyclerViewEmpty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/keyline_2"
android:layout_marginEnd="@dimen/keyline_2"
android:visibility="gone"
android:orientation="horizontal">

<com.duckduckgo.common.ui.view.griditem.DaxNewTabGridItem
app:placeholder="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<com.duckduckgo.common.ui.view.griditem.DaxNewTabGridItem
app:placeholder="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<com.duckduckgo.common.ui.view.griditem.DaxNewTabGridItem
app:placeholder="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<com.duckduckgo.common.ui.view.griditem.DaxNewTabGridItem
app:placeholder="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

<com.duckduckgo.common.ui.view.text.DaxTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand All @@ -64,7 +95,7 @@
app:textType="primary"
app:typography="h4" />

<com.duckduckgo.common.ui.view.DaxNewTabGridItem
<com.duckduckgo.common.ui.view.griditem.DaxNewTabGridItem
android:id="@+id/newTabShortcutBookmarks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down
43 changes: 36 additions & 7 deletions app/src/main/res/layout/include_quick_access_items.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="@dimen/keyline_2">
android:orientation="vertical">

<com.duckduckgo.common.ui.view.text.DaxTextView
android:layout_width="match_parent"
Expand All @@ -42,14 +41,45 @@
android:layout_height="wrap_content"
android:clipToPadding="false"
android:overScrollMode="never"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:itemCount="8"
android:layout_marginStart="@dimen/keyline_2"
android:layout_marginEnd="@dimen/keyline_2"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:itemCount="8"
tools:listItem="@layout/view_quick_access_item"
tools:showIn="@layout/activity_system_search"
tools:spanCount="4" />

<LinearLayout
android:id="@+id/quickAccessRecyclerViewEmpty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/keyline_2"
android:layout_marginEnd="@dimen/keyline_2"
android:visibility="gone"
android:orientation="horizontal">

<com.duckduckgo.common.ui.view.griditem.DaxNewTabGridItem
app:placeholder="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<com.duckduckgo.common.ui.view.griditem.DaxNewTabGridItem
app:placeholder="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<com.duckduckgo.common.ui.view.griditem.DaxNewTabGridItem
app:placeholder="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<com.duckduckgo.common.ui.view.griditem.DaxNewTabGridItem
app:placeholder="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

<com.duckduckgo.common.ui.view.text.DaxTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand All @@ -60,12 +90,11 @@
app:textType="primary"
android:text="@string/newTabPageShortcuts" />

<com.duckduckgo.common.ui.view.DaxNewTabGridItem
<com.duckduckgo.common.ui.view.griditem.DaxNewTabGridItem
android:id="@+id/newTabShortcutBookmarks"
android:layout_marginStart="@dimen/keyline_2"
app:primaryText="@string/newTabPageShortcutBookmarks"
app:leadingIcon="@drawable/ic_folder_24"
app:placeholder="true"
app:leadingIcon="@drawable/ic_bookmark_20"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

Expand Down

0 comments on commit 031ee35

Please sign in to comment.