Notifications and UI color scheme update

This commit is contained in:
maddiebaka
2023-06-07 15:47:26 -04:00
parent a894876623
commit 90be4321b0
6 changed files with 100 additions and 14 deletions
+17 -9
View File
@@ -29,6 +29,8 @@ class MoonrakerSocketManager: ObservableObject, WebSocketDelegate {
@Published var connection: NWConnection?
@Published var friendlyHostname: String = ""
var notification = UserNotificationHandler.shared
private var socket: WebSocket?
private var lastPingDate = Date()
private var starscreamEngine: Engine
@@ -67,11 +69,11 @@ class MoonrakerSocketManager: ObservableObject, WebSocketDelegate {
print("\(key): \(value)")
})
// if isConnected == true {
// connection?.cancel()
// socket?.disconnect()
// }
//
// if isConnected == true {
// connection?.cancel()
// socket?.disconnect()
// }
//
if connection == nil {
connection = NWConnection(to: endpoint, using: .tcp)
}
@@ -95,7 +97,7 @@ class MoonrakerSocketManager: ObservableObject, WebSocketDelegate {
connection?.cancel()
DispatchQueue.main.async {
friendlyHostname = endpoint.toFriendlyString()
self.friendlyHostname = endpoint.toFriendlyString()
self.socketHost = sanitizedHost
self.socketPort = "\(port)"
self.openWebsocket()
@@ -115,7 +117,7 @@ class MoonrakerSocketManager: ObservableObject, WebSocketDelegate {
connection = nil
socket?.disconnect()
}
// MARK: Private functions
@@ -221,16 +223,22 @@ class MoonrakerSocketManager: ObservableObject, WebSocketDelegate {
// Parse a JSON-RPC query-response message
func parse_response(_ response: jsonRpcResponse) {
state = response.result.status.print_stats?.state ?? ""
progress = response.result.status.virtual_sdcard?.progress ?? 0.0
extruderTemperature = response.result.status.extruder?.temperature ?? 0.0
bedTemperature = response.result.status.heater_bed?.temperature ?? 0.0
print(response)
}
// Parse a JSON-RPC update message
func parse_update(_ update: jsonRpcUpdate) {
if let newState = update.params.status?.print_stats?.state {
print("Printer state: \(newState)")
// Issue a notification when state changes from printing to complete
// TODO: Handle this better
if newState == "complete" && state == "printing" {
notification.sendNotification(.printComplete)
}
state = newState
}
if let newProgress = update.params.status?.virtual_sdcard?.progress {
@@ -0,0 +1,42 @@
//
// UserNotificationProtocol.swift
// Soyuz
//
// Created by Madeline Pace on 5/28/23.
//
import UserNotifications
class UserNotificationHandler {
static var shared = UserNotificationHandler()
private var center = UNUserNotificationCenter.current()
enum NotificationType {
case printComplete
}
private init() {
center.requestAuthorization(options: [.alert, .sound, .badge, .provisional]) { granted, error in
if let error = error {
print("Error requesting authorization: \(error)")
}
}
}
func sendNotification(_ type: NotificationType) {
print("Sending notification.")
// Build notification request
let content = UNMutableNotificationContent()
// TODO: Replace this with localized strings
content.title = "Print Complete! 🎉"
let request = UNNotificationRequest(identifier: "Print Finished", content: content, trigger: nil)
// Dispatch notification to system
center.add(request) { (error: Error?) in
if let theError = error {
print("Error: \(theError)")
}
}
}
}