refactored RCBit, added characterSet on RCConfiguration
This commit is contained in:
@@ -22,22 +22,21 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
enum RCBit: Int, CustomDebugStringConvertible {
|
||||
enum RCBit: Int {
|
||||
|
||||
case zero
|
||||
case one
|
||||
|
||||
init?(_ character: Character) {
|
||||
switch character {
|
||||
case "0":
|
||||
self = .zero
|
||||
case "1":
|
||||
self = .one
|
||||
default:
|
||||
return nil
|
||||
case "0": self = .zero
|
||||
case "1": self = .one
|
||||
default: return nil
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension RCBit: CustomDebugStringConvertible {
|
||||
var debugDescription: String {
|
||||
String(rawValue)
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ import UIKit
|
||||
|
||||
public final class RCCoder {
|
||||
|
||||
private let configuration: RCCoderConfiguration
|
||||
private var configuration: RCCoderConfiguration
|
||||
|
||||
public init(configuration: RCCoderConfiguration = .defaultConfiguration) {
|
||||
self.configuration = configuration
|
||||
@@ -34,8 +34,9 @@ public final class RCCoder {
|
||||
public extension RCCoder {
|
||||
func encode(_ image: RCImage) throws -> UIImage {
|
||||
let bits = try encode(message: image.message)
|
||||
return UIImage()
|
||||
|
||||
image.bits = stride(from: 0, to: bits.count, by: bits.count / 4).map({Array(bits[$0 ..< min($0 + bits.count / 4, bits.count)])})
|
||||
let drawer = RCDrawer(image: image)
|
||||
return drawer.draw()
|
||||
}
|
||||
|
||||
func decode(_ data: UIImage) throws -> String {
|
||||
@@ -44,8 +45,7 @@ public extension RCCoder {
|
||||
}
|
||||
|
||||
func validate(_ text: String) -> Bool {
|
||||
let characterset = CharacterSet(charactersIn: configuration.symbols.map({String($0)}).reduce("", +))
|
||||
return text.trimmingCharacters(in: characterset).isEmpty && text.count <= configuration.maxMessageCount
|
||||
return text.trimmingCharacters(in: configuration.characterSet).isEmpty && text.count <= configuration.maxMessageCount
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,7 +77,6 @@ extension RCCoder {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public extension RCCoder {
|
||||
enum RCCoderError: String, Error {
|
||||
case inValidText
|
||||
|
||||
@@ -22,11 +22,14 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
public final class RCCoderConfiguration {
|
||||
public struct RCCoderConfiguration {
|
||||
|
||||
public let symbols: [Character]
|
||||
public let maxMessageCount: Int
|
||||
internal let bitesPerSymbol: Int
|
||||
internal lazy var characterSet: CharacterSet = {
|
||||
CharacterSet(charactersIn: symbols.map({String($0)}).reduce("", +))
|
||||
}()
|
||||
|
||||
public init(symbols: String, maxMessageCount: Int) throws {
|
||||
var hash = [Character : Int]()
|
||||
|
||||
@@ -25,17 +25,11 @@ import UIKit
|
||||
public class RCImage {
|
||||
|
||||
public var message = ""
|
||||
public var type = ImageType.png
|
||||
public var size = CGSize(width: 300, height: 300)
|
||||
public var tintColor = UIColor.purple
|
||||
public var size = CGFloat(300)
|
||||
public var tintColor = UIColor.systemBlue
|
||||
public var attachmentImage: UIImage?
|
||||
public var isTransparent = false
|
||||
internal var bits = [[RCBit]]()
|
||||
|
||||
}
|
||||
|
||||
public extension RCImage {
|
||||
enum ImageType {
|
||||
case png
|
||||
case jpg(quality: Double)
|
||||
}
|
||||
public init() {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user