Skip to content

Commit

Permalink
Refactor ContentView with view modifier
Browse files Browse the repository at this point in the history
  • Loading branch information
vinhnx committed Sep 2, 2022
1 parent 6496fb9 commit e51e214
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 61 deletions.
4 changes: 4 additions & 0 deletions Clendar.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@
A3DE24882248E1C600EB3B64 /* Dictionary+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3DE24872248E1C600EB3B64 /* Dictionary+Extensions.swift */; };
A3E0B8962780981900758EC4 /* CalendarIdentifer.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3E0B8952780981900758EC4 /* CalendarIdentifer.swift */; };
A3E23633258CD7A7001BCB9F /* Unwrap.swift in Sources */ = {isa = PBXBuildFile; fileRef = A35B9B5B258CD4A30027E1B3 /* Unwrap.swift */; };
A3EA185828C2333200F36A22 /* ContentViewModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3EA185728C2333100F36A22 /* ContentViewModifier.swift */; };
A3EBA83F27561146000188FD /* ConfettiSwiftUI in Frameworks */ = {isa = PBXBuildFile; productRef = A3EBA83E27561146000188FD /* ConfettiSwiftUI */; };
A3EDE72925669F640046C1F5 /* QuickEventView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3EDE72825669F640046C1F5 /* QuickEventView.swift */; };
A3EDE72E25669F8F0046C1F5 /* EventEditorWrapperView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3EDE72D25669F8F0046C1F5 /* EventEditorWrapperView.swift */; };
Expand Down Expand Up @@ -456,6 +457,7 @@
A3DCD7BC224A56C600EAE380 /* UIView+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView+Extensions.swift"; sourceTree = "<group>"; };
A3DE24872248E1C600EB3B64 /* Dictionary+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Dictionary+Extensions.swift"; sourceTree = "<group>"; };
A3E0B8952780981900758EC4 /* CalendarIdentifer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CalendarIdentifer.swift; sourceTree = "<group>"; };
A3EA185728C2333100F36A22 /* ContentViewModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentViewModifier.swift; sourceTree = "<group>"; };
A3EDE72825669F640046C1F5 /* QuickEventView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QuickEventView.swift; sourceTree = "<group>"; };
A3EDE72D25669F8F0046C1F5 /* EventEditorWrapperView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventEditorWrapperView.swift; sourceTree = "<group>"; };
A3EDE73225669FA60046C1F5 /* EventViewerWrapperView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventViewerWrapperView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -849,6 +851,7 @@
A31C7A58224E4BC900CB93A5 /* BaseNavigationController.swift */,
A33F810122468B69009CC46A /* BaseViewController.swift */,
A30BFEBE25654D3400BA7F38 /* ContentView.swift */,
A3EA185728C2333100F36A22 /* ContentViewModifier.swift */,
);
path = Base;
sourceTree = "<group>";
Expand Down Expand Up @@ -1619,6 +1622,7 @@
buildActionMask = 2147483647;
files = (
A315FA2625CF0FC300E909DB /* WidgetEntry.swift in Sources */,
A3EA185828C2333200F36A22 /* ContentViewModifier.swift in Sources */,
A36A50502250EE6A00B12034 /* LoggerConfiguration.swift in Sources */,
A38199F625484D9E00C71D41 /* ClendarEvent.swift in Sources */,
A31C7A59224E4BC900CB93A5 /* BaseNavigationController.swift in Sources */,
Expand Down
70 changes: 9 additions & 61 deletions Clendar/Base/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@

import EventKit
import SwiftUI
import ConfettiSwiftUI
import Shift
import Then
import SwiftDate
import WhatsNewKit
import CalendarView
import ClendarTheme
import Shift
import Then
import ConfettiSwiftUI

struct ContentView: View {
@EnvironmentObject var store: SharedStore
Expand Down Expand Up @@ -140,66 +138,16 @@ struct ContentView: View {
return NavigationSplitView {
buildContainerView()
} detail: {}
.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)
}
.modifier(
ContentViewModifier(store: store, eventKitWrapper: eventKitWrapper, confettiCounter: confettiCounter)
)
} else {
return NavigationView {
buildContainerView()
}
.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)
}
.modifier(
ContentViewModifier(store: store, eventKitWrapper: eventKitWrapper, confettiCounter: confettiCounter)
)
}
}
}
Expand Down
68 changes: 68 additions & 0 deletions Clendar/Base/ContentViewModifier.swift
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
}
}

0 comments on commit e51e214

Please sign in to comment.