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
0 parents
commit 51aeb8f
Showing
7 changed files
with
149 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
/*.xcodeproj | ||
xcuserdata/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
.swiftpm/xcode/package.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
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,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>IDEDidComputeMac32BitWarning</key> | ||
<true/> | ||
</dict> | ||
</plist> |
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,25 @@ | ||
# Boost Software License - Version 1.0 | ||
|
||
Boost Software License - Version 1.0 - August 17th, 2003 | ||
|
||
Permission is hereby granted, free of charge, to any person or organization | ||
obtaining a copy of the software and accompanying documentation covered by | ||
this license (the "Software") to use, reproduce, display, distribute, | ||
execute, and transmit the Software, and to prepare derivative works of the | ||
Software, and to permit third-parties to whom the Software is furnished to | ||
do so, all subject to the following: | ||
|
||
The copyright notices in the Software and this entire statement, including | ||
the above license grant, this restriction and the following disclaimer, | ||
must be included in all copies of the Software, in whole or in part, and | ||
all derivative works of the Software, unless such copies or derivative | ||
works are solely in the form of machine-executable object code generated by | ||
a source language processor. | ||
|
||
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, TITLE AND NON-INFRINGEMENT. IN NO EVENT | ||
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE | ||
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, | ||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
DEALINGS IN THE SOFTWARE. |
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,29 @@ | ||
// swift-tools-version:5.2 | ||
// The swift-tools-version declares the minimum version of Swift required to build this package. | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "MisoViewOptional", | ||
platforms: [.macOS(.v10_15), | ||
.iOS(.v13), | ||
.tvOS(.v13), | ||
.watchOS(.v6)], | ||
products: [ | ||
// Products define the executables and libraries produced by a package, and make them visible to other packages. | ||
.library( | ||
name: "MisoViewOptional", | ||
targets: ["MisoViewOptional"]), | ||
], | ||
dependencies: [ | ||
// Dependencies declare other packages that this package depends on. | ||
// .package(url: /* package url */, from: "1.0.0"), | ||
], | ||
targets: [ | ||
// Targets are the basic building blocks of a package. A target can define a module or a test suite. | ||
// Targets can depend on other targets in this package, and on products in packages which this package depends on. | ||
.target( | ||
name: "MisoViewOptional", | ||
dependencies: []) | ||
] | ||
) |
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,59 @@ | ||
# MisoViewOptional | ||
|
||
## SwiftUI package to allow for conditional compilation of modifiers | ||
|
||
This is the simplest Swift package ever. It's mostly a design pattern to allow for an optional modifier that returns an AnyView. | ||
|
||
### Why? | ||
|
||
When you compile something conditionally, sometimes, a modifier is not available in a platform. For example, macOS doesn't support `navigationBarItems`, and you cannot simply do | ||
|
||
``` | ||
VStack { | ||
Spacer() | ||
} | ||
#if !os(macOS) | ||
.navigationBarItems(...) | ||
#endif | ||
``` | ||
|
||
Also, the usual pattern of setting someting to nil doesn't actually work. | ||
|
||
There are also some other uses, especially when some modifiers don't support a `nil` default. You can `if` its usage completely away through the `.optional` modifier. | ||
|
||
### Why AnyView? | ||
|
||
Not only is this not hurting the performance, but also, due to Swift's lack of preprocessor, the `#if` is actually defined as a command. And because the `#else` is undefined, the system cannot determine what should be the return type. There might be a better design pattern, and please don't hesitate to provide a better implementation. | ||
|
||
## Usage | ||
|
||
``` | ||
import MisoViewOptional | ||
VStack { | ||
Spacer() | ||
} | ||
.frame(width: 123, height: 456) | ||
.optional() { content in | ||
#if os(macOS) | ||
return AnyView(content) | ||
#else | ||
return AnyView(content.navigationBarTitle("MyTitle") | ||
.navigationBarItems(trailing: doneButton)) | ||
#endif | ||
} | ||
``` | ||
|
||
## Colophon | ||
|
||
[The official address for this package][0] | ||
|
||
[The git / package url][1] | ||
|
||
This package is created and maintained by [Misoservices Inc.][2] and is [licensed under the BSL-1.0: Boost Software License - Version 1.0][3]. | ||
|
||
|
||
[0]: https://github.com/Misoservices/MisoViewOptional | ||
[1]: https://github.com/Misoservices/MisoViewOptional.git | ||
[2]: https://misoservices.com | ||
[3]: https://choosealicense.com/licenses/bsl-1.0/ |
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,16 @@ | ||
// | ||
// View+optional.swift | ||
// MisoViewOptional | ||
// | ||
// Created by Michel Donais on 2020-03-03. | ||
// Copyright © 2020 Misoservices Inc. All rights reserved. | ||
// [BSL-1.0] This package is Licensed under the Boost Software License - Version 1.0 | ||
// | ||
|
||
import SwiftUI | ||
|
||
public extension View { | ||
func optional( modifiers: (Self)->AnyView ) -> AnyView { | ||
modifiers(self) | ||
} | ||
} |