mirror of
https://github.com/SideStore/SideStore.git
synced 2026-02-12 00:03:27 +01:00
Reorganize AltStore project into UIKit and SwiftUI folders
This commit is contained in:
80
AltStore/SwiftUI/Manager/NotificationManager.swift
Normal file
80
AltStore/SwiftUI/Manager/NotificationManager.swift
Normal file
@@ -0,0 +1,80 @@
|
||||
//
|
||||
// NotificationManager.swift
|
||||
// SideStore
|
||||
//
|
||||
// Created by Fabian Thies on 21.11.22.
|
||||
// Copyright © 2022 Fabian Thies. All rights reserved.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import AltStoreCore
|
||||
|
||||
class NotificationManager: ObservableObject {
|
||||
|
||||
struct Notification: Identifiable {
|
||||
let id: UUID
|
||||
let title: String
|
||||
let message: String?
|
||||
}
|
||||
|
||||
static let shared = NotificationManager()
|
||||
|
||||
@Published
|
||||
var notifications: [UUID: Notification] = [:]
|
||||
|
||||
private init() {}
|
||||
|
||||
func reportError(error: Error) {
|
||||
if case OperationError.cancelled = error {
|
||||
// Ignore
|
||||
return
|
||||
}
|
||||
|
||||
var error = error as NSError
|
||||
var underlyingError = error.underlyingError
|
||||
|
||||
if
|
||||
let unwrappedUnderlyingError = underlyingError,
|
||||
error.domain == AltServerErrorDomain && error.code == ALTServerError.Code.underlyingError.rawValue
|
||||
{
|
||||
// Treat underlyingError as the primary error.
|
||||
|
||||
error = unwrappedUnderlyingError as NSError
|
||||
underlyingError = nil
|
||||
}
|
||||
|
||||
let text: String
|
||||
let detailText: String?
|
||||
|
||||
if let failure = error.localizedFailure
|
||||
{
|
||||
text = failure
|
||||
detailText = error.localizedFailureReason ?? error.localizedRecoverySuggestion ?? underlyingError?.localizedDescription ?? error.localizedDescription
|
||||
}
|
||||
else if let reason = error.localizedFailureReason
|
||||
{
|
||||
text = reason
|
||||
detailText = error.localizedRecoverySuggestion ?? underlyingError?.localizedDescription
|
||||
}
|
||||
else
|
||||
{
|
||||
text = error.localizedDescription
|
||||
detailText = underlyingError?.localizedDescription ?? error.localizedRecoverySuggestion
|
||||
}
|
||||
|
||||
self.showNotification(title: text, detailText: detailText)
|
||||
}
|
||||
|
||||
func showNotification(title: String, detailText: String? = nil) {
|
||||
let notificationId = UUID()
|
||||
|
||||
DispatchQueue.main.async {
|
||||
self.notifications[notificationId] = Notification(id: notificationId, title: title, message: detailText)
|
||||
}
|
||||
|
||||
let dismissWorkItem = DispatchWorkItem {
|
||||
self.notifications.removeValue(forKey: notificationId)
|
||||
}
|
||||
DispatchQueue.main.asyncAfter(deadline: .now().advanced(by: .seconds(5)), execute: dismissWorkItem)
|
||||
}
|
||||
}
|
||||
56
AltStore/SwiftUI/Manager/OutputCapturer.swift
Normal file
56
AltStore/SwiftUI/Manager/OutputCapturer.swift
Normal file
@@ -0,0 +1,56 @@
|
||||
//
|
||||
// OutputCapturer.swift
|
||||
// SideStore
|
||||
//
|
||||
// Created by Fabian Thies on 12.02.23.
|
||||
// Copyright © 2023 SideStore. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import LocalConsole
|
||||
|
||||
class OutputCapturer {
|
||||
|
||||
public static let shared = OutputCapturer()
|
||||
|
||||
private let consoleManager = LCManager.shared
|
||||
|
||||
private var inputPipe = Pipe()
|
||||
private var errorPipe = Pipe()
|
||||
private var outputPipe = Pipe()
|
||||
|
||||
private init() {
|
||||
// Setup pipe file handlers
|
||||
self.inputPipe.fileHandleForReading.readabilityHandler = { [weak self] fileHandle in
|
||||
self?.handle(data: fileHandle.availableData)
|
||||
}
|
||||
self.errorPipe.fileHandleForReading.readabilityHandler = { [weak self] fileHandle in
|
||||
self?.handle(data: fileHandle.availableData, isError: true)
|
||||
}
|
||||
|
||||
// Keep STDOUT
|
||||
dup2(STDOUT_FILENO, self.outputPipe.fileHandleForWriting.fileDescriptor)
|
||||
|
||||
// Intercept STDOUT and STDERR
|
||||
dup2(self.inputPipe.fileHandleForWriting.fileDescriptor, STDOUT_FILENO)
|
||||
dup2(self.errorPipe.fileHandleForWriting.fileDescriptor, STDERR_FILENO)
|
||||
}
|
||||
|
||||
deinit {
|
||||
try? self.inputPipe.fileHandleForReading.close()
|
||||
try? self.errorPipe.fileHandleForReading.close()
|
||||
}
|
||||
|
||||
private func handle(data: Data, isError: Bool = false) {
|
||||
// Write output to STDOUT
|
||||
self.outputPipe.fileHandleForWriting.write(data)
|
||||
|
||||
guard let string = String(data: data, encoding: .utf8) else {
|
||||
return
|
||||
}
|
||||
|
||||
DispatchQueue.main.async {
|
||||
self.consoleManager.print(string)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user