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 Jul 11, 2018
1 parent 2bfb577 commit c68652d
Show file tree
Hide file tree
Showing 50 changed files with 682 additions and 75 deletions.
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
Kinvey.framework.zip
scripts/RealtimeSend/Rome

# Swift Package Manager
/Kinvey.xcodeproj
.DS_Store
/.build
/Packages
8 changes: 4 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
import ObjectMapper

class AclTransformType: TransformType {
Expand Down Expand Up @@ -47,10 +45,10 @@ public final class Acl: Object, BuilderType {
open dynamic var creator: String?

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

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

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

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

}

Expand Down
12 changes: 11 additions & 1 deletion Kinvey/Kinvey/Cache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,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 @@ -262,3 +262,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)

}
22 changes: 17 additions & 5 deletions Kinvey/Kinvey/CacheManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
//

import Foundation

#if canImport(RealmSwift)
import Realm
import RealmSwift
#endif

internal class CacheManager: NSObject {

Expand All @@ -33,29 +36,37 @@ internal class CacheManager: NSObject {
}
}

#if canImport(RealmSwift)
return AnyCache(RealmCache<T>(persistenceId: persistenceId, fileURL: fileURL, encryptionKey: encryptionKey, schemaVersion: schemaVersion))
#else
return nil
#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 nil
#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 +77,7 @@ internal class CacheManager: NSObject {
}
}
}
#endif
}

}
3 changes: 1 addition & 2 deletions Kinvey/Kinvey/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,7 @@ 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)
filePath.appendPathComponent("\(tag).realm")
return filePath
}
Expand Down
2 changes: 2 additions & 0 deletions Kinvey/Kinvey/DataStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1193,9 +1193,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
53 changes: 52 additions & 1 deletion Kinvey/Kinvey/Entity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,57 @@
//

import Foundation
import ObjectMapper

#if canImport(RealmSwift)
import Realm
import RealmSwift
import ObjectMapper

public typealias List<T: RealmSwift.Object> = RealmSwift.List<T>
public typealias Object = RealmSwift.Object
#else
open class List<T> {

private var array = Array<T>()

public init() {
}

open func append(_ value: T) {
array.append(value)
}

open var count: Int {
return array.count
}

open var first: T? {
return array.first
}

open var last: T? {
return array.last
}

}

open class Object: NSObject {

public required override init() {
super.init()
}

public subscript(key: String) -> Any? {
get {
return value(forKey: key)
}
set {
setValue(newValue, forKey: key)
}
}

}
#endif

internal func StringFromClass(cls: AnyClass) -> String {
var className = NSStringFromClass(cls)
Expand Down Expand Up @@ -90,14 +135,17 @@ open class Entity: Object, Persistable {
@objc
public dynamic var acl: Acl?

#if canImport(RealmSwift)
internal var realmConfiguration: Realm.Configuration?
internal var reference: ThreadSafeReference<Entity>?
#endif

/// Default Constructor.
public required init() {
super.init()
}

#if canImport(Realm)
/**
WARNING: This is an internal initializer not intended for public use.
:nodoc:
Expand All @@ -113,6 +161,7 @@ open class Entity: Object, Persistable {
public required init(value: Any, schema: RLMSchema) {
super.init(value: value, schema: schema)
}
#endif

/// Override this method to tell how to map your own objects.
open func propertyMapping(_ map: Map) {
Expand All @@ -121,6 +170,7 @@ open class Entity: Object, Persistable {
acl <- ("acl", map[CodingKeys.acl])
}

#if canImport(RealmSwift)
/**
WARNING: This is an internal initializer not intended for public use.
:nodoc:
Expand Down Expand Up @@ -155,6 +205,7 @@ open class Entity: Object, Persistable {
}
return properties
}
#endif

/// This function is where all variable mappings should occur. It is executed by Mapper during the mapping (serialization and deserialization) process.
public func mapping(map: Map) {
Expand Down
6 changes: 5 additions & 1 deletion Kinvey/Kinvey/File.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
//

import Foundation
#if canImport(RealmSwift)
import RealmSwift
import Realm
#endif
import ObjectMapper

/// Class that represents a file in the backend holding all metadata of the file, but don't hold the data itself.
Expand All @@ -23,7 +25,7 @@ open class File: Object, Mappable {
open dynamic var fileName: String?

/// `size` property of the file.
open let size = RealmOptional<Int64>()
open let size = KinveyOptional<Int64>()

/// `mimeType` property of the file.
@objc
Expand Down Expand Up @@ -146,6 +148,7 @@ open class File: Object, Mappable {
uploadHeaders <- map["_requiredHeaders"]
}

#if canImport(RealmSwift)
open override class func primaryKey() -> String? {
return "fileId"
}
Expand All @@ -161,5 +164,6 @@ open class File: Object, Mappable {
]
return props
}
#endif

}
40 changes: 31 additions & 9 deletions Kinvey/Kinvey/FileStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,11 @@ open class FileStore<FileType: File> {
}
}
if let requests = requests {
requests.progress.addChild(request.progress, withPendingUnitCount: 1)
if #available(OSX 10.11, *) {
requests.progress.addChild(request.progress, withPendingUnitCount: 1)
} else {
fatalError("macOS 10.11+ is required")
}
requests += request
}
}
Expand Down Expand Up @@ -625,7 +629,11 @@ open class FileStore<FileType: File> {
}
}
let urlSessionTaskRequest = URLSessionTaskRequest<ResultType>(client: client, options: options, task: dataTask)
requests.progress.addChild(urlSessionTaskRequest.progress, withPendingUnitCount: 1)
if #available(OSX 10.11, *) {
requests.progress.addChild(urlSessionTaskRequest.progress, withPendingUnitCount: 1)
} else {
fatalError("macOS 10.11+ is required")
}
requests += urlSessionTaskRequest
dataTask.resume()
} else {
Expand Down Expand Up @@ -694,7 +702,11 @@ open class FileStore<FileType: File> {
handler(data, response, error)
}
let urlSessionTaskRequest = URLSessionTaskRequest<ResultType>(client: self.client, options: options, task: uploadTask)
requests.progress.addChild(urlSessionTaskRequest.progress, withPendingUnitCount: 98)
if #available(OSX 10.11, *) {
requests.progress.addChild(urlSessionTaskRequest.progress, withPendingUnitCount: 98)
} else {
fatalError("macOS 10.11+ is required")
}
requests += urlSessionTaskRequest
uploadTask.resume()
case let .url(url):
Expand All @@ -708,7 +720,11 @@ open class FileStore<FileType: File> {
handler(data, response, error)
}
let urlSessionTaskRequest = URLSessionTaskRequest<ResultType>(client: self.client, options: options, task: uploadTask)
requests.progress.addChild(urlSessionTaskRequest.progress, withPendingUnitCount: 98)
if #available(OSX 10.11, *) {
requests.progress.addChild(urlSessionTaskRequest.progress, withPendingUnitCount: 98)
} else {
fatalError("macOS 10.11+ is required")
}
requests += urlSessionTaskRequest
uploadTask.resume()
case let .stream(stream):
Expand All @@ -724,7 +740,11 @@ open class FileStore<FileType: File> {
handler(data, response, error)
}
let urlSessionTaskRequest = URLSessionTaskRequest<ResultType>(client: self.client, options: options, task: dataTask)
requests.progress.addChild(urlSessionTaskRequest.progress, withPendingUnitCount: 98)
if #available(OSX 10.11, *) {
requests.progress.addChild(urlSessionTaskRequest.progress, withPendingUnitCount: 98)
} else {
fatalError("macOS 10.11+ is required")
}
requests += urlSessionTaskRequest
dataTask.resume()
}
Expand Down Expand Up @@ -861,10 +881,8 @@ open class FileStore<FileType: File> {
let fileManager = FileManager()
if let entityId = entityId
{
let baseFolder = cacheBasePath
do {
var baseFolderURL = URL(fileURLWithPath: baseFolder)
baseFolderURL = baseFolderURL.appendingPathComponent(self.client.appKey!).appendingPathComponent("files")
let baseFolderURL = cacheBasePath.appendingPathComponent(self.client.appKey!).appendingPathComponent("files")
if !fileManager.fileExists(atPath: baseFolderURL.path) {
try fileManager.createDirectory(at: baseFolderURL, withIntermediateDirectories: true, attributes: nil)
}
Expand Down Expand Up @@ -1153,7 +1171,11 @@ open class FileStore<FileType: File> {
downloadURL: downloadURL,
options: options
)
multiRequest.progress.addChild(request.progress, withPendingUnitCount: 99)
if #available(OSX 10.11, *) {
multiRequest.progress.addChild(request.progress, withPendingUnitCount: 99)
} else {
fatalError("macOS 10.11+ is required")
}
multiRequest += request
return promise
case .data(let data):
Expand Down
Loading

0 comments on commit c68652d

Please sign in to comment.