Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix popup logic #265

Merged
merged 2 commits into from
Sep 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading