Skip to content
This repository has been archived by the owner on May 12, 2022. It is now read-only.

Commit

Permalink
Introduce new api for Codable
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii committed Oct 2, 2017
1 parent e5c50e5 commit a48c144
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 208 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# * http://www.objc.io/issue-6/travis-ci.html
# * https://github.com/supermarin/xcpretty#usage

osx_image: xcode8.3
osx_image: xcode9
language: objective-c

script:
Expand Down
21 changes: 10 additions & 11 deletions OneStore.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

/* Begin PBXBuildFile section */
4B7F1CE41E9210D2005521BD /* OneStoreType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B7F1CE31E9210D2005521BD /* OneStoreType.swift */; };
4B8DB37B1F82459B00312734 /* Box.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B8DB37A1F82459B00312734 /* Box.swift */; };
4B9D4EB91F828FB5009F8159 /* ReservedOneStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B9D4EB81F828FB5009F8159 /* ReservedOneStore.swift */; };
4BC39C9A1DC30A6200CB0834 /* OneStore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4BC39C901DC30A6200CB0834 /* OneStore.framework */; };
4BC39C9F1DC30A6200CB0834 /* OneStoreTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BC39C9E1DC30A6200CB0834 /* OneStoreTests.swift */; };
4BC39CA11DC30A6200CB0834 /* OneStore.h in Headers */ = {isa = PBXBuildFile; fileRef = 4BC39C931DC30A6200CB0834 /* OneStore.h */; settings = {ATTRIBUTES = (Public, ); }; };
4BC39CAD1DC30A7300CB0834 /* OneStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BC39CAA1DC30A7300CB0834 /* OneStore.swift */; };
4BC39CAE1DC30A7300CB0834 /* OneStoreValueProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BC39CAB1DC30A7300CB0834 /* OneStoreValueProtocol.swift */; };
4BC39CAF1DC30A7300CB0834 /* Stack.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BC39CAC1DC30A7300CB0834 /* Stack.swift */; };
4BC39CB11DC30E7700CB0834 /* Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BC39CB01DC30E7700CB0834 /* Tests.swift */; };
4BCA4DB61E659D1E0099D52A /* NonOptionalOneStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4BCA4DB51E659D1E0099D52A /* NonOptionalOneStore.swift */; };
Expand All @@ -31,14 +31,14 @@

/* Begin PBXFileReference section */
4B7F1CE31E9210D2005521BD /* OneStoreType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OneStoreType.swift; sourceTree = "<group>"; };
4B8DB37A1F82459B00312734 /* Box.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Box.swift; sourceTree = "<group>"; };
4B9D4EB81F828FB5009F8159 /* ReservedOneStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReservedOneStore.swift; sourceTree = "<group>"; };
4BC39C901DC30A6200CB0834 /* OneStore.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = OneStore.framework; sourceTree = BUILT_PRODUCTS_DIR; };
4BC39C931DC30A6200CB0834 /* OneStore.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OneStore.h; sourceTree = "<group>"; };
4BC39C941DC30A6200CB0834 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4BC39C991DC30A6200CB0834 /* OneStoreTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = OneStoreTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
4BC39C9E1DC30A6200CB0834 /* OneStoreTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OneStoreTests.swift; sourceTree = "<group>"; };
4BC39CA01DC30A6200CB0834 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
4BC39CAA1DC30A7300CB0834 /* OneStore.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OneStore.swift; sourceTree = "<group>"; };
4BC39CAB1DC30A7300CB0834 /* OneStoreValueProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OneStoreValueProtocol.swift; sourceTree = "<group>"; };
4BC39CAC1DC30A7300CB0834 /* Stack.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Stack.swift; sourceTree = "<group>"; };
4BC39CB01DC30E7700CB0834 /* Tests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Tests.swift; sourceTree = "<group>"; };
4BCA4DB51E659D1E0099D52A /* NonOptionalOneStore.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NonOptionalOneStore.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -87,8 +87,9 @@
children = (
4B7F1CE31E9210D2005521BD /* OneStoreType.swift */,
4BC39CAA1DC30A7300CB0834 /* OneStore.swift */,
4B8DB37A1F82459B00312734 /* Box.swift */,
4BCA4DB51E659D1E0099D52A /* NonOptionalOneStore.swift */,
4BC39CAB1DC30A7300CB0834 /* OneStoreValueProtocol.swift */,
4B9D4EB81F828FB5009F8159 /* ReservedOneStore.swift */,
4BC39CAC1DC30A7300CB0834 /* Stack.swift */,
4BC39C931DC30A6200CB0834 /* OneStore.h */,
4BC39C941DC30A6200CB0834 /* Info.plist */,
Expand All @@ -101,7 +102,6 @@
children = (
4BC39CB01DC30E7700CB0834 /* Tests.swift */,
4BCA4DB71E659D3D0099D52A /* NonOptionalOneStoreSpec.swift */,
4BC39C9E1DC30A6200CB0834 /* OneStoreTests.swift */,
4BC39CA01DC30A6200CB0834 /* Info.plist */,
);
path = OneStoreTests;
Expand Down Expand Up @@ -169,7 +169,6 @@
TargetAttributes = {
4BC39C8F1DC30A6200CB0834 = {
CreatedOnToolsVersion = 8.0;
DevelopmentTeam = KU2QEJ9K3Z;
LastSwiftMigration = 0800;
ProvisioningStyle = Automatic;
};
Expand Down Expand Up @@ -222,8 +221,9 @@
files = (
4BCA4DB61E659D1E0099D52A /* NonOptionalOneStore.swift in Sources */,
4B7F1CE41E9210D2005521BD /* OneStoreType.swift in Sources */,
4BC39CAE1DC30A7300CB0834 /* OneStoreValueProtocol.swift in Sources */,
4B8DB37B1F82459B00312734 /* Box.swift in Sources */,
4BC39CAF1DC30A7300CB0834 /* Stack.swift in Sources */,
4B9D4EB91F828FB5009F8159 /* ReservedOneStore.swift in Sources */,
4BC39CAD1DC30A7300CB0834 /* OneStore.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -233,7 +233,6 @@
buildActionMask = 2147483647;
files = (
4BCA4DB81E659D3D0099D52A /* NonOptionalOneStoreSpec.swift in Sources */,
4BC39C9F1DC30A6200CB0834 /* OneStoreTests.swift in Sources */,
4BC39CB11DC30E7700CB0834 /* Tests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -358,7 +357,7 @@
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "";
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = KU2QEJ9K3Z;
DEVELOPMENT_TEAM = "";
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
Expand All @@ -379,7 +378,7 @@
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_IDENTITY = "";
DEFINES_MODULE = YES;
DEVELOPMENT_TEAM = KU2QEJ9K3Z;
DEVELOPMENT_TEAM = "";
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
Expand Down
31 changes: 31 additions & 0 deletions OneStore/Box.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// Box.swift
//
// Copyright (c) 2017 muukii
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import Foundation

struct Box<T: Codable> : Codable {
let value: T

init(_ value: T) {
self.value = value
}
}
39 changes: 29 additions & 10 deletions OneStore/NonOptionalOneStore.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
//
// NonOptionalOneStore.swift
// OneStore
// NonOptionalOneStore.swift
//
// Created by muukii on 2/28/17.
// Copyright © 2017 muukii. All rights reserved.
// Copyright (c) 2017 muukii
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import Foundation

open class NonOptionalOneStore<T: OneStoreValueProtocol>: OneStoreType {
open class NonOptionalOneStore<T: Codable> : OneStoreType {

public let source: OneStore<T>
private let initialValue: T
Expand All @@ -25,11 +40,6 @@ open class NonOptionalOneStore<T: OneStoreValueProtocol>: OneStoreType {
return source.rawStoreKey
}

public init(stack: Stack, key: String, initialValue: T) {
self.source = OneStore<T>(stack: stack, key: key, initialValue: initialValue)
self.initialValue = initialValue
}

open var value: T {
get {
guard let value = source.value else {
Expand All @@ -42,4 +52,13 @@ open class NonOptionalOneStore<T: OneStoreValueProtocol>: OneStoreType {
source.value = newValue
}
}

public init(stack: Stack, key: String, initialize value: T) {
self.source = OneStore<T>(stack: stack, key: key, initialize: value)
self.initialValue = value
}

public func reset() {
source.reset()
}
}
72 changes: 49 additions & 23 deletions OneStore/OneStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import Foundation

open class OneStore<T: OneStoreValueProtocol>: OneStoreType {
open class OneStore<T: Codable> : OneStoreType {

public typealias Value = T

Expand All @@ -33,49 +33,75 @@ open class OneStore<T: OneStoreValueProtocol>: OneStoreType {
return "\(stack.domain).\(storeKey)"
}

/// set value on "MainThread" synchronously.
open var writeOnMainThread: Bool = true
private let initializeValue: T?

private let initialValue: T?

public init(stack: Stack, key: String, initialValue: T? = nil) {
public init(stack: Stack, key: String, initialize value: T? = nil) {

precondition(key.isEmpty == false, "key must be not empty")

self.storeKey = key
self.stack = stack
self.initialValue = initialValue
self.initializeValue = value
self.value = value
stack.addManagedKey(rawStoreKey)
}

open var value: T? {
get {

if let initialValue = initialValue, T.getOneStoreValue(stack.userDefaults, key: rawStoreKey) == nil {
if Thread.isMainThread == false && writeOnMainThread {
DispatchQueue.main.sync {
initialValue.setOneStoreValue(stack.userDefaults, key: rawStoreKey)
}
} else {
initialValue.setOneStoreValue(stack.userDefaults, key: rawStoreKey)
}
if isPrimitive(type: T.self) {
return stack.userDefaults.value(forKey: rawStoreKey) as? T
}

guard let data = stack.userDefaults.data(forKey: rawStoreKey) else {
return nil
}

return T.getOneStoreValue(stack.userDefaults, key: rawStoreKey)
do {
let decoder = JSONDecoder()
let decoded = try decoder.decode(Box<T>.self, from: data)
return decoded.value
} catch {
assertionFailure("OneStore Error : \(error)")
return nil
}
}
set {
guard let value = newValue else {
stack.remove(key: rawStoreKey)
return
}

if Thread.isMainThread == false && writeOnMainThread {
DispatchQueue.main.sync {
value.setOneStoreValue(stack.userDefaults, key: rawStoreKey)
}
} else {
value.setOneStoreValue(stack.userDefaults, key: rawStoreKey)

if isPrimitive(type: T.self) {
stack.userDefaults.set(value, forKey: rawStoreKey)
return
}

do {
let encoder = JSONEncoder()
let encoded = try encoder.encode(Box(value))
stack.userDefaults.set(encoded, forKey: rawStoreKey)
} catch {
assertionFailure("OneStore Error : \(error)")
}
}
}

public func reset() {
value = initializeValue
}

private func isPrimitive<ValueType>(type: ValueType.Type) -> Bool {
switch type {
case
is String.Type,
is Bool.Type,
is Int.Type,
is Float.Type,
is Double.Type:
return true
default:
return false
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions OneStore/OneStoreType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ public protocol OneStoreType {
var storeKey: String { get }
var stack: Stack { get }
var rawStoreKey: String { get }
func reset()
func exists() -> Bool
}

extension OneStoreType {

public func remove() {
stack.remove(key: rawStoreKey)
}

internal func exists() -> Bool {
public func exists() -> Bool {
return stack.exists(key: rawStoreKey)
}
}
Loading

0 comments on commit a48c144

Please sign in to comment.