Skip to content

Commit 7237062

Browse files
author
Mincheol Kim
authored
Merge pull request #56 from Nexters/refactor/badge-vm
refactor: badge 리팩토링
2 parents 2f22a84 + c58793b commit 7237062

File tree

12 files changed

+179
-151
lines changed

12 files changed

+179
-151
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"images" : [
3+
{
4+
"filename" : "special_locked.pdf",
5+
"idiom" : "universal",
6+
"scale" : "1x"
7+
},
8+
{
9+
"idiom" : "universal",
10+
"scale" : "2x"
11+
},
12+
{
13+
"idiom" : "universal",
14+
"scale" : "3x"
15+
}
16+
],
17+
"info" : {
18+
"author" : "xcode",
19+
"version" : 1
20+
}
21+
}
Binary file not shown.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"images" : [
3+
{
4+
"filename" : "special_welcome.pdf",
5+
"idiom" : "universal",
6+
"scale" : "1x"
7+
},
8+
{
9+
"idiom" : "universal",
10+
"scale" : "2x"
11+
},
12+
{
13+
"idiom" : "universal",
14+
"scale" : "3x"
15+
}
16+
],
17+
"info" : {
18+
"author" : "xcode",
19+
"version" : 1
20+
}
21+
}
Binary file not shown.

Sticky/Features/Banner/Components/Banner.swift

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,60 +22,62 @@ struct Banner: View {
2222
var body: some View {
2323
var nextMonthlyBadge = nextBadge(
2424
badgeType: BadgeType.monthly,
25-
badges: self.badgeViewModel.monthly.items
25+
badges: self.badgeViewModel.monthly
2626
)
27-
// 월간 배지 획득까지 남은 seconds; 뱃지 필요한 시간 - (챌린지 시간 + 이번 달 누적 시간)
2827
let remainMonthlyBadge = (Int(nextMonthlyBadge.badgeValue) ?? 0) * 3600
2928
- (challengeState.timeData.toSeconds() + user.thisMonthAccumulateSeconds)
30-
var nextContiousBadge = nextBadge(
31-
badgeType: BadgeType.continuous,
32-
badges: self.badgeViewModel.continuous.items
33-
)
34-
// 연속 배지 획득까지 남은 seconds; 뱃지 필요한 시간 - 챌린지 시간
35-
var remainContinuousBadge = nextContiousBadge.badgeValue == "0.5" ? 12 * 3600 : (Int(nextContiousBadge.badgeValue) ?? 0) * 3600 * 24
36-
remainContinuousBadge -= challengeState.timeData.toSeconds()
37-
3829
if remainMonthlyBadge <= 0 {
39-
let key = nextMonthlyBadge.badgeValue
4030
nextMonthlyBadge.updated = Date()
41-
nextMonthlyBadge.count = 1
42-
badgeViewModel.monthly.items[key] = CountAndUpdated(count: 1, date: Date())
31+
nextMonthlyBadge.count += 1
32+
badgeViewModel.monthly = badgeViewModel.monthly
4333
badgeViewModel.badgeQueue.append(nextMonthlyBadge)
4434
} else {
4535
print("월간 배지 획득 남은 시간: \(remainMonthlyBadge)")
4636
}
37+
38+
var nextContiousBadge = nextBadge(
39+
badgeType: BadgeType.continuous,
40+
badges: self.badgeViewModel.continuous
41+
)
42+
// 연속 배지 획득까지 남은 seconds; 뱃지 필요한 시간 - 챌린지 시간
43+
var remainContinuousBadge = nextContiousBadge.badgeValue == "0.5" ? 12 * 3600 : (Int(nextContiousBadge.badgeValue) ?? 0) * 3600 * 24
44+
remainContinuousBadge -= challengeState.timeData.toSeconds()
4745
if remainContinuousBadge <= 0 {
48-
let key = nextContiousBadge.badgeValue
4946
nextContiousBadge.updated = Date()
50-
nextContiousBadge.count = 1
51-
badgeViewModel.continuous.items[key] = CountAndUpdated(count: 1, date: Date())
47+
nextContiousBadge.count += 1
48+
badgeViewModel.continuous = badgeViewModel.continuous
5249
badgeViewModel.badgeQueue.append(nextContiousBadge)
5350
} else {
5451
print("연속 배지 획득 남은 시간: \(remainContinuousBadge)")
5552
}
5653

5754
return ScrollView(.horizontal, showsIndicators: false) {
5855
HStack {
59-
Button(action: {
60-
self.bannerDetailPresented = true
61-
self.badgeViewModel.select = nextMonthlyBadge
62-
}) {
63-
BannerItem(
64-
image: nextMonthlyBadge.image,
65-
title: nextMonthlyBadge.name,
66-
subtitle: "\(remainTime(remainMonthlyBadge)) 남음"
67-
)
68-
}
56+
Button(
57+
action: {
58+
self.bannerDetailPresented = true
59+
self.badgeViewModel.select = nextMonthlyBadge
60+
},
61+
label: {
62+
BannerItem(
63+
image: nextMonthlyBadge.image,
64+
title: nextMonthlyBadge.name,
65+
subtitle: "\(remainTime(remainMonthlyBadge)) 남음"
66+
)
67+
}
68+
)
69+
6970
Button(action: {
7071
self.bannerDetailPresented = true
7172
self.badgeViewModel.select = nextContiousBadge
72-
}) {
73+
}, label: {
7374
BannerItem(
7475
image: nextContiousBadge.image,
7576
title: nextContiousBadge.name,
7677
subtitle: "\(remainTime(remainContinuousBadge)) 남음"
7778
)
78-
}
79+
})
80+
7981
Button(action: { self.mypagePresented = true }) {
8082
RoundedRectangle(cornerRadius: 20)
8183
.frame(width: 96, height: 60)

Sticky/Features/MyPage/Components/BadgePanel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ struct BadgePanel: View {
3939
}
4040

4141
LazyVGrid(columns: columns) {
42-
ForEach(badges!, id: \.self) { badge in
42+
ForEach(badges!, id: \.id) { badge in
4343
BadgeItem(
4444
badge: badge,
4545
selection: $selection,

Sticky/Features/MyPage/Models/Badge.swift

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Foundation
1717
- count: 획득 횟수
1818
- active: 활성여부
1919
*/
20-
struct Badge: Codable, Hashable {
20+
class Badge: Codable, Hashable, Identifiable {
2121
// MARK: Lifecycle
2222

2323
init(
@@ -34,23 +34,38 @@ struct Badge: Codable, Hashable {
3434
self.count = count
3535
}
3636

37+
// MARK: Public
38+
39+
public var updated: Date?
40+
public var count: Int = 0
41+
3742
// MARK: Internal
3843

44+
var id = UUID()
3945
var badgeType: BadgeType
4046
var badgeValue: String
4147
var _name: String = ""
42-
var updated: Date?
43-
var count: Int = 0
4448

49+
// 업데이트 일자가 이번 달에만 활성화
4550
var active: Bool {
46-
count > 0 || updated != nil
51+
guard let date = updated else { return false }
52+
switch badgeType {
53+
case .monthly:
54+
return isItThisMonth(date: date)
55+
case .continuous,
56+
.level,
57+
.special:
58+
// 한 번이라도 획득했으면 무조건 활성화
59+
return count > 0
60+
}
4761
}
4862

4963
var image: String {
5064
switch badgeType {
65+
case .special:
66+
return active ? "\(badgeType)_\(badgeValue)" : "special_locked"
5167
case .continuous,
52-
.monthly,
53-
.special:
68+
.monthly:
5469
return "\(badgeType)_\(badgeValue)\(active ? "" : "_locked")"
5570
case .level:
5671
return "level\(badgeValue)"
@@ -89,6 +104,22 @@ struct Badge: Codable, Hashable {
89104
return "준비 중입니다."
90105
}
91106
}
107+
108+
static func == (lhs: Badge, rhs: Badge) -> Bool {
109+
return lhs.badgeType == rhs.badgeType
110+
&& lhs.badgeValue == rhs.badgeValue
111+
&& lhs._name == rhs._name
112+
&& lhs.updated == rhs.updated
113+
&& lhs.count == rhs.count
114+
}
115+
116+
func hash(into hasher: inout Hasher) {
117+
hasher.combine(badgeType)
118+
hasher.combine(badgeValue)
119+
hasher.combine(_name)
120+
hasher.combine(updated)
121+
hasher.combine(count)
122+
}
92123
}
93124

94125
/// 다국어 지원 시 locale 변경필요
@@ -121,7 +152,7 @@ extension BadgeType {
121152
func toString(value: String) -> String {
122153
switch self {
123154
case .special:
124-
return "특별한 배지"
155+
return value
125156
case .monthly:
126157
return "한달 동안 집에서 보낸 시간\n\(value)을 달성했습니다!"
127158
case .continuous:
@@ -144,20 +175,3 @@ extension BadgeType {
144175
}
145176
}
146177
}
147-
148-
// MARK: - Special
149-
150-
enum Special: String {
151-
case first
152-
}
153-
154-
func makeBadges(badgeType: BadgeType, dict: [String: CountAndUpdated]) -> [Badge] {
155-
return dict.sorted { Double($0.0)! < Double($1.0)! }.map { key, value in
156-
Badge(
157-
badgeType: badgeType,
158-
badgeValue: key,
159-
updated: value.date,
160-
count: value.count
161-
)
162-
}
163-
}

0 commit comments

Comments
 (0)