Skip to content
This repository was archived by the owner on Dec 30, 2021. It is now read-only.

Commit b60b871

Browse files
committed
Update README
1 parent c59c715 commit b60b871

File tree

2 files changed

+208
-2
lines changed

2 files changed

+208
-2
lines changed
152 KB
Loading

README.md

Lines changed: 208 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,209 @@
1-
# AdaptiveCard
1+
# AdaptiveCardUI
2+
[![CI](https://github.com/gonzalezreal/AdaptiveCardUI/workflows/CI/badge.svg)](https://github.com/gonzalezreal/AdaptiveCardUI/actions?query=workflow%3ACI)
3+
![Swift 5.3](https://img.shields.io/badge/Swift-5.3-blue.svg)
4+
![Platforms](https://img.shields.io/badge/Platforms-macOS%20|%20iOS%20|%20tvOS%20|%20watchOS-blue.svg?style=flat)
5+
[![Twitter: @gonzalezreal](https://img.shields.io/badge/twitter-@gonzalezreal-blue.svg?style=flat)](https://twitter.com/gonzalezreal)
26

3-
A description of this package.
7+
AdaptiveCardUI is a library for rendering Adaptive Cards in SwiftUI.
8+
9+
Adaptive Cards are snippets of UI, authored in JSON, that apps and services can openly exchange. AdaptiveCardUI transforms this JSON into a native SwiftUI view tree, allowing the integration of lightweight UI into your SwiftUI app.
10+
11+
* [Motivation](#motivation)
12+
* [Adaptive Card Basics](#adaptive-card-basics)
13+
* [Displaying Adaptive Cards](#displaying-adaptive-cards)
14+
* [Customizing Appearance](#customizing-appearance)
15+
* [Adding Custom Elements](#adding-custom-elements)
16+
* [Compatibility](#compatibility)
17+
* [Installation](#installation)
18+
* [Other Libraries](#other-libraries)
19+
20+
## Motivation
21+
Microsoft builds and maintains the Adaptive Card schema. They have SDKs available for [several platforms](https://github.com/microsoft/AdaptiveCards), including a [UIKit Objective-C++ based implementation](https://github.com/microsoft/AdaptiveCards/tree/main/source/ios) for iOS.
22+
23+
AdaptiveCardUI aims to provide a more natural experience for Swift developers:
24+
* It uses Swift `Codable` for decoding and encoding Adaptive Cards
25+
* Allows appearance customization through view modifiers and the SwiftUI `Environment`
26+
* Supports extensibility through Custom Elements
27+
* Supports Dark Mode and Dynamic Type
28+
* Works on all the platforms where SwiftUI is present
29+
30+
## Adaptive Card Basics
31+
Adaptive Cards are a great way to display and interact with data, clearly and consistently. You can use them to display rich text alongside images, allow users to interact with buttons, or even collect form data.
32+
33+
Here is a simple card that includes a text, followed by an image and a button.
34+
35+
```json
36+
{
37+
"type": "AdaptiveCard",
38+
"version": "1.3",
39+
"body": [
40+
{
41+
"type": "TextBlock",
42+
"text": "Here is very cute dog"
43+
},
44+
{
45+
"type": "Image",
46+
"url": "https://picsum.photos/id/237/300"
47+
}
48+
],
49+
"actions": [
50+
{
51+
"type": "Action.OpenUrl",
52+
"url": "https://picsum.photos",
53+
"title": "Lorem Picsum Photos"
54+
}
55+
]
56+
}
57+
```
58+
59+
The root object is the Adaptive Card itself and specifies the `version` required to display it. The `body` is composed of building blocks known as *elements*. You can arrange them in different ways to create many types of cards. Additionally, Adaptive Cards may include `actions` on which the user can act.
60+
61+
The most fundamental elements of an Adaptive Card are:
62+
* `TextBlock` - a block of text and its appearance
63+
* `RichTextBlock` - allows for inline text formatting
64+
* `Image` - an image located in a URL and its appearance
65+
66+
Adaptive Cards can also have containers, which arrange a collection of child elements.
67+
* `Container` - a collection of elements stacked vertically
68+
* `ColumnSet` - divides a region into Columns, allowing elements to sit side-by-side
69+
* `ActionSet` - displays a set of actions
70+
* `FactSet` - displays a series of name/value pairs in a tabular form
71+
72+
With just these few elements, you can design Adaptive Cards with quite complex layouts.
73+
74+
![Screenshot](Examples/AdaptiveCardVisualizer/Screenshot.png)
75+
76+
Adaptive Cards can collect form data using `Input` elements. AdaptiveCardUI can't render `Input` elements yet, but we plan to support them [soon](https://github.com/gonzalezreal/AdaptiveCardUI/issues?q=is%3Aissue+is%3Aopen+label%3Aenhancement).
77+
78+
### Learn More
79+
* Run the [AdaptiveCardVisualizer example](Examples/AdaptiveCardVisualizer)
80+
* Requires Xcode 12 and Swift 5.3. If you want to try the macOS version, you will need to install macOS Big Sur 11 Beta 9 and Xcode 12.2 Beta 2.
81+
* [Browse sample cards](Examples/AdaptiveCardVisualizer/Cards)
82+
* Use the [Schema Explorer](https://adaptivecards.io/explorer) to browse the capabilities of the different elements
83+
84+
## Displaying Adaptive Cards
85+
You can create an adaptive card view by providing the URL where the adaptive card is located.
86+
87+
```swift
88+
AdaptiveCardView(url: URL(string: "https://adaptivecards.io/payloads/ActivityUpdate.json")!)
89+
```
90+
91+
Or by providing an adaptive card fetched previously.
92+
93+
```swift
94+
let adaptiveCard = try JSONDecoder().decode(AdaptiveCard.self, from: jsonData)
95+
...
96+
AdaptiveCardView(adaptiveCard)
97+
```
98+
99+
Before displaying the card, the view checks that its version is supported and downloads its content asynchronously.
100+
101+
## Customizing Appearance
102+
You can customize an adaptive card's appearance by providing a configuration. An `AdaptiveCardConfiguration` is a set of values that specify how the library renders the different elements. To set a specific configuration for all the adaptive cards within a view, use the `adaptiveCardConfiguration(_:)` modifier.
103+
104+
```swift
105+
VStack {
106+
AdaptiveCardView(url: URL(string: "https://adaptivecards.io/payloads/ActivityUpdate.json")!)
107+
AdaptiveCardView(response.adaptiveCard)
108+
}
109+
.adaptiveCardConfiguration(AdaptiveCardConfiguration(...))
110+
```
111+
112+
Alternatively, you can customize only a specific aspect of an adaptive card's appearance, like the actions or the different spacing values. Use one of the following modifiers to customize a specific aspect of all the adaptive cards within a view:
113+
* `actionSetConfiguration(_:)` to customize the appearance of the adaptive card actions.
114+
* `containerStyleConfiguration(_:)` to customize the colors of the different container styles.
115+
* `factSetConfiguration(_:)` to customize the appearance of the fact set elements.
116+
* `imageSizeConfiguration(_:)` to provide custom values for the different image size cases.
117+
* `spacingConfiguration(_:)` to provide custom values for the different spacing cases.
118+
* `fontTypeConfiguration(_:)` to provide custom fonts for the different font types and sizes.
119+
120+
## Adding Custom Elements
121+
Adaptive cards are extensible, so you can add your own elements and the views that display them.
122+
123+
As an example, imagine that we are designing a card to present the summary of a GitHub repo. One of the elements is the repo language, which GitHub represents with a circle filled with a given color, next to the language name. The JSON representation of the element could look like this:
124+
125+
```json
126+
{
127+
"type": "RepoLanguage",
128+
"horizontalAlignment": "center",
129+
"language": "Swift",
130+
"color": "#ffac45"
131+
}
132+
```
133+
134+
We have omitted inherited optional properties like `"id"`, `"isVisible"`, `"spacing"`, `"separator"`, etc. But you will need to support them, nevertheless.
135+
136+
To add this new element, you need to create a type that conforms to `CustomCardElement`, `Codable`, and `Equatable`.
137+
138+
```swift
139+
struct RepoLanguage: CustomCardElement, Codable, Equatable {
140+
// CustomCardElement
141+
142+
@ItemIdentifier var id: String
143+
@Default<True> var isVisible: Bool
144+
@Default<False> var separator: Bool
145+
@Default<FirstCase> var spacing: Spacing
146+
@Default<Fallback.None> var fallback: Fallback<CardElement>
147+
@Default<EmptyDictionary> var requires: [String: SemanticVersion]
148+
149+
// RepoLanguage
150+
151+
@Default<FirstCase> var horizontalAlignment: HAlignment
152+
var language: String
153+
var color: String
154+
}
155+
```
156+
157+
In case you are wondering, the `Default` property wrapper lets you provide default values for JSON properties that are not required, removing much of the boilerplate. See [DefaultCodable](https://github.com/gonzalezreal/DefaultCodable) for more information.
158+
159+
As the new element conforms to `CustomCardElement`, you can add it to the body of an adaptive card, a container, or a column in a column set. It will have a spacing relative to the previous element and an optional separator. Besides that, you can provide a fallback element for those clients that still don't support it.
160+
161+
Notice that you will need to register the new element before any adaptive card decoding happens:
162+
163+
```swift
164+
CardElement.register(RepoLanguage.self)
165+
```
166+
167+
You can create the view that renders the new element as any other SwiftUI view:
168+
169+
```swift
170+
struct RepoLanguageView: View {
171+
var repoLanguage: RepoLanguage
172+
173+
var body: some View {
174+
HAlign(repoLanguage.horizontalAlignment) {
175+
Label {
176+
Text(repoLanguage.language)
177+
} icon: {
178+
Image(systemName: "circle.fill")
179+
.imageScale(.small)
180+
.foregroundColor(Color(argbHex: repoLanguage.color))
181+
}
182+
}
183+
}
184+
}
185+
```
186+
187+
Finally, to associate the new element and its view to all the adaptive card views within a view hierarchy, use the `customCardElement(_:, content:)` modifier:
188+
189+
```swift
190+
VStack {
191+
AdaptiveCardView(url: URL(string: "https://adaptivecards.io/payloads/ActivityUpdate.json")!)
192+
AdaptiveCardView(response.adaptiveCard)
193+
}
194+
.customCardElement(RepoLanguage.self) {
195+
RepoLanguageView($0)
196+
}
197+
```
198+
199+
## Compatibility
200+
AdaptiveCardUI requires Xcode 12 and Swift 5.3. It works on iOS 14.0 and later. If you want to try the macOS support, you will need to install macOS Big Sur 11 Beta 9 and Xcode 12.2 Beta 2.
201+
202+
## Installation
203+
You can add AdaptiveCardUI to an Xcode project by adding it as a package dependency.
204+
1. From the **File** menu, select **Swift Packages › Add Package Dependency…**
205+
1. Enter `https://github.com/gonzalezreal/AdaptiveCardUI` into the package repository URL text field
206+
1. Link **AdaptiveCardUI** to your application target
207+
208+
## Other Libraries
209+
* [Microsoft Adaptive Cards](https://github.com/microsoft/AdaptiveCards)

0 commit comments

Comments
 (0)