Skip to content

Commit

Permalink
Merge pull request #72 from olgadanylova/master
Browse files Browse the repository at this point in the history
Changes and fixes
  • Loading branch information
Olha Danylova authored May 28, 2019
2 parents fa0064e + 92949f0 commit 5a38641
Show file tree
Hide file tree
Showing 21 changed files with 161 additions and 273 deletions.
4 changes: 2 additions & 2 deletions BackendlessSwift.podspec
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Pod::Spec.new do |s|
s.name = "BackendlessSwift"
s.module_name = "Backendless"
s.version = "0.0.6"
s.source = { :git => 'https://github.com/Backendless/Swift-SDK.git', :tag => '0.0.6' }
s.version = "0.0.7"
s.source = { :git => 'https://github.com/Backendless/Swift-SDK.git', :tag => '0.0.7' }
s.license = { :type => 'MIT', :text => 'Copyright (c) 2013-2019 by Backendless Corp' }
s.homepage = "http://backendless.com"
s.authors = { 'Mark Piller' => '[email protected]', 'Olha Danylova' => '[email protected]' }
Expand Down
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
# RELEASE HISTORY

### 0.0.7, / Nay, 28 2019
### 0.0.7, / May, 28 2019
* fixed bug in the CacheService methods when key contains spaces
* fixed crashes in DataPermission when object is Dictionary
* added the segmentQuery property to the DeliveryOptions
* removed the device registration methods that don't take deviceToken as parameter
* removed the `refreshDeviceToken` method
* removed the unnecessary init methods
* customized Serialization/Deserialization for next classes: ObjectProperty, BackendlessFileInfo, GeoCategory, BackendlessUser, UserProperty
```
init(from decoder: Decoder) throws
func encode(to encoder: Encoder) throws
```

Expand Down
12 changes: 6 additions & 6 deletions Sources/SwiftSDK/Cache/CacheService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
open func put(key: String, object: Any, timeToLiveSec: Int, responseHandler: (() -> Void)!, errorHandler: ((Fault) -> Void)!) {
let headers = ["Content-Type": "application/json"]
let parameters = jsonUtils.objectToJSON(objectToParse: object)
BackendlessRequestManager(restMethod: "cache/\(key)?timeout=\(timeToLiveSec)", httpMethod: .PUT, headers: headers, parameters: parameters).makeRequest(getResponse: { response in
BackendlessRequestManager(restMethod: "cache/\(dataTypesUtils.stringToUrlString(originalString: key))?timeout=\(timeToLiveSec)", httpMethod: .PUT, headers: headers, parameters: parameters).makeRequest(getResponse: { response in
if let result = self.processResponse.adapt(response: response, to: NoReply.self) {
if result is Fault {
errorHandler(result as! Fault)
Expand All @@ -46,7 +46,7 @@
}

open func get(key: String, responseHandler: ((Any) -> Void)!, errorHandler: ((Fault) -> Void)!) {
BackendlessRequestManager(restMethod: "cache/\(key)", httpMethod: .GET, headers: nil, parameters: nil).makeRequest(getResponse: { response in
BackendlessRequestManager(restMethod: "cache/\(dataTypesUtils.stringToUrlString(originalString: key))", httpMethod: .GET, headers: nil, parameters: nil).makeRequest(getResponse: { response in
if let result = self.processResponse.adapt(response: response, to: JSON.self) {
if result is Fault {
errorHandler(result as! Fault)
Expand Down Expand Up @@ -77,7 +77,7 @@
}

open func contains(key: String, responseHandler: ((Bool) -> Void)!, errorHandler: ((Fault) -> Void)!) {
BackendlessRequestManager(restMethod: "cache/\(key)/check", httpMethod: .GET, headers: nil, parameters: nil).makeRequest(getResponse: { response in
BackendlessRequestManager(restMethod: "cache/\(dataTypesUtils.stringToUrlString(originalString: key))/check", httpMethod: .GET, headers: nil, parameters: nil).makeRequest(getResponse: { response in
if let responseData = response.data {
do {
responseHandler(try JSONSerialization.jsonObject(with: responseData, options: .allowFragments) as! Bool)
Expand All @@ -92,7 +92,7 @@
}

open func expireIn(key: String, seconds: Int, responseHandler: (() -> Void)!, errorHandler: ((Fault) -> Void)!) {
BackendlessRequestManager(restMethod: "cache/\(key)/expireIn?timeout=\(seconds)", httpMethod: .PUT, headers: nil, parameters: nil).makeRequest(getResponse: { response in
BackendlessRequestManager(restMethod: "cache/\(dataTypesUtils.stringToUrlString(originalString: key))/expireIn?timeout=\(seconds)", httpMethod: .PUT, headers: nil, parameters: nil).makeRequest(getResponse: { response in
if let result = self.processResponse.adapt(response: response, to: NoReply.self) {
if result is Fault {
errorHandler(result as! Fault)
Expand All @@ -105,7 +105,7 @@
}

open func expireAt(key: String, date: Date, responseHandler: (() -> Void)!, errorHandler: ((Fault) -> Void)!) {
BackendlessRequestManager(restMethod: "cache/\(key)/expireAt?timestamp=\(dataTypesUtils.dateToInt(date: date))", httpMethod: .PUT, headers: nil, parameters: nil).makeRequest(getResponse: { response in
BackendlessRequestManager(restMethod: "cache/\(dataTypesUtils.stringToUrlString(originalString: key))/expireAt?timestamp=\(dataTypesUtils.dateToInt(date: date))", httpMethod: .PUT, headers: nil, parameters: nil).makeRequest(getResponse: { response in
if let result = self.processResponse.adapt(response: response, to: NoReply.self) {
if result is Fault {
errorHandler(result as! Fault)
Expand All @@ -118,7 +118,7 @@
}

open func remove(key: String, responseHandler: (() -> Void)!, errorHandler: ((Fault) -> Void)!) {
BackendlessRequestManager(restMethod: "cache/\(key)", httpMethod: .DELETE, headers: nil, parameters: nil).makeRequest(getResponse: { response in
BackendlessRequestManager(restMethod: "cache/\(dataTypesUtils.stringToUrlString(originalString: key))", httpMethod: .DELETE, headers: nil, parameters: nil).makeRequest(getResponse: { response in
if let result = self.processResponse.adapt(response: response, to: NoReply.self) {
if result is Fault {
errorHandler(result as! Fault)
Expand Down
24 changes: 8 additions & 16 deletions Sources/SwiftSDK/Files/BackendlessFileInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,15 @@
case url
}

public init(name: String?, createdOn: Int, publicUrl: String?, _size: Int?, url: String?) {
self.name = name
self.createdOn = createdOn
self.publicUrl = publicUrl
self._size = _size
self.url = url
}

public override init() { }

convenience public required init?(coder aDecoder: NSCoder) {
let name = aDecoder.decodeObject(forKey: CodingKeys.name.rawValue) as? String
let createdOn = aDecoder.decodeInteger(forKey: CodingKeys.createdOn.rawValue)
let publicUrl = aDecoder.decodeObject(forKey: CodingKeys.publicUrl.rawValue) as? String
let _size = aDecoder.decodeInteger(forKey: CodingKeys._size.rawValue)
let url = aDecoder.decodeObject(forKey: CodingKeys.url.rawValue) as? String
self.init(name: name, createdOn: createdOn, publicUrl: publicUrl, _size: _size, url: url)
self.init()
self.name = aDecoder.decodeObject(forKey: CodingKeys.name.rawValue) as? String
self.createdOn = aDecoder.decodeInteger(forKey: CodingKeys.createdOn.rawValue)
self.publicUrl = aDecoder.decodeObject(forKey: CodingKeys.publicUrl.rawValue) as? String
self._size = aDecoder.decodeInteger(forKey: CodingKeys._size.rawValue)
self.url = aDecoder.decodeObject(forKey: CodingKeys.url.rawValue) as? String
}

public func encode(with aCoder: NSCoder) {
Expand All @@ -74,7 +68,6 @@

required public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

name = try container.decodeIfPresent(String.self, forKey: .name)
createdOn = try container.decodeIfPresent(Int.self, forKey: .createdOn) ?? 0
publicUrl = try container.decodeIfPresent(String.self, forKey: .publicUrl)
Expand All @@ -84,7 +77,6 @@

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encodeIfPresent(name, forKey: .name)
try container.encode(createdOn, forKey: .createdOn)
try container.encodeIfPresent(publicUrl, forKey: .publicUrl)
Expand Down
38 changes: 12 additions & 26 deletions Sources/SwiftSDK/Geo/BackendlessGeoQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,33 +72,19 @@

public override init() { }

public init(geoPoint: GeoPoint?, radius: Double?, categories: [String]?, includemetadata: Bool, _metadata: JSON?, whereClause: String?, rectangle: GeoQueryRectangle?, pageSize: Int, offset: Int, degreePerPixel: Double, clusterGridSize: Double) {
self.geoPoint = geoPoint
self.radius = radius
self.categories = categories
self.includemetadata = includemetadata
self._metadata = _metadata
self.whereClause = whereClause
self.rectangle = rectangle
self.pageSize = pageSize
self.offset = offset
self.degreePerPixel = degreePerPixel
self.clusterGridSize = clusterGridSize
}

convenience public required init?(coder aDecoder: NSCoder) {
let geoPoint = aDecoder.decodeObject(forKey: CodingKeys.geoPoint.rawValue) as? GeoPoint
let radius = aDecoder.decodeDouble(forKey: CodingKeys.radius.rawValue)
let categories = aDecoder.decodeObject(forKey: CodingKeys.categories.rawValue) as? [String]
let includemetadata = aDecoder.decodeBool(forKey: CodingKeys.includemetadata.rawValue)
let _metadata = aDecoder.decodeObject(forKey: CodingKeys._metadata.rawValue) as? JSON
let whereClause = aDecoder.decodeObject(forKey: CodingKeys.whereClause.rawValue) as? String
let rectangle = aDecoder.decodeObject(forKey: CodingKeys.rectangle.rawValue) as? GeoQueryRectangle
let pageSize = aDecoder.decodeInteger(forKey: CodingKeys.pageSize.rawValue)
let offset = aDecoder.decodeInteger(forKey: CodingKeys.offset.rawValue)
let degreePerPixel = aDecoder.decodeDouble(forKey: CodingKeys.degreePerPixel.rawValue)
let clusterGridSize = aDecoder.decodeDouble(forKey: CodingKeys.clusterGridSize.rawValue)
self.init(geoPoint: geoPoint, radius: radius, categories: categories, includemetadata: includemetadata, _metadata: _metadata, whereClause: whereClause, rectangle: rectangle, pageSize: pageSize, offset: offset, degreePerPixel: degreePerPixel, clusterGridSize: clusterGridSize)
self.init()
self.geoPoint = aDecoder.decodeObject(forKey: CodingKeys.geoPoint.rawValue) as? GeoPoint
self.radius = aDecoder.decodeDouble(forKey: CodingKeys.radius.rawValue)
self.categories = aDecoder.decodeObject(forKey: CodingKeys.categories.rawValue) as? [String]
self.includemetadata = aDecoder.decodeBool(forKey: CodingKeys.includemetadata.rawValue)
self._metadata = aDecoder.decodeObject(forKey: CodingKeys._metadata.rawValue) as? JSON
self.whereClause = aDecoder.decodeObject(forKey: CodingKeys.whereClause.rawValue) as? String
self.rectangle = aDecoder.decodeObject(forKey: CodingKeys.rectangle.rawValue) as? GeoQueryRectangle
self.pageSize = aDecoder.decodeInteger(forKey: CodingKeys.pageSize.rawValue)
self.offset = aDecoder.decodeInteger(forKey: CodingKeys.offset.rawValue)
self.degreePerPixel = aDecoder.decodeDouble(forKey: CodingKeys.degreePerPixel.rawValue)
self.clusterGridSize = aDecoder.decodeDouble(forKey: CodingKeys.clusterGridSize.rawValue)
}

public func encode(with aCoder: NSCoder) {
Expand Down
29 changes: 11 additions & 18 deletions Sources/SwiftSDK/Messaging/DeliveryOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

open var publishAt: Date?
open var repeatExpiresAt: Date?
open var segmentQuery: String?

private var _repeatEvery: Int?
open var repeatEvery: NSNumber? {
Expand Down Expand Up @@ -67,25 +68,17 @@

public override init() { }

public init(publishAt: Date?, repeatExpiresAt: Date?, _repeatEvery: Int?, pushSinglecast: [String]?, publishPolicy: Int, pushBroadcast: Int) {
self.publishAt = publishAt
self.repeatExpiresAt = repeatExpiresAt
self._repeatEvery = _repeatEvery
if pushSinglecast != nil {
self.pushSinglecast = pushSinglecast!
}
self.publishPolicy = publishPolicy
self.pushBroadcast = pushBroadcast
}

convenience public required init?(coder aDecoder: NSCoder) {
let publishAt = aDecoder.decodeObject(forKey: CodingKeys.publishAt.rawValue) as? Date
let repeatExpiresAt = aDecoder.decodeObject(forKey: CodingKeys.repeatExpiresAt.rawValue) as? Date
let _repeatEvery = aDecoder.decodeInteger(forKey: CodingKeys._repeatEvery.rawValue)
let pushSinglecast = aDecoder.decodeObject(forKey: CodingKeys.pushSinglecast.rawValue) as? [String]
let publishPolicy = aDecoder.decodeInteger(forKey: CodingKeys.publishPolicy.rawValue)
let pushBroadcast = aDecoder.decodeInteger(forKey: CodingKeys.pushBroadcast.rawValue)
self.init(publishAt: publishAt, repeatExpiresAt: repeatExpiresAt, _repeatEvery: _repeatEvery, pushSinglecast: pushSinglecast, publishPolicy: publishPolicy, pushBroadcast: pushBroadcast)
self.init()
self.publishAt = aDecoder.decodeObject(forKey: CodingKeys.publishAt.rawValue) as? Date
self.repeatExpiresAt = aDecoder.decodeObject(forKey: CodingKeys.repeatExpiresAt.rawValue) as? Date
self._repeatEvery = aDecoder.decodeInteger(forKey: CodingKeys._repeatEvery.rawValue)
self.pushSinglecast = []
if let pushSinglecast = aDecoder.decodeObject(forKey: CodingKeys.pushSinglecast.rawValue) as? [String] {
self.pushSinglecast = pushSinglecast
}
self.publishPolicy = aDecoder.decodeInteger(forKey: CodingKeys.publishPolicy.rawValue)
self.pushBroadcast = aDecoder.decodeInteger(forKey: CodingKeys.pushBroadcast.rawValue)
}

public func encode(with aCoder: NSCoder) {
Expand Down
60 changes: 5 additions & 55 deletions Sources/SwiftSDK/Messaging/MessagingService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,6 @@
return channel
}

open func registerDevice(responseHandler: ((String) -> Void)!, errorHandler: ((Fault) -> Void)!) {
registerDevice(deviceToken: nil, channels: [DEFAULT_CHANNEL_NAME], expirationDate: nil, responseHandler: responseHandler, errorHandler: errorHandler)
}

open func registerDevice(channels: [String], responseHandler: ((String) -> Void)!, errorHandler: ((Fault) -> Void)!) {
registerDevice(deviceToken: nil, channels: channels, expirationDate: nil, responseHandler: responseHandler, errorHandler: errorHandler)
}

open func registerDevice(expiration: Date, responseHandler: ((String) -> Void)!, errorHandler: ((Fault) -> Void)!) {
registerDevice(deviceToken: nil, channels: [DEFAULT_CHANNEL_NAME], expirationDate: expiration, responseHandler: responseHandler, errorHandler: errorHandler)
}

open func registerDevice(channels: [String], expiration: Date, responseHandler: ((String) -> Void)!, errorHandler: ((Fault) -> Void)!) {
registerDevice(deviceToken: nil, channels: channels, expirationDate: expiration, responseHandler: responseHandler, errorHandler: errorHandler)
}

open func registerDevice(deviceToken: Data, responseHandler: ((String) -> Void)!, errorHandler: ((Fault) -> Void)!) {
registerDevice(deviceToken: deviceToken, channels: [DEFAULT_CHANNEL_NAME], expirationDate: nil, responseHandler: responseHandler, errorHandler: errorHandler)
}
Expand All @@ -95,22 +79,9 @@
registerDevice(deviceToken: deviceToken, channels: channels, expirationDate: expiration, responseHandler: responseHandler, errorHandler: errorHandler)
}

func registerDevice(deviceToken: Data?, channels: [String], expirationDate: Date?, responseHandler: ((String) -> Void)!, errorHandler: ((Fault) -> Void)!) {
var token = deviceToken
if token == nil {
if let tokenData = userDefaultsHelper.getDeviceToken() {
token = tokenData
}
else {
errorHandler(Fault(message: "Device token not found", faultCode: 0))
return
}
}
userDefaultsHelper.saveDeviceToken(deviceToken: token!)
let deviceTokenString = token!.map { String(format: "%02.2hhx", $0) }.joined()

func registerDevice(deviceToken: Data, channels: [String], expirationDate: Date?, responseHandler: ((String) -> Void)!, errorHandler: ((Fault) -> Void)!) {
let headers = ["Content-Type": "application/json"]
var parameters = ["deviceToken": deviceTokenString, "channels": channels] as [String : Any]
var parameters = ["deviceToken": deviceToken.map { String(format: "%02.2hhx", $0) }.joined(), "channels": channels] as [String : Any]
if let deviceId = deviceRegistration.deviceId {
parameters["deviceId"] = deviceId
keychainUtils.saveDeviceId(deviceId: deviceId)
Expand Down Expand Up @@ -226,30 +197,6 @@
errorHandler(fault)
})
}

open func refreshDeviceToken(newDeviceToken: Data, responseHandler: ((Bool) -> Void)!, errorHandler: ((Fault) -> Void)!) {
let dataQueryBuilder = DataQueryBuilder()
dataQueryBuilder.setWhereClause(whereClause: String(format: "deviceId='%@'", deviceHelper.deviceId))
Backendless.shared.data.of(DeviceRegistration.self).find(queryBuilder: dataQueryBuilder, responseHandler: { deviceRegs in
if let deviceRegs = deviceRegs as? [DeviceRegistration] {
let group = DispatchGroup()
for deviceReg in deviceRegs {
deviceReg.deviceToken = newDeviceToken.map { String(format: "%02.2hhx", $0) }.joined()
group.enter()
Backendless.shared.data.of(DeviceRegistration.self).update(entity: deviceReg, responseHandler: { updatedReg in
group.leave()
}, errorHandler: { fault in
errorHandler(fault)
})
}
group.notify(queue: OperationQueue.current!.underlyingQueue!, execute: {
responseHandler(true)
})
}
}, errorHandler: { fault in
errorHandler(fault)
})
}
#endif

open func publish(channelName: String, message: Any, responseHandler: ((MessageStatus) -> Void)!, errorHandler: ((Fault) -> Void)!) {
Expand Down Expand Up @@ -311,6 +258,9 @@
if let pushSinglecast = deliveryOptions?.getPushSinglecast(), pushSinglecast.count > 0 {
parameters["pushSinglecast"] = pushSinglecast
}
if let segmentQuery = deliveryOptions?.segmentQuery {
parameters["segmentQuery"] = segmentQuery
}
BackendlessRequestManager(restMethod: "messaging/\(channelName)", httpMethod: .POST, headers: headers, parameters: parameters).makeRequest(getResponse: { response in
if let result = self.processResponse.adapt(response: response, to: MessageStatus.self) {
if result is Fault {
Expand Down
11 changes: 3 additions & 8 deletions Sources/SwiftSDK/Messaging/PublishOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,10 @@
self.headers = ["ios-content-available": "1"]
}

public init(publisherId: String?, _headers: JSON?) {
self.publisherId = publisherId
self._headers = _headers
}

convenience public required init?(coder aDecoder: NSCoder) {
let publisherId = aDecoder.decodeObject(forKey: CodingKeys.publisherId.rawValue) as? String
let _headers = aDecoder.decodeObject(forKey: CodingKeys._headers.rawValue) as? JSON
self.init(publisherId: publisherId, _headers: _headers)
self.init()
self.publisherId = aDecoder.decodeObject(forKey: CodingKeys.publisherId.rawValue) as? String
self._headers = aDecoder.decodeObject(forKey: CodingKeys._headers.rawValue) as? JSON
}

public func encode(with aCoder: NSCoder) {
Expand Down
4 changes: 1 addition & 3 deletions Sources/SwiftSDK/Messaging/RT/RTMessaging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,10 @@ class RTMessaging: RTListener {

func addMessageListener(selector: String?, responseHandler: ((PublishMessageInfo) -> Void)!, errorHandler: ((Fault) -> Void)!) -> RTSubscription? {
let wrappedBlock: (Any) -> () = { response in

if let response = response as? [String : Any] {
let publishMessageInfo = self.processResponse.adapt(responseDictionary: response, to: PublishMessageInfo.self)
responseHandler(publishMessageInfo as! PublishMessageInfo)
}

}
}
if self.channel.isJoined {
var options = [String : Any]()
Expand Down
Loading

0 comments on commit 5a38641

Please sign in to comment.