Commit c9484de2 authored by Jose Blaya's avatar Jose Blaya

Refactor

parent 5b232920
......@@ -147,9 +147,9 @@ public protocol AccountProvider: class {
/**
Returns the available messages from the API.
- Parameter callback: Returns `nil` on success.
- Parameter callback: Returns the message`InAppMessage` on success.
*/
func inAppMessages(_ callback: SuccessLibraryCallback?)
func inAppMessages(_ callback: LibraryCallback<InAppMessage>?)
#if os(iOS)
/**
......
......@@ -28,18 +28,41 @@ public enum InAppMessageType {
case link
}
public enum InAppMessageLevel {
case system
case api
}
public struct InAppMessage {
private let message: [String: String]
private let linkMessage: [String: String]
private let type: InAppMessageType
public let id: String
public let message: [String: String]
public let linkMessage: [String: String]
public let type: InAppMessageType
public let level: InAppMessageLevel
public let settingAction: [String: Bool]?
public let settingView: String?
public let settingLink: String?
init(withMessage message: [String: String], id: String, link: [String: String], type: InAppMessageType, level: InAppMessageLevel, actions: [String:Bool]?, view: String?, uri: String?) {
self.id = id
self.message = message
self.linkMessage = link
self.type = type
self.level = level
self.settingAction = actions
self.settingView = view
self.settingLink = uri
}
private let settingAction: [String: Bool]?
private let settingView: String?
private let settingLink: String?
}
extension InAppMessage {
init(withMessage messageInformation: MessagesInformation.Message) {
init(withMessage messageInformation: MessagesInformation.Message, andLevel level: InAppMessageLevel) {
self.id = "messageInformation.id"
self.message = messageInformation.message
self.linkMessage = messageInformation.link.text
......@@ -64,6 +87,8 @@ public struct InAppMessage {
self.settingAction = nil
}
self.level = level
}
}
......@@ -74,5 +74,5 @@ protocol WebServices: class {
// MARK: Messages
func messages(_ callback: SuccessLibraryCallback?)
func messages(_ callback: LibraryCallback<InAppMessage>?)
}
......@@ -328,9 +328,9 @@ class DefaultAccountProvider: AccountProvider, ConfigurationAccess, DatabaseAcce
}
}
func inAppMessages(_ callback: SuccessLibraryCallback?) {
webServices.messages { (error) in
callback?(nil)
func inAppMessages(_ callback: LibraryCallback<InAppMessage>?) {
webServices.messages { (message, error) in
callback?(message, error)
}
}
......
......@@ -421,28 +421,29 @@ class PIAWebServices: WebServices, ConfigurationAccess {
}
// MARK: Messages
func messages(_ callback: SuccessLibraryCallback?) {
func messages(_ callback: LibraryCallback<InAppMessage>?) {
if let token = Client.providers.accountProvider.token {
self.accountAPI.messages(token: token, callback: { (messages, error) in
if let error = error {
callback?(ClientError.malformedResponseData)
callback?(nil, ClientError.malformedResponseData)
return
}
if let message = messages.first {
let inAppMessage = InAppMessage(withMessage: message)
//Client.message...
let inAppMessage = InAppMessage(withMessage: message, andLevel: .api)
callback?(inAppMessage, nil)
} else {
callback?(nil, nil)
}
callback?(nil)
})
} else {
callback?(ClientError.unauthorized)
callback?(nil, ClientError.unauthorized)
}
}
......
......@@ -310,7 +310,7 @@ public class MockAccountProvider: AccountProvider, WebServicesConsumer {
callback?(nil)
}
public func inAppMessages(_ callback: SuccessLibraryCallback?) {
callback?(nil)
public func inAppMessages(_ callback: LibraryCallback<InAppMessage>?) {
callback?(nil, nil)
}
}
......@@ -135,7 +135,8 @@ class MockWebServices: WebServices {
callback?(["mock-test"], nil)
}
func messages(_ callback: SuccessLibraryCallback?) {
callback?(nil)
func messages(_ callback: LibraryCallback<InAppMessage>?) {
callback?(InAppMessage(withMessage: ["en" : "Message"], id: "123", link: ["en" : "Message"], type: .link, level: .api, actions: nil, view: nil, uri: "https://www.privateinternetaccess.com"), nil)
}
}
......@@ -434,8 +434,7 @@ class EphemeralAccountProvider: AccountProvider, ProvidersAccess, InAppAccess {
callback?(nil)
}
func inAppMessages(_ callback: SuccessLibraryCallback?) {
callback?(nil)
func inAppMessages(_ callback: LibraryCallback<InAppMessage>?) {
callback?(nil, nil)
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment