Skip to content

Commit e70c72a

Browse files
authored
Merge pull request #485 from Iterable/MOB-3115-reorganization
[MOB-3115] project reorganization
2 parents 2887763 + bea5380 commit e70c72a

26 files changed

+455
-477
lines changed

.codeclimate.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ checks:
2929
argument-count:
3030
enabled: true
3131
config:
32-
threshold: 4
32+
threshold: 5
3333
complex-logic:
3434
enabled: true
3535
config:
@@ -57,7 +57,7 @@ checks:
5757
return-statements:
5858
enabled: true
5959
config:
60-
threshold: 4
60+
threshold: 5
6161
similar-code:
6262
enabled: true
6363
config:

swift-sdk.xcodeproj/project.pbxproj

+61-73
Large diffs are not rendered by default.

swift-sdk/Internal/AuthManager.swift

+1-8
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,7 @@
44

55
import Foundation
66

7-
@objc public protocol IterableInternalAuthManagerProtocol {
8-
func getAuthToken() -> String?
9-
func resetFailedAuthCount()
10-
func requestNewAuthToken(hasFailedPriorAuth: Bool, onSuccess: ((String?) -> Void)?)
11-
func logoutUser()
12-
}
13-
14-
class AuthManager: IterableInternalAuthManagerProtocol {
7+
class AuthManager: IterableAuthManagerProtocol {
158
init(delegate: IterableAuthDelegate?,
169
expirationRefreshPeriod: TimeInterval,
1710
localStorage: LocalStorageProtocol,

swift-sdk/Internal/ClassExtensions.swift

-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ extension UIColor {
115115

116116
return (red, green, blue, alpha)
117117
}
118-
119118
}
120119

121120
extension Data {

swift-sdk/Internal/DependencyContainer.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protocol DependencyContainerProtocol {
2323
config: IterableConfig,
2424
endPoint: String,
2525
authProvider: AuthProvider?,
26-
authManager: IterableInternalAuthManagerProtocol,
26+
authManager: IterableAuthManagerProtocol,
2727
deviceMetadata: DeviceMetadata,
2828
offlineMode: Bool) -> RequestHandlerProtocol
2929
func createHealthMonitorDataProvider(persistenceContextProvider: IterablePersistenceContextProvider) -> HealthMonitorDataProviderProtocol
@@ -49,7 +49,7 @@ extension DependencyContainerProtocol {
4949
retryInterval: config.inAppDisplayInterval)
5050
}
5151

52-
func createAuthManager(config: IterableConfig) -> IterableInternalAuthManagerProtocol {
52+
func createAuthManager(config: IterableConfig) -> IterableAuthManagerProtocol {
5353
AuthManager(delegate: config.authDelegate,
5454
expirationRefreshPeriod: config.expiringAuthTokenRefreshPeriod,
5555
localStorage: localStorage,
@@ -60,7 +60,7 @@ extension DependencyContainerProtocol {
6060
config: IterableConfig,
6161
endPoint: String,
6262
authProvider: AuthProvider?,
63-
authManager: IterableInternalAuthManagerProtocol,
63+
authManager: IterableAuthManagerProtocol,
6464
deviceMetadata: DeviceMetadata,
6565
offlineMode: Bool) -> RequestHandlerProtocol {
6666
if #available(iOS 10.0, *) {

swift-sdk/Internal/InAppCalculations.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,18 @@ struct InAppCalculations {
6363
location: IterableMessageLocation,
6464
safeAreaInsets: UIEdgeInsets) -> ViewPosition {
6565
let startPosition: ViewPosition
66+
6667
switch location {
6768
case .top:
6869
startPosition = ViewPosition(width: position.width,
6970
height: position.height,
7071
center: CGPoint(x: position.center.x,
71-
y: position.center.y - position.height - safeAreaInsets.top))
72+
y: position.center.y - position.height - safeAreaInsets.top))
7273
case .bottom:
7374
startPosition = ViewPosition(width: position.width,
7475
height: position.height,
7576
center: CGPoint(x: position.center.x,
76-
y: position.center.y + position.height + safeAreaInsets.bottom))
77+
y: position.center.y + position.height + safeAreaInsets.bottom))
7778
case .center, .full:
7879
startPosition = position
7980
}

swift-sdk/Internal/InboxViewControllerViewModel.swift

+38-65
Original file line numberDiff line numberDiff line change
@@ -14,46 +14,7 @@ enum RowDiff {
1414
case sectionUpdate(IndexSet)
1515
}
1616

17-
protocol InboxViewControllerViewModelView: AnyObject {
18-
// All these methods should be called on the main thread
19-
func onViewModelChanged(diffs: [RowDiff])
20-
func onImageLoaded(for indexPath: IndexPath)
21-
var currentlyVisibleRowIndexPaths: [IndexPath] { get }
22-
}
23-
24-
protocol InboxViewControllerViewModelProtocol {
25-
var view: InboxViewControllerViewModelView? { get set }
26-
func set(comparator: ((IterableInAppMessage, IterableInAppMessage) -> Bool)?,
27-
filter: ((IterableInAppMessage) -> Bool)?,
28-
sectionMapper: ((IterableInAppMessage) -> Int)?)
29-
var numSections: Int { get }
30-
func numRows(in section: Int) -> Int
31-
var unreadCount: Int { get }
32-
func isEmpty() -> Bool
33-
func message(atIndexPath indexPath: IndexPath) -> InboxMessageViewModel
34-
func remove(atIndexPath indexPath: IndexPath)
35-
func set(read: Bool, forMessage message: InboxMessageViewModel)
36-
func createInboxMessageViewController(for message: InboxMessageViewModel, withInboxMode inboxMode: IterableInboxViewController.InboxMode) -> UIViewController?
37-
func refresh() -> Future<Bool, Error> // Talks to the server and refreshes
38-
// this works hand in hand with listener.onViewModelChanged.
39-
// Internal model can't be changed until the view begins update (tableView.beginUpdates()).
40-
func beganUpdates()
41-
func endedUpdates()
42-
func viewWillAppear()
43-
func viewWillDisappear()
44-
func visibleRowsChanged()
45-
}
46-
4717
class InboxViewControllerViewModel: InboxViewControllerViewModelProtocol {
48-
weak var view: InboxViewControllerViewModelView?
49-
50-
func set(comparator: ((IterableInAppMessage, IterableInAppMessage) -> Bool)?, filter: ((IterableInAppMessage) -> Bool)?, sectionMapper: ((IterableInAppMessage) -> Int)?) {
51-
self.comparator = comparator
52-
self.filter = filter
53-
self.sectionMapper = sectionMapper
54-
sectionedMessages = sortAndFilter(messages: allMessagesInSections())
55-
}
56-
5718
init(internalAPIProvider: @escaping @autoclosure () -> InternalIterableAPI? = IterableAPI.internalImplementation) {
5819
ITBInfo()
5920

@@ -73,23 +34,45 @@ class InboxViewControllerViewModel: InboxViewControllerViewModelProtocol {
7334
NotificationCenter.default.removeObserver(self)
7435
}
7536

37+
// MARK: - InboxViewControllerViewModelProtocol
38+
39+
weak var view: InboxViewControllerViewModelView?
40+
7641
var numSections: Int {
7742
sectionedMessages.sections.count
7843
}
7944

80-
func numRows(in section: Int) -> Int {
81-
sectionedMessages[section].1.count
82-
}
83-
8445
var unreadCount: Int {
8546
allMessagesInSections().filter { $0.read == false }.count
8647
}
8748

49+
func refresh() -> Future<Bool, Error> {
50+
internalInAppManager?.scheduleSync() ?? Promise(error: IterableError.general(description: "Did not find inAppManager"))
51+
}
52+
53+
func createInboxMessageViewController(for message: InboxMessageViewModel, withInboxMode inboxMode: IterableInboxViewController.InboxMode) -> UIViewController? {
54+
internalInAppManager?.createInboxMessageViewController(for: message.iterableMessage, withInboxMode: inboxMode, inboxSessionId: sessionManager.sessionStartInfo?.id)
55+
}
56+
57+
func set(comparator: ((IterableInAppMessage, IterableInAppMessage) -> Bool)?, filter: ((IterableInAppMessage) -> Bool)?, sectionMapper: ((IterableInAppMessage) -> Int)?) {
58+
self.comparator = comparator
59+
self.filter = filter
60+
self.sectionMapper = sectionMapper
61+
sectionedMessages = sortAndFilter(messages: allMessagesInSections())
62+
}
63+
8864
func isEmpty() -> Bool {
89-
return
90-
sectionedMessages.sectionsAndValues.reduce(0) { count, sectionAndValue in
91-
count + sectionAndValue.1.count
92-
} == 0
65+
return sectionedMessages.sectionsAndValues.reduce(0) { count, sectionAndValue in
66+
count + sectionAndValue.1.count
67+
} == 0
68+
}
69+
70+
func numRows(in section: Int) -> Int {
71+
sectionedMessages[section].1.count
72+
}
73+
74+
func set(read: Bool, forMessage message: InboxMessageViewModel) {
75+
internalInAppManager?.set(read: read, forMessage: message.iterableMessage)
9376
}
9477

9578
func message(atIndexPath indexPath: IndexPath) -> InboxMessageViewModel {
@@ -106,24 +89,6 @@ class InboxViewControllerViewModel: InboxViewControllerViewModelProtocol {
10689
inboxSessionId: sessionManager.sessionStartInfo?.id)
10790
}
10891

109-
func set(read: Bool, forMessage message: InboxMessageViewModel) {
110-
internalInAppManager?.set(read: read, forMessage: message.iterableMessage)
111-
}
112-
113-
func refresh() -> Future<Bool, Error> {
114-
internalInAppManager?.scheduleSync() ?? Promise(error: IterableError.general(description: "Did not find inAppManager"))
115-
}
116-
117-
func createInboxMessageViewController(for message: InboxMessageViewModel, withInboxMode inboxMode: IterableInboxViewController.InboxMode) -> UIViewController? {
118-
internalInAppManager?.createInboxMessageViewController(for: message.iterableMessage, withInboxMode: inboxMode, inboxSessionId: sessionManager.sessionStartInfo?.id)
119-
}
120-
121-
func beganUpdates() {
122-
sectionedMessages = newSectionedMessages
123-
}
124-
125-
func endedUpdates() {}
126-
12792
func viewWillAppear() {
12893
ITBInfo()
12994
startSession()
@@ -139,6 +104,14 @@ class InboxViewControllerViewModel: InboxViewControllerViewModelProtocol {
139104
updateVisibleRows()
140105
}
141106

107+
func beganUpdates() {
108+
sectionedMessages = newSectionedMessages
109+
}
110+
111+
func endedUpdates() {}
112+
113+
// MARK: - Private/Internal
114+
142115
private func updateVisibleRows() {
143116
ITBDebug()
144117

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// Copyright © 2021 Iterable. All rights reserved.
3+
//
4+
5+
import UIKit
6+
7+
protocol InboxViewControllerViewModelProtocol {
8+
var view: InboxViewControllerViewModelView? { get set }
9+
var unreadCount: Int { get }
10+
var numSections: Int { get }
11+
12+
// Talks to the server and refreshes
13+
// this works hand in hand with listener.onViewModelChanged.
14+
// Internal model can't be changed until the view begins update (tableView.beginUpdates()).
15+
func refresh() -> Future<Bool, Error>
16+
17+
func createInboxMessageViewController(for message: InboxMessageViewModel, withInboxMode inboxMode: IterableInboxViewController.InboxMode) -> UIViewController?
18+
19+
func set(comparator: ((IterableInAppMessage, IterableInAppMessage) -> Bool)?,
20+
filter: ((IterableInAppMessage) -> Bool)?,
21+
sectionMapper: ((IterableInAppMessage) -> Int)?)
22+
23+
func isEmpty() -> Bool
24+
func numRows(in section: Int) -> Int
25+
func set(read: Bool, forMessage message: InboxMessageViewModel)
26+
func message(atIndexPath indexPath: IndexPath) -> InboxMessageViewModel
27+
func remove(atIndexPath indexPath: IndexPath)
28+
29+
func viewWillAppear()
30+
func viewWillDisappear()
31+
func visibleRowsChanged()
32+
func beganUpdates()
33+
func endedUpdates()
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//
2+
// Copyright © 2021 Iterable. All rights reserved.
3+
//
4+
5+
import Foundation
6+
7+
protocol InboxViewControllerViewModelView: AnyObject {
8+
/// All these methods should be called on the main thread
9+
func onViewModelChanged(diffs: [RowDiff])
10+
func onImageLoaded(for indexPath: IndexPath)
11+
var currentlyVisibleRowIndexPaths: [IndexPath] { get }
12+
}

swift-sdk/Internal/InternalIterableAPI.swift

+1-11
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
6666
deviceMetadata: deviceMetadata)
6767
}()
6868

69-
lazy var authManager: IterableInternalAuthManagerProtocol = {
69+
lazy var authManager: IterableAuthManagerProtocol = {
7070
self.dependencyContainer.createAuthManager(config: self.config)
7171
}()
7272

@@ -588,16 +588,6 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
588588
}
589589
}
590590

591-
private func handleDDL(json: [AnyHashable: Any]) {
592-
if let serverResponse = try? JSONDecoder().decode(ServerResponse.self, from: JSONSerialization.data(withJSONObject: json, options: [])),
593-
serverResponse.isMatch,
594-
let destinationUrlString = serverResponse.destinationUrl {
595-
handleUrl(urlString: destinationUrlString, fromSource: .universalLink)
596-
}
597-
598-
localStorage.ddlChecked = true
599-
}
600-
601591
private func handleUrl(urlString: String, fromSource source: IterableActionSource) {
602592
guard let action = IterableAction.actionOpenUrl(fromUrlString: urlString) else {
603593
ITBError("Could not create action from: \(urlString)")

swift-sdk/Internal/IterableRequestUtil.swift

+22-22
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,21 @@
88
import Foundation
99

1010
struct IterableRequestUtil {
11+
static func createGetRequest(forApiEndPoint apiEndPoint: String,
12+
path: String,
13+
headers: [String: String]? = nil,
14+
args: [String: String]? = nil) -> URLRequest? {
15+
guard let url = getUrlComponents(forApiEndPoint: apiEndPoint, path: path, args: args)?.url else {
16+
return nil
17+
}
18+
19+
var request = URLRequest(url: url)
20+
addHeaders(headers: headers, toRequest: &request)
21+
request.httpMethod = Const.Http.GET
22+
23+
return request
24+
}
25+
1126
static func createPostRequest(forApiEndPoint apiEndPoint: String,
1227
path: String,
1328
headers: [String: String]? = nil,
@@ -49,19 +64,16 @@ struct IterableRequestUtil {
4964
return request
5065
}
5166

52-
static func createGetRequest(forApiEndPoint apiEndPoint: String,
53-
path: String,
54-
headers: [String: String]? = nil,
55-
args: [String: String]? = nil) -> URLRequest? {
56-
guard let url = getUrlComponents(forApiEndPoint: apiEndPoint, path: path, args: args)?.url else {
67+
static func dictToJsonData(_ dict: [AnyHashable: Any]?) -> Data? {
68+
guard let dict = dict else {
5769
return nil
5870
}
5971

60-
var request = URLRequest(url: url)
61-
addHeaders(headers: headers, toRequest: &request)
62-
request.httpMethod = Const.Http.GET
63-
64-
return request
72+
return try? JSONSerialization.data(withJSONObject: dict, options: [])
73+
}
74+
75+
static func pathCombine(paths: [String]) -> String {
76+
paths.reduce("", pathCombine)
6577
}
6678

6779
private static func addHeaders(headers: [String: String]?, toRequest request: inout URLRequest) {
@@ -88,18 +100,6 @@ struct IterableRequestUtil {
88100
return components
89101
}
90102

91-
static func dictToJsonData(_ dict: [AnyHashable: Any]?) -> Data? {
92-
guard let dict = dict else {
93-
return nil
94-
}
95-
96-
return try? JSONSerialization.data(withJSONObject: dict, options: [])
97-
}
98-
99-
static func pathCombine(paths: [String]) -> String {
100-
paths.reduce("", pathCombine)
101-
}
102-
103103
private static func pathCombine(path1: String, path2: String) -> String {
104104
var result = path1
105105

swift-sdk/Internal/LegacyRequestHandler.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Foundation
88
class LegacyRequestHandler: RequestHandlerProtocol {
99
init(apiKey: String,
1010
authProvider: AuthProvider?,
11-
authManager: IterableInternalAuthManagerProtocol?,
11+
authManager: IterableAuthManagerProtocol?,
1212
endPoint: String,
1313
networkSession: NetworkSessionProtocol,
1414
deviceMetadata: DeviceMetadata,
@@ -233,7 +233,7 @@ class LegacyRequestHandler: RequestHandlerProtocol {
233233
}
234234

235235
private let apiClient: ApiClientProtocol
236-
private weak var authManager: IterableInternalAuthManagerProtocol?
236+
private weak var authManager: IterableAuthManagerProtocol?
237237

238238
@discardableResult
239239
private func register(registerTokenInfo: RegisterTokenInfo,

0 commit comments

Comments
 (0)