Skip to content

Commit

Permalink
Merge pull request #3 from niamster/dm/dock
Browse files Browse the repository at this point in the history
a workaround to keep the app hidden from the dock
  • Loading branch information
niamster committed Jan 12, 2022
2 parents 02dcb79 + f999f31 commit 93f58a4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
4 changes: 2 additions & 2 deletions gh-notifications-osx.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 1.4;
MARKETING_VERSION = 1.5;
PRODUCT_BUNDLE_IDENTIFIER = "gh.gh-notifications-osx";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand All @@ -334,7 +334,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 1.4;
MARKETING_VERSION = 1.5;
PRODUCT_BUNDLE_IDENTIFIER = "gh.gh-notifications-osx";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_EMIT_LOC_STRINGS = YES;
Expand Down
28 changes: 25 additions & 3 deletions gh-notifications-osx/Notifications.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,15 @@ class GHNotificationsUpdate: Object {

extension String: Error {}

class Notifications {
class Notifications: NSObject, UNUserNotificationCenterDelegate {
var statusItem: NSStatusItem!
var popover: NSPopover!
var error: Error?
let logger = Puppy.default

init() {
override init() {
super.init()

let bundleId = Bundle.main.bundleIdentifier ?? "gh-notifications-osx"
Puppy.default.add(OSLogger("os", category: "notifications"))
do {
Expand Down Expand Up @@ -106,8 +108,9 @@ class Notifications {
logger.info("Started GitHub notifications notifier: '\(Bundle.main.bundleIdentifier ?? "?")'")
logger.info("Realm DB path: \(Realm.Configuration.defaultConfiguration.fileURL!.absoluteString)")

NSApp.setActivationPolicy(.prohibited)
hideFromDock()

UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { _, error in
if let error = error {
self.logger.error("Failed to request authorization: '\(error.localizedDescription)'")
Expand Down Expand Up @@ -147,6 +150,12 @@ class Notifications {
return String(decoding: password, as: UTF8.self)
}

func hideFromDock() {
DispatchQueue.main.async {
NSApp.setActivationPolicy(.prohibited)
}
}

func markErrorMaybe() {
if error == nil {
return
Expand Down Expand Up @@ -182,6 +191,19 @@ class Notifications {
}
}

func userNotificationCenter(_: UNUserNotificationCenter, didReceive _: UNNotificationResponse, withCompletionHandler: () -> Void) {
Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { timer in
let activationPolicy = NSApp.activationPolicy()
if activationPolicy == .prohibited {
timer.invalidate()
return
}
self.logger.debug("Activation policy was altered: '\(activationPolicy)'")
self.hideFromDock()
}
withCompletionHandler()
}

func showDelta(_ notifications: [NotificationThread]) throws {
let realm = try Realm()
let now = Date()
Expand Down

0 comments on commit 93f58a4

Please sign in to comment.