-
Notifications
You must be signed in to change notification settings - Fork 131
/
NativeAdLayout.kt
112 lines (106 loc) · 3.95 KB
/
NativeAdLayout.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.startapp.demo.compose
import android.view.View
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
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.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import com.startapp.sdk.ads.nativead.NativeAdDetails
@Composable
fun NativeAdLayout(ad: NativeAdDetails) {
Box(
modifier = Modifier
.fillMaxWidth()
// NOTE extremely important to specify .height(IntrinsicSize.Max) in order to make the native ad clickable
.height(IntrinsicSize.Max)
.background(color = Color(0xffefefef))
) {
Row {
if (ad.imageBitmap != null) {
Box(
modifier = Modifier
.width(160.dp)
.height(160.dp)
.padding(16.dp),
) {
Image(
bitmap = ad.imageBitmap.asImageBitmap(),
contentDescription = "Ad image",
contentScale = ContentScale.Fit,
modifier = Modifier.fillMaxSize()
)
}
}
Column(
modifier = Modifier
.fillMaxWidth()
.padding(
start = if (ad.imageBitmap != null) 0.dp else 16.dp,
end = 16.dp,
top = 16.dp,
bottom = 16.dp,
),
) {
Text(
text = ad.title,
style = MaterialTheme.typography.titleLarge,
)
Text(
text = "Rating: %.1f⭐".format(ad.rating),
style = MaterialTheme.typography.bodyLarge,
)
Text(
text = "Category: %s".format(ad.category),
style = MaterialTheme.typography.bodyLarge,
)
Text(
text = ad.description,
style = MaterialTheme.typography.bodyMedium,
)
if (ad.callToAction.isNotBlank()) {
// NOTE onClick is configured inside AndroidView after the registerViewForInteraction()
Button(
modifier = Modifier.align(Alignment.End),
onClick = { /* do nothing */ },
) {
Text(ad.callToAction)
}
}
}
}
Text(
text = "Ad",
modifier = Modifier
.align(Alignment.TopEnd)
.padding(16.dp)
.border(1.dp, Color(0xff888888))
.padding(horizontal = 8.dp, vertical = 1.dp),
)
AndroidView(
modifier = Modifier.fillMaxSize(),
factory = { context ->
val result = View(context)
ad.registerViewForInteraction(result)
return@AndroidView result
},
)
}
}