@@ -14,46 +14,7 @@ enum RowDiff {
14
14
case sectionUpdate( IndexSet )
15
15
}
16
16
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
-
47
17
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
-
57
18
init ( internalAPIProvider: @escaping @autoclosure ( ) -> InternalIterableAPI ? = IterableAPI . internalImplementation) {
58
19
ITBInfo ( )
59
20
@@ -73,23 +34,45 @@ class InboxViewControllerViewModel: InboxViewControllerViewModelProtocol {
73
34
NotificationCenter . default. removeObserver ( self )
74
35
}
75
36
37
+ // MARK: - InboxViewControllerViewModelProtocol
38
+
39
+ weak var view : InboxViewControllerViewModelView ?
40
+
76
41
var numSections : Int {
77
42
sectionedMessages. sections. count
78
43
}
79
44
80
- func numRows( in section: Int ) -> Int {
81
- sectionedMessages [ section] . 1 . count
82
- }
83
-
84
45
var unreadCount : Int {
85
46
allMessagesInSections ( ) . filter { $0. read == false } . count
86
47
}
87
48
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
+
88
64
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)
93
76
}
94
77
95
78
func message( atIndexPath indexPath: IndexPath ) -> InboxMessageViewModel {
@@ -106,24 +89,6 @@ class InboxViewControllerViewModel: InboxViewControllerViewModelProtocol {
106
89
inboxSessionId: sessionManager. sessionStartInfo? . id)
107
90
}
108
91
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
-
127
92
func viewWillAppear( ) {
128
93
ITBInfo ( )
129
94
startSession ( )
@@ -139,6 +104,14 @@ class InboxViewControllerViewModel: InboxViewControllerViewModelProtocol {
139
104
updateVisibleRows ( )
140
105
}
141
106
107
+ func beganUpdates( ) {
108
+ sectionedMessages = newSectionedMessages
109
+ }
110
+
111
+ func endedUpdates( ) { }
112
+
113
+ // MARK: - Private/Internal
114
+
142
115
private func updateVisibleRows( ) {
143
116
ITBDebug ( )
144
117
0 commit comments