Skip to content

Commit

Permalink
EditorContentDecoding can now throw errors (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
rajdeep authored Jun 18, 2023
1 parent 0f00f11 commit 440aade
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,13 @@ class CommandsExampleViewController: ExamplesBaseViewController {

@objc
func decodeContents(sender: UIButton) {
let text = EditorContentJSONDecoder().decode(mode: .editor, maxSize: editor.frame.size, value: encodedContents, context: nil)
self.editor.attributedText = text
let text = try? EditorContentJSONDecoder(
).decode(mode: .editor,
maxSize: editor.frame.size,
value: encodedContents,
context: nil)
self.editor.attributedText = text ?? NSAttributedString(string: "<Error decoding contents>",
attributes: [.foregroundColor: UIColor.red])
}

@objc
Expand All @@ -266,9 +271,10 @@ class CommandsExampleViewController: ExamplesBaseViewController {
return
}

let text = EditorContentJSONDecoder().decode(mode: .editor, maxSize: editor.frame.size, value: contents, context: nil)
let text = try? EditorContentJSONDecoder().decode(mode: .editor, maxSize: editor.frame.size, value: contents, context: nil)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.6) {
self.editor.attributedText = text
self.editor.attributedText = text ?? NSAttributedString(string: "<Error decoding contents>",
attributes: [.foregroundColor: UIColor.red])
}
}

Expand Down
4 changes: 2 additions & 2 deletions ExampleApp/ExampleApp/Decoding/Decoder/PanelDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import UIKit
import Proton

struct PanelDecoder: EditorContentDecoding {
func decode(mode: EditorContentMode, maxSize: CGSize, value: JSON, context: EditorDecodingContext?) -> NSAttributedString {
func decode(mode: EditorContentMode, maxSize: CGSize, value: JSON, context: EditorDecodingContext?) throws -> NSAttributedString {
let frame = CGRect(origin: .zero, size: CGSize(width: 200, height: 30))
let attachment = PanelAttachment(frame: frame)
// attachment.readOnly = (mode == .readOnly)

attachment.attributedText = EditorContentJSONDecoder().decode(mode: mode, maxSize: maxSize, value: value, context: context)
attachment.attributedText = try EditorContentJSONDecoder().decode(mode: mode, maxSize: maxSize, value: value, context: context)
return attachment.string
}
}
4 changes: 2 additions & 2 deletions ExampleApp/ExampleApp/Decoding/Decoder/ParagraphDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ import UIKit
import Proton

struct ParagraphDecoder: EditorContentDecoding {
func decode(mode: EditorContentMode, maxSize: CGSize, value: JSON, context: EditorDecodingContext?) -> NSAttributedString {
func decode(mode: EditorContentMode, maxSize: CGSize, value: JSON, context: EditorDecodingContext?) throws -> NSAttributedString {
let string = NSMutableAttributedString()
var attr = Attributes()
if let style = value["style"] as? JSON,
let decoder = EditorContentJSONDecoder.attributeDecoders["style"] {
attr = decoder.decode(style)

string.append(EditorContentJSONDecoder().decode(mode: mode, maxSize: maxSize, value: value, context: context))
string.append(try EditorContentJSONDecoder().decode(mode: mode, maxSize: maxSize, value: value, context: context))
}
string.append(NSAttributedString(string: "\n"))
string.addAttributes(attr, range: string.fullRange)
Expand Down
4 changes: 2 additions & 2 deletions ExampleApp/ExampleApp/Decoding/JSONDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ struct EditorContentJSONDecoder: EditorContentDecoding {
"style": AnyAttributedStringAttributeDecoding(ParagraphStyleDecoder()),
]

func decode(mode: EditorContentMode, maxSize: CGSize, value: JSON, context: EditorDecodingContext?) -> NSAttributedString {
func decode(mode: EditorContentMode, maxSize: CGSize, value: JSON, context: EditorDecodingContext?) throws -> NSAttributedString {
let string = NSMutableAttributedString()
for content in value.contents ?? [] {
if let type = content.type {
let typeName = EditorContent.Name(type)
let decoder = EditorContentJSONDecoder.contentDecoders[typeName]
let contentValue = decoder?.decode(mode: mode, maxSize: maxSize, value: content, context: context) ?? NSAttributedString()
let contentValue = try decoder?.decode(mode: mode, maxSize: maxSize, value: content, context: context) ?? NSAttributedString()
string.append(contentValue)
}
}
Expand Down
8 changes: 4 additions & 4 deletions Proton/Sources/Swift/Decoding/EditorContentDecoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public protocol EditorContentDecoding {
/// - maxSize: Maximum available size of the container in which the content will be rendered.
/// - value: Value to decode.
/// - context: Context used for decoding.
func decode(mode: EditorContentMode, maxSize: CGSize, value: TypeToDecode, context: DecodingContext) -> NSAttributedString
func decode(mode: EditorContentMode, maxSize: CGSize, value: TypeToDecode, context: DecodingContext) throws -> NSAttributedString
}

/// A type-erased implementation of `EditorContentDecoding`
/// - SeeAlso:
/// `EditorContentDecoding`
public struct AnyEditorContentDecoding<T, C>: EditorContentDecoding {
let decoding: (EditorContentMode, CGSize, T, C) -> NSAttributedString
let decoding: (EditorContentMode, CGSize, T, C) throws -> NSAttributedString

/// Initializes AnyEditorContentDecoding
/// - Parameter decoder: Decoder to use
Expand All @@ -64,7 +64,7 @@ public struct AnyEditorContentDecoding<T, C>: EditorContentDecoding {
/// - maxSize: Maximum available size of the container in which the content will be rendered.
/// - value: Value to decode.
/// - context: Context used for decoding.
public func decode(mode: EditorContentMode, maxSize: CGSize, value: T, context: C) -> NSAttributedString {
return decoding(mode, maxSize, value, context)
public func decode(mode: EditorContentMode, maxSize: CGSize, value: T, context: C) throws -> NSAttributedString {
return try decoding(mode, maxSize, value, context)
}
}

0 comments on commit 440aade

Please sign in to comment.