-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHomeRestaurantViewController.swift
More file actions
572 lines (461 loc) ยท 21.5 KB
/
Copy pathHomeRestaurantViewController.swift
File metadata and controls
572 lines (461 loc) ยท 21.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
//
// HomeRestaurantViewController.swift
// EatSSU-iOS
//
// Created by ์ต์ง์ฐ on 2023/08/08.
import UIKit
import Moya
import SnapKit
import Combine
import EATSSUDesign
// ๋ฉ๋ด ๋ฆฌ๋ทฐ ์ ๋ณด๋ฅผ ์ ๋ฌํ๋ ๋ธ๋ฆฌ๊ฒ์ดํธ ํ๋กํ ์ฝ
protocol ReviewMenuTypeInfoDelegate: AnyObject {
func didDelegateReviewMenuTypeInfo(for menuTypeData: ReviewMenuTypeInfo)
}
// ์๋น ์ ๋ณด ๋ฒํผ ํญ ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌํ๋ ๋ธ๋ฆฌ๊ฒ์ดํธ ํ๋กํ ์ฝ
protocol RestaurantInfoDelegate: AnyObject {
func didTappedRestaurantInfo(restaurantName: String)
}
final class HomeRestaurantViewController: BaseViewController {
private enum RestaurantIdentifier: String, CaseIterable {
case haksik = "HAKSIK"
case dodam = "DODAM"
case dormitory = "DORMITORY"
case faculty = "FACULTY"
case snackCorner = "SNACK_CORNER"
}
private enum MealTime: String {
case morning = "MORNING"
case lunch = "LUNCH"
case dinner = "DINNER"
}
// MARK: - Properties
// Combine ์์ฒญ์ ๊ด๋ฆฌํ๊ธฐ ์ํ Set
private var cancellables = Set<AnyCancellable>()
// ๋ฉ๋ด ํ์ดํ ์
์ ๊ฐ์ (์น์
๋ง๋ค 1๊ฐ ๊ณ ์ )
private let restaurantTableViewMenuTitleCellCount = 1
// ํ
์ด๋ธ๋ทฐ ์น์
ํค๋์ ๋์ด
private let headerHeight: CGFloat = 48
// ์๋น ์ ๋ณด ํญ ์ ๋ธ๋ฆฌ๊ฒ์ดํธ
weak var infoDelegate: RestaurantInfoDelegate?
// ๋ฆฌ๋ทฐ ์์ฑ ๋ทฐ๋ก ๋ฐ์ดํฐ ์ ๋ฌํ ๋ธ๋ฆฌ๊ฒ์ดํธ
var delegate: ReviewMenuTypeInfoDelegate?
// ๊ณ ์ ๋ฉ๋ด ๋๋ฏธ ๋ฐ์ดํฐ (์ฃผ๋ง/๋ฐฉํ ๋๋น์ฉ)
private let fixedDummy = FixedMenuInfoData.Dummy()
// ์น์
ํค๋์ ๋ค์ด๊ฐ ์๋น๋ช
๋ฌธ์์ด ๋ฐฐ์ด
private let sectionHeaderRestaurant = [TextLiteral.Restaurant.studentRestaurant,
TextLiteral.Restaurant.dodamRestaurant,
TextLiteral.Restaurant.dormitoryRestaurant,
TextLiteral.Restaurant.facultyRestaurant,
TextLiteral.Restaurant.snackCorner]
// ๋ฒํผ์ ํ์๋๋ ํ์ดํ์ ๋ฐฑ์๋ ์๋น ์ด๋ฆ์ผ๋ก ๋งคํ
let restaurantButtonTitleToName = [TextLiteral.Restaurant.studentRestaurant: RestaurantIdentifier.haksik.rawValue,
TextLiteral.Restaurant.dodamRestaurant: RestaurantIdentifier.dodam.rawValue,
TextLiteral.Restaurant.dormitoryRestaurant: RestaurantIdentifier.dormitory.rawValue,
TextLiteral.Restaurant.facultyRestaurant: RestaurantIdentifier.faculty.rawValue,
TextLiteral.Restaurant.snackCorner: RestaurantIdentifier.snackCorner.rawValue]
// ๋ณ๊ฒฝ ๋ฉ๋ด๋ฅผ ๊ฐ์ ธ์ฌ ์๋น ์๋ณ์ ๋ชฉ๋ก(์ค๋ต ์ ์ธ)
private var changeRestaurantIDs: [String] {
RestaurantIdentifier.allCases
.map { $0.rawValue }
.filter { $0 != RestaurantIdentifier.snackCorner.rawValue }
}
// info ๋ฒํผ UIAction ๊ณ ์ ์๋ณ์
private static let infoActionID = UIAction.Identifier("com.eatssu.header.infoTap")
// ํ์ฌ ์์ผ์ด ์ฃผ๋ง์ธ์ง ์ฌ๋ถ ํ๋จ
var isWeekend = false
// ์
์ ํ ๊ฐ๋ฅ ์ฌ๋ถ (๊ฐ๊ฒฉ ์ ๋ฌด์ ๋ฐ๋ผ ๋ค๋ฆ)
var isSelectable = false
// ๋ณ๊ฒฝ ๋ฉ๋ด ๋ฐ์ดํฐ (์๋น๋ช
: ๋ฉ๋ด ๋ฐฐ์ด)
var changeMenuTableViewData: [String: [ChangeMenuTableResponse]] = [:]
// ๊ณ ์ ๋ฉ๋ด ๋ฐ์ดํฐ (๊ฐ์์ฝ๋)
var fixMenuTableViewData: [String: [Menus]] = [:]
// MARK: - UI Components
// ๋ทฐ ์ ๋ฐ์ ๊ตฌ์ฑํ๋ restaurantView
let restaurantView = HomeRestaurantView()
// MARK: - Life Cycles
override func viewDidLoad() {
super.viewDidLoad()
setDelegate() // ํ
์ด๋ธ๋ทฐ ๋ธ๋ฆฌ๊ฒ์ดํธ ์ฐ๊ฒฐ
setTableView() // ํ
์ด๋ธ๋ทฐ ์
๋ฑ๋ก
}
// MARK: - Functions
// ์ ์ฒด UI ๊ตฌ์ฑ
override func configureUI() {
view.addSubviews(restaurantView)
}
// SnapKit์ ์ด์ฉํ ๋ ์ด์์ ์ค์
override func setLayout() {
restaurantView.snp.makeConstraints {
$0.edges.equalToSuperview()
}
}
// ํ
์ด๋ธ๋ทฐ delegate/dataSource ์ฐ๊ฒฐ
func setDelegate() {
restaurantView.restaurantTableView.dataSource = self
restaurantView.restaurantTableView.delegate = self
}
// ํ
์ด๋ธ๋ทฐ ์
๋ฐ ํค๋ ๋ฑ๋ก
func setTableView() {
restaurantView.restaurantTableView.register(RestaurantTableViewHeader.self,
forHeaderFooterViewReuseIdentifier: RestaurantTableViewHeader.identifier)
restaurantView.restaurantTableView.register(RestaurantMenuGroupCell.self,
forCellReuseIdentifier: RestaurantMenuGroupCell.identifier)
restaurantView.restaurantTableView.rowHeight = UITableView.automaticDimension
restaurantView.restaurantTableView.estimatedRowHeight = 100
}
// ์๋น identifier๋ก ์น์
์ธ๋ฑ์ค ๋ฐํ
func getSectionIndex(for restaurant: String) -> Int? {
for (idx, title) in sectionHeaderRestaurant.enumerated() {
if restaurantButtonTitleToName[title] == restaurant {
return idx
}
}
return nil
}
// ์น์
์ธ๋ฑ์ค๋ก ์๋น identifier ๋ฐํ (ํ์ ํ์ดํ โ identifier ๋งคํ ๊ธฐ๋ฐ)
func getSectionKey(for section: Int) -> String {
let title = sectionHeaderRestaurant[section]
return restaurantButtonTitleToName[title] ?? ""
}
// ๋ ์ง์ ์๊ฐ์ ๋ฐ๋ผ ๋ฉ๋ด ๋ฐ์ดํฐ fetch
func fetchData(date: Date, time: String) {
// ์๋ก์ด ๋ฐ์ดํฐ ์์ฒญ ์ , ์ด์ ์์ฒญ๋ค์ ๋ชจ๋ ์ทจ์
cancellables.forEach { $0.cancel() }
cancellables.removeAll()
let formatDate = changeDateFormat(date: date)
// ๋ณ๊ฒฝ ๋ฉ๋ด ์ผ๊ด ์์ฒญ(์ค๋ต ์ ์ธ, UI ์์๋๋ก)
let task = _Concurrency.Task {
// ๋ณ๊ฒฝ ๋ฉ๋ด๋ค์ ๋ณ๋ ฌ๋ก ์์ฒญ
await withTaskGroup(of: Void.self) { group in
for id in changeRestaurantIDs {
group.addTask {
await self.fetchChangeMenuData(date: formatDate, restaurant: id, time: time)
}
}
}
}
// Task๋ฅผ AnyCancellable๋ก ๋ณํํด์ ์ ์ฅ (์ทจ์ ๊ฐ๋ฅํ๋๋ก)
cancellables.insert(AnyCancellable { task.cancel() })
let weekday = Weekday.from(date: date)
isWeekend = weekday.isWeekend
guard time == MealTime.lunch.rawValue else {
hideSnackCorner()
return
}
let snackTask = _Concurrency.Task { [weak self] in
guard let self else { return }
let isHoliday = await self.isHoliday(date: date)
if !FirebaseRemoteConfig.shared.isVacationPeriod,
!weekday.isWeekend,
!isHoliday {
self.isSelectable = true
await self.fetchFixedMenuData(restaurant: RestaurantIdentifier.snackCorner.rawValue)
} else {
self.hideSnackCorner()
}
}
cancellables.insert(AnyCancellable { snackTask.cancel() })
}
// MARK: - Private Functions
/// ํน์ ๋ ์ง๊ฐ ๊ณตํด์ผ์ธ์ง ํ๋จ
private func isHoliday(date: Date) async -> Bool {
let targetDate = changeDateFormat(date: date)
let holidayDates = await fetchHolidays(date: date)
return holidayDates.contains(targetDate)
}
// ๋ ์ง๋ฅผ yyyyMMdd ๋ฌธ์์ด๋ก ๋ณํ
private func changeDateFormat(date: Date) -> String {
return Self.yyyyMMddDateFormatter.string(from: date)
}
///"yyyyMMdd ํฌ๋งทํ
ํ๊ธฐ ์ํ DateFormatter ์ธ์คํด์ค
private static let yyyyMMddDateFormatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyyMMdd"
return formatter
}()
/// ์ค๋ต์ฝ๋ ๋ฉ๋ด๋ฅผ ๋น์ฐ๊ณ UI๋ฅผ ๊ฐฑ์
private func hideSnackCorner() {
isSelectable = false
fixMenuTableViewData[RestaurantIdentifier.snackCorner.rawValue] = []
if let sectionIndex = getSectionIndex(for: RestaurantIdentifier.snackCorner.rawValue) {
restaurantView.restaurantTableView.reloadSections(
IndexSet(integer: sectionIndex),
with: .none
)
}
}
}
// MARK: - UITableViewDataSource
extension HomeRestaurantViewController: UITableViewDataSource {
// ์น์
๊ฐ์ ์ค์ (๊ธฐ์์ฌ, ๋๋ด, ํ์, ๊ฐ์)
func numberOfSections(in _: UITableView) -> Int {
sectionHeaderRestaurant.count
}
// ๊ฐ ์น์
๋ง๋ค ๋ณด์ฌ์ค ์
๊ฐ์ ๊ณ์ฐ
func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
// ์
๊ตฌ์ฑ
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(
withIdentifier: RestaurantMenuGroupCell.identifier,
for: indexPath
) as! RestaurantMenuGroupCell
let sectionKey = getSectionKey(for: indexPath.section)
let isSnackCorner = (sectionKey == RestaurantIdentifier.snackCorner.rawValue)
let menuList: [MenuTypeInfo] = isSnackCorner
? (fixMenuTableViewData[sectionKey]?.map { .fix($0) } ?? [])
: (changeMenuTableViewData[sectionKey]?.map { .change($0) } ?? [])
cell.configure(with: menuList, at: indexPath) { [weak self] indexPath, menuIndex in
self?.handleMenuTap(section: indexPath.section, menuIndex: menuIndex)
}
return cell
}
private func handleMenuTap(section: Int, menuIndex: Int) {
if RealmService.shared.isAccessTokenPresent() == false {
presentLoginAlert()
return
}
let restaurant = getSectionKey(for: section)
// firebase - click_menu ์ด๋ฒคํธ ํธ์ถ
if section < sectionHeaderRestaurant.count {
let restaurantName = sectionHeaderRestaurant[section]
HomeAnalyticsManager.shared.logClickMenu(restaurantName: restaurantName)
}
let isSnackCorner = (restaurant == RestaurantIdentifier.snackCorner.rawValue)
var reviewMenuTypeInfo = ReviewMenuTypeInfo(menuType: "", menuID: 0)
if section < sectionHeaderRestaurant.count {
reviewMenuTypeInfo.restaurantName = sectionHeaderRestaurant[section]
}
if !isSnackCorner {
guard let menus = changeMenuTableViewData[restaurant],
menuIndex < menus.count else {
print("๋ฉ๋ด ์ธ๋ฑ์ค ๋ฒ์ ์ด๊ณผ: \(menuIndex)")
return
}
let menu = menus[menuIndex]
reviewMenuTypeInfo.menuType = "VARIABLE"
reviewMenuTypeInfo.menuID = menu.mealId ?? 0
reviewMenuTypeInfo.changeMenuIDList = menu.briefMenus.compactMap(\.menuId)
} else {
if !isSelectable { return }
guard let menus = fixMenuTableViewData[restaurant],
menuIndex < menus.count else {
print("๊ฐ์ ๋ฉ๋ด ์ธ๋ฑ์ค ๋ฒ์ ์ด๊ณผ: \(menuIndex)")
return
}
reviewMenuTypeInfo.menuType = "FIXED"
reviewMenuTypeInfo.menuID = menus[menuIndex].menuId
reviewMenuTypeInfo.changeMenuIDList = nil
}
let reviewViewController = ReviewViewController()
// ์์ ๋ถ๋ชจ์์ CustomTabBarContainerController ์ฐพ๊ธฐ
var parentVC = self.parent
while parentVC != nil {
if let customTabBar = parentVC as? CustomTabBarContainerController {
customTabBar.setTabBarHidden(true, animated: false)
break
}
parentVC = parentVC?.parent
}
// delegate ์ฐ๊ฒฐ
delegate = reviewViewController
delegate?.didDelegateReviewMenuTypeInfo(for: reviewMenuTypeInfo)
// push๋ก ๋์ฐ๊ธฐ
navigationController?.pushViewController(reviewViewController, animated: true)
}
private func presentLoginAlert() {
let alert = UIAlertController(title: TextLiteral.Common.needLogin,
message: TextLiteral.Common.askLogin,
preferredStyle: .alert)
let confirm = UIAlertAction(title: TextLiteral.Common.confirm, style: .default) { _ in
let loginVC = LoginViewController()
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
let sceneDelegate = windowScene.delegate as? SceneDelegate,
let window = sceneDelegate.window {
window.replaceRootViewController(loginVC)
}
}
alert.addAction(confirm)
alert.addAction(UIAlertAction(title: TextLiteral.Common.cancel, style: .cancel))
present(alert, animated: true)
}
// ์น์
ํค๋ ๋ทฐ ์ค์
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
guard let header = tableView.dequeueReusableHeaderFooterView(
withIdentifier: RestaurantTableViewHeader.identifier
) as? RestaurantTableViewHeader else {
return nil
}
let restaurantName = sectionHeaderRestaurant[section]
let koreanName = koreanRestaurantName(from: restaurantName)
header.titleLabel.text = restaurantName
if let info = RestaurantInfoData.restaurantInfoData.first(where: { $0.name == koreanName }) {
var container = AttributeContainer()
container.font = EATSSUDesignFontFamily.Pretendard.medium.font(size: 10)
header.infoButton.configuration?.attributedTitle = AttributedString(info.location, attributes: container)
}
// ์ฌ์ฌ์ฉ ํค๋์ ์ด์ ์ก์
์ ๊ฑฐ
header.infoButton.removeAction(identifiedBy: Self.infoActionID, for: .touchUpInside)
// ๋ค์ ์ก์
๋ฑ๋ก
header.infoButton.addAction(
UIAction(title: "", image: nil, identifier: Self.infoActionID, handler: { [weak self] _ in
guard let self else { return }
// firebase - click_restaurant_info ์ด๋ฒคํธ ํธ์ถ
HomeAnalyticsManager.shared.logClickRestaurantInfo(restaurantName: restaurantName)
let vc = RestaurantInfoViewController()
vc.modalPresentationStyle = .pageSheet
vc.loadViewIfNeeded()
self.infoDelegate = vc
self.infoDelegate?.didTappedRestaurantInfo(restaurantName: restaurantName)
if let sheet = vc.sheetPresentationController {
if #available(iOS 16.0, *) {
let height = vc.calculatePreferredHeight()
sheet.detents = [.custom { _ in height }]
} else {
sheet.detents = [.large()]
}
sheet.prefersGrabberVisible = true
}
self.present(vc, animated: true)
}),
for: .touchUpInside
)
return header
}
// TODO: - ์ถํ ์ญ์ ํ์: RestaurantInfoData ๊ด๋ จ Firebase ๋ฐํ ๊ฐ์์ id ์ถ๊ฐ๋ก ๋ฐ์์ ์ด๋ฆ ๋ง๊ณ id๋ก ๋งค์นญํ๋ ๋ฐฉ์์ผ๋ก ๋ณ๊ฒฝ ํ์
private func koreanRestaurantName(from name: String) -> String {
switch name {
case TextLiteral.Restaurant.dodamRestaurant:
return "๋๋ด ์๋น"
case TextLiteral.Restaurant.studentRestaurant:
return "ํ์ ์๋น"
case TextLiteral.Restaurant.snackCorner:
return "์ค๋ต ์ฝ๋"
case TextLiteral.Restaurant.dormitoryRestaurant:
return "๊ธฐ์์ฌ ์๋น"
case TextLiteral.Restaurant.facultyRestaurant:
return "FACULTY (๊ต์ง์ ์ ์ฉ)"
default:
return name
}
}
}
// MARK: - UITableViewDelegate
extension HomeRestaurantViewController: UITableViewDelegate {
// ํค๋ ๋์ด ์ค์
func tableView(_: UITableView, heightForHeaderInSection _: Int) -> CGFloat {
headerHeight
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
// didSelectRowAt์ ์ฌ์ฉํ์ง ์์ผ๋ฏ๋ก, ๊ธฐ์กด ๋ก์ง์ handleMenuTap์ผ๋ก ์ด๋.
}
}
// MARK: - Network
extension HomeRestaurantViewController {
// ๋ณ๊ฒฝ ๋ฉ๋ด ์์ฒญ API ํธ์ถ
func fetchChangeMenuData(date: String, restaurant: String, time: String) async {
// UI ์
๋ฐ์ดํธ์ ์ฌ์ฉํ ๋ฉ๋ด ๋ชฉ๋ก๊ณผ ์ ๋๋ฉ์ด์
ํ์
์ ์ ์ฅํ ๋ณ์
let menusToUpdate: [ChangeMenuTableResponse]
let animation: UITableView.RowAnimation
do {
// ๋น๋๊ธฐ ๋คํธ์ํฌ ์์ฒญ์ ํตํด ๋ฉ๋ด ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
let menus: [ChangeMenuTableResponse] = try await withCheckedThrowingContinuation { continuation in
NetworkService.shared.request(
HomeRouter.getChangeMenuTableResponse(date: date, restaurant: restaurant, time: time),
responseType: [ChangeMenuTableResponse].self
) { result in
continuation.resume(with: result)
}
}
menusToUpdate = menus.filter { !($0.briefMenus.first?.name.isEmpty ?? true) }
animation = .fade
} catch {
print("\(restaurant) ๋ณ๊ฒฝ ๋ฉ๋ด ์กฐํ ์คํจ: \(error.localizedDescription)")
menusToUpdate = []
animation = .none
}
// ๋ฉ์ธ ์ค๋ ๋์์ UI ์
๋ฐ์ดํธ
await MainActor.run {
self.changeMenuTableViewData[restaurant] = menusToUpdate
if let sectionIndex = self.getSectionIndex(for: restaurant) {
self.restaurantView.restaurantTableView.reloadSections(IndexSet(integer: sectionIndex), with: animation)
}
}
}
// ๊ณ ์ ๋ฉ๋ด ์์ฒญ API ํธ์ถ
func fetchFixedMenuData(restaurant: String) async {
var menuData: [Menus] = []
var animation: UITableView.RowAnimation = .none
do {
let response: FixedMenuTableResponse = try await withCheckedThrowingContinuation { continuation in
NetworkService.shared.request(
HomeRouter.getFixedMenuTableResponse(restaurant: restaurant),
responseType: FixedMenuTableResponse.self
) { result in
continuation.resume(with: result)
}
}
menuData = response.categoryMenuListCollection.flatMap { $0.menus }
animation = .fade
} catch {
print("\(restaurant) ๊ณ ์ ๋ฉ๋ด ์กฐํ ์คํจ: \(error.localizedDescription)")
}
// ๋ฉ์ธ ์ค๋ ๋์์ UI ์
๋ฐ์ดํธ
await MainActor.run {
self.fixMenuTableViewData[restaurant] = menuData
if let sectionIndex = self.getSectionIndex(for: restaurant) {
self.restaurantView.restaurantTableView.reloadSections(IndexSet(integer: sectionIndex), with: animation)
}
}
}
// ๊ณตํด์ผ ์กฐํ API ํธ์ถ
func fetchHolidays(date: Date) async -> [String] {
let year = Calendar.current.component(.year, from: date)
let month = Calendar.current.component(.month, from: date)
let cacheKey = "\(HolidayAPIConstant.cacheKeyPrefix)\(year)_\(String(format: "%02d", month))"
if let cached = UserDefaults.standard.array(forKey: cacheKey) as? [String] {
return cached
}
let serviceKey = Bundle.main.object(
forInfoDictionaryKey: HolidayAPIConstant.infoPlistKey
) as? String ?? ""
let trimmedServiceKey = serviceKey.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmedServiceKey.isEmpty else {
return []
}
let provider = MoyaProvider<PublicHolidayRouter>()
do {
let response = try await withCheckedThrowingContinuation { continuation in
provider.request(
.getPublicHolidays(
serviceKey: trimmedServiceKey,
year: year,
month: month
)
) { result in
continuation.resume(with: result)
}
}
let decoded = try response.map(PublicHolidayResponseDTO.self)
guard decoded.response.header.resultCode == HolidayAPIConstant.successResultCode else {
return []
}
let items = decoded.response.body.items?.item.values ?? []
let holidayDates = Array(
Set(
items
.filter { $0.isHoliday == HolidayAPIConstant.holidayFlag }
.map { String($0.locdate) }
)
).sorted()
UserDefaults.standard.set(holidayDates, forKey: cacheKey)
return holidayDates
} catch {
print("๊ณตํด์ผ ์กฐํ ์คํจ: \(error.localizedDescription)")
return []
}
}
}