-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
96910e4
commit ca6d896
Showing
11 changed files
with
137 additions
and
111 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 was deleted.
Oops, something went wrong.
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,48 @@ | ||
#if os(iOS) | ||
import UIKit | ||
typealias OSView = UIView | ||
#elseif os(macOS) | ||
import AppKit | ||
typealias OSView = NSView | ||
#endif | ||
|
||
final class Remakeable<Content: OSView>: OSView { | ||
private(set) var wrappedValue: Content { | ||
didSet { | ||
action?(wrappedValue) | ||
} | ||
} | ||
private let content: () -> Content | ||
private var action: ((Content) -> Void)? | ||
|
||
init(content: @escaping () -> Content) { | ||
self.content = content | ||
wrappedValue = content() | ||
super.init(frame: .zero) | ||
addSubview(wrappedValue) | ||
setConstraints() | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
func remake() { | ||
wrappedValue.removeFromSuperview() | ||
wrappedValue = content() | ||
addSubview(wrappedValue) | ||
setConstraints() | ||
} | ||
|
||
func onRemake(perform action: @escaping (Content) -> Void) { | ||
self.action = action | ||
} | ||
|
||
private func setConstraints() { | ||
wrappedValue.translatesAutoresizingMaskIntoConstraints = false | ||
wrappedValue.topAnchor.constraint(equalTo: topAnchor).isActive = true | ||
wrappedValue.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true | ||
wrappedValue.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true | ||
wrappedValue.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true | ||
} | ||
} |
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
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
Oops, something went wrong.