Skip to content

Commit

Permalink
Fix popup logic (#265)
Browse files Browse the repository at this point in the history
* Fix popup logic

* Mark the method private
  • Loading branch information
shp7724 authored Sep 11, 2023
1 parent 37c6f7e commit 6b8e00a
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions SNUTT-2022/SNUTT/Services/PopupService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,15 @@ struct PopupService: PopupServiceProtocol {
func getRecentPopupList() async throws {
let remotePopupDtos = try await popupRepository.getRecentPopupList()
let localPopupDtos = userDefaultsRepository.get([PopupDto].self, key: .popupList, defaultValue: [])
let concatPopupDtos = (localPopupDtos + remotePopupDtos).map { ($0.key, Popup(from: $0)) }
let popupDictByKey = Dictionary(concatPopupDtos, uniquingKeysWith: { key, _ in key })

appState.popup.currentList = popupDictByKey.values.map {
var popup = $0
let mergedPopupDtos = mergePopups(local: localPopupDtos, into: remotePopupDtos)
appState.popup.currentList = mergedPopupDtos.map {
var popup = Popup(from: $0)
if !popup.dontShowForWhile {
popup.dismissedAt = nil
}
return popup
}
userDefaultsRepository.set([PopupDto].self, key: .popupList, value: mergedPopupDtos)
}

func dismissPopup(popup: Popup, dontShowForWhile: Bool) {
Expand All @@ -53,6 +52,21 @@ struct PopupService: PopupServiceProtocol {
}
}

extension PopupService {
private func mergePopups(local: [PopupDto], into remote: [PopupDto]) -> [PopupDto] {
let localPopupByKey = Dictionary(grouping: local, by: { $0.key })
return remote.map { popupDto in
guard let localPopup = localPopupByKey[popupDto.key]?.first else {
return popupDto
}
if popupDto.hidden_days != localPopup.hidden_days {
return popupDto
}
return localPopup
}
}
}

class FakePopupService: PopupServiceProtocol {
func getRecentPopupList() async throws {}
func dismissPopup(popup _: Popup, dontShowForWhile _: Bool) {}
Expand Down

0 comments on commit 6b8e00a

Please sign in to comment.