Skip to content

Commit

Permalink
fix: Present an error if folder settings fail to decode (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbmorley authored Feb 26, 2024
1 parent 7b46f0e commit 59b6eb7
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 19 deletions.
4 changes: 4 additions & 0 deletions Folders.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
D8F349B52B8150690037D66A /* UTType.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8F349B42B8150690037D66A /* UTType.swift */; };
D8F6A6092B8D3D7E0003B1A6 /* Yams in Frameworks */ = {isa = PBXBuildFile; productRef = D8F6A6082B8D3D7E0003B1A6 /* Yams */; };
D8F6A60B2B8D41F90003B1A6 /* yams-license in Resources */ = {isa = PBXBuildFile; fileRef = D8F6A60A2B8D41F90003B1A6 /* yams-license */; };
D8F6A60D2B8D46720003B1A6 /* FolderSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8F6A60C2B8D46720003B1A6 /* FolderSettings.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -117,6 +118,7 @@
D8ED594E2B687FE800010FB3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = "<group>"; };
D8F349B42B8150690037D66A /* UTType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UTType.swift; sourceTree = "<group>"; };
D8F6A60A2B8D41F90003B1A6 /* yams-license */ = {isa = PBXFileReference; lastKnownFileType = text; path = "yams-license"; sourceTree = "<group>"; };
D8F6A60C2B8D46720003B1A6 /* FolderSettings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FolderSettings.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -273,6 +275,7 @@
D89622DA2ACD394A006F7D2E /* ApplicationModel.swift */,
D83312E82B77132B00B994B3 /* Details.swift */,
D870EC9D2B7EC47B00704688 /* FolderModel.swift */,
D8F6A60C2B8D46720003B1A6 /* FolderSettings.swift */,
D8DDBCC32B2A0F8E003EAF4E /* PreviewItem.swift */,
D89622E52ACD42F2006F7D2E /* SceneModel.swift */,
D89622DD2ACD3B82006F7D2E /* Settings.swift */,
Expand Down Expand Up @@ -452,6 +455,7 @@
D89622E22ACD4200006F7D2E /* Sidebar.swift in Sources */,
D870EC9E2B7EC47B00704688 /* FolderModel.swift in Sources */,
D8472A682B2518320070DB64 /* FixedItemSizeCollectionViewLayout.swift in Sources */,
D8F6A60D2B8D46720003B1A6 /* FolderSettings.swift in Sources */,
D8472A6E2B2518D00070DB64 /* InteractiveCollectionView.swift in Sources */,
D83312E92B77132B00B994B3 /* Details.swift in Sources */,
D83312E32B76FE4E00B994B3 /* Filter.swift in Sources */,
Expand Down
23 changes: 4 additions & 19 deletions Folders/Models/FolderModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,26 @@

import SwiftUI

import Yams

struct FolderSettings: Codable {

struct Link: Codable {
let url: URL
let title: String?
}

let links: [Link]

}

class FolderModel: ObservableObject {

let url: URL

@Published var settings: FolderSettings?
@Published var error: Error?

init(url: URL) {
self.url = url
}

func start() {
let settingsURL = url.appendingPathComponent("folders-settings.yaml") // TODO: Could I get macOS to hide these for me?
let settingsURL = url.appendingPathComponent("folders-settings.yaml")
guard FileManager.default.fileExists(atPath: settingsURL.path) else {
return
}
do {
let data = try Data(contentsOf: settingsURL)
let decoder = YAMLDecoder()
let settings = try decoder.decode(FolderSettings.self, from: data)
self.settings = settings
self.settings = try FolderSettings(contentsOf: settingsURL)
} catch {
print("Failed to load folder settings with error \(error).")
self.error = error
}
}

Expand Down
42 changes: 42 additions & 0 deletions Folders/Models/FolderSettings.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// MIT License
//
// Copyright (c) 2023-2024 Jason Morley
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import SwiftUI

import Yams

struct FolderSettings: Codable {

struct Link: Codable {
let url: URL
let title: String?
}

let links: [Link]

init(contentsOf url: URL) throws {
let data = try Data(contentsOf: url)
let decoder = YAMLDecoder()
self = try decoder.decode(FolderSettings.self, from: data)
}

}
1 change: 1 addition & 0 deletions Folders/Views/FolderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ struct FolderView: View {
}
}
.navigationTitle(url.displayName)
.presents($folderModel.error)
.onAppear {
folderModel.start()
}
Expand Down

0 comments on commit 59b6eb7

Please sign in to comment.