Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
* Better documentation
* Rework names
* Tweak implementations
  • Loading branch information
jordanbaird committed Sep 23, 2024
1 parent 7a6de7f commit 6d60ab8
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Ice/MenuBar/Search/MenuBarSearchPanel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private final class MenuBarSearchHostingView: NSHostingView<AnyView> {
rootView: MenuBarSearchContentView(closePanel: closePanel)
.environmentObject(appState.itemManager)
.environmentObject(appState.imageCache)
.erased()
.erasedToAnyView()
)
}

Expand Down
4 changes: 2 additions & 2 deletions Ice/Settings/SettingsPanes/GeneralSettingsPane.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct GeneralSettingsPane: View {
@EnvironmentObject var appState: AppState
@State private var isImportingCustomIceIcon = false
@State private var isPresentingError = false
@State private var presentedError: AnyLocalizedError?
@State private var presentedError: LocalizedErrorWrapper?
@State private var isApplyingOffset = false
@State private var tempItemSpacingOffset: CGFloat = 0 // Temporary state for the slider

Expand Down Expand Up @@ -149,7 +149,7 @@ struct GeneralSettingsPane: View {
manager.iceIcon = ControlItemImageSet(name: .custom, image: .data(data))
}
} catch {
presentedError = AnyLocalizedError(error: error)
presentedError = LocalizedErrorWrapper(error)
isPresentingError = true
}
}
Expand Down
2 changes: 1 addition & 1 deletion Ice/UI/IceBar/IceBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ private final class IceBarHostingView: NSHostingView<AnyView> {
.environmentObject(appState.itemManager)
.environmentObject(appState.menuBarManager)
.environmentObject(colorManager)
.erased()
.erasedToAnyView()
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//
// Erased.swift
// ErasedToAnyView.swift
// Ice
//

import SwiftUI

extension View {
/// Returns a view that has been erased to the `AnyView` type.
func erased() -> AnyView {
func erasedToAnyView() -> AnyView {
AnyView(erasing: self)
}
}
7 changes: 2 additions & 5 deletions Ice/UI/ViewModifiers/LayoutBarStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ import SwiftUI
extension View {
/// Returns a view that is drawn in the style of a layout bar.
///
/// - Note: The view this modifier is applied to must be transparent,
/// or the style will be drawn incorrectly.
///
/// - Parameter appState: The shared app state.
@MainActor
/// - Note: The view this modifier is applied to must be transparent, or the style
/// will be drawn incorrectly.
@ViewBuilder
func layoutBarStyle(appState: AppState, averageColorInfo: MenuBarAverageColorInfo?) -> some View {
background {
Expand Down
16 changes: 9 additions & 7 deletions Ice/UI/ViewModifiers/ReadWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ private struct WindowReader: NSViewRepresentable {
final class Coordinator: ObservableObject {
private var cancellable: AnyCancellable?

func configure(for view: NSView, onWindowChange: @escaping (NSWindow?) -> Void) {
cancellable = view.publisher(for: \.window)
.receive(on: DispatchQueue.main)
.sink(receiveValue: onWindowChange)
func configure(for view: NSView, onWindowChange: @MainActor @escaping (NSWindow?) -> Void) {
cancellable = view.publisher(for: \.window).sink { window in
Task { @MainActor in
onWindowChange(window)
}
}
}
}

let onWindowChange: (NSWindow?) -> Void
let onWindowChange: @MainActor (NSWindow?) -> Void

func makeNSView(context: Context) -> NSView {
let view = NSView()
Expand All @@ -28,7 +30,7 @@ private struct WindowReader: NSViewRepresentable {
}

func makeCoordinator() -> Coordinator {
Coordinator()
return Coordinator()
}

func updateNSView(_: NSView, context: Context) { }
Expand All @@ -39,7 +41,7 @@ extension View {
/// the window changes.
///
/// - Parameter onChange: A closure to perform when the window changes.
func readWindow(onChange: @escaping (NSWindow?) -> Void) -> some View {
func readWindow(onChange: @MainActor @escaping (_ window: NSWindow?) -> Void) -> some View {
background {
WindowReader(onWindowChange: onChange)
}
Expand Down
1 change: 1 addition & 0 deletions Ice/UI/Views/BetaBadge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import SwiftUI

/// A view that displays a badge indicating a beta feature.
struct BetaBadge: View {
var body: some View {
Text("BETA")
Expand Down
14 changes: 7 additions & 7 deletions Ice/Updates/UpdatesManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import SwiftUI

/// Manager for app updates.
final class UpdatesManager: ObservableObject {
/// A Boolean value that indicates whether the user can
/// check for updates.
/// A Boolean value that indicates whether the user can check for updates.
@Published var canCheckForUpdates = false

/// The date of the last update check.
Expand All @@ -23,8 +22,7 @@ final class UpdatesManager: ObservableObject {
updaterController.updater
}

/// A Boolean value that indicates whether to automatically
/// check for updates.
/// A Boolean value that indicates whether to automatically check for updates.
var automaticallyChecksForUpdates: Bool {
get {
updater.automaticallyChecksForUpdates
Expand All @@ -35,8 +33,7 @@ final class UpdatesManager: ObservableObject {
}
}

/// A Boolean value that indicates whether to automatically
/// download updates.
/// A Boolean value that indicates whether to automatically download updates.
var automaticallyDownloadsUpdates: Bool {
get {
updater.automaticallyDownloadsUpdates
Expand All @@ -47,6 +44,7 @@ final class UpdatesManager: ObservableObject {
}
}

/// Creates an updates manager.
init() {
self.updaterController = SPUStandardUpdaterController(
startingUpdater: true,
Expand All @@ -56,16 +54,18 @@ final class UpdatesManager: ObservableObject {
configureCancellables()
}

/// Configures the internal observers for the manager.
private func configureCancellables() {
updater.publisher(for: \.canCheckForUpdates)
.assign(to: &$canCheckForUpdates)
updater.publisher(for: \.lastUpdateCheckDate)
.assign(to: &$lastUpdateCheckDate)
}

/// Checks for app updates.
@objc func checkForUpdates() {
#if DEBUG
// checking for updates hangs in debug mode
// Checking for updates hangs in debug mode.
let alert = NSAlert()
alert.messageText = "Checking for updates is not supported in debug mode."
alert.runModal()
Expand Down
11 changes: 5 additions & 6 deletions Ice/Utilities/BindingExposable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import SwiftUI

/// A type that acts as a lens that exposes bindings to the
/// writable properties of a base object.
/// A type that acts as a lens that exposes bindings to the writable properties
/// of a base object.
@dynamicMemberLookup
struct ExposedBindings<Base: BindingExposable> {
/// The base object whose bindings are exposed.
Expand All @@ -29,12 +29,11 @@ struct ExposedBindings<Base: BindingExposable> {

/// A type that exposes its writable properties as bindings.
protocol BindingExposable {
/// A type that acts as a lens that exposes bindings to the
/// writable properties of this type.
/// A type that acts as a lens that exposes bindings to the writable properties
/// of this type.
typealias Bindings = ExposedBindings<Self>

/// A lens that exposes bindings to the writable properties
/// of this instance.
/// A lens that exposes bindings to the writable properties of this instance.
var bindings: Bindings { get }
}

Expand Down
2 changes: 2 additions & 0 deletions Ice/Utilities/IconResource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ enum IconResource: Hashable {
case assetCatalog(_ resource: ImageResource)

/// The view produced by the resource.
@ViewBuilder
var view: some View {
image
.resizable()
.aspectRatio(contentMode: .fit)
}

/// The image produced by the resource.
private var image: Image {
switch self {
case .systemSymbol(let name):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// AnyLocalizedError.swift
// LocalizedErrorWrapper.swift
// Ice
//

Expand All @@ -9,14 +9,15 @@ import Foundation
///
/// If the error used to initialize the box is also a `LocalizedError`, its
/// information is passed through to the box. Otherwise, a description of the
/// error is passed to the box.
struct AnyLocalizedError: LocalizedError {
/// error is passed to the wrapper.
struct LocalizedErrorWrapper: LocalizedError {
let errorDescription: String?
let failureReason: String?
let helpAnchor: String?
let recoverySuggestion: String?

init(error: any Error) {
/// Creates a wrapper with the given error.
init(_ error: any Error) {
if let error = error as? any LocalizedError {
self.errorDescription = error.errorDescription
self.failureReason = error.failureReason
Expand Down

0 comments on commit 6d60ab8

Please sign in to comment.