Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retain Pinned Programs on External Volumes #1308

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Whisky/Views/Bottle/Pins/PinView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ struct PinView: View {
self.image = await task.value
}
.onChange(of: name) {
if let index = bottle.settings.pins.firstIndex(where: { $0.url == pin.url }) {
if let index = bottle.settings.pins.firstIndex(where: {
let exists = FileManager.default.fileExists(atPath: pin.url?.path(percentEncoded: false) ?? "")
return $0.url == pin.url && exists
}) {
bottle.settings.pins[index].name = name
}
}
Expand Down
11 changes: 8 additions & 3 deletions Whisky/Views/Programs/ProgramsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,18 @@ struct ProgramsView: View {

private func loadData() {
loadPrograms()
blocklist = bottle.settings.blocklist
blocklist = bottle.settings.blocklist.filter({
return FileManager.default.fileExists(atPath: $0.path(percentEncoded: false))
})
}

private func loadPrograms() {
let programs = bottle.programs.filter({
return FileManager.default.fileExists(atPath: $0.url.path(percentEncoded: false))
})
sortedPrograms = [
bottle.programs.pinned.sorted { $0.name < $1.name },
bottle.programs.unpinned.sorted { $0.name < $1.name }
programs.pinned.sorted { $0.name < $1.name },
programs.unpinned.sorted { $0.name < $1.name }
].flatMap { $0 }
}
}
Expand Down
13 changes: 11 additions & 2 deletions WhiskyKit/Sources/WhiskyKit/Whisky/Bottle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public final class Bottle: ObservableObject, Equatable, Hashable, Identifiable,
public var pinnedPrograms: [(pin: PinnedProgram, program: Program, // swiftlint:disable:this large_tuple
id: String)] {
return settings.pins.compactMap { pin in
guard let program = programs.first(where: { $0.url == pin.url }) else { return nil }
let exists = FileManager.default.fileExists(atPath: pin.url?.path(percentEncoded: false) ?? "")
guard let program = programs.first(where: { $0.url == pin.url && exists }) else { return nil }
return (pin, program, "\(pin.name)//\(program.url)")
}
}
Expand All @@ -63,7 +64,15 @@ public final class Bottle: ObservableObject, Equatable, Hashable, Identifiable,
guard let url = pin.url else { return false }
guard !found.contains(url) else { return false }
found.insert(url)
return FileManager.default.fileExists(atPath: url.path(percentEncoded: false))
let urlPath = url.path(percentEncoded: false)
let volume: URL?
do {
volume = try url.resourceValues(forKeys: [.volumeURLKey]).volume ?? nil
} catch {
volume = nil
}
let legallyRemoved = pin.removable && volume == nil
return FileManager.default.fileExists(atPath: urlPath) || legallyRemoved
}
}

Expand Down
8 changes: 8 additions & 0 deletions WhiskyKit/Sources/WhiskyKit/Whisky/BottleSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,24 @@ import os.log
public struct PinnedProgram: Codable, Hashable, Equatable {
public var name: String
public var url: URL?
public var removable: Bool

public init(name: String, url: URL) {
self.name = name
self.url = url
do {
let volume = try url.resourceValues(forKeys: [.volumeURLKey]).volume
self.removable = try !(volume?.resourceValues(forKeys: [.volumeIsInternalKey]).volumeIsInternal ?? false)
} catch {
self.removable = false
}
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.name = try container.decodeIfPresent(String.self, forKey: .name) ?? ""
self.url = try container.decodeIfPresent(URL.self, forKey: .url)
self.removable = try container.decodeIfPresent(Bool.self, forKey: .removable) ?? false
}
}

Expand Down