JSON โ Native SwiftUI ๐
Eventually also:
- Swift on the server โ Native SwiftUI
- JSON or Swift on the server โ Jetpack Compose
![image](https://private-user-images.githubusercontent.com/5012557/283159300-a65ec658-9132-4262-8c17-d353ad670b6a.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjQzNzk0MDYsIm5iZiI6MTcyNDM3OTEwNiwicGF0aCI6Ii81MDEyNTU3LzI4MzE1OTMwMC1hNjVlYzY1OC05MTMyLTQyNjItOGMxNy1kMzUzYWQ2NzBiNmEucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDgyMyUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA4MjNUMDIxMTQ2WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9NjVjYTgzM2RjZDkzOTdhMzE5Y2NjMGJmYjRkNzIzNWExYTYzMjMzZWZhMWUwMDI3ZmRhZDgxZThiOTE3ZjNkOCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.lpnyxoeDs2-7aztLfCKw63tXKUwH6bX66eZNnk78D1M)
- Clone or download the repo.
- Run
./build_framework.sh
- Grab the framework from
_build_framework/PicassoKit.xcframework
and drag into your Xcode project. - Add AnyCodable as a dependency to your target.
-
import PicassoKit
-
Provide an encoder and decoder:
Parser.shared = Parser(encoder: ..., decoder: ...)
. Picasso is tested on 3 options:- ZippyJSON for decoding (pair with Apple's
JSONEncoder
). - Apple's
JSONDecoder
andJSONEncoder
. - MessagePacker for MessagePack support.
- ZippyJSON for decoding (pair with Apple's
-
Create a
PicassoView
anywhere in your SwiftUI code:struct MyCoolView: View { var body: some View { VStack { Text("Hello!") PicassoView(URL("my.server.com/picasso/coolcontent")!) // <-- } } }
Instead of typing the view hierarchy in JSON yourself (or if you want to use MessagePack), construct your UI from various PC views (PCStack
, PCText
etc.) and encode the main view:
func someView() -> PCStackView {
PCStackView(...)
}
let view = someView()
let data = try someEncoder.encode(view)
let json = String(data: data, encoding: .utf8)
// upload the result json to your backend etc.
While not a comprehensive guide, this should provide an idea of how the JSON syntax looks like:
{
"VStack": [
{
"text": "Hello!",
"modifiers": {
"font": { "weight": "bold", "style": "title" },
"padding": { "top": 30, "leading": 100 }
}
},
{
"HStack": [...other views...],
"alignment": "bottomTrailing",
"modifiers": {
"overlay": {
"alignment": "bottom",
"content": {...some view...}
}
}
},
{
"optional": {
"content": { "text": "Hi there! :)" },
"presentationFlag": "show-text" }
},
{
"button": "Toggle Hidden Text",
"toggleFlag": "show-text",
},
{
"image": "https://picsum.photos/200/400",
"scale": 2
}
]
}
(beyond the obvious missing stuff...)
- Eventually split
Parser/modifiers(from:)
to categories: text, etc. - Remove the SwiftUI.View conformance from the types, make it separate (to later compile on Linux)
- Find a way to type erase the modifiers like
AnyPCView
? - Somehow single pass on the JSON?
- Fix the
.indices
in containers body (stack and scrollview) - Add MessagePack with perf tests against JSON
-
Figure out the crash in msgpack-swift when trying to parse modifiers(bug in the library)