Skip to content

Commit 66192c4

Browse files
authored
Storage result to swift result (#41)
* fix broken provisioning profiles * fix broken embedded framework * remove LocksmithError * move storage files * rename CelySecureStatus to CelyStorageError * port over to Swift Result * rename CelyStorageStatus.success to CelyStorageStatus.noError * skeleton for verbosity cely flag * Refactored code to throw inside of internal classes but still return `Result` with public facing methods
1 parent aa81716 commit 66192c4

18 files changed

+1496
-289
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,6 @@ fastlane/report.xml
6363
fastlane/Preview.html
6464
fastlane/screenshots
6565
fastlane/test_output
66-
docs/
66+
docs/
67+
68+
Cely.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist

Cely Demo/AppDelegate.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1717
Cely.setup(with: window!, forModel: User(), requiredProperties: [.token], withOptions: [
1818
.loginCompletionBlock: { (username: String, password: String) in
1919
if username == "asdf", password == "asdf" {
20-
if User.save("FAKETOKEN:\(username)\(password)", as: .token) == .success {
20+
if case .success = User.save("FAKETOKEN:\(username)\(password)", as: .token) {
2121
Cely.changeStatus(to: .loggedIn)
2222
}
2323
}

Cely Demo/Cely Demo.entitlements

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<dict>
55
<key>keychain-access-groups</key>
66
<array>
7-
<string>$(AppIdentifierPrefix)com.cely.Cely-Demo</string>
7+
<string>$(AppIdentifierPrefix)com.cely-tools.Cely-Demo</string>
88
</array>
99
</dict>
1010
</plist>

Cely Demo/User.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct User: CelyUser {
3333
}
3434
}
3535

36-
@discardableResult func save(_ value: Any) -> StorageResult {
36+
@discardableResult func save(_ value: Any) -> Result<Void, Error> {
3737
return Cely.save(value, forKey: rawValue, securely: securely(), persisted: persisted())
3838
}
3939

@@ -46,7 +46,7 @@ struct User: CelyUser {
4646
// MARK: - Save/Get User Properties
4747

4848
extension User {
49-
@discardableResult static func save(_ value: Any, as property: Property) -> StorageResult {
49+
@discardableResult static func save(_ value: Any, as property: Property) -> Result<Void, Error> {
5050
return property.save(value)
5151
}
5252

Cely.xcodeproj/project.pbxproj

+52-40
Large diffs are not rendered by default.

Sources/Cely.swift

+30-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public struct Cely {
2121
/// A Completion Block that is expecting a `username:String` and a `password:String`
2222
public static var loginCompletionBlock: CelyLoginCompletion?
2323

24+
// TODO: decide on having a verbosity CelyOption flag.
25+
// There's some errors (`.itemNotFound`) that are expected to happen and
26+
// I'm not sure if I want to silence them
27+
internal static var verbosity = false
28+
2429
/// Sets up Cely within your application
2530
///
2631
/// - parameter window: `UIWindow` of your application.
@@ -84,8 +89,14 @@ extension Cely {
8489
/// - parameter persist: `Boolean`: Keep data after logout
8590
///
8691
/// - returns: `Boolean`: Whether or not your value was successfully set.
87-
@discardableResult public static func save(_ value: Any?, forKey key: String, toStorage store: CelyStorageProtocol = store, securely secure: Bool = false, persisted persist: Bool = false) -> StorageResult {
88-
return store.set(value, forKey: key, securely: secure, persisted: persist)
92+
@discardableResult
93+
public static func save(_ value: Any?, forKey key: String, toStorage store: CelyStorageProtocol = store, securely secure: Bool = false, persisted persist: Bool = false) -> Result<Void, Error> {
94+
do {
95+
try store.set(value, forKey: key, securely: secure, persisted: persist)
96+
return .success(())
97+
} catch {
98+
return .failure(error)
99+
}
89100
}
90101

91102
/// Perform action like `LoggedIn` or `LoggedOut`
@@ -98,9 +109,15 @@ extension Cely {
98109
/// Log user out
99110
///
100111
/// - parameter store: Storage `Cely` will be using. Defaulted to `CelyStorageProtocol`
101-
public static func logout(useStorage store: CelyStorageProtocol = store) {
102-
store.removeAllData()
103-
changeStatus(to: .loggedOut)
112+
@discardableResult
113+
public static func logout(useStorage store: CelyStorageProtocol = store) -> Result<Void, Error> {
114+
do {
115+
try store.clearStorage()
116+
changeStatus(to: .loggedOut)
117+
return .success(())
118+
} catch {
119+
return .failure(error)
120+
}
104121
}
105122

106123
/// Returns whether or not the user is logged in
@@ -110,3 +127,11 @@ extension Cely {
110127
return currentLoginStatus() == .loggedIn
111128
}
112129
}
130+
131+
internal extension Cely {
132+
static func debugPrint(str: CustomStringConvertible) {
133+
if verbosity {
134+
print(str)
135+
}
136+
}
137+
}

Sources/CelyConstants.swift

-23
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,13 @@ public enum CelyStatus: CelyCommands {
2323
/// Options that you can pass into Cely on `Cely.setup(_:)`
2424
public enum CelyOptions {
2525
case storage
26-
@available(*, deprecated, renamed: "homeViewController", message: "We will no longer support UIStoryboard as a way to instantiate ViewControllers.")
27-
case homeStoryboard
28-
@available(*, deprecated, renamed: "loginViewController", message: "We will no longer support UIStoryboard as a way to instantiate ViewControllers.")
29-
case loginStoryboard
3026
case homeViewController
3127
case loginViewController
3228
case loginCompletionBlock
3329
case loginStyle
3430
case celyAnimator
3531
}
3632

37-
// enum result on whether or not Cely successfully saved your data
38-
public enum StorageResult: Equatable {
39-
case success
40-
case fail(LocksmithError)
41-
}
42-
43-
public func == (lhs: StorageResult, rhs: StorageResult) -> Bool {
44-
switch (lhs, rhs) {
45-
case let (.fail(error1), .fail(error2)):
46-
return error1 == error2
47-
48-
case (.success, .success):
49-
return true
50-
51-
default:
52-
return false
53-
}
54-
}
55-
5633
internal extension UITextField {
5734
@IBInspectable var leftSpacer: CGFloat {
5835
get {

Sources/CelyKeychain.swift

-78
This file was deleted.

Sources/CelyProtocols.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ public protocol CelyUser {
1616

1717
/// Protocol a storage class must abide by in order for Cely to use it
1818
public protocol CelyStorageProtocol {
19-
func set(_ value: Any?, forKey key: String, securely secure: Bool, persisted persist: Bool) -> StorageResult
19+
func set(_ value: Any?, forKey key: String, securely secure: Bool, persisted persist: Bool) throws
2020
func get(_ key: String) -> Any?
21-
func removeAllData()
21+
func clearStorage() throws
2222
}
2323

2424
/// Protocol that allows styles to be applied to Cely's default LoginViewController

Sources/CelySecureStorage.swift

-50
This file was deleted.

Sources/LocksmithError.swift

-55
This file was deleted.

0 commit comments

Comments
 (0)