-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add Markdown support using SwiftyMarkdown library
- Loading branch information
1 parent
ace2a6d
commit e635ab7
Showing
26 changed files
with
2,718 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
Sources/StreamChatUI/StreamSwiftyMarkdown/SwiftyMarkdown/CharacterRule.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// | ||
// Copyright © 2022 Stream.io Inc. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
enum SpaceAllowed { | ||
case no | ||
case bothSides | ||
case oneSide | ||
case leadingSide | ||
case trailingSide | ||
} | ||
|
||
enum Cancel { | ||
case none | ||
case allRemaining | ||
case currentSet | ||
} | ||
|
||
enum CharacterRuleTagType { | ||
case open | ||
case close | ||
case metadataOpen | ||
case metadataClose | ||
case repeating | ||
} | ||
|
||
struct CharacterRuleTag { | ||
let tag: String | ||
let type: CharacterRuleTagType | ||
|
||
init(tag: String, type: CharacterRuleTagType) { | ||
self.tag = tag | ||
self.type = type | ||
} | ||
} | ||
|
||
struct CharacterRule: CustomStringConvertible { | ||
let primaryTag: CharacterRuleTag | ||
let tags: [CharacterRuleTag] | ||
let escapeCharacters: [Character] | ||
let styles: [Int: CharacterStyling] | ||
let minTags: Int | ||
let maxTags: Int | ||
var metadataLookup: Bool = false | ||
var isRepeatingTag: Bool { | ||
primaryTag.type == .repeating | ||
} | ||
|
||
var definesBoundary = false | ||
var shouldCancelRemainingRules = false | ||
var balancedTags = false | ||
|
||
var description: String { | ||
"Character Rule with Open tag: \(primaryTag.tag) and current styles : \(styles) " | ||
} | ||
|
||
func tag(for type: CharacterRuleTagType) -> CharacterRuleTag? { | ||
tags.filter { $0.type == type }.first ?? nil | ||
} | ||
|
||
init( | ||
primaryTag: CharacterRuleTag, | ||
otherTags: [CharacterRuleTag], | ||
escapeCharacters: [Character] = ["\\"], | ||
styles: [Int: CharacterStyling] = [:], | ||
minTags: Int = 1, | ||
maxTags: Int = 1, | ||
metadataLookup: Bool = false, | ||
definesBoundary: Bool = false, | ||
shouldCancelRemainingRules: Bool = false, | ||
balancedTags: Bool = false | ||
) { | ||
self.primaryTag = primaryTag | ||
tags = otherTags | ||
self.escapeCharacters = escapeCharacters | ||
self.styles = styles | ||
self.metadataLookup = metadataLookup | ||
self.definesBoundary = definesBoundary | ||
self.shouldCancelRemainingRules = shouldCancelRemainingRules | ||
self.minTags = maxTags < minTags ? maxTags : minTags | ||
self.maxTags = minTags > maxTags ? minTags : maxTags | ||
self.balancedTags = balancedTags | ||
} | ||
} | ||
|
||
enum ElementType { | ||
case tag | ||
case escape | ||
case string | ||
case space | ||
case newline | ||
case metadata | ||
} | ||
|
||
struct Element { | ||
let character: Character | ||
var type: ElementType | ||
var boundaryCount: Int = 0 | ||
var isComplete: Bool = false | ||
var styles: [CharacterStyling] = [] | ||
var metadata: [String] = [] | ||
} | ||
|
||
extension CharacterSet { | ||
func containsUnicodeScalars(of character: Character) -> Bool { | ||
character.unicodeScalars.allSatisfy(contains(_:)) | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
Sources/StreamChatUI/StreamSwiftyMarkdown/SwiftyMarkdown/PerfomanceLog.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// Copyright © 2022 Stream.io Inc. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import os.log | ||
|
||
class PerformanceLog { | ||
var timer: TimeInterval = 0 | ||
let enablePerfomanceLog: Bool | ||
let log: OSLog | ||
let identifier: String | ||
|
||
init(with environmentVariableName: String, identifier: String, log: OSLog) { | ||
self.log = log | ||
enablePerfomanceLog = (ProcessInfo.processInfo.environment[environmentVariableName] != nil) | ||
self.identifier = identifier | ||
} | ||
|
||
func start() { | ||
guard enablePerfomanceLog else { return } | ||
timer = Date().timeIntervalSinceReferenceDate | ||
os_log("--- TIMER %{public}@ began", log: log, type: .info, identifier) | ||
} | ||
|
||
func tag(with string: String) { | ||
guard enablePerfomanceLog else { return } | ||
if timer == 0 { | ||
start() | ||
} | ||
os_log("TIMER %{public}@: %f %@", log: log, type: .info, identifier, Date().timeIntervalSinceReferenceDate - timer, string) | ||
} | ||
|
||
func end() { | ||
guard enablePerfomanceLog else { return } | ||
timer = Date().timeIntervalSinceReferenceDate | ||
os_log( | ||
"--- TIMER %{public}@ finished. Total time: %f", | ||
log: log, | ||
type: .info, | ||
identifier, | ||
Date().timeIntervalSinceReferenceDate - timer | ||
) | ||
timer = 0 | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
Sources/StreamChatUI/StreamSwiftyMarkdown/SwiftyMarkdown/String+SwiftyMarkdown.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// Copyright © 2022 Stream.io Inc. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Some helper functions based on this: | ||
/// https://stackoverflow.com/questions/32305891/index-of-a-substring-in-a-string-with-swift/32306142#32306142 | ||
extension StringProtocol { | ||
func index<S: StringProtocol>(of string: S, options: String.CompareOptions = []) -> Index? { | ||
range(of: string, options: options)?.lowerBound | ||
} | ||
|
||
func endIndex<S: StringProtocol>(of string: S, options: String.CompareOptions = []) -> Index? { | ||
range(of: string, options: options)?.upperBound | ||
} | ||
|
||
func indices<S: StringProtocol>(of string: S, options: String.CompareOptions = []) -> [Index] { | ||
var indices: [Index] = [] | ||
var startIndex = self.startIndex | ||
while startIndex < endIndex, | ||
let range = self[startIndex...] | ||
.range(of: string, options: options) { | ||
indices.append(range.lowerBound) | ||
startIndex = range.lowerBound < range.upperBound ? range.upperBound : | ||
index(range.lowerBound, offsetBy: 1, limitedBy: endIndex) ?? endIndex | ||
} | ||
return indices | ||
} | ||
|
||
func ranges<S: StringProtocol>(of string: S, options: String.CompareOptions = []) -> [Range<String.Index>] { | ||
var result: [Range<Index>] = [] | ||
var startIndex = self.startIndex | ||
while startIndex < endIndex, | ||
let range = self[startIndex...] | ||
.range(of: string, options: options) { | ||
result.append(range) | ||
startIndex = range.lowerBound < range.upperBound ? range.upperBound : | ||
index(range.lowerBound, offsetBy: 1, limitedBy: endIndex) ?? endIndex | ||
} | ||
return result | ||
} | ||
} |
Oops, something went wrong.