Skip to content

Commit

Permalink
Fix previews rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
Reeywhaar committed Sep 13, 2023
1 parent c92e0fb commit 1b902ee
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
14 changes: 8 additions & 6 deletions MiddleMe/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ struct ContentView: View {

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
let s1 = Settings()
let s2 = Settings()
let _ = {s2.permissionGranted = true}()

ContentView().environmentObject(s1)
ContentView().environmentObject(s2)
ContentView().environmentObject(makeSettings(false))
ContentView().environmentObject(makeSettings(true))
}

static func makeSettings(_ granted: Bool) -> Settings {
let settings = Settings()
settings.permissionGranted = granted
return settings
}
}

Expand Down
22 changes: 16 additions & 6 deletions MiddleMe/MiddleMeApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,23 @@ struct MiddleMeApp: App {
private let settings = Settings()

private let handler = GlobalEventMonitor()
private let isPreview = ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1"
#if DEBUG
private let isDebug = true
#else
private let isDebug = true
#endif

init() {
if isPreview { return }

handler.start()
handler.register()

appLogger.info("Registering login item")
#if !DEBUG
try? SMAppService.mainApp.register()
#endif
if !isPreview && !isDebug {
try? SMAppService.mainApp.register()
}

NotificationCenter.default.addObserver(forName: NSApplication.willTerminateNotification, object: nil, queue: .main, using: self.handleTerminate)
}
Expand All @@ -41,6 +49,8 @@ struct MiddleMeApp: App {
settings.checkAccess()
}
.onAppear {
if isPreview { return }

appLogger.info("Making app active")
NSApplication.shared.activate(ignoringOtherApps: true)
}
Expand All @@ -50,9 +60,9 @@ struct MiddleMeApp: App {
private func handleTerminate(notification: Notification) {
if(appDelegate.activeExit) {
appLogger.info("Unregistering login item")
#if !DEBUG
try? SMAppService.mainApp.unregister()
#endif
if !isPreview && !isDebug {
try? SMAppService.mainApp.unregister()
}
}
}
}
Expand Down

0 comments on commit 1b902ee

Please sign in to comment.