Skip to content

Commit

Permalink
MLIBZ-921: Swift Package Manager Beta Support
Browse files Browse the repository at this point in the history
  • Loading branch information
heyzooi committed May 2, 2019
1 parent 69e560e commit 2446cb9
Show file tree
Hide file tree
Showing 63 changed files with 1,531 additions and 246 deletions.
6 changes: 6 additions & 0 deletions .cirrus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ task:
xcode_version_script: xcodebuild -version
xcode_show_destinations_script: make show-destinations
test_ios_script: make test-ios

task:
name: Swift Linux
container:
image: swift
test_script: swift test
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@
KinveyKit/build
KinveyKit/doc
KinveyKit/LICENSES.html
.DS_Store
devtools
DocTemplates
KinveyKit/Documents
Tools/upload-to-amazon-aws-s3/node_modules
Tools/kinvey-dump/node_modules
Pods
docs
.build
/build
Kinvey/build
Carthage
!Carthage/Checkouts/NSPredicate-MongoDB-Adaptor/Sources/MongoDBPredicateAdaptor/*.swift
Kinvey.framework.zip
scripts/RealtimeSend/Rome

# Swift Package Manager
/Kinvey.xcodeproj
.DS_Store
/.build
/Packages
4 changes: 4 additions & 0 deletions Kinvey/Kinvey.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@
57CCBCCF20D2CD3F00197CC2 /* PubNub.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 57E6BD001EC51F80000E5C52 /* PubNub.framework */; };
57CCBCD120D2CD3F00197CC2 /* CacheMigrationTestCaseData.zip in Resources */ = {isa = PBXBuildFile; fileRef = 573CC99C20D19AB900BDF726 /* CacheMigrationTestCaseData.zip */; };
57CDF46522656539006596A7 /* Nimble.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 57373AB31ECCE670002842CE /* Nimble.framework */; };
57D18DCD217D9DBE0088FF12 /* SQLiteCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 57D18DCC217D9DBE0088FF12 /* SQLiteCache.swift */; };
57D386A21CC19D0C0014050D /* Kinvey.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 57A27C811C178F17000DF951 /* Kinvey.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
57D386A31CC19D7C0014050D /* Kinvey.framework in CopyFiles */ = {isa = PBXBuildFile; fileRef = 57A27C811C178F17000DF951 /* Kinvey.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
57D643001CA3268000F6D16E /* CacheMigrationTestCaseStep1.swift in Sources */ = {isa = PBXBuildFile; fileRef = 577155531CA1D65D00C91B4B /* CacheMigrationTestCaseStep1.swift */; };
Expand Down Expand Up @@ -1109,6 +1110,7 @@
57CDF45D22653DBC006596A7 /* AutoDataStoreSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AutoDataStoreSpec.swift; sourceTree = "<group>"; };
57CDF46022654226006596A7 /* Quick.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Quick.framework; sourceTree = "<group>"; };
57CDF463226564E1006596A7 /* Quick.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = Quick.framework; sourceTree = "<group>"; };
57D18DCC217D9DBE0088FF12 /* SQLiteCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLiteCache.swift; sourceTree = "<group>"; };
57D643141CA3268000F6D16E /* KinveyTests Migration Database Step 1.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "KinveyTests Migration Database Step 1.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
57D6433B1CA3268600F6D16E /* KinveyTests Migration Database Step 2.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "KinveyTests Migration Database Step 2.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
57D6433D1CA326DE00F6D16E /* CacheMigrationTestCaseStep2.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CacheMigrationTestCaseStep2.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1723,6 +1725,7 @@
57E7C7A41C504AC500848748 /* Cache.swift */,
57E7C7A61C504B0900848748 /* CacheManager.swift */,
573DD9841D11CCD6006ECF1C /* RealmCache.swift */,
57D18DCC217D9DBE0088FF12 /* SQLiteCache.swift */,
57E7C7A81C504E7B00848748 /* PendingOperation.swift */,
57E7C7AE1C50539200848748 /* Sync.swift */,
57E7C7B01C50539C00848748 /* SyncManager.swift */,
Expand Down Expand Up @@ -3461,6 +3464,7 @@
574B0FAD1C729F3300CDC48F /* RemoveByQueryOperation.swift in Sources */,
5728212F1C63E10700373EC8 /* FileStore.swift in Sources */,
575772861C4728E5001D56F5 /* TTL.swift in Sources */,
57D18DCD217D9DBE0088FF12 /* SQLiteCache.swift in Sources */,
573DD9851D11CCD6006ECF1C /* RealmCache.swift in Sources */,
57A27CA21C17910E000DF951 /* Acl.swift in Sources */,
57AC52881D395F7D000887D3 /* AuthSource.swift in Sources */,
Expand Down
31 changes: 27 additions & 4 deletions Kinvey/Kinvey/Acl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
//

import Foundation
import Realm
import RealmSwift

/// This class represents the ACL (Access Control List) for a record.
public final class Acl: Object, BuilderType {
Expand All @@ -18,10 +16,10 @@ public final class Acl: Object, BuilderType {
public dynamic var creator: String?

/// The `userId` of the `User` used to create the record.
public let globalRead = RealmOptional<Bool>()
public let globalRead = KinveyOptional<Bool>()

/// The `userId` of the `User` used to create the record.
public let globalWrite = RealmOptional<Bool>()
public let globalWrite = KinveyOptional<Bool>()

@objc
fileprivate dynamic var readersValue: String?
Expand Down Expand Up @@ -79,13 +77,34 @@ public final class Acl: Object, BuilderType {
self.writers = writers
}

#if canImport(RealmSwift)
/**
WARNING: This is an internal initializer not intended for public use.
:nodoc:
*/
public override class func ignoredProperties() -> [String] {
return ["readers", "writers"]
}
#endif

public convenience required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let creator = try container.decode(String.self, forKey: .creator)
self.init(creator: creator)
globalRead.value = try container.decodeIfPresent(Bool.self, forKey: .globalRead)
globalWrite.value = try container.decodeIfPresent(Bool.self, forKey: .globalWrite)
readers = try container.decodeIfPresent([String].self, forKey: .readers)
writers = try container.decodeIfPresent([String].self, forKey: .writers)
}

public override func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encodeIfPresent(creator, forKey: .creator)
try container.encodeIfPresent(globalRead.value, forKey: .globalRead)
try container.encodeIfPresent(globalWrite.value, forKey: .globalWrite)
try container.encodeIfPresent(readers, forKey: .readers)
try container.encodeIfPresent(writers, forKey: .writers)
}

}

Expand All @@ -110,6 +129,7 @@ extension Acl: JSONDecodable {

}

<<<<<<< HEAD
extension Acl: Decodable {

public convenience init(from decoder: Decoder) throws {
Expand Down Expand Up @@ -138,6 +158,9 @@ extension Acl: Encodable {
}

@available(*, deprecated, message: "Deprecated in version 3.18.0. Please use Swift.Codable instead")
=======
@available(*, deprecated: 3.18.0, message: "Please use Swift.Codable instead")
>>>>>>> MLIBZ-921: Swift Package Manager Beta Support
extension Acl: Mappable {

/// Constructor that validates if the map contains at least the creator.
Expand Down
12 changes: 11 additions & 1 deletion Kinvey/Kinvey/Cache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ class AnyCache<T: Persistable>: CacheType {
func invalidateLastSync(query: Query) -> Date? {
return _invalidateLastSync(query)
}

func observe(_ query: Query?, completionHandler: @escaping (CollectionChange<AnyRandomAccessCollection<T>>) -> Void) -> AnyNotificationToken {
return _observe(query, completionHandler)
}
Expand All @@ -291,3 +291,13 @@ class AnyCache<T: Persistable>: CacheType {
}

}

public enum CollectionChange<CollectionType> {

case initial(CollectionType)

case update(CollectionType, deletions: [Int], insertions: [Int], modifications: [Int])

case error(Swift.Error)

}
60 changes: 50 additions & 10 deletions Kinvey/Kinvey/CacheManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,44 @@
//

import Foundation

#if canImport(RealmSwift)
import Realm
import RealmSwift
#else
var sqliteMap = [URL : SQLiteWrapper]()

func sqliteWrapper(fileURL: URL?) -> SQLiteWrapper {
let fileURL = fileURL ?? cacheBasePath.appendingPathComponent("kinvey.sqlite")
if let sqlite = sqliteMap[fileURL] {
return sqlite
}
let sqlite = try! SQLiteWrapper(fileURL: fileURL)
sqliteMap[fileURL] = sqlite
return sqlite
}
#endif

internal class CacheManager: NSObject {

fileprivate let persistenceId: String
fileprivate let encryptionKey: Data?
fileprivate let schemaVersion: UInt64
private let persistenceId: String
private let jsonParser: JSONParser
private let encryptionKey: Data?
private let schemaVersion: UInt64

init(persistenceId: String, encryptionKey: Data? = nil, schemaVersion: UInt64 = 0) {
init(
persistenceId: String,
jsonParser: JSONParser,
encryptionKey: Data? = nil,
schemaVersion: UInt64 = 0
) {
self.persistenceId = persistenceId
self.jsonParser = jsonParser
self.encryptionKey = encryptionKey
self.schemaVersion = schemaVersion
}

func cache<T: Persistable>(fileURL: URL? = nil, type: T.Type) throws -> AnyCache<T>? where T: NSObject {
func cache<T: Persistable>(fileURL: URL? = nil, type: T.Type) throws -> AnyCache<T>? where T: Object {
let fileManager = FileManager.default
if let fileURL = fileURL {
do {
Expand All @@ -33,29 +55,46 @@ internal class CacheManager: NSObject {
}
}

#if canImport(RealmSwift)
return AnyCache(try RealmCache<T>(persistenceId: persistenceId, fileURL: fileURL, encryptionKey: encryptionKey, schemaVersion: schemaVersion))
#else
return AnyCache(
try SQLiteCache<T>(
persistenceId: persistenceId,
sqlite: sqliteWrapper(fileURL: fileURL)
)
)
#endif
}

func fileCache<FileType: File>(fileURL: URL? = nil) -> AnyFileCache<FileType>? {
#if canImport(RealmSwift)
return AnyFileCache(RealmFileCache<FileType>(persistenceId: persistenceId, fileURL: fileURL, encryptionKey: encryptionKey, schemaVersion: schemaVersion))
#else
return AnyFileCache(
try! SQLiteFileCache<FileType>(
sqlite: sqliteWrapper(fileURL: fileURL)
)
)
#endif
}

func clearAll(_ tag: String? = nil) {
let path = cacheBasePath
let basePath = (path as NSString).appendingPathComponent(persistenceId)
#if canImport(RealmSwift)
let basePath = cacheBasePath.appendingPathComponent(persistenceId)

let fileManager = FileManager.default

var isDirectory = ObjCBool(false)
let exists = fileManager.fileExists(atPath: basePath, isDirectory: &isDirectory)
let exists = fileManager.fileExists(atPath: basePath.path, isDirectory: &isDirectory)
if exists && isDirectory.boolValue {
var array = try! fileManager.subpathsOfDirectory(atPath: basePath)
var array = try! fileManager.subpathsOfDirectory(atPath: basePath.path)
array = array.filter({ (path) -> Bool in
return path.hasSuffix(".realm") && (tag == nil || path.caseInsensitiveCompare(tag! + ".realm") == .orderedSame)
})
for realmFile in array {
var realmConfiguration = Realm.Configuration.defaultConfiguration
realmConfiguration.fileURL = URL(fileURLWithPath: (basePath as NSString).appendingPathComponent(realmFile))
realmConfiguration.fileURL = basePath.appendingPathComponent(realmFile)
if let encryptionKey = encryptionKey {
realmConfiguration.encryptionKey = encryptionKey
}
Expand All @@ -66,6 +105,7 @@ internal class CacheManager: NSObject {
}
}
}
#endif
}

}
20 changes: 16 additions & 4 deletions Kinvey/Kinvey/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ open class Client: Credential {
sharedKeychain.kinveyAuth = kinveyAuth
}
} else if let appKey = appKey {
CacheManager(persistenceId: appKey, encryptionKey: encryptionKey as Data?).clearAll()
CacheManager(
persistenceId: appKey,
jsonParser: jsonParser,
encryptionKey: encryptionKey as Data?
).clearAll()
try! Keychain(appKey: appKey, client: self).removeAll()
if let sharedKeychain = sharedKeychain {
try! sharedKeychain.removeAll()
Expand Down Expand Up @@ -464,7 +468,12 @@ open class Client: Credential {
return
}

cacheManager = CacheManager(persistenceId: appKey, encryptionKey: encryptionKey as Data?, schemaVersion: schemaVersion)
cacheManager = CacheManager(
persistenceId: appKey,
jsonParser: jsonParser,
encryptionKey: encryptionKey as Data?,
schemaVersion: schemaVersion
)
syncManager = SyncManager(persistenceId: appKey, encryptionKey: encryptionKey as Data?, schemaVersion: schemaVersion)

var apiHostName = apiHostName
Expand Down Expand Up @@ -531,9 +540,12 @@ open class Client: Credential {
}

internal class func fileURL(appKey: String, tag: String = defaultTag) -> URL {
let path = cacheBasePath as NSString
var filePath = URL(fileURLWithPath: path.appendingPathComponent(appKey))
var filePath = cacheBasePath.appendingPathComponent(appKey)
#if canImport(RealmSwift)
filePath.appendPathComponent("\(tag).realm")
#else
filePath.appendPathComponent("\(tag).sqlite")
#endif
return filePath
}

Expand Down
4 changes: 3 additions & 1 deletion Kinvey/Kinvey/DataStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import PromiseKit

/// Class to interact with a specific collection in the backend.
open class DataStore<T: Persistable> where T: NSObject {
open class DataStore<T: Persistable> where T: Object {

@available(*, deprecated, message: "Deprecated in version 3.17.0. Please use Result<AnyRandomAccessCollection<T>, Swift.Error> instead")
public typealias ArrayCompletionHandler = ([T]?, Swift.Error?) -> Void
Expand Down Expand Up @@ -1300,9 +1300,11 @@ open class DataStore<T: Persistable> where T: NSObject {
return AnyRequest(request)
}

#if canImport(RealmSwift)
public func observe(_ query: Query? = nil, completionHandler: @escaping (CollectionChange<AnyRandomAccessCollection<T>>) -> Void) -> AnyNotificationToken? {
return cache?.observe(query, completionHandler: completionHandler)
}
#endif

}

Expand Down
Loading

0 comments on commit 2446cb9

Please sign in to comment.