@@ -8,7 +8,8 @@ import SwiftUI
8
8
public struct NotificationsListView : View {
9
9
@Environment ( BSkyClient . self) private var client
10
10
11
- @State private var notifications : [ AppBskyLexicon . Notification . Notification ] = [ ]
11
+ @State private var notificationsGroups : [ NotificationsGroup ] = [ ]
12
+ @State private var cursor : String ?
12
13
13
14
public init ( ) { }
14
15
@@ -17,21 +18,81 @@ public struct NotificationsListView: View {
17
18
List {
18
19
HeaderView ( title: " Notifications " , showBack: false )
19
20
. padding ( . bottom)
20
- ForEach ( notifications, id: \. notificationURI) { notification in
21
- Text ( notification. notificationReason. rawValue)
21
+
22
+ ForEach ( notificationsGroups) { group in
23
+ Section {
24
+ switch group. type {
25
+ case . reply:
26
+ SingleNotificationRow (
27
+ notification: group. notifications [ 0 ] ,
28
+ postItem: group. postItem,
29
+ actionText: " replied to your post "
30
+ )
31
+ case . follow:
32
+ GroupedNotificationRow ( group: group) { count in
33
+ count == 1 ? " followed you " : " and \( count - 1 ) others followed you "
34
+ }
35
+ case . like:
36
+ GroupedNotificationRow ( group: group) { count in
37
+ count == 1 ? " liked your post " : " and \( count - 1 ) others liked your post "
38
+ }
39
+ case . repost:
40
+ GroupedNotificationRow ( group: group) { count in
41
+ count == 1 ? " reposted your post " : " and \( count - 1 ) others reposted your post "
42
+ }
43
+ case . mention:
44
+ SingleNotificationRow (
45
+ notification: group. notifications [ 0 ] ,
46
+ postItem: group. postItem,
47
+ actionText: " mentioned you "
48
+ )
49
+ case . quote:
50
+ SingleNotificationRow (
51
+ notification: group. notifications [ 0 ] ,
52
+ postItem: group. postItem,
53
+ actionText: " quoted you "
54
+ )
55
+ case . starterpackjoined:
56
+ EmptyView ( )
57
+ }
58
+ }
59
+ }
60
+
61
+ if cursor != nil {
62
+ ProgressView ( )
63
+ . task {
64
+ await fetchNotifications ( )
65
+ }
22
66
}
23
67
}
24
68
. listStyle ( . plain)
25
69
}
26
70
. task {
71
+ cursor = nil
72
+ await fetchNotifications ( )
73
+ }
74
+ . refreshable {
75
+ cursor = nil
27
76
await fetchNotifications ( )
28
77
}
29
78
}
30
79
31
80
private func fetchNotifications( ) async {
32
81
do {
33
- self . notifications = try await client. protoClient. listNotifications ( priority: false )
34
- . notifications
82
+ if let cursor {
83
+ let response = try await client. protoClient. listNotifications (
84
+ priority: false , cursor: cursor)
85
+ self . notificationsGroups. append (
86
+ contentsOf: await NotificationsGroup . groupNotifications (
87
+ client: client, response. notifications)
88
+ )
89
+ self . cursor = response. cursor
90
+ } else {
91
+ let response = try await client. protoClient. listNotifications ( priority: false )
92
+ self . notificationsGroups = await NotificationsGroup . groupNotifications (
93
+ client: client, response. notifications)
94
+ self . cursor = response. cursor
95
+ }
35
96
} catch {
36
97
print ( error)
37
98
}
0 commit comments