Skip to content

Commit

Permalink
Merge pull request #24 from ZamzamInc/feature/refactors
Browse files Browse the repository at this point in the history
Refactor updates
  • Loading branch information
basememara authored Apr 2, 2020
2 parents e27df9e + d2f4310 commit 377f52c
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 84 deletions.
21 changes: 21 additions & 0 deletions Sources/ZamzamCore/Extensions/Equatable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,24 @@ public extension Equatable {
values.contains(self)
}
}

public extension CaseIterable where Self: Equatable {

/// Returns the first index where the specified case appears in the enum.
///
/// enum Direction: CaseIterable {
/// case north
/// case east
/// case south
/// case west
/// }
///
/// Direction.north.index() // 0
/// Direction.east.index() // 1
/// Direction.south.index() // 2
/// Direction.west.index() // 3
///
func index() -> Self.AllCases.Index? {
Self.allCases.firstIndex(of: self)
}
}
20 changes: 20 additions & 0 deletions Sources/ZamzamCore/Infixes/NilOrEmptyAssignment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,24 @@ public extension Optional where Wrapped == String {
guard let left = left, !left.isEmpty else { return right }
return left
}

/// Assign value if not nil or empty, otherwise use default value.
///
/// var test: String
/// var value: String?
///
/// test = value ??+ "Abc"
/// // test == "Abc"
///
/// value = ""
/// test = value ??+ "Lmn"
/// // test == "Abc"
///
/// let value2: String? = "Xyz"
/// let test2 = value2 ??+ nil
/// // test2 == "Xyz"
static func ??+ (left: Wrapped?, right: Wrapped?) -> Wrapped? {
guard let left = left, !left.isEmpty else { return right }
return left
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public struct SecuredPreferences: SecuredPreferencesType {

public extension SecuredPreferences {

func get(_ key: SecuredPreferencesAPI.Key, completion: @escaping (String?) -> Void) {
service.get(key, completion: completion)
func get(_ key: SecuredPreferencesAPI.Key) -> String? {
service.get(key)
}

func set(_ value: String?, forKey key: SecuredPreferencesAPI.Key) -> Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,22 @@
/// )
///
/// keychain.set("kjn989hi", forKey: .token)
///
/// keychain.get(.token) {
/// print($0) // "kjn989hi"
/// }
/// keychain.get(.token) // "kjn989hi"
///
/// // Define strongly-typed keys
/// extension SecuredPreferencesAPI.Key {
/// static let token = SecuredPreferencesAPI.Key("token")
/// }
///
public protocol SecuredPreferencesType {

/// Retrieves the value from keychain that corresponds to the given key.
///
/// Accessing the underlying Keychain storage is an expensive operation.
/// Use a background thread when possible, then store within memory for future retrievals.
///
/// - Parameter key: The key that is used to read the user defaults item.
func get(_ key: SecuredPreferencesAPI.Key, completion: @escaping (String?) -> Void)
func get(_ key: SecuredPreferencesAPI.Key) -> String?

/// Stores the value in the keychain item under the given key.
///
Expand All @@ -50,7 +51,7 @@ public protocol SecuredPreferencesType {
// MARK: - Service

public protocol SecuredPreferencesService {
func get(_ key: SecuredPreferencesAPI.Key, completion: @escaping (String?) -> Void)
func get(_ key: SecuredPreferencesAPI.Key) -> String?
func set(_ value: String?, forKey key: SecuredPreferencesAPI.Key) -> Bool
func remove(_ key: SecuredPreferencesAPI.Key) -> Bool
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,9 @@ public struct SecuredPreferencesKeychainService: SecuredPreferencesService {
}

public extension SecuredPreferencesKeychainService {
private static let queue = DispatchQueue(label: "\(DispatchQueue.labelPrefix).SecuredPreferencesKeychainService", qos: .userInitiated)

func get(_ key: SecuredPreferencesAPI.Key, completion: @escaping (String?) -> Void) {
Self.queue.async {
let value = self.keychain.get(key.name)

DispatchQueue.main.async {
completion(value)
}
}
func get(_ key: SecuredPreferencesAPI.Key) -> String? {
keychain.get(key.name)
}
}

Expand Down
7 changes: 1 addition & 6 deletions Sources/ZamzamUI/Views/UIKit/Controls/RoundedViews.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,9 @@ open class RoundedButton: UIButton {
/// A circular `UIImageView`.
open class RoundedImageView: UIImageView {

open override func awakeFromNib() {
super.awakeFromNib()
layer.masksToBounds = true

}

open override func layoutSubviews() {
super.layoutSubviews()
layer.masksToBounds = true
layer.cornerRadius = frame.height / 2
}
}
Expand Down
20 changes: 13 additions & 7 deletions Sources/ZamzamUI/Views/UIKit/Extensions/UIView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,20 @@ public extension UIView {

/// Anchor all sides of the view using auto layout constraints.
///
/// - Parameter view: The ancestor view to pin edges to.
func edges(to view: UIView?) {
guard let view = view else { return }
/// - Parameters:
/// - view: The ancestor view to pin edges to.
/// - insets: The inset distances for view padding.
func edges(to view: UIView?, insets: UIEdgeInsets = .zero) {
guard let view = view else {
assertionFailure("Missing superview for constraint")
return
}

translatesAutoresizingMaskIntoConstraints = false
topAnchor.constraint(equalTo: view.topAnchor).isActive = true
bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
topAnchor.constraint(equalTo: view.topAnchor, constant: insets.top).isActive = true
bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -insets.bottom).isActive = true
leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: insets.left).isActive = true
trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -insets.right).isActive = true
}
}
#endif
23 changes: 21 additions & 2 deletions Tests/ZamzamCoreTests/Extensions/EquatableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,29 @@
import XCTest
import ZamzamCore

final class EquatableTests: XCTestCase {
final class EquatableTests: XCTestCase {}

func testEquatableWithin() {
extension EquatableTests {

func testWithinSequence() {
XCTAssert("def".within(["abc", "def", "ghi"]))
XCTAssert(!"xyz".within(["abc", "def", "ghi"]))
}
}

extension EquatableTests {

func testCaseIterableIndex() {
enum Direction: CaseIterable {
case north
case east
case south
case west
}

XCTAssertEqual(Direction.north.index(), 0)
XCTAssertEqual(Direction.east.index(), 1)
XCTAssertEqual(Direction.south.index(), 2)
XCTAssertEqual(Direction.west.index(), 3)
}
}
19 changes: 0 additions & 19 deletions Tests/ZamzamCoreTests/Extensions/StringTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -314,22 +314,3 @@ extension StringTests {
XCTAssertTrue(!test.isNilOrEmpty)
}
}

extension StringTests {

func testIsNilOrEmptyInfix() {
var result: String
var test: String?

result = test ??+ "Abc"
XCTAssertEqual(result, "Abc")

test = ""
result = test ??+ "Abc"
XCTAssertEqual(result, "Abc")

test = "Xyz"
result = test ??+ "Abc"
XCTAssertEqual(result, "Xyz")
}
}
23 changes: 23 additions & 0 deletions Tests/ZamzamCoreTests/InfixTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,26 @@ final class InfixTests: XCTestCase {
XCTAssertEqual(test, 456)
}
}

extension InfixTests {

func testIsNilOrEmptyInfix() {
var result: String
var test: String?

result = test ??+ "Abc"
XCTAssertEqual(result, "Abc")

test = ""
result = test ??+ "Abc"
XCTAssertEqual(result, "Abc")

test = "Xyz"
result = test ??+ "Abc"
XCTAssertEqual(result, "Xyz")

let test2: String? = "Xyz2"
let result2 = test2 ??+ nil
XCTAssertEqual(result2, "Xyz2")
}
}
39 changes: 6 additions & 33 deletions Tests/ZamzamCoreTests/Preferences/SecuredPreferencesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ extension SecuredPreferencesTests {

func testString() {
// Given
let promise1 = expectation(description: "\(#function)1")
let promise2 = expectation(description: "\(#function)2")
let value1 = "abc"
let value2 = "xyz"

Expand All @@ -30,26 +28,15 @@ extension SecuredPreferencesTests {
keychain.set(value2, forKey: .testString2)

// Then
keychain.get(.testString1) {
XCTAssertEqual($0, value1)
promise1.fulfill()
}

keychain.get(.testString2) {
XCTAssertEqual($0, value2)
promise2.fulfill()
}

wait(for: [promise1, promise2], timeout: 10)
XCTAssertEqual(keychain.get(.testString1), value1)
XCTAssertEqual(keychain.get(.testString2), value2)
}
}

extension SecuredPreferencesTests {

func testRemove() {
// Given
let promise1 = expectation(description: "\(#function)1")
let promise2 = expectation(description: "\(#function)2")
let value1 = "abc"
let value2 = "xyz"

Expand All @@ -60,17 +47,8 @@ extension SecuredPreferencesTests {
keychain.remove(.testString2)

// Then
keychain.get(.testString1) {
XCTAssertNil($0)
promise1.fulfill()
}

keychain.get(.testString2) {
XCTAssertNil($0)
promise2.fulfill()
}

wait(for: [promise1, promise2], timeout: 10)
XCTAssertNil(keychain.get(.testString1))
XCTAssertNil(keychain.get(.testString2))
}
}

Expand All @@ -87,13 +65,8 @@ private extension SecuredPreferencesAPI.Key {
private class SecuredPreferencesTestService: SecuredPreferencesService {
var values = [String: String?]()

func get(_ key: SecuredPreferencesAPI.Key, completion: @escaping (String?) -> Void) {
guard let value = values[key.name] else {
completion(nil)
return
}

completion(value)
func get(_ key: SecuredPreferencesAPI.Key) -> String? {
values[key.name] ?? nil
}

func set(_ value: String?, forKey key: SecuredPreferencesAPI.Key) -> Bool {
Expand Down

0 comments on commit 377f52c

Please sign in to comment.