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

GUI: Add selectable silence periods for FAA notifications #60

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 7 additions & 13 deletions Source/gui/SNTBinaryMessageWindowView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -264,22 +264,16 @@ struct SNTBinaryMessageWindowView: View {

let c = SNTConfigurator()

@State public var preventFutureNotifications = false

let preventNotificationPeriods: [TimeInterval] = [86400, 604800, 2678400]
@State public var preventFutureNotifications = false
@State public var preventFutureNotificationPeriod: TimeInterval = 86400
let dateFormatter = DateComponentsFormatter()

init(window: NSWindow, event: SNTStoredEvent?, customMsg: NSString?, customURL: NSString?, uiStateCallback: ((TimeInterval) -> Void)?) {
self.window = window
self.event = event
self.customMsg = customMsg
self.customURL = customURL
self.uiStateCallback = uiStateCallback

dateFormatter.unitsStyle = .spellOut
dateFormatter.allowedUnits = [.day, .month, .weekOfMonth]
}
let dateFormatter : DateComponentsFormatter = {
let df = DateComponentsFormatter()
df.unitsStyle = .spellOut
df.allowedUnits = [.day, .month, .weekOfMonth]
return df
}()

var body: some View {
VStack(spacing:15.0) {
Expand Down
4 changes: 2 additions & 2 deletions Source/gui/SNTFileAccessMessageWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ - (void)showWindow:(id)sender {
customURL:self.customURL]
.absoluteString
customText:self.customText
uiStateCallback:^(BOOL preventNotificationsForADay) {
self.silenceFutureNotificationsPeriod = preventNotificationsForADay ? 86400 : 0;
uiStateCallback:^(NSTimeInterval preventNotificationsPeriod) {
self.silenceFutureNotificationsPeriod = preventNotificationsPeriod;
}];

self.window.delegate = self;
Expand Down
48 changes: 39 additions & 9 deletions Source/gui/SNTFileAccessMessageWindowView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import santa_common_SNTFileAccessEvent
customMessage: NSAttributedString?,
customURL: NSString?,
customText: NSString?,
uiStateCallback: ((Bool) -> Void)?) -> NSViewController {
uiStateCallback: ((TimeInterval) -> Void)?) -> NSViewController {
return NSHostingController(rootView:SNTFileAccessMessageWindowView(window:window,
event:event,
customMessage:customMessage,
Expand Down Expand Up @@ -113,10 +113,20 @@ struct SNTFileAccessMessageWindowView: View {
let customMessage: NSAttributedString?
let customURL: String?
let customText: String?
let uiStateCallback: ((Bool) -> Void)?
let uiStateCallback: ((TimeInterval) -> Void)?

@Environment(\.openURL) var openURL
@State public var checked = false

let preventNotificationPeriods: [TimeInterval] = [86400, 604800, 2678400]
@State public var preventFutureNotifications = false
@State public var preventFutureNotificationPeriod: TimeInterval = 86400

let dateFormatter : DateComponentsFormatter = {
let df = DateComponentsFormatter()
df.unitsStyle = .spellOut
df.allowedUnits = [.day, .month, .weekOfMonth]
return df
}()

var body: some View {
VStack(spacing:20.0) {
Expand All @@ -131,10 +141,26 @@ struct SNTFileAccessMessageWindowView: View {

Event(e: event!, window: window)

Toggle(isOn: $checked) {
Text("Prevent future notifications for this application for a day")
.font(Font.system(size: 11.0));
}
// TODO: Make a shared view component for this "prevent notifications" checkbox and picker, as it's
// useful in multiple views.
// Create a wrapper binding around $preventFutureNotificationsPeriod so that we can automatically
// check the checkbox if the user has selected a new period.
let pi = Binding<TimeInterval>(get: { return self.preventFutureNotificationPeriod }, set: {
self.preventFutureNotifications = true
self.preventFutureNotificationPeriod = $0
})

Toggle(isOn: $preventFutureNotifications) {
HStack(spacing:0.0) {
Text("Prevent future notifications for this application for ").font(Font.system(size: 11.0));
Picker("", selection: pi) {
ForEach(preventNotificationPeriods, id: \.self) { period in
let text = dateFormatter.string(from: period) ?? "unknown"
Text(text).font(Font.system(size: 11.0))
}
}.fixedSize()
}
}.padding(10.0)

VStack(spacing:15) {
if customURL != nil {
Expand Down Expand Up @@ -171,8 +197,12 @@ struct SNTFileAccessMessageWindowView: View {
}

func dismissButton() {
if let block = uiStateCallback {
block(self.checked)
if let callback = uiStateCallback {
if self.preventFutureNotifications {
callback(self.preventFutureNotificationPeriod)
} else {
callback(0)
}
}
window?.close()
}
Expand Down