Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow filter to be augmented for Install App prompt #2070

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion datalayer/phone-ui/api/current.api
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package com.google.android.horologist.datalayer.phone.ui.prompt.installapp {
@com.google.android.horologist.annotations.ExperimentalHorologistApi public final class InstallAppPrompt {
ctor public InstallAppPrompt(com.google.android.horologist.datalayer.phone.PhoneDataLayerAppHelper phoneDataLayerAppHelper);
method public android.content.Intent getIntent(android.content.Context context, String appPackageName, @DrawableRes int image, String topMessage, String bottomMessage);
method public suspend Object? shouldDisplayPrompt(kotlin.coroutines.Continuation<? super com.google.android.horologist.data.apphelper.AppHelperNodeStatus>);
method public suspend Object? shouldDisplayPrompt(optional kotlin.jvm.functions.Function1<? super com.google.android.horologist.data.apphelper.AppHelperNodeStatus,java.lang.Boolean>? predicate, optional kotlin.coroutines.Continuation<? super com.google.android.horologist.data.apphelper.AppHelperNodeStatus>);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,25 @@ import com.google.android.horologist.datalayer.phone.PhoneDataLayerAppHelper

@ExperimentalHorologistApi
public class InstallAppPrompt(private val phoneDataLayerAppHelper: PhoneDataLayerAppHelper) {

/**
* Returns a [AppHelperNodeStatus] that meets the criteria to show this prompt, otherwise
* returns null.
*
* @param predicate augments the criteria applying a [filter][List.filter] with this predicate.
*/
public suspend fun shouldDisplayPrompt(): AppHelperNodeStatus? =
phoneDataLayerAppHelper.connectedNodes().firstOrNull { !it.appInstalled }
public suspend fun shouldDisplayPrompt(
predicate: ((AppHelperNodeStatus) -> Boolean)? = null,
): AppHelperNodeStatus? =
phoneDataLayerAppHelper.connectedNodes()
.filter { !it.appInstalled }
.let {
if (predicate != null) {
it.filter(predicate)
} else {
it
}
}
.firstOrNull()

/**
* Returns the [Intent] to display an install app prompt to the user.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@ import android.content.Intent
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Checkbox
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
Expand Down Expand Up @@ -75,7 +80,7 @@ fun InstallAppPromptDemoScreen(
@Composable
fun InstallAppPromptDemoScreen(
state: InstallAppPromptDemoScreenState,
onRunDemoClick: () -> Unit,
onRunDemoClick: (shouldFilterByNearby: Boolean) -> Unit,
getInstallPromptIntent: () -> Intent,
onInstallPromptLaunched: () -> Unit,
onInstallPromptInstallClick: () -> Unit,
Expand All @@ -92,13 +97,23 @@ fun InstallAppPromptDemoScreen(
}
}

var shouldFilterByNearby by remember { mutableStateOf(false) }

Column(
modifier = modifier.padding(all = 10.dp),
) {
Text(text = stringResource(id = R.string.install_app_prompt_api_call_demo_message))

Row(
modifier = Modifier.padding(top = 10.dp),
verticalAlignment = Alignment.CenterVertically,
) {
Checkbox(checked = shouldFilterByNearby, onCheckedChange = { shouldFilterByNearby = it })
Text(text = stringResource(id = R.string.install_app_prompt_checkbox_label))
}

Button(
onClick = onRunDemoClick,
onClick = { onRunDemoClick(shouldFilterByNearby) },
modifier = Modifier
.padding(top = 10.dp)
.align(Alignment.CenterHorizontally),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.google.android.horologist.datalayer.sample.screens.inappprompts.inst
import androidx.annotation.MainThread
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.android.horologist.data.apphelper.AppHelperNodeStatus
import com.google.android.horologist.datalayer.phone.PhoneDataLayerAppHelper
import com.google.android.horologist.datalayer.phone.ui.prompt.installapp.InstallAppPrompt
import dagger.hilt.android.lifecycle.HiltViewModel
Expand Down Expand Up @@ -57,11 +58,16 @@ class InstallAppPromptDemoViewModel
}
}

fun onRunDemoClick() {
fun onRunDemoClick(shouldFilterByNearby: Boolean) {
_uiState.value = InstallAppPromptDemoScreenState.Loading

viewModelScope.launch {
val node = installAppPrompt.shouldDisplayPrompt()
val filter = if (shouldFilterByNearby) {
{ node: AppHelperNodeStatus -> node.isNearby }
} else {
null
}
val node = installAppPrompt.shouldDisplayPrompt(filter)

_uiState.value = if (node != null) {
InstallAppPromptDemoScreenState.WatchFound
Expand Down
1 change: 1 addition & 0 deletions datalayer/sample/phone/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@

<!-- In-app prompt - Install App Demo screen -->
<string name="install_app_prompt_api_call_demo_message">This demo calls the Horologist API to show the install app prompt for the watch demo app. The API will only display the prompt if there is a watch connected and the watch does not have the app installed.\n\nGoogle Play won\'t find this app as it is not published.</string>
<string name="install_app_prompt_checkbox_label">Filter by nearby watches</string>
<string name="install_app_prompt_run_demo_button_label">Run demo</string>
<string name="install_app_prompt_demo_prompt_top_message">Test the interactions between the phone and the watch with the demo app.</string>
<string name="install_app_prompt_demo_prompt_bottom_message">Install the demo app on your Wear OS watch.</string>
Expand Down
Loading