forked from vinhnx/Clendar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor ContentView with view modifier
- Loading branch information
Showing
3 changed files
with
81 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// | ||
// ContentViewModifier.swift | ||
// Clendar | ||
// | ||
// Created by Vinh Nguyen on 02/09/2022. | ||
// Copyright © 2022 Vinh Nguyen. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
import Shift | ||
|
||
struct ContentViewModifier: ViewModifier { | ||
@StateObject var store: SharedStore | ||
@StateObject var eventKitWrapper: Shift | ||
@State var confettiCounter = 0 | ||
|
||
func body(content: Content) -> some View { | ||
content | ||
.whatsNewSheet() | ||
.onAppear { | ||
configure() | ||
} | ||
.onReceive(store.$selectedDate) { date in | ||
selectDate(date) | ||
} | ||
.onReceive(NotificationCenter.default.publisher(for: .didAuthorizeCalendarAccess)) { _ in | ||
selectDate(Date()) | ||
} | ||
.onReceive(NotificationCenter.default.publisher(for: .EKEventStoreChanged)) { _ in | ||
selectDate(store.selectedDate) | ||
} | ||
.onReceive(NotificationCenter.default.publisher(for: .didDeleteEvent)) { _ in | ||
selectDate(store.selectedDate) | ||
} | ||
.onReceive(NotificationCenter.default.publisher(for: .didChangeSavedCalendarsPreferences)) { _ in | ||
selectDate(store.selectedDate) | ||
} | ||
.onReceive(NotificationCenter.default.publisher(for: .didChangeUserInterfacePreferences)) { _ in | ||
store.appBackgroundColor = .backgroundColor() | ||
} | ||
.onReceive(NotificationCenter.default.publisher(for: .inAppPurchaseSuccess)) { (_) in | ||
confettiCounter += 1 | ||
} | ||
.onReceive(NotificationCenter.default.publisher(for: .didChangeCalendarType)) { notification in | ||
handleDidChangeCalendarTypeEvent(notification) | ||
} | ||
} | ||
|
||
private func configure() { | ||
selectDate(Date()) | ||
} | ||
|
||
private func selectDate(_ date: Date) { | ||
genLightHaptic() | ||
fetchEvents(for: date) | ||
} | ||
|
||
private func fetchEvents(for date: Date = Date()) { | ||
Task { | ||
try await eventKitWrapper.fetchEvents(for: date, filterCalendarIDs: UserDefaults.savedCalendarIDs, calendar: CalendarIdentifier.current.calendar) | ||
} | ||
} | ||
|
||
private func handleDidChangeCalendarTypeEvent(_ notification: Notification) { | ||
guard let calendar = notification.object as? Calendar else { return } | ||
store.calendar = calendar | ||
} | ||
} |