Skip to content

Commit

Permalink
feat: writing to sottings json
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoBorai committed Dec 20, 2023
1 parent e47e321 commit 4706282
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,20 @@ extension SettingsData {
}

/// List of Glob Patterns that determine which files or directories to ignore
var ignoreGlobPatterns: [String] = [
"Testing"
]
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 {
print("From decoder")
let container = try decoder.container(keyedBy: CodingKeys.self)

self.ignoreGlobPatterns = try container.decodeIfPresent(
[GlobPattern].self,
forKey: .ignoreGlobPatterns
) ?? []
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@

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:**
Expand Down Expand Up @@ -41,7 +49,7 @@ final class SearchSettingsModel: ObservableObject {
}

/// The currently existent Search Ignore Glob Patterns.
@Published var ignoreGlobPatterns: [String] {
@Published var ignoreGlobPatterns: [GlobPattern] {
didSet {
DispatchQueue.main.async {
Settings[\.search].ignoreGlobPatterns = self.ignoreGlobPatterns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,36 @@

import SwiftUI

struct GlobPattern: Identifiable, Hashable {
/// Ephimeral UUID used to track its representation in the UI
let id = UUID()

/// The Glob Pattern to render
var value: String
}

struct SearchSettingsView: View {

@AppSettings(\.search.ignoreGlobPatterns)
var ignoreGlobPatterns
@ObservedObject private var searchSettingsModel: SearchSettingsModel = .shared

@FocusState private var focusedField: String?
@State private var selection: Set<String> = []

@State var globPatterns: [GlobPattern] = []

init() {
globPatterns = ignoreGlobPatterns.map {
GlobPattern(value: $0)
}
}

func addIgnoreGlobPattern() {
globPatterns.append(.init(value: ""))
searchSettingsModel.ignoreGlobPatterns.append(GlobPattern(value: ""))
}

var body: some View {
SettingsForm {
Section {
List($globPatterns, id: \.self, selection: $selection) { $globPattern in
TextField("", text: $globPattern.value)
.focused($focusedField, equals: globPattern.id.uuidString)
List($searchSettingsModel.ignoreGlobPatterns, selection: $selection) { ignorePattern in
TextField("", text: ignorePattern.value)
.disableAutocorrection(true)
.autocorrectionDisabled()
.focused($focusedField, equals: ignorePattern.id.uuidString)
.labelsHidden()
.onAppear {
if globPatterns.isEmpty {
if $searchSettingsModel.ignoreGlobPatterns.isEmpty {
addIgnoreGlobPattern()
}
}
.onSubmit {
if globPattern.value.isEmpty {
print("Remove \(globPattern)")
if $searchSettingsModel.ignoreGlobPatterns.isEmpty {
print("Remove \(ignorePattern)")
} else {
if globPattern == globPatterns.last {
if ignorePattern.id == $searchSettingsModel.ignoreGlobPatterns.last?.id {
addIgnoreGlobPattern()
}
}
Expand All @@ -69,7 +54,7 @@ struct SearchSettingsView: View {
} label: {
Image(systemName: "minus")
}
.disabled(ignoreGlobPatterns.isEmpty)
.disabled($searchSettingsModel.ignoreGlobPatterns.isEmpty)
Spacer()
Button {
print("More")
Expand Down

0 comments on commit 4706282

Please sign in to comment.