Skip to content

Commit 951b17b

Browse files
authored
Merge pull request #67 from wishkit/feature/all-selection
Feature/all selection
2 parents 118af81 + b275295 commit 951b17b

File tree

4 files changed

+147
-71
lines changed

4 files changed

+147
-71
lines changed

Sources/WishKit/Model/WishModel.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ final class WishModel: ObservableObject {
2121
@available(*, deprecated, message: "Use `completedList` instead.")
2222
var implementedWishlist: [WishResponse] = []
2323

24+
@Published
25+
var all: [WishResponse] = []
26+
2427
@Published
2528
var pendingList: [WishResponse] = []
2629

@@ -36,6 +39,7 @@ final class WishModel: ObservableObject {
3639
@Published
3740
var completedList: [WishResponse] = []
3841

42+
/// This list includes pending feedback from all users.
3943
@Published
4044
var fullList: [WishResponse] = []
4145

@@ -57,11 +61,9 @@ final class WishModel: ObservableObject {
5761
switch result {
5862
case .success(let response):
5963
DispatchQueue.main.async {
60-
withAnimation {
61-
self.updateAllLists(with: response.list)
62-
self.shouldShowWatermark = response.shouldShowWatermark
63-
self.fullList = response.list
64-
}
64+
self.updateAllLists(with: response.list)
65+
self.shouldShowWatermark = response.shouldShowWatermark
66+
self.fullList = response.list
6567
}
6668
case .failure(let error):
6769
printError(self, error.reason.description)
@@ -90,6 +92,8 @@ final class WishModel: ObservableObject {
9092
self.inProgressList = sortedList.filter { wish in wish.state == .inProgress }
9193
self.completedList = sortedList.filter { wish in wish.state == .completed || wish.state == .implemented}
9294

95+
self.all = (self.pendingList + self.inReviewList + self.plannedList + self.inProgressList + self.completedList).sorted { $0.votingUsers.count > $1.votingUsers.count }
96+
9397
self.implementedWishlist = sortedList
9498
}
9599
}

Sources/WishKit/SwiftUI/WishlistView.swift

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,27 @@ struct WishlistView: View {
2525
var selectedWish: WishResponse? = nil
2626

2727
@Binding
28-
var listType: WishState
28+
var selectedWishState: LocalWishState
2929

3030
func getList() -> [WishResponse] {
31-
switch listType {
32-
case .approved:
33-
wishModel.approvedWishlist
34-
case .implemented:
35-
wishModel.implementedWishlist
36-
case .pending:
37-
wishModel.pendingList
38-
case .inReview:
39-
wishModel.inReviewList
40-
case .planned:
41-
wishModel.plannedList
42-
case .inProgress:
43-
wishModel.inProgressList
44-
case .completed:
45-
wishModel.completedList
46-
case .rejected:
47-
[]
31+
switch selectedWishState {
32+
case .all:
33+
return wishModel.all
34+
case .library(let state):
35+
switch state {
36+
case .pending:
37+
return wishModel.pendingList
38+
case .inReview, .approved:
39+
return wishModel.inReviewList
40+
case .planned:
41+
return wishModel.plannedList
42+
case .inProgress:
43+
return wishModel.inProgressList
44+
case .completed, .implemented:
45+
return wishModel.completedList
46+
case .rejected:
47+
return []
48+
}
4849
}
4950
}
5051

Sources/WishKit/SwiftUI/iOS+Catalyst/WishlistView+iOS.swift

Lines changed: 67 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,33 @@ extension View {
2222
}
2323
}
2424

25+
enum LocalWishState: Hashable, Identifiable {
26+
case all
27+
case library(WishState)
28+
29+
var id: String { description }
30+
31+
var description: String {
32+
switch self {
33+
case .all:
34+
return "All"
35+
case .library(let wishState):
36+
return wishState.description
37+
}
38+
}
39+
40+
func hash(into hasher: inout Hasher) {
41+
hasher.combine(description)
42+
}
43+
}
44+
2545
struct WishlistViewIOS: View {
2646

2747
@Environment(\.colorScheme)
2848
private var colorScheme
2949

3050
@State
31-
private var selectedWishState: WishState = .inReview
51+
private var selectedWishState: LocalWishState = .all
3252

3353
@ObservedObject
3454
var wishModel: WishModel
@@ -66,41 +86,58 @@ struct WishlistViewIOS: View {
6686
}
6787
}
6888

69-
private var feedbackStateSelection: [WishState] {
70-
return [.pending, .inReview, .planned, .inProgress, .completed]
89+
private var feedbackStateSelection: [LocalWishState] {
90+
return [
91+
.all,
92+
.library(.pending),
93+
.library(.inReview),
94+
.library(.planned),
95+
.library(.inProgress),
96+
.library(.completed),
97+
]
7198
}
7299

73100
private func getList() -> [WishResponse] {
74101
switch selectedWishState {
75-
case .pending:
76-
return wishModel.pendingList
77-
case .inReview, .approved:
78-
return wishModel.inReviewList
79-
case .planned:
80-
return wishModel.plannedList
81-
case .inProgress:
82-
return wishModel.inProgressList
83-
case .completed, .implemented:
84-
return wishModel.completedList
85-
case .rejected:
86-
return []
102+
case .all:
103+
return wishModel.all
104+
case .library(let state):
105+
switch state {
106+
case .pending:
107+
return wishModel.pendingList
108+
case .inReview, .approved:
109+
return wishModel.inReviewList
110+
case .planned:
111+
return wishModel.plannedList
112+
case .inProgress:
113+
return wishModel.inProgressList
114+
case .completed, .implemented:
115+
return wishModel.completedList
116+
case .rejected:
117+
return []
118+
}
87119
}
88120
}
89121

90-
private func getCountFor(state: WishState) -> Int {
122+
private func getCountFor(state: LocalWishState) -> Int {
91123
switch state {
92-
case .pending:
93-
return wishModel.pendingList.count
94-
case .inReview, .approved:
95-
return wishModel.inReviewList.count
96-
case .planned:
97-
return wishModel.plannedList.count
98-
case .inProgress:
99-
return wishModel.inProgressList.count
100-
case .completed, .implemented:
101-
return wishModel.completedList.count
102-
case .rejected:
103-
return 0
124+
case .all:
125+
return wishModel.all.count
126+
case .library(let wishState):
127+
switch wishState {
128+
case .pending:
129+
return wishModel.pendingList.count
130+
case .inReview, .approved:
131+
return wishModel.inReviewList.count
132+
case .planned:
133+
return wishModel.plannedList.count
134+
case .inProgress:
135+
return wishModel.inProgressList.count
136+
case .completed, .implemented:
137+
return wishModel.completedList.count
138+
case .rejected:
139+
return 0
140+
}
104141
}
105142
}
106143

@@ -123,8 +160,9 @@ struct WishlistViewIOS: View {
123160
Spacer(minLength: 15)
124161

125162
Picker("", selection: $selectedWishState) {
126-
ForEach(feedbackStateSelection) { state in
163+
ForEach(feedbackStateSelection, id: \.self) { state in
127164
Text("\(state.description) (\(getCountFor(state: state)))")
165+
.tag(state)
128166
}
129167
}
130168
}

Sources/WishKit/SwiftUI/macOS/WishlistContainer+macOS.swift

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,33 @@
1010
import SwiftUI
1111
import WishKitShared
1212

13+
enum LocalWishState: Hashable, Identifiable {
14+
case all
15+
case library(WishState)
16+
17+
var id: String { description }
18+
19+
var description: String {
20+
switch self {
21+
case .all:
22+
return "All"
23+
case .library(let wishState):
24+
return wishState.description
25+
}
26+
}
27+
28+
func hash(into hasher: inout Hasher) {
29+
hasher.combine(description)
30+
}
31+
}
32+
1333
struct WishlistContainer: View {
1434

1535
@Environment(\.colorScheme)
1636
private var colorScheme
1737

1838
@State
19-
private var listType: WishState = .inReview
39+
private var selectedWishState: LocalWishState = .all
2040

2141
@State
2242
private var isRefreshing = false
@@ -50,40 +70,53 @@ struct WishlistContainer: View {
5070
noSegmentedControlView
5171
}
5272

53-
WishlistView(wishModel: wishModel, listType: $listType)
73+
WishlistView(wishModel: wishModel, selectedWishState: $selectedWishState)
5474
.background(systemBackgroundColor)
5575

5676
}.background(systemBackgroundColor)
5777
}
5878

59-
private var feedbackStateSelection: [WishState] {
60-
return [.pending, .inReview, .planned, .inProgress, .completed]
79+
private var feedbackStateSelection: [LocalWishState] {
80+
return [
81+
.all,
82+
.library(.pending),
83+
.library(.inReview),
84+
.library(.planned),
85+
.library(.inProgress),
86+
.library(.completed),
87+
]
6188
}
6289

63-
private func getCountFor(state: WishState) -> Int {
90+
private func getCountFor(state: LocalWishState) -> Int {
6491
switch state {
65-
case .pending:
66-
return wishModel.pendingList.count
67-
case .inReview, .approved:
68-
return wishModel.inReviewList.count
69-
case .planned:
70-
return wishModel.plannedList.count
71-
case .inProgress:
72-
return wishModel.inProgressList.count
73-
case .completed, .implemented:
74-
return wishModel.completedList.count
75-
case .rejected:
76-
return 0
92+
case .all:
93+
return wishModel.all.count
94+
case .library(let wishState):
95+
switch wishState {
96+
case .pending:
97+
return wishModel.pendingList.count
98+
case .inReview, .approved:
99+
return wishModel.inReviewList.count
100+
case .planned:
101+
return wishModel.plannedList.count
102+
case .inProgress:
103+
return wishModel.inProgressList.count
104+
case .completed, .implemented:
105+
return wishModel.completedList.count
106+
case .rejected:
107+
return 0
108+
}
77109
}
78110
}
79111

80112
var segmentedControlView: some View {
81113
HStack {
82114

83115
if WishKit.config.buttons.segmentedControl.display == .show {
84-
Picker("", selection: $listType) {
85-
ForEach(feedbackStateSelection) { state in
116+
Picker("", selection: $selectedWishState) {
117+
ForEach(feedbackStateSelection, id: \.self) { state in
86118
Text("\(state.description) (\(getCountFor(state: state)))")
119+
.tag(state)
87120
}
88121
}.frame(maxWidth: 150)
89122
}

0 commit comments

Comments
 (0)