-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
143 additions
and
56 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 was deleted.
Oops, something went wrong.
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,63 @@ | ||
// | ||
// PersistenceHelper.swift | ||
// PlayAways | ||
// | ||
// Created by Leonardo Cardoso on 09/12/2016. | ||
// Copyright © 2016 Guilherme Rambo. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol DefaultsStorage { | ||
func synchronize() -> Bool | ||
func set(_ value: Any?, forKey defaultName: String) | ||
func removeObject(forKey defaultName: String) | ||
func object(forKey defaultName: String) -> Any? | ||
} | ||
|
||
extension UserDefaults: DefaultsStorage { } | ||
extension NSUbiquitousKeyValueStore: DefaultsStorage { } | ||
|
||
final class PersistenceHelper { | ||
|
||
fileprivate let storage: DefaultsStorage | ||
|
||
init(storage: DefaultsStorage) { | ||
self.storage = storage | ||
} | ||
|
||
func save(value: AnyObject, key: String) { | ||
storage.set(value, forKey: key) | ||
} | ||
|
||
func read(key: String) -> AnyObject? { | ||
return storage.object(forKey: key) as AnyObject? | ||
} | ||
|
||
func remove(key: String) { | ||
storage.removeObject(forKey: key) | ||
} | ||
|
||
} | ||
|
||
extension PersistenceHelper { | ||
|
||
private struct Keys { | ||
static let storagePath = "PlayPath" | ||
} | ||
|
||
class var userDefaults: PersistenceHelper { | ||
return PersistenceHelper(storage: UserDefaults.standard) | ||
} | ||
|
||
/// The path where the app will store the user's playgrounds by default | ||
var storagePath: String? { | ||
get { | ||
return storage.object(forKey: Keys.storagePath) as? String | ||
} | ||
set { | ||
storage.set(newValue, forKey: Keys.storagePath) | ||
} | ||
} | ||
|
||
} |
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