Notifications and UI color scheme update
This commit is contained in:
		@@ -2,6 +2,8 @@
 | 
			
		||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
 | 
			
		||||
<plist version="1.0">
 | 
			
		||||
<dict>
 | 
			
		||||
	<key>ITSAppUsesNonExemptEncryption</key>
 | 
			
		||||
	<false/>
 | 
			
		||||
	<key>CFBundleHelpBookFolder</key>
 | 
			
		||||
	<string>Soyuz.help</string>
 | 
			
		||||
	<key>CFBundleHelpBookName</key>
 | 
			
		||||
 
 | 
			
		||||
@@ -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  {
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										42
									
								
								Soyuz/ViewModels/UserNotificationHandler.swift
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								Soyuz/ViewModels/UserNotificationHandler.swift
									
									
									
									
									
										Normal file
									
								
							@@ -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)")
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -6,6 +6,7 @@
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
import SwiftUI
 | 
			
		||||
import UserNotifications
 | 
			
		||||
import AppKit
 | 
			
		||||
import Network
 | 
			
		||||
 | 
			
		||||
@@ -21,6 +22,7 @@ struct SoyuzMenuBarExtraView: View {
 | 
			
		||||
    @State var printPercentage: Double = 0
 | 
			
		||||
    
 | 
			
		||||
    @Binding var currentMenuBarIcon: String
 | 
			
		||||
    var notification = UserNotificationHandler.shared
 | 
			
		||||
    
 | 
			
		||||
    @State var hotendHotTemp: Bool = false
 | 
			
		||||
    @State var bedHotTemp: Bool = false
 | 
			
		||||
@@ -53,7 +55,7 @@ struct SoyuzMenuBarExtraView: View {
 | 
			
		||||
                        // Hot-end temperature
 | 
			
		||||
                        HStack {
 | 
			
		||||
                            Image(systemName: "flame")
 | 
			
		||||
                                .foregroundColor( printerManager.extruderTemperature > DANGERTEMP ? .red : .white )
 | 
			
		||||
                                .foregroundColor( printerManager.extruderTemperature > DANGERTEMP ? .red : Color(nsColor: .labelColor))
 | 
			
		||||
                                .opacity( printerManager.extruderTemperature > DANGERTEMP ? 1.0 : 0.3 )
 | 
			
		||||
                            Text("Hotend")
 | 
			
		||||
                                .font(.headline)
 | 
			
		||||
@@ -63,7 +65,7 @@ struct SoyuzMenuBarExtraView: View {
 | 
			
		||||
                        // Bed temperature
 | 
			
		||||
                        HStack {
 | 
			
		||||
                            Image(systemName: "flame")
 | 
			
		||||
                                .foregroundColor( printerManager.bedTemperature > DANGERTEMP ? .red : .white )
 | 
			
		||||
                                .foregroundColor( printerManager.bedTemperature > DANGERTEMP ? .red : Color(nsColor: .labelColor) )
 | 
			
		||||
                                .opacity( printerManager.bedTemperature > DANGERTEMP ? 1.0 : 0.3 )
 | 
			
		||||
                            Text("Plate")
 | 
			
		||||
                                .font(.headline)
 | 
			
		||||
@@ -79,12 +81,18 @@ struct SoyuzMenuBarExtraView: View {
 | 
			
		||||
        // Footer information
 | 
			
		||||
        HStack {
 | 
			
		||||
            Button {
 | 
			
		||||
                print("Button pressed")
 | 
			
		||||
                openWindow(id: "soyuz_cfg")
 | 
			
		||||
            } label: {
 | 
			
		||||
                Text("Printers")
 | 
			
		||||
                    .foregroundColor(Color("ButtonForegroundColor"))
 | 
			
		||||
            }
 | 
			
		||||
            /* Debugging Stuff */
 | 
			
		||||
            Button {
 | 
			
		||||
                notification.sendNotification(.printComplete)
 | 
			
		||||
            } label: {
 | 
			
		||||
                Text("Notify")
 | 
			
		||||
            }
 | 
			
		||||
            /* Debugging Stuff */
 | 
			
		||||
            Spacer()
 | 
			
		||||
            if(printerManager.isConnected) {
 | 
			
		||||
                Image(systemName: "network")
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user