Skip to content
This repository has been archived by the owner on Oct 17, 2024. It is now read-only.

Commit

Permalink
Fix onAppear(_:) and onUpdate(_:)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-swift committed Nov 29, 2023
1 parent 1db1513 commit 5ab9387
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 25 deletions.
1 change: 0 additions & 1 deletion Documentation/Reference/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
- [Submenu](structs/Submenu.md)
- [Text](structs/Text.md)
- [ToolbarView](structs/ToolbarView.md)
- [UpdateObserver](structs/UpdateObserver.md)
- [VStack](structs/VStack.md)
- [Window](structs/Window.md)

Expand Down
12 changes: 3 additions & 9 deletions Documentation/Reference/extensions/View.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ Add a style class to the view.
- Parameter style: The style class.
- Returns: A view.

### `onAppear(_:)`
### `onUpdate(_:)`

Run a function when the view appears for the first time.
- Parameter closure: The function.
Run a function when the view gets an update.
- Parameter onUpdate: The function.
- Returns: A view.

### `stopModifiers()`
Expand All @@ -128,9 +128,3 @@ Add a bottom toolbar to the view.
- toolbar: The toolbar's content.
- visible: Whether the toolbar is visible.
- Returns: A view.

### `onUpdate(_:)`

Run a function when the view gets an update.
- Parameter onUpdate: The function.
- Returns: A view.
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
//
// UpdateObserver.swift
// AppearObserver.swift
// Adwaita
//
// Created by david-swift on 10.09.23.
// Created by david-swift on 29.11.23.
//

import Libadwaita

/// A widget which executes a custom code when being updated.
struct UpdateObserver: Widget {
/// A widget which executes a custom code when being rendered for the first time.
struct AppearObserver: Widget {

/// The function.
var onUpdate: () -> Void
var onAppear: () -> Void
/// The content.
var content: View

/// Get the content's container.
/// - Parameter modifiers: Modify views before being updated.
/// - Returns: The content's container.
func container(modifiers: [(View) -> View]) -> ViewStorage {
onUpdate()
onAppear()
return content.storage(modifiers: modifiers)
}

Expand All @@ -28,19 +28,18 @@ struct UpdateObserver: Widget {
/// - storage: The content's storage.
/// - modifiers: Modify views before being updated.
func update(_ storage: ViewStorage, modifiers: [(View) -> View]) {
onUpdate()
content.updateStorage(storage, modifiers: modifiers)
}

}

extension View {

/// Run a function when the view gets an update.
/// - Parameter onUpdate: The function.
/// Run a function when the view appears for the first time.
/// - Parameter closure: The function.
/// - Returns: A view.
public func onUpdate(_ onUpdate: @escaping () -> Void) -> View {
UpdateObserver(onUpdate: onUpdate, content: self)
public func onAppear(_ closure: @escaping () -> Void) -> View {
AppearObserver(onAppear: closure, content: self)
}

}
8 changes: 4 additions & 4 deletions Sources/Adwaita/View/Modifiers/InspectorWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ extension View {
inspect { _ = $0?.addStyle(style) }
}

/// Run a function when the view appears for the first time.
/// - Parameter closure: The function.
/// Run a function when the view gets an update.
/// - Parameter onUpdate: The function.
/// - Returns: A view.
public func onAppear(_ closure: @escaping () -> Void) -> View {
inspect { _ in closure() }
public func onUpdate(_ onUpdate: @escaping () -> Void) -> View {
inspect { _ in onUpdate() }
}

}

0 comments on commit 5ab9387

Please sign in to comment.