Skip to content

Commit

Permalink
OSS-15 Initial submit.
Browse files Browse the repository at this point in the history
  • Loading branch information
sakamura committed Mar 4, 2020
0 parents commit 51aeb8f
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
7 changes: 7 additions & 0 deletions .swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -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>
25 changes: 25 additions & 0 deletions LICENSE.md
@@ -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.
29 changes: 29 additions & 0 deletions Package.swift
@@ -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: [])
]
)
59 changes: 59 additions & 0 deletions README.md
@@ -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/
16 changes: 16 additions & 0 deletions Sources/MisoViewOptional/View+optional.swift
@@ -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)
}
}

0 comments on commit 51aeb8f

Please sign in to comment.