Skip to content

Commit

Permalink
Added notification center support
Browse files Browse the repository at this point in the history
  • Loading branch information
Ranoiaetep committed Jan 27, 2021
1 parent 04cf990 commit 306b408
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions AutoRefersher/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,42 @@

import SwiftUI
import AppKit
import UserNotifications

struct ContentView: View {
let doubleFormatter = NumberFormatter()
let dateFormatter = DateComponentsFormatter()

let center = UNUserNotificationCenter.current()
let notification = UNMutableNotificationContent()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.3, repeats: false)

@State var addressList: [String] = [String()]
@State private var visitTimes: Double = 100
@State private var remainingTimes: Double = 100
@State private var optionMenu: Bool = false
@State private var interval: Double = 1.8
@State private var started: Bool = false
@State private var firstRun: Bool = true
@State private var notificationOn: Bool = true

init() {
center.requestAuthorization(options: [.alert, .sound, .badge, .provisional]) { _,_ in}

center.getNotificationSettings { settings in
guard (settings.authorizationStatus == .authorized) ||
(settings.authorizationStatus == .provisional) else { return }

if settings.alertSetting == .enabled {
// Schedule an alert-only notification.
} else {
// Schedule a notification with a badge and sound.
}
}

// notification.title = "Auto Refresher"
notification.body = "􀁣 Job done!"

self.started = started
doubleFormatter.usesSignificantDigits = true
doubleFormatter.maximumFractionDigits = 2
Expand Down Expand Up @@ -83,6 +105,10 @@ struct ContentView: View {
NSWorkspace.shared.open(url)
}
remainingTimes -= 1
if remainingTimes == 0 && notificationOn {
let request = UNNotificationRequest(identifier: "request", content: notification, trigger: trigger)
center.add(request) { _ in}
}
}
}
})
Expand Down Expand Up @@ -146,7 +172,7 @@ struct ContentView: View {
}
}
if optionMenu {
OptionMenu(interval: $interval, doubleFormatter: doubleFormatter)
OptionMenu(interval: $interval, notification: $notificationOn, doubleFormatter: doubleFormatter)
}
}
.padding()
Expand All @@ -155,19 +181,27 @@ struct ContentView: View {

struct OptionMenu: View {
@Binding var interval: Double
@Binding var notification: Bool
var doubleFormatter: NumberFormatter

var body: some View {
HStack {
Spacer()
GroupBox() {
HStack {
Spacer()
Text("Interval:")
Stepper(value: $interval, step: 0.2)
{
TextField("", value: $interval, formatter: doubleFormatter)
.frame(width: 40)
}
Spacer()
Toggle(isOn: $notification) {
Text("Notification:")
}
.toggleStyle(SwitchToggleStyle())
Spacer()
}
.frame(minWidth: 300, idealWidth: 400, maxWidth: 400)
}
Expand All @@ -181,7 +215,7 @@ struct ContentView_Previews: PreviewProvider {
static var previews: some View {
Group {
ContentView()
OptionMenu(interval: .constant(2.5), doubleFormatter: NumberFormatter())
OptionMenu(interval: .constant(2.5), notification: .constant(true), doubleFormatter: NumberFormatter())
}
.padding(5)
}
Expand Down

0 comments on commit 306b408

Please sign in to comment.