-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(settings): section with glob pattern crud (#1507)
* feat: setup search section in app settings * feat: list for ignore glob patterns * feat: open/close modal for add glob pattern * feat: introduce view model for search settings glob pattern * fix: method name * feat: use list to render values * feat: writing to sottings json * feat: persist settings and load from settings * feat: capability to remove selection * fix: editable table lines * fix: fmt * feat: check list size before removing * Update CodeEdit/Features/Settings/Pages/SearchSettings/Models/SearchSettings.swift Co-authored-by: Tom Ludwig <[email protected]> * fix: pr review items * Update CodeEdit/Features/Settings/Pages/SearchSettings/SearchSettingsView.swift Co-authored-by: Tom Ludwig <[email protected]> --------- Co-authored-by: Tom Ludwig <[email protected]>
- Loading branch information
1 parent
a7ee1c2
commit acc3d4b
Showing
10 changed files
with
372 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
CodeEdit/Features/Settings/Pages/SearchSettings/IgnorePatternListItemView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// | ||
// IgnorePatternListItemView.swift | ||
// CodeEdit | ||
// | ||
// Created by Esteban on 2/2/24. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct IgnorePatternListItem: View { | ||
@Binding var pattern: GlobPattern | ||
@Binding var selectedPattern: GlobPattern? | ||
var addPattern: () -> Void | ||
var removePattern: (GlobPattern) -> Void | ||
var focusedField: FocusState<String?>.Binding | ||
var isLast: Bool | ||
|
||
@State var value: String | ||
|
||
@FocusState private var isFocused: Bool | ||
|
||
init( | ||
pattern: Binding<GlobPattern>, | ||
selectedPattern: Binding<GlobPattern?>, | ||
addPattern: @escaping () -> Void, | ||
removePattern: @escaping (GlobPattern) -> Void, | ||
focusedField: FocusState<String?>.Binding, | ||
isLast: Bool | ||
) { | ||
self._pattern = pattern | ||
self._selectedPattern = selectedPattern | ||
self.addPattern = addPattern | ||
self.removePattern = removePattern | ||
self.focusedField = focusedField | ||
self.isLast = isLast | ||
self._value = State(initialValue: pattern.wrappedValue.value) | ||
} | ||
|
||
var body: some View { | ||
TextField("", text: $value) | ||
.focused(focusedField, equals: pattern.id.uuidString) | ||
.focused($isFocused) | ||
.disableAutocorrection(true) | ||
.autocorrectionDisabled() | ||
.labelsHidden() | ||
.onSubmit { | ||
if !value.isEmpty && isLast { | ||
addPattern() | ||
} | ||
} | ||
.onChange(of: isFocused) { newIsFocused in | ||
if newIsFocused { | ||
if selectedPattern != pattern { | ||
selectedPattern = pattern | ||
} | ||
} else { | ||
if value.isEmpty { | ||
removePattern(pattern) | ||
} else { | ||
pattern.value = value | ||
} | ||
} | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
CodeEdit/Features/Settings/Pages/SearchSettings/Models/SearchSettings.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// SearchSettings.swift | ||
// CodeEdit | ||
// | ||
// Created by Esteban on 12/10/23. | ||
// | ||
|
||
import Foundation | ||
|
||
extension SettingsData { | ||
struct SearchSettings: Codable, Hashable, SearchableSettingsPage { | ||
|
||
/// The search keys | ||
var searchKeys: [String] { | ||
[ | ||
"Ignore Glob Patterns", | ||
"Ignore Patterns" | ||
] | ||
.map { NSLocalizedString($0, comment: "") } | ||
} | ||
|
||
/// List of Glob Patterns that determine which files or directories to ignore | ||
var ignoreGlobPatterns: [GlobPattern] = .init() | ||
|
||
/// Default initializer | ||
init() {} | ||
|
||
/// Explicit decoder init for setting default values when key is not present in `JSON` | ||
init(from decoder: Decoder) throws { | ||
let container = try decoder.container(keyedBy: CodingKeys.self) | ||
|
||
self.ignoreGlobPatterns = try container.decodeIfPresent( | ||
[GlobPattern].self, | ||
forKey: .ignoreGlobPatterns | ||
) ?? [] | ||
} | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
CodeEdit/Features/Settings/Pages/SearchSettings/Models/SearchSettingsModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// | ||
// SearchSettingsModel.swift | ||
// CodeEdit | ||
// | ||
// Created by Esteban on 12/10/23. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct GlobPattern: Identifiable, Hashable, Decodable, Encodable { | ||
/// Ephimeral UUID used to track its representation in the UI | ||
var id = UUID() | ||
|
||
/// The Glob Pattern to render | ||
var value: String | ||
} | ||
|
||
/// The Search Settings View Model. Accessible via the singleton "``SearchSettings/shared``". | ||
/// | ||
/// **Usage:** | ||
/// ```swift | ||
/// @StateObject | ||
/// private var searchSettigs: SearchSettingsModel = .shared | ||
/// ``` | ||
final class SearchSettingsModel: ObservableObject { | ||
/// Reads settings file for Search Settings and updates the values in this model | ||
/// correspondingly | ||
private init() { | ||
let value = Settings[\.search].ignoreGlobPatterns | ||
self.ignoreGlobPatterns = value | ||
} | ||
|
||
static let shared: SearchSettingsModel = .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 `search` folder | ||
internal var searchURL: URL { | ||
baseURL.appendingPathComponent("search", isDirectory: true) | ||
} | ||
|
||
/// The URL of the `Extensions` folder | ||
internal var extensionsURL: URL { | ||
baseURL.appendingPathComponent("Extensions", isDirectory: true) | ||
} | ||
|
||
/// The URL of the `settings.json` file | ||
internal var settingsURL: URL { | ||
baseURL.appendingPathComponent("settings.json", isDirectory: true) | ||
} | ||
|
||
/// Stores the new values from the Search Settings Model into the settings.json whenever | ||
/// `ignoreGlobPatterns` is updated | ||
@Published var ignoreGlobPatterns: [GlobPattern] { | ||
didSet { | ||
DispatchQueue.main.async { | ||
Settings[\.search].ignoreGlobPatterns = self.ignoreGlobPatterns | ||
} | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...Edit/Features/Settings/Pages/SearchSettings/SearchSettingsIgnoreGlobPatternItemView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// SearchSettingsIgnoreGlobPatternItemView.swift | ||
// CodeEdit | ||
// | ||
// Created by Esteban on 12/10/23. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct SearchSettingsIgnoreGlobPatternItemView: View { | ||
@Binding var globPattern: String | ||
|
||
var body: some View { | ||
Text(globPattern) | ||
} | ||
} |
Oops, something went wrong.