Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add final attributes, add further code documentation #9

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions Sources/Deskpro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import UIKit
/// The `DeskPro` class provides methods for initializing the Messenger, managing user information,
/// handling push notifications, and presenting the DeskPro messaging interface.
///
public class DeskPro: Messenger {
public final class DeskPro: Messenger {

private var messengerConfig: MessengerConfig

Expand Down Expand Up @@ -43,7 +43,7 @@ public class DeskPro: Messenger {
/// The method is intended to simulate a test scenario and provide a String result based on the outcome of the test.
///
/// - Returns: A String representing the result of the test operation.
public func test() -> String {
public final func test() -> String {
return "Hello world from Messenger!"
}

Expand All @@ -54,7 +54,7 @@ public class DeskPro: Messenger {
///
/// - Parameter user: The [User](x-source-tag://User) object containing the user information.
///
public func setUserInfo(user: User) {
public final func setUserInfo(user: User) {
appUserDefaults?.setUserInfo(user)
}

Expand All @@ -64,7 +64,7 @@ public class DeskPro: Messenger {
///
/// - Returns: `true` if the token is successfully saved, `false` otherwise.
@discardableResult
public func authorizeUser(userJwt: String) -> Bool {
public final func authorizeUser(userJwt: String) -> Bool {
appUserDefaults?.setJwtToken(userJwt)
return true
}
Expand All @@ -75,7 +75,7 @@ public class DeskPro: Messenger {
///
/// - Returns: `true` if the logout operation is successful; `false` otherwise.
@discardableResult
public func forgetUser() -> Bool {
public final func forgetUser() -> Bool {
appUserDefaults?.clear()
return true
}
Expand All @@ -87,7 +87,7 @@ public class DeskPro: Messenger {
/// - Parameter token: The push registration token obtained from the device.
///
/// - Returns: `true` if the push registration token is successfully set; `false` otherwise.
public func setPushRegistrationToken(token: String) -> Bool {
public final func setPushRegistrationToken(token: String) -> Bool {
// TODO: Not yet implemented
return true
}
Expand All @@ -101,7 +101,7 @@ public class DeskPro: Messenger {
/// - Returns: `true` if the push notification is related to DeskPro; `false` otherwise.
///
/// - Tag: isDeskProPushNotification
public func isDeskProPushNotification(pushNotification: PushNotificationData) -> Bool {
public final func isDeskProPushNotification(pushNotification: PushNotificationData) -> Bool {
// TODO: Not yet implemented
return true
}
Expand All @@ -114,7 +114,7 @@ public class DeskPro: Messenger {
///
/// - Parameter pushNotification: The push notification data to be handled.
///
public func handlePushNotification(pushNotification: PushNotificationData) {
public final func handlePushNotification(pushNotification: PushNotificationData) {
// TODO: Not yet implemented
}

Expand All @@ -124,7 +124,7 @@ public class DeskPro: Messenger {
///
/// - Returns: A [PresentBuilder](x-source-tag://PresentBuilder) instance to start building presentation paths.
///
public func present() -> PresentBuilder {
public final func present() -> PresentBuilder {
let url = messengerConfig.appUrl//.appending(messengerConfig.appId)
//return PresentBuilder(url: url, containingViewController: containingViewController)
return PresentBuilder(url: url, appId: messengerConfig.appId, coordinator: coordinator)
Expand All @@ -134,7 +134,7 @@ public class DeskPro: Messenger {
///
/// This method closes the currently displayed chat view, terminating the user's interaction with the DeskPro content.
///
public func close() {
public final func close() {
// TODO: Not yet implemented
}

Expand All @@ -144,7 +144,7 @@ public class DeskPro: Messenger {
///
/// - Returns: The number of unread conversations in the inbox.
///
public func getUnreadConversationCount() -> Int {
public final func getUnreadConversationCount() -> Int {
// TODO: Not yet implemented
return 0
}
Expand All @@ -153,7 +153,7 @@ public class DeskPro: Messenger {
///
/// This method turns on logging for the DeskPro SDK, allowing detailed information to be logged for debugging and troubleshooting purposes.
///
public func enableLogging() {
public final func enableLogging() {
// TODO: Not yet implemented
}
}
19 changes: 18 additions & 1 deletion Sources/Logs/EventRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,44 @@

import Foundation

/// Notifies the outside subscriber if an event occured. Prints all of the events if the autologging is enabled
public final class EventRouter {

/// Invoked when an event occurs
public var handleEventCallback: ((DeskproEvent) -> Void)? = nil

private var enableAutologging: Bool

/// Initializes the EventRouter class
///
///- Parameter enableAutologging: If true, the EventRouter class will print all of the events that occur (during debug mode).
///
init(enableAutologging: Bool = false) {
self.enableAutologging = enableAutologging
}

/// Called when an event occurs (from [PresentCoordinator](x-source-tag://PresentCoordinator)). Logs the event if needed and invokes the callback for outside handling
final func handleOrLogEvent(event: DeskproEvent) {
if enableAutologging {
dprint("[DeskproLogger]: \(event.debugDescription)")
}

handleEventCallback?(event)
}
}

/// An event that occurs during a chat session. The observation of these events occurs through the EventRouter class
public enum DeskproEvent {
/// The event prefix which is appended in front of each event for easier identification
static let eventPrefix = "appEvent_"

/// The data inside each event is stored as raw JSON in string format, which can be further transformed if needed
case newChat(data: String)
case chatEnded(data: String)
case newChatMessage(data: String)
case chatUploadRequest(data: String)
case custom(data: String)

/// The identifier for each event
var identifier: String {
switch self {
case .newChat: return "chat.new"
Expand All @@ -44,6 +55,7 @@ public enum DeskproEvent {
}
}

/// If the raw JSON contains this string, the according event is instantiated
var containedIdentifier: String {
switch self {
case .newChat: return "\"id\":\"chat.new\""
Expand All @@ -54,6 +66,7 @@ public enum DeskproEvent {
}
}

/// Raw event JSON in string format
public var data: String {
switch self {
case let .newChat(data): return data
Expand All @@ -64,11 +77,13 @@ public enum DeskproEvent {
}
}

/// Returns the date of the event and the raw json for printing purposes
public var debugDescription: String {
"\(Date.nowString) [AppEvent] \(data)"
}
}

/// Returns the current date as String for printing purposes
private extension Date {
static var nowString: String {
let dateFormatter = DateFormatter()
Expand All @@ -77,13 +92,15 @@ private extension Date {
}
}

/// Prints items only if debug mode is active
func dprint(_ object: Any...) {
#if DEBUG
for item in object {
print(item)
}
#endif
}

func dprint(_ object: Any) {
#if DEBUG
print(object)
Expand Down
2 changes: 1 addition & 1 deletion Sources/MessengerConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
/// Configuration data class for initializing the Messenger feature in the application.
///
/// This data class holds configuration parameters required for setting up and initializing the Messenger feature within the application. The configuration includes the base URL of the Messenger service, the application ID, and the application key.
public class MessengerConfig {
public final class MessengerConfig {

public var appUrl: String
public var appId: String
Expand Down
10 changes: 5 additions & 5 deletions Sources/PresentBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import UIKit
/// The final constructed path can then be presented in a WebView using the [show](x-source-tag://show) method.
///
///- Tag: PresentBuilder
public class PresentBuilder {
public final class PresentBuilder {

/// The coordinator for easier WebView presentation handling.
private weak var coordinator: PresentCoordinator?
Expand Down Expand Up @@ -45,7 +45,7 @@ public class PresentBuilder {
/// - Parameter chatId: The identifier of the chat for which the history is requested.
///
/// - Returns: The [PresentBuilder](x-source-tag://PresentBuilder) instance for method chaining.
public func chatHistory(_ chatId: Int) -> PresentBuilder {
public final func chatHistory(_ chatId: Int) -> PresentBuilder {
path.append("/chat_history/\(chatId)")
return self
}
Expand All @@ -55,15 +55,15 @@ public class PresentBuilder {
/// - Parameter articleId: The identifier of the article to be presented.
///
/// - Returns: The [PresentBuilder](x-source-tag://PresentBuilder) instance for method chaining.
public func article(_ articleId: Int) -> PresentBuilder {
public final func article(_ articleId: Int) -> PresentBuilder {
path.append("/article/\(articleId)")
return self
}

/// Appends the comments path to the constructed URL path.
///
/// - Returns: The [PresentBuilder](x-source-tag://PresentBuilder) instance for method chaining.
public func comments() -> PresentBuilder {
public final func comments() -> PresentBuilder {
path.append("/comments")
return self
}
Expand All @@ -73,7 +73,7 @@ public class PresentBuilder {
/// If the application context is null, a message is logged, and the method returns early. Otherwise, the WebView is started with the constructed path and application ID.
///
///- Tag: show
public func show() {
public final func show() {
coordinator?.present(path: path, appId: appId)
}
}
12 changes: 8 additions & 4 deletions Sources/PresentCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
import UIKit

/// The coordinator for easier WebView presentation handling. It will make sure that only one instance of WebView is presented at the time.
public class PresentCoordinator: WebViewDelegate {
/// - Tag: PresentCoordinator
public final class PresentCoordinator: WebViewDelegate {

/// The ViewController from which the WebView will be presented.
weak var containingViewController: UIViewController?

var path: String = ""
var webViewInstance: CustomWebView?
var eventRouter: EventRouter

///- Parameter containingViewController: The ViewController from which the WebView will be presented.
///- Parameter eventRouter: The unique event router for this messenger instance. Enables event handling
public init(
containingViewController: UIViewController,
eventRouter: EventRouter
Expand All @@ -40,11 +42,13 @@ public class PresentCoordinator: WebViewDelegate {
}
}

func webViewDismissed() {
/// Delegate method: invoked when the web view is dismissed in order to make the instance nil for presentation purposes
final func webViewDismissed() {
webViewInstance = nil
}

func eventOccured(_ event: DeskproEvent) {
/// Delegate method: invoked when an event occurs
final func eventOccured(_ event: DeskproEvent) {
eventRouter.handleOrLogEvent(event: event)
}
}
Loading
Loading