-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Jetpack Compose Sample LazyList Banner.
PiperOrigin-RevId: 654936228
- Loading branch information
1 parent
05920e2
commit 2b1fee6
Showing
14 changed files
with
244 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 0 additions & 6 deletions
6
...mo/app/src/main/java/com/google/android/gms/example/jetpackcomposedemo/NavDestinations.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
...c/main/java/com/google/android/gms/example/jetpackcomposedemo/formats/LazyBannerScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
/* | ||
* Copyright 2024 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.android.gms.example.jetpackcomposedemo.formats | ||
|
||
import android.content.Context | ||
import android.util.Log | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.height | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.width | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.foundation.lazy.items | ||
import androidx.compose.material3.CircularProgressIndicator | ||
import androidx.compose.material3.MaterialTheme | ||
import androidx.compose.material3.Surface | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalConfiguration | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.platform.LocalInspectionMode | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
import com.example.jetpackcomposedemo.R | ||
import com.google.android.gms.ads.AdListener | ||
import com.google.android.gms.ads.AdRequest | ||
import com.google.android.gms.ads.AdSize | ||
import com.google.android.gms.ads.AdView | ||
import com.google.android.gms.ads.LoadAdError | ||
import com.google.android.gms.compose_util.BannerAd | ||
import com.google.android.gms.example.jetpackcomposedemo.GoogleMobileAdsApplication.Companion.BANNER_AD_UNIT_ID | ||
import com.google.android.gms.example.jetpackcomposedemo.GoogleMobileAdsApplication.Companion.TAG | ||
import com.google.android.gms.example.jetpackcomposedemo.ui.theme.JetpackComposeDemoTheme | ||
import kotlin.coroutines.resume | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.awaitAll | ||
import kotlinx.coroutines.supervisorScope | ||
import kotlinx.coroutines.suspendCancellableCoroutine | ||
|
||
@Composable | ||
fun LazyBannerScreen(modifier: Modifier = Modifier) { | ||
val context = LocalContext.current | ||
val isPreviewMode = LocalInspectionMode.current | ||
val deviceCurrentWidth = LocalConfiguration.current.screenWidthDp | ||
val adsToLoad = 5 | ||
|
||
// State for loading and completed. | ||
var isLoading by remember { mutableStateOf(true) } | ||
var loadedAds by remember { mutableStateOf<List<AdView>>(emptyList()) } | ||
val fillerText = loadFillerText(context) | ||
|
||
// Load ads when on launch of the composition. | ||
LaunchedEffect(Unit) { | ||
if (!isPreviewMode) { | ||
loadedAds = loadBannerAds(context, adsToLoad, deviceCurrentWidth) | ||
} | ||
isLoading = false | ||
} | ||
|
||
// Display a loading indicator if ads are still being fetched. | ||
if (isLoading) { | ||
CircularProgressIndicator(modifier = modifier.width(100.dp).height(100.dp)) | ||
} else { | ||
// Display a Lazy list of loaded ads. | ||
LazyColumn(verticalArrangement = Arrangement.spacedBy(8.dp)) { | ||
items(loadedAds) { adView -> | ||
Column { | ||
BannerAd(adView, modifier.fillMaxWidth()) | ||
// Display the filler content. | ||
fillerText.forEach { content -> | ||
Box( | ||
modifier | ||
.fillMaxWidth() | ||
.background(MaterialTheme.colorScheme.primaryContainer) | ||
.padding(8.dp) | ||
) { | ||
Text( | ||
text = content, | ||
modifier.padding(8.dp), | ||
style = MaterialTheme.typography.bodyMedium, | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Clean up the AdViews after use. | ||
DisposableEffect(Unit) { onDispose { loadedAds.forEach { adView -> adView.destroy() } } } | ||
} | ||
|
||
@Preview | ||
@Composable | ||
private fun LazyBannerScreenPreview() { | ||
JetpackComposeDemoTheme { | ||
Surface(color = MaterialTheme.colorScheme.background) { LazyBannerScreen() } | ||
} | ||
} | ||
|
||
/** The result of attempting to load an [AdView] from `suspend. */ | ||
sealed interface LoadAdResult { | ||
|
||
/** Result for when an [AdView] successfully loads. */ | ||
class Success(val ad: AdView) : LoadAdResult | ||
|
||
/** Result for when an `AdView` fails to load. */ | ||
class Failure(val error: LoadAdError) : LoadAdResult | ||
} | ||
|
||
private suspend fun loadBannerAd(context: Context, width: Int): LoadAdResult { | ||
return suspendCancellableCoroutine { continuation -> | ||
val adView = AdView(context) | ||
adView.adUnitId = BANNER_AD_UNIT_ID | ||
val adSize = AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(context, width) | ||
adView.setAdSize(adSize) | ||
adView.adListener = | ||
object : AdListener() { | ||
override fun onAdLoaded() { | ||
Log.d(TAG, "Banner ad was loaded.") | ||
continuation.resume(LoadAdResult.Success(adView)) | ||
} | ||
|
||
override fun onAdFailedToLoad(error: LoadAdError) { | ||
Log.e(TAG, "Banner ad failed to load: ${error.message}") | ||
adView.destroy() | ||
continuation.resume(LoadAdResult.Failure(error)) | ||
} | ||
|
||
override fun onAdImpression() { | ||
Log.d(TAG, "Banner ad had an impression.") | ||
} | ||
|
||
override fun onAdClicked() { | ||
Log.d(TAG, "Banner ad was clicked.") | ||
} | ||
} | ||
|
||
continuation.invokeOnCancellation { | ||
// When the coroutine is cancelled, clean up resources. | ||
adView.destroy() | ||
} | ||
|
||
val adRequest = AdRequest.Builder().build() | ||
adView.loadAd(adRequest) | ||
} | ||
} | ||
|
||
private suspend fun loadBannerAds(context: Context, count: Int, width: Int): List<AdView> = | ||
supervisorScope { | ||
List(count) { | ||
async { | ||
when (val result = loadBannerAd(context, width)) { | ||
is LoadAdResult.Failure -> null | ||
is LoadAdResult.Success -> result.ad | ||
} | ||
} | ||
} | ||
.awaitAll() | ||
.filterNotNull() | ||
} | ||
|
||
fun loadFillerText(context: Context): List<String> { | ||
val tipsArray = context.resources.getStringArray(R.array.lorem_ipsum) | ||
return tipsArray.toList() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...example/jetpackcomposedemo/MainUiState.kt → ...le/jetpackcomposedemo/main/MainUiState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...p/src/main/java/com/google/android/gms/example/jetpackcomposedemo/main/NavDestinations.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.google.android.gms.example.jetpackcomposedemo.main | ||
|
||
enum class NavDestinations { | ||
Home, | ||
Banner, | ||
LazyBanner, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.