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

Adds ability to switch the local cache directory #155

Merged
merged 1 commit into from
Oct 8, 2021
Merged
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
17 changes: 17 additions & 0 deletions Xcodes/Backend/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ class AppState: ObservableObject {

@Published var error: Error?
@Published var authError: Error?

// MARK: Advanced Preferences
@Published var localPath = "" {
didSet {
Current.defaults.set(localPath, forKey: "localPath")
}
}

// MARK: - Publisher Cancellables

Expand Down Expand Up @@ -84,6 +91,11 @@ class AppState: ObservableObject {
try? loadCachedAvailableXcodes()
checkIfHelperIsInstalled()
setupAutoInstallTimer()
setupDefaults()
}

func setupDefaults() {
localPath = Current.defaults.string(forKey: "localPath") ?? Path.defaultXcodesApplicationSupport.string
}

// MARK: Timer
Expand Down Expand Up @@ -393,6 +405,11 @@ class AppState: ObservableObject {
guard let installedXcode = Current.files.installedXcodes(Path.root/"Applications").first(where: { $0.version == id }) else { return }
NSWorkspace.shared.activateFileViewerSelecting([installedXcode.path.url])
}

func reveal(path: String) {
let url = URL(fileURLWithPath: path)
NSWorkspace.shared.activateFileViewerSelecting([url])
}

/// Make an Xcode active, a.k.a select it, in the `xcode-select` sense.
///
Expand Down
17 changes: 15 additions & 2 deletions Xcodes/Backend/Path+.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
import Path
import Foundation

extension Path {
static let xcodesApplicationSupport = Path.applicationSupport/"com.robotsandpencils.XcodesApp"
static let cacheFile = xcodesApplicationSupport/"available-xcodes.json"
static let defaultXcodesApplicationSupport = Path.applicationSupport/"com.robotsandpencils.XcodesApp"
static var xcodesApplicationSupport: Path {
guard let savedApplicationSupport = Current.defaults.string(forKey: "localPath") else {
return defaultXcodesApplicationSupport
}
guard let path = Path(savedApplicationSupport) else {
return defaultXcodesApplicationSupport
}
return path
}

static var cacheFile: Path {
return xcodesApplicationSupport/"available-xcodes.json"
}
}
41 changes: 40 additions & 1 deletion Xcodes/Frontend/Preferences/AdvancedPreferencePane.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,52 @@
import AppleAPI
import SwiftUI
import Path

struct AdvancedPreferencePane: View {
@EnvironmentObject var appState: AppState

@AppStorage("dataSource") var dataSource: DataSource = .xcodeReleases
@AppStorage("downloader") var downloader: Downloader = .aria2

var body: some View {
VStack(alignment: .leading, spacing: 20) {

GroupBox(label: Text("Local Cache Path")) {
VStack(alignment: .leading) {
HStack(alignment: .top, spacing: 5) {
Text(appState.localPath).font(.footnote)
.fixedSize(horizontal: false, vertical: true)
.lineLimit(2)
Button(action: { appState.reveal(path: appState.localPath) }) {
Image(systemName: "arrow.right.circle.fill")
}
.buttonStyle(PlainButtonStyle())
.help("Reveal in Finder")
.fixedSize()
}
Button("Change") {
let panel = NSOpenPanel()
panel.allowsMultipleSelection = false
panel.canChooseDirectories = true
panel.canChooseFiles = false
panel.canCreateDirectories = true
panel.allowedContentTypes = [.folder]
panel.directoryURL = URL(fileURLWithPath: appState.localPath)

if panel.runModal() == .OK {

guard let pathURL = panel.url, let path = Path(url: pathURL) else { return }
self.appState.localPath = path.string
}
}
Text("Xcodes caches available Xcode versions and temporary downloads new versions to a directory")
.font(.footnote)
.fixedSize(horizontal: false, vertical: true)
}

}
.groupBoxStyle(PreferencesGroupBoxStyle())

GroupBox(label: Text("Data Source")) {
VStack(alignment: .leading) {
Picker("Data Source", selection: $dataSource) {
Expand Down Expand Up @@ -66,7 +105,7 @@ struct AdvancedPreferencePane: View {
}
.groupBoxStyle(PreferencesGroupBoxStyle())
}
.frame(width: 400)
.frame(width: 500)
}

private var dataSourceFootnote: NSAttributedString {
Expand Down
2 changes: 1 addition & 1 deletion Xcodes/Frontend/Preferences/GeneralPreferencePane.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct GeneralPreferencePane: View {
}
.groupBoxStyle(PreferencesGroupBoxStyle())
}
.frame(width: 400)
.frame(width: 500)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Xcodes/Frontend/Preferences/UpdatesPreferencePane.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ struct UpdatesPreferencePane: View {
}
.groupBoxStyle(PreferencesGroupBoxStyle())
}
.frame(width: 400)
.frame(width: 500)
}

private var lastUpdatedString: String {
Expand Down