|
|
|
@ -16,6 +16,7 @@
|
|
|
|
|
|
|
|
|
|
package com.google.samples.apps.nowinandroid.feature.foryou
|
|
|
|
|
|
|
|
|
|
import android.net.Uri
|
|
|
|
|
import android.os.Build.VERSION
|
|
|
|
|
import android.os.Build.VERSION_CODES
|
|
|
|
|
import androidx.activity.compose.ReportDrawnWhen
|
|
|
|
@ -63,7 +64,9 @@ import androidx.compose.runtime.LaunchedEffect
|
|
|
|
|
import androidx.compose.runtime.getValue
|
|
|
|
|
import androidx.compose.ui.Alignment
|
|
|
|
|
import androidx.compose.ui.Modifier
|
|
|
|
|
import androidx.compose.ui.graphics.toArgb
|
|
|
|
|
import androidx.compose.ui.layout.layout
|
|
|
|
|
import androidx.compose.ui.platform.LocalContext
|
|
|
|
|
import androidx.compose.ui.platform.LocalDensity
|
|
|
|
|
import androidx.compose.ui.platform.testTag
|
|
|
|
|
import androidx.compose.ui.res.painterResource
|
|
|
|
@ -91,6 +94,7 @@ import com.google.samples.apps.nowinandroid.core.ui.NewsFeedUiState
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.ui.TrackScreenViewEvent
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.ui.TrackScrollJank
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.ui.UserNewsResourcePreviewParameterProvider
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.ui.launchCustomChromeTab
|
|
|
|
|
import com.google.samples.apps.nowinandroid.core.ui.newsFeed
|
|
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
@ -102,12 +106,15 @@ internal fun ForYouRoute(
|
|
|
|
|
val onboardingUiState by viewModel.onboardingUiState.collectAsStateWithLifecycle()
|
|
|
|
|
val feedState by viewModel.feedState.collectAsStateWithLifecycle()
|
|
|
|
|
val isSyncing by viewModel.isSyncing.collectAsStateWithLifecycle()
|
|
|
|
|
val deepLinkedUserNewsResource by viewModel.deepLinkedNewsResource.collectAsStateWithLifecycle()
|
|
|
|
|
|
|
|
|
|
ForYouScreen(
|
|
|
|
|
isSyncing = isSyncing,
|
|
|
|
|
onboardingUiState = onboardingUiState,
|
|
|
|
|
feedState = feedState,
|
|
|
|
|
deepLinkedUserNewsResource = deepLinkedUserNewsResource,
|
|
|
|
|
onTopicCheckedChanged = viewModel::updateTopicSelection,
|
|
|
|
|
onDeepLinkOpened = viewModel::onDeepLinkOpened,
|
|
|
|
|
onTopicClick = onTopicClick,
|
|
|
|
|
saveFollowedTopics = viewModel::dismissOnboarding,
|
|
|
|
|
onNewsResourcesCheckedChanged = viewModel::updateNewsResourceSaved,
|
|
|
|
@ -121,8 +128,10 @@ internal fun ForYouScreen(
|
|
|
|
|
isSyncing: Boolean,
|
|
|
|
|
onboardingUiState: OnboardingUiState,
|
|
|
|
|
feedState: NewsFeedUiState,
|
|
|
|
|
deepLinkedUserNewsResource: UserNewsResource?,
|
|
|
|
|
onTopicCheckedChanged: (String, Boolean) -> Unit,
|
|
|
|
|
onTopicClick: (String) -> Unit,
|
|
|
|
|
onDeepLinkOpened: (String) -> Unit,
|
|
|
|
|
saveFollowedTopics: () -> Unit,
|
|
|
|
|
onNewsResourcesCheckedChanged: (String, Boolean) -> Unit,
|
|
|
|
|
onNewsResourceViewed: (String) -> Unit,
|
|
|
|
@ -205,7 +214,11 @@ internal fun ForYouScreen(
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
TrackScreenViewEvent(screenName = "ForYou")
|
|
|
|
|
requestNotificationsPermission()
|
|
|
|
|
NotificationPermissionEffect()
|
|
|
|
|
DeepLinkEffect(
|
|
|
|
|
deepLinkedUserNewsResource,
|
|
|
|
|
onDeepLinkOpened,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -392,7 +405,7 @@ fun TopicIcon(
|
|
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
|
@OptIn(ExperimentalPermissionsApi::class)
|
|
|
|
|
private fun requestNotificationsPermission() {
|
|
|
|
|
private fun NotificationPermissionEffect() {
|
|
|
|
|
if (VERSION.SDK_INT < VERSION_CODES.TIRAMISU) return
|
|
|
|
|
val notificationsPermissionState = rememberPermissionState(
|
|
|
|
|
android.Manifest.permission.POST_NOTIFICATIONS,
|
|
|
|
@ -405,6 +418,26 @@ private fun requestNotificationsPermission() {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
|
private fun DeepLinkEffect(
|
|
|
|
|
userNewsResource: UserNewsResource?,
|
|
|
|
|
onDeepLinkOpened: (String) -> Unit,
|
|
|
|
|
) {
|
|
|
|
|
val context = LocalContext.current
|
|
|
|
|
val backgroundColor = MaterialTheme.colorScheme.background.toArgb()
|
|
|
|
|
|
|
|
|
|
LaunchedEffect(userNewsResource) {
|
|
|
|
|
if (userNewsResource == null) return@LaunchedEffect
|
|
|
|
|
if (!userNewsResource.hasBeenViewed) onDeepLinkOpened(userNewsResource.id)
|
|
|
|
|
|
|
|
|
|
launchCustomChromeTab(
|
|
|
|
|
context = context,
|
|
|
|
|
uri = Uri.parse(userNewsResource.url),
|
|
|
|
|
toolbarColor = backgroundColor,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@DevicePreviews
|
|
|
|
|
@Composable
|
|
|
|
|
fun ForYouScreenPopulatedFeed(
|
|
|
|
@ -419,11 +452,13 @@ fun ForYouScreenPopulatedFeed(
|
|
|
|
|
feedState = NewsFeedUiState.Success(
|
|
|
|
|
feed = userNewsResources,
|
|
|
|
|
),
|
|
|
|
|
deepLinkedUserNewsResource = null,
|
|
|
|
|
onTopicCheckedChanged = { _, _ -> },
|
|
|
|
|
saveFollowedTopics = {},
|
|
|
|
|
onNewsResourcesCheckedChanged = { _, _ -> },
|
|
|
|
|
onNewsResourceViewed = {},
|
|
|
|
|
onTopicClick = {},
|
|
|
|
|
onDeepLinkOpened = {},
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -443,11 +478,13 @@ fun ForYouScreenOfflinePopulatedFeed(
|
|
|
|
|
feedState = NewsFeedUiState.Success(
|
|
|
|
|
feed = userNewsResources,
|
|
|
|
|
),
|
|
|
|
|
deepLinkedUserNewsResource = null,
|
|
|
|
|
onTopicCheckedChanged = { _, _ -> },
|
|
|
|
|
saveFollowedTopics = {},
|
|
|
|
|
onNewsResourcesCheckedChanged = { _, _ -> },
|
|
|
|
|
onNewsResourceViewed = {},
|
|
|
|
|
onTopicClick = {},
|
|
|
|
|
onDeepLinkOpened = {},
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -470,11 +507,13 @@ fun ForYouScreenTopicSelection(
|
|
|
|
|
feedState = NewsFeedUiState.Success(
|
|
|
|
|
feed = userNewsResources,
|
|
|
|
|
),
|
|
|
|
|
deepLinkedUserNewsResource = null,
|
|
|
|
|
onTopicCheckedChanged = { _, _ -> },
|
|
|
|
|
saveFollowedTopics = {},
|
|
|
|
|
onNewsResourcesCheckedChanged = { _, _ -> },
|
|
|
|
|
onNewsResourceViewed = {},
|
|
|
|
|
onTopicClick = {},
|
|
|
|
|
onDeepLinkOpened = {},
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -489,11 +528,13 @@ fun ForYouScreenLoading() {
|
|
|
|
|
isSyncing = false,
|
|
|
|
|
onboardingUiState = OnboardingUiState.Loading,
|
|
|
|
|
feedState = NewsFeedUiState.Loading,
|
|
|
|
|
deepLinkedUserNewsResource = null,
|
|
|
|
|
onTopicCheckedChanged = { _, _ -> },
|
|
|
|
|
saveFollowedTopics = {},
|
|
|
|
|
onNewsResourcesCheckedChanged = { _, _ -> },
|
|
|
|
|
onNewsResourceViewed = {},
|
|
|
|
|
onTopicClick = {},
|
|
|
|
|
onDeepLinkOpened = {},
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -513,11 +554,13 @@ fun ForYouScreenPopulatedAndLoading(
|
|
|
|
|
feedState = NewsFeedUiState.Success(
|
|
|
|
|
feed = userNewsResources,
|
|
|
|
|
),
|
|
|
|
|
deepLinkedUserNewsResource = null,
|
|
|
|
|
onTopicCheckedChanged = { _, _ -> },
|
|
|
|
|
saveFollowedTopics = {},
|
|
|
|
|
onNewsResourcesCheckedChanged = { _, _ -> },
|
|
|
|
|
onNewsResourceViewed = {},
|
|
|
|
|
onTopicClick = {},
|
|
|
|
|
onDeepLinkOpened = {},
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|