Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

Keep owing the disk warning while macOS won't show it

Țugui Dragoșbe67412048cf
diff --git a/Packages/PeelCore/Sources/PeelCore/Home/LowDiskSpace.swift b/Packages/PeelCore/Sources/PeelCore/Home/LowDiskSpace.swift
index 77bf1d89..79496fc4 100644
--- a/Packages/PeelCore/Sources/PeelCore/Home/LowDiskSpace.swift
+++ b/Packages/PeelCore/Sources/PeelCore/Home/LowDiskSpace.swift
@@ -13,10 +13,13 @@ public enum LowDiskSpace {
static let nearlyFull = 0.10
static let roomAgain = 0.125
- public static func check(_ storage: DeviceInfo.Storage, told: Bool) -> Answer {
+ /// `canTell` is whether macOS shows Peel's notifications now: a warning it can't show stays owed.
+ public static func check(_ storage: DeviceInfo.Storage, told: Bool, canTell: Bool) -> Answer {
guard storage.total > 0 else { return Answer(tell: false, told: told) }
let free = Double(storage.free) / Double(storage.total)
- if free < nearlyFull { return Answer(tell: !told, told: true) }
+ if free < nearlyFull {
+ return canTell ? Answer(tell: !told, told: true) : Answer(tell: false, told: told)
+ }
return Answer(tell: false, told: told && free < roomAgain)
}
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/LowDiskSpaceTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/LowDiskSpaceTests.swift
index 68de0767..9d97a2f1 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/LowDiskSpaceTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/LowDiskSpaceTests.swift
@@ -8,20 +8,32 @@ struct LowDiskSpaceTests {
DeviceInfo.Storage(total: total, free: free)
}
+ private static func check(free gigabytes: Int64, told: Bool) -> LowDiskSpace.Answer {
+ LowDiskSpace.check(disk(free: gigabytes * gigabyte), told: told, canTell: true)
+ }
+
@Test func tellsOnceWhenLessThanATenthIsFree() {
- #expect(LowDiskSpace.check(Self.disk(free: 60 * Self.gigabyte), told: false) == .init(tell: false, told: false))
- #expect(LowDiskSpace.check(Self.disk(free: 49 * Self.gigabyte), told: false) == .init(tell: true, told: true))
- #expect(LowDiskSpace.check(Self.disk(free: 20 * Self.gigabyte), told: true) == .init(tell: false, told: true))
+ #expect(Self.check(free: 60, told: false) == .init(tell: false, told: false))
+ #expect(Self.check(free: 49, told: false) == .init(tell: true, told: true))
+ #expect(Self.check(free: 20, told: true) == .init(tell: false, told: true))
}
@Test func tellsAgainOnlyAfterTheDiskHadRoom() {
- #expect(LowDiskSpace.check(Self.disk(free: 60 * Self.gigabyte), told: true) == .init(tell: false, told: true))
- #expect(LowDiskSpace.check(Self.disk(free: 49 * Self.gigabyte), told: true) == .init(tell: false, told: true))
- #expect(LowDiskSpace.check(Self.disk(free: 65 * Self.gigabyte), told: true) == .init(tell: false, told: false))
- #expect(LowDiskSpace.check(Self.disk(free: 49 * Self.gigabyte), told: false) == .init(tell: true, told: true))
+ #expect(Self.check(free: 60, told: true) == .init(tell: false, told: true))
+ #expect(Self.check(free: 49, told: true) == .init(tell: false, told: true))
+ #expect(Self.check(free: 65, told: true) == .init(tell: false, told: false))
+ #expect(Self.check(free: 49, told: false) == .init(tell: true, told: true))
+ }
+
+ @Test func aWarningMacOSCannotShowIsStillOwed() {
+ let full = Self.disk(free: 49 * Self.gigabyte)
+ #expect(LowDiskSpace.check(full, told: false, canTell: false) == .init(tell: false, told: false))
+ #expect(LowDiskSpace.check(full, told: false, canTell: true) == .init(tell: true, told: true))
+ #expect(LowDiskSpace.check(full, told: true, canTell: false) == .init(tell: false, told: true))
}
@Test func saysNothingOfADiskWhoseSizeIsUnknown() {
- #expect(LowDiskSpace.check(Self.disk(free: 0, of: 0), told: false) == .init(tell: false, told: false))
+ let unknown = Self.disk(free: 0, of: 0)
+ #expect(LowDiskSpace.check(unknown, told: false, canTell: true) == .init(tell: false, told: false))
}
}
diff --git a/Peel/Model/PeelNotifications.swift b/Peel/Model/PeelNotifications.swift
index 9954795c..6dfd95a7 100644
--- a/Peel/Model/PeelNotifications.swift
+++ b/Peel/Model/PeelNotifications.swift
@@ -24,6 +24,11 @@ final class PeelNotifications: NSObject, UNUserNotificationCenterDelegate {
status == .denied
}
+ static func mayShow() async -> Bool {
+ let status = await UNUserNotificationCenter.current().notificationSettings().authorizationStatus
+ return status == .authorized || status == .provisional
+ }
+
func notify(applicationTrashed url: URL) {
let content = UNMutableNotificationContent()
let name = AppInspector.displayName(of: url)
diff --git a/Peel/PeelApp.swift b/Peel/PeelApp.swift
index f30ac19e..cc403769 100644
--- a/Peel/PeelApp.swift
+++ b/Peel/PeelApp.swift
@@ -276,9 +276,11 @@ struct PeelApp: App {
private func checkFreeSpace() async {
let storage = await LowDiskSpace.storage()
+ // Read before `told`, so two checks running at once can't both tell.
+ let canTell = await PeelNotifications.mayShow()
let defaults = UserDefaults.standard
let told = defaults.bool(forKey: SettingsKey.toldDiskIsNearlyFull)
- let answer = LowDiskSpace.check(storage, told: told)
+ let answer = LowDiskSpace.check(storage, told: told, canTell: canTell)
// Written only when it changes: every write to the defaults makes each `AppStorage` in the app read again.
if answer.told != told {
defaults.set(answer.told, forKey: SettingsKey.toldDiskIsNearlyFull)