Skip to content

Commit

Permalink
Merge pull request #634 from rymorale/content-cards
Browse files Browse the repository at this point in the history
Add kotlin usage examples for content card / code docs
  • Loading branch information
sbenedicadb authored Jun 26, 2024
2 parents d9d0510 + 6429364 commit 31103b2
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
noIndex: true
---

<Variant platform="android" api="get-propositions-for-surfaces" repeat="6"/>
<Variant platform="android" api="get-propositions-for-surfaces" repeat="9"/>

#### Java

Expand All @@ -15,7 +15,31 @@ public static void getPropositionsForSurfaces(@NonNull final List<Surface> surfa
* _surfaces_ is a list of surfaces for which propositions are requested.
* _callback_ `call` method is invoked with propositions map of type `Map<Surface, List<Proposition>>`. If the callback is an instance of [AdobeCallbackWithError](../../../../home/base/mobile-core/api-reference.md#adobecallbackwitherror), and if the operation times out or an error occurs in retrieving propositions, the `fail` method is invoked with the appropriate [AdobeError](../../../../home/base/mobile-core/api-reference.md#adobeerror).

**Syntax**
**Example**

#### Kotlin

```kotlin
val surface1 = Surface("myActivity#button")
val surface2 = Surface("myActivityAttributes")
val surfaces = listOf(surface1, surface2)

Messaging.getPropositionsForSurfaces(surfaces) {
it?.let { propositionsMap ->
if (propositionsMap.isNotEmpty()) {
// get the propositions for the given surfaces
propositionsMap[surface1]?.let {
// read surface1 propositions
}
propositionsMap[surface2]?.let {
// read surface2 propositions
}
}
}
}
```

#### Java

```java
final Surface surface1 = new Surface("myActivity#button");
Expand Down Expand Up @@ -61,7 +85,7 @@ static func getPropositionsForSurfaces(_ surfacePaths: [Surface], _ completion:
* _surfaces_ is an array of surfaces for which propositions are requested.
* _completion_ is invoked with propositions dictionary of type `[Surface: [Proposition]]`. An `Error` is returned if SDK fails to retrieve the propositions.

**Syntax**
**Example**

```swift
let surface1 = Surface(path: "myView#button")
Expand Down Expand Up @@ -101,7 +125,7 @@ Messaging.getPropositionsForSurfaces([surface1, surface2]) { propositionsDict, e
* _surfaces_ is an array of surfaces for which propositions are requested.
* _completion_ is invoked with propositions dictionary of type `NSDictionary<AEPSurface*, NSArray<AEPProposition*>*>`. An `NSError` is returned if SDK fails to retrieve the propositions.

**Syntax**
**Example**

```objc
AEPSurface* surface1 = [[AEPSurface alloc] initWithPath: @"myView#button"];
Expand All @@ -122,7 +146,7 @@ AEPSurface* surface2 = [[AEPSurface alloc] initWithPath: @"myView#button"];
}];
```
<Variant platform="android" api="update-propositions-for-surfaces" repeat="6"/>
<Variant platform="android" api="update-propositions-for-surfaces" repeat="9"/>
#### Java
Expand All @@ -134,7 +158,19 @@ public static void updatePropositionsForSurfaces(@NonNull final List<Surface> su

* _surfaces_ is a list of surfaces for which propositions need updating.

**Syntax**
**Example**

#### Kotlin

```kotlin
val surface1 = Surface("myActivity#button")
val surface2 = Surface("myActivityAttributes")
val surfaces = listOf(surface1, surface2)

Messaging.updatePropositionsForSurfaces(surfaces)
```

#### Java

```java
final Surface surface1 = new Surface("myActivity#button");
Expand All @@ -159,7 +195,7 @@ static func updatePropositionsForSurfaces(_ surfaces: [Surface])

* _surfaces_ is an array of surfaces for which propositions need updating.

**Syntax**
**Example**

```swift
let surface1 = Surface(path: "myView#button")
Expand All @@ -178,7 +214,7 @@ Messaging.updatePropositionsForSurfaces([surface1, surface2])

* _surfaces_ is an array of surfaces for which propositions need updating.

**Syntax**
**Example**

```objc
AEPSurface* surface1 = [[AEPSurface alloc] initWithPath: @"myView#button"];
Expand Down
84 changes: 79 additions & 5 deletions src/pages/edge/adobe-journey-optimizer/code-based/tabs/tutorial.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
<Variant platform="android" function="updatepropositionsforsurfaces" repeat="2"/>
<Variant platform="android" function="updatepropositionsforsurfaces" repeat="4"/>

#### Kotlin

```kotlin
val surface1 = Surface("mainActivity#banner")
val surface2 = Surface("secondActivity#promotions")
val surfaces = listOf(surface1, surface2)

// fetch propositions from server and cache in-memory
Messaging.updatePropositionsForSurfaces(surfaces)
```

#### Java

Expand Down Expand Up @@ -26,7 +37,29 @@ let surface2 = Surface("secondActivity#promotions")
Messaging.updatePropositionsForSurfaces([surface1, surface2])
```

<Variant platform="android" function="getpropositionsforsurfaces" repeat="2"/>
<Variant platform="android" function="getpropositionsforsurfaces" repeat="4"/>

#### Kotlin

```kotlin
val surface1 = Surface("mainActivity#banner")
val surface2 = Surface("secondActivity#promotions")
val surfaces = listOf(surface1, surface2)

Messaging.getPropositionsForSurfaces(surfaces) {
it?.let { propositionsMap ->
if (propositionsMap.isNotEmpty()) {
// get the propositions for the given surfaces
propositionsMap[surface1]?.let {
// read surface1 propositions
}
propositionsMap[surface2]?.let {
// read surface2 propositions
}
}
}
}
```

#### Java

Expand Down Expand Up @@ -91,7 +124,25 @@ Messaging.getPropositionsForSurfaces([surface1, surface2]) { propositionsDict, e
}
```

<Variant platform="android" function="using-propositions" repeat="2"/>
<Variant platform="android" function="using-propositions" repeat="4"/>

#### Kotlin

```kotlin
// get the propositions for surface1
// bail early if no propositions are found for surface1
if (propositionsForSurface1 == null || propositionsForSurface1.isEmpty()) return

// iterate through items in proposition
for (propositionItem in propositionsForSurface1.first()::items) {
if (propositionItem.schema == SchemaType.HTML_CONTENT) {
// retrieve the HTML content
val htmlContent = propositionItem.htmlContent

// use retrieved html content
}
}
```

#### Java

Expand Down Expand Up @@ -132,7 +183,19 @@ if let codePropositions: [Proposition] = propositionsDict?[surface1], !codePropo
}
```

<Variant platform="android" function="track" repeat="2"/>
<Variant platform="android" function="track" repeat="4"/>

#### Kotlin

```kotlin
// Tracking display of PropositionItem
// use the same propositionItem object that was used to get the content in the previous section
propositionItem.track(MessagingEdgeEventType.DISPLAY)

// Tracking interaction with PropositionItem
// use the same propositionItem object that was used to get the content in the previous section
propositionItem.track("click", MessagingEdgeEventType.INTERACT, null)
```

#### Java

Expand Down Expand Up @@ -160,7 +223,18 @@ propositionItem.track(withEdgeEventType: MessagingEdgeEventType.display)
propositionItem.track("click", withEdgeEventType: MessagingEdgeEventType.display)
```

<Variant platform="android" function="track-with-tokens" repeat="2"/>
<Variant platform="android" function="track-with-tokens" repeat="4"/>

#### Kotlin

```kotlin
// Tracking interaction with PropositionItem with tokens
// Extract the tokens from the PropositionItem item data
val tokenList = mutableListOf<String>()
tokenList += dataItemToken1
tokenList += dataItemToken2
propositionItem.track("click", MessagingEdgeEventType.INTERACT, tokenList)
```

#### Java

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ This document describes how to fetch, display and track code-based experiences u

## Pre-requisites

[Integrate and register Messaging extension](../index.md#implement-extension-in-mobile-app) in your app.
[Integrate and register Messaging extension](../../index.md#implement-extension-in-mobile-app) in your app.

## Fetch and cache the code-based content

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public class ContentCardSchemaData implements SchemaData {

Tries to convert the `content` of this `ContentCardSchemaData` into a [`ContentCard`](./content-card.md) object.

Returns `nil` if the `contentType` is not equal to `.applicationJson` or the data in `content` is not decodable into a `ContentCard`.
Returns `null` if the `contentType` is not equal to `ContentType.APPLICATION_JSON` or the data in `content` is not decodable into a `ContentCard`.

#### Syntax

Expand All @@ -147,14 +147,25 @@ Returns `nil` if the `contentType` is not equal to `.applicationJson` or the dat

#### Example

#### Kotlin

```kotlin
val propisitionItem: PropositionItem
val contentCardData = item?.contentCardSchemaData
val contentCard = contentCardData?.contentCard
contentCard?.let {
// do something with the ContentCard object
}
```

#### Java

```java
PropositionItem propositionItem;
ContentCardSchemaData contentCardSchemaData = propositionItem.getContentCardSchemaData();
if (contentCardSchemaData != null) {
ContentCard contentCard = contentCardSchemaData.getContentCard();
if (contentCard != null) {
// do something with the ContentCard object
}
ContentCard contentCard = contentCardSchemaData.getContentCard();
if (contentCard != null) {
// do something with the ContentCard object
}
```

Expand All @@ -175,12 +186,26 @@ public void track(final String interaction, final MessagingEdgeEventType eventTy

#### Example

#### Kotlin

```kotlin
val contentCardSchemaData = item?.contentCardSchemaData

// tracking a display
contentCardSchemaData?.track(null, MessagingEdgeEventType.DISPLAY)

// tracking a user interaction
contentCardSchemaData?.track("itemSelected", MessagingEdgeEventType.INTERACT)
```

#### Java

```java
ContentCardSchemaData contentCardSchemaData;

// tracking a display
contentCardSchemaData.track(null, MessagingEdgeEventType.display);
contentCardSchemaData.track(null, MessagingEdgeEventType.DISPLAY);

// tracking a user interaction
contentCardSchemaData.track("itemSelected", MessagingEdgeEventType.interact);
contentCardSchemaData.track("itemSelected", MessagingEdgeEventType.INTERACT);
```
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,30 @@ public void track(final String interaction, final MessagingEdgeEventType eventTy

#### Example

#### Kotlin

```kotlin
// Get content card schema data from a PropositionItem object
val contentCardData = item?.contentCardSchemaData
val contentCard = contentCardData?.contentCard

// tracking a display
contentCard?.track(null, MessagingEdgeEventType.DISPLAY)

// tracking a user interaction
contentCard?.track("itemSelected", MessagingEdgeEventType.INTERACT)
```

#### Java

```java
ContentCard contentCard;
// Get content card schema data from a PropositionItem object
ContentCardSchemaData contentCardData = item.getContentCardSchemaData();
ContentCard contentCard = contentCardData.getContentCard();

// tracking a display
contentCard.track(null, MessagingEdgeEventType.display);
contentCard.track(null, MessagingEdgeEventType.DISPLAY);

// tracking a user interaction
contentCard.track("itemSelected", MessagingEdgeEventType.interact);
contentCard.track("itemSelected", MessagingEdgeEventType.INTERACT);
```
Loading

0 comments on commit 31103b2

Please sign in to comment.