Skip to content

Commit

Permalink
Moved Themes and Preferences to Application Support (#827)
Browse files Browse the repository at this point in the history
* Changed OpenWithCodeEdit Signing Certificate to Development to match CodeEdit Signing Certificate. This was causing a build failure in our release workflow

* Moved themes and preferences to user application support folder

* Put properties at the top of ThemeModel

* Using baseURL to load preferences
  • Loading branch information
austincondiff authored Oct 13, 2022
1 parent dddcaed commit 29470d9
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This package manages **Preferences** of the app.

## Overview

The main task is to get the preferences from `~/.codeedit/preferences.json` and load them into an ``AppPreferences/AppPreferences`` model.
The main task is to get the preferences from `~/Library/Application Support/CodeEdit/preferences.json` and load them into an ``AppPreferences/AppPreferences`` model.
Once a value changes the changes are written to the `preferences.json` file.

It also contains all preferences section views and necessary sub-models.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Overview

A ``AppPreferences/Theme`` is stored in a `theme_name.json` file in the `~/.codeedit/themes/` directory. There are a
A ``AppPreferences/Theme`` is stored in a `theme_name.json` file in the `~/Library/Application Support/CodeEdit/themes/` directory. There are a
couple of bundled themes that will automatically be put there once the app starts.

Once a `JSON` file is loaded, the ``AppPreferences/Theme`` gets added to ``AppPreferences/ThemeModel/themes``.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SwiftUI
///
/// The model structure of settings for `CodeEdit`
///
/// A `JSON` representation is persisted in `~/.codeedit/preference.json`.
/// A `JSON` representation is persisted in `~/Library/Application Support/CodeEdit/preference.json`.
/// - Attention: Don't use `UserDefaults` for persisting user accessible settings.
/// If a further setting is needed, extend the struct like ``GeneralPreferences``,
/// ``ThemePreferences``, or ``TerminalPreferences`` does.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,10 @@ public final class AppPreferencesModel: ObservableObject {
}

/// Load and construct ``AppPreferences`` model from
/// `~/.codeedit/preferences.json`
/// `~/Library/Application Support/CodeEdit/preferences.json`
private func loadPreferences() -> AppPreferences {
if !filemanager.fileExists(atPath: preferencesURL.path) {
let codeEditURL = filemanager
.homeDirectoryForCurrentUser
.appendingPathComponent(".codeedit", isDirectory: true)
try? filemanager.createDirectory(at: codeEditURL, withIntermediateDirectories: false)
try? filemanager.createDirectory(at: baseURL, withIntermediateDirectories: false)
return .init()
}

Expand All @@ -56,7 +53,7 @@ public final class AppPreferencesModel: ObservableObject {
}

/// Save``AppPreferences`` model to
/// `~/.codeedit/preferences.json`
/// `~/Library/Application Support/CodeEdit/preferences.json`
private func savePreferences() throws {
let data = try JSONEncoder().encode(preferences)
let json = try JSONSerialization.jsonObject(with: data)
Expand All @@ -69,16 +66,16 @@ public final class AppPreferencesModel: ObservableObject {

/// The base URL of preferences.
///
/// Points to `~/.codeedit/`
/// Points to `~/Library/Application Support/CodeEdit/`
internal var baseURL: URL {
filemanager
.homeDirectoryForCurrentUser
.appendingPathComponent(".codeedit", isDirectory: true)
.appendingPathComponent("Library/Application Support/CodeEdit", isDirectory: true)
}

/// The URL of the `preferences.json` settings file.
///
/// Points to `~/.codeedit/preferences.json`
/// Points to `~/Library/Application Support/CodeEdit/preferences.json`
private var preferencesURL: URL {
baseURL
.appendingPathComponent("preferences")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ public final class ThemeModel: ObservableObject {

public static let shared: ThemeModel = .init()

/// Default instance of the `FileManager`
private let filemanager = FileManager.default

/// The base folder url `~/Library/Application Support/CodeEdit/`
private var baseURL: URL {
filemanager.homeDirectoryForCurrentUser.appendingPathComponent("Library/Application Support/CodeEdit")
}

/// The URL of the `themes` folder
internal var themesURL: URL {
baseURL.appendingPathComponent("themes", isDirectory: true)
}

/// Selected 'light' theme
/// Used for auto-switching theme to match macOS system appearance
@Published
Expand Down Expand Up @@ -101,14 +114,14 @@ public final class ThemeModel: ObservableObject {
}
}

/// Loads all available themes from `~/.codeedit/themes/`
/// Loads all available themes from `~/Library/Application Support/CodeEdit/themes/`
///
/// If no themes are available, it will create a default theme and save
/// it to the location mentioned above.
///
/// When overrides are found in `~/.codeedit/.preferences.json`
/// When overrides are found in `~/Library/Application Support/CodeEdit/.preferences.json`
/// they are applied to the loaded themes without altering the original
/// the files in `~/.codeedit/themes/`.
/// the files in `~/Library/Application Support/CodeEdit/themes/`.
public func loadThemes() throws {
// remove all themes from memory
themes.removeAll()
Expand Down Expand Up @@ -217,10 +230,10 @@ public final class ThemeModel: ObservableObject {
}

/// Removes all overrides of the given theme in
/// `~/.codeedit/preferences.json`
/// `~/Library/Application Support/CodeEdit/preferences.json`
///
/// After removing overrides, themes are reloaded
/// from `~/.codeedit/themes`. See ``loadThemes()``
/// from `~/Library/Application Support/CodeEdit/themes`. See ``loadThemes()``
/// for more information.
///
/// - Parameter theme: The theme to reset
Expand All @@ -233,10 +246,10 @@ public final class ThemeModel: ObservableObject {
}
}

/// Removes the given theme from `–/.codeedit/themes`
/// Removes the given theme from `–/Library/Application Support/CodeEdit/themes`
///
/// After removing the theme, themes are reloaded
/// from `~/.codeedit/themes`. See ``loadThemes()``
/// from `~/Library/Application Support/CodeEdit/themes`. See ``loadThemes()``
/// for more information.
///
/// - Parameter theme: The theme to delete
Expand All @@ -259,12 +272,12 @@ public final class ThemeModel: ObservableObject {
}

/// Saves changes on theme properties to `overrides`
/// in `~/.codeedit/preferences.json`.
/// in `~/Library/Application Support/CodeEdit/preferences.json`.
private func saveThemes() {
let url = themesURL
themes.forEach { theme in
do {
// load the original theme from `~/.codeedit/themes/`
// load the original theme from `~/Library/Application Support/CodeEdit/themes/`
let originalUrl = url.appendingPathComponent(theme.name).appendingPathExtension("json")
let originalData = try Data(contentsOf: originalUrl)
let originalTheme = try JSONDecoder().decode(Theme.self, from: originalData)
Expand Down Expand Up @@ -299,17 +312,4 @@ public final class ThemeModel: ObservableObject {
}
}
}

/// Default instance of the `FileManager`
private let filemanager = FileManager.default

/// The base folder url `~/.codeedit/`
private var baseURL: URL {
filemanager.homeDirectoryForCurrentUser.appendingPathComponent(".codeedit")
}

/// The URL of the `themes` folder
internal var themesURL: URL {
baseURL.appendingPathComponent("themes", isDirectory: true)
}
}

0 comments on commit 29470d9

Please sign in to comment.