Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dynamic Environments ¶ SwiftUI Rules #284

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

ericchuhong
Copy link

@ericchuhong ericchuhong commented Dec 22, 2019

这篇文章对应的 Issue 链接是:
#263
本文的大致阅读时长:[ 15 ] 分钟。

PR Checklist:

请校对同学帮忙校对。

Sorry, something went wrong.

@ericchuhong ericchuhong changed the title 【feature】新增翻译 Dynamic Environments ¶ SwiftUI Rules Dec 23, 2019

> [EnvironmentValues](https://developer.apple.com/documentation/swiftui/environmentvalues) 这个结构体代表了视图当前使用中的值。开发者不能直接访问到它底层的数据。想要访问的当前状态,下标值需要用 **type** 作为 key 。

所以我们应该如何使用它呢?还是跟之前展示过的一样!有的 View 通过使用 [@Environment](https://developer.apple.com/documentation/swiftui/environment) 属性封装器访问了我们新开发的优秀 key —— `fancyColor`:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有的 -> 让某个

}
```

并且由一个 View 提供了它:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

另一个 View 来提供它的值

}
```

顺着视图层次结构传递值显得简单并相当高效。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

显得简单并相当高效 -> 多么简单和高效


顺着视图层次结构传递值显得简单并相当高效。

> 那如果是 [EnvironmentObject](https://developer.apple.com/documentation/swiftui/environmentobject) 呢?它们和常规的 environment 值很相似,但他们也规定了相应的值需要是一个遵循了 ObservableObject 协议的类。要做的这些事情对一些简单的行限制或者颜色限制规则来说,显得有点太多了。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

但他们也规定了相应的值需要是一个遵循了 ObservableObject 协议的类 -> 但规定了对应值的类型需要遵循 ObservableObject 协议


# SwiftUI 规则

这已经是个相当棒而且强大的概念了!但目前为止 environment key 仍需要有静态值的支持。它们需要显式地推入到当前那个用 `.environment` 视图封装器(如果为空则是 `defaultValue` )的 enviroment 中。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这已经是个相当棒而且强大的概念了 -> 这个概念已经……


# SwiftUI 规则

这已经是个相当棒而且强大的概念了!但目前为止 environment key 仍需要有静态值的支持。它们需要显式地推入到当前那个用 `.environment` 视图封装器(如果为空则是 `defaultValue` )的 enviroment 中。
Copy link
Collaborator

@Nemocdz Nemocdz Dec 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

第二句话不通顺,它们需要通过xxx来赋值到当前的xxx


这已经是个相当棒而且强大的概念了!但目前为止 environment key 仍需要有静态值的支持。它们需要显式地推入到当前那个用 `.environment` 视图封装器(如果为空则是 `defaultValue` )的 enviroment 中。

如果我们能够避免 `.environment(\.fancyColor, .red)` 这样的声明,然后基于其他 environment key 的值,定义我们自己的 `fancyColor`。甚至可以 **制定** 如何从其他 key 值衍生的规则。**欢迎来到 [SwiftUI 规则](https://github.com/DirectToSwift/SwiftUIRules)**:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果……呢?这甚至……


### Environment Key

**environment key** 适用于我们例子中的 `fancyColor`。那意味着如果一个 View 想要 `fancyColor` 环境值,规则引擎将会查找每一个规则,看哪一个可以应用到那个 key。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

查找每一个规则,看哪一个可以应用到那个 key -> 找到规则中xxxx


大概意思就是:给导航栏使用这个标题。这个标题是待办的标题。除非待办的是 "IMPORTANT",我们才用 "Can wait." 重写它。

详细讲讲过程中发生了的事情:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

了 去掉

Copy link
Collaborator

@Nemocdz Nemocdz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

一次校对

详细讲讲过程中发生了的事情:

1. `PageView` 中的 `@Environment` 想获取 `navigationBarTitle`,
2. 规则系统查看了模型,并给标题查找到了这个规则:`\.navigationBarTitle <= \.title`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

并给标题查找到了这个规则 -> 并找到了标题的这个规则


1. `PageView` 中的 `@Environment` 想获取 `navigationBarTitle`,
2. 规则系统查看了模型,并给标题查找到了这个规则:`\.navigationBarTitle <= \.title`,
3. 规则系统向自己请求 `title` 的值,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

向自己 -> 从自身

6. 它去查找了在 environment 中的 `todo` 对象,然后将它标题和常量 “IMPORTANT” 对比。假若情况是谓词匹配到了,规则就会被使用,`title` 的值将会是 `“Can wait.”`。
7. 规则系统现在决策出 `title` 的值 - `"Can wait."`,然后返回给 `navigationBarTitle`,并把值推入给 `PageView`。

要留意的关键点是一个规则可以通过其他规则中声明。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

从……中/通过……来


# 使用 SwiftUI 规则

这个 [仓库](https://github.com/DirectToSwift/SwiftUIRules) 主要是以在
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

主要是以……为主要组成 不通顺!


这个 [仓库](https://github.com/DirectToSwift/SwiftUIRules) 主要是以在
[`Samples`](https://github.com/DirectToSwift/SwiftUIRules/tree/develop/Samples/)
子目录下一个很小型的 [示例应用](https://github.com/DirectToSwift/SwiftUIRules/tree/develop/Samples/RulesTestApp) 为主要组成,去看看吧。它是一个没有条理可言的示例,但是展示了如何设置和运行整个架构。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

虽然是个没什么条理的

]
```

你可以在 SwiftUI 视图层次架构中的任何地方拦截注入到规则系统,但我们再一次推荐在每个最上层的视图中做。例如,在 Xcode 中新生成一个应用,你可以给生成的 `ContentView` 做如下的修改:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

拦截注入到规则系统 -> 拦截注入规则系统

]
```

你可以在 SwiftUI 视图层次架构中的任何地方拦截注入到规则系统,但我们再一次推荐在每个最上层的视图中做。例如,在 Xcode 中新生成一个应用,你可以给生成的 `ContentView` 做如下的修改:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

再一次 -> 依旧

```swift
\.todos.count > 10 => \.person.status <= "VIP"
```
也就是赋值给一个多组合关键路径(`\.person.status`)。那 **运行不了** 的。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这样是无法运行的


## 联系方式

嘿!我们希望你喜欢这篇文章,并且我们喜欢反馈!Twitter,或他们任一一位:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这下面任意一个的 Twitter

Copy link
Collaborator

@Nemocdz Nemocdz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还有一些小问题

}
```

请注意 environment 可以在任意一视图层次中 _变换_:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

变换 这里不需要空格

```
这样就可以了。我们可以开始使用我们的新 key 了。

> [EnvironmentValues](https://developer.apple.com/documentation/swiftui/environmentvalues) 这个结构体代表了视图当前使用中的值。开发者不能直接访问到它底层的数据。想要访问的当前状态,下标值需要用 **type** 作为 key 。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

key 后面多了一个空格

]
```

首先这里假设当前 environment 带有一个 `todo` 的 environment key,而且这个 environment key 持有一个 todo 对象。然后我们定义了值 `fancyColor` 将带有 todo 的 priority 值。通过用 **规则**,我们 **声明** 了它。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

中文之间不需要空格

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

下文同


# 使用 SwiftUI 规则

这个 [仓库](https://github.com/DirectToSwift/SwiftUIRules) 的主要组成是在
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

第一句话翻译不对,不通顺,这个仓库在 Sample 子文件夹中提供了一个小型的示例应用


## 用例

哈!没完没了的 🤓 它和 “Think In Rules”™(又称声明式)相当不同,但是他们允许你使用一种高度解耦的方式组合你的应用,实际上就是“声明式”方式。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

第一句不太通顺

\.lineLimit <= 2
]
```
**还不能用**。这是当下依赖系统使用的键来显式创建才有的 [`DynamicEnvironmentKey`](https://github.com/DirectToSwift/SwiftUIRules/blob/develop/Sources/SwiftUIRules/DynamicEnvironment/DynamicEnvironmentKey.swift#L17) 类型。所以你没办法立刻就用上。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还不能用 -> 这没有效果

@ericchuhong
Copy link
Author

@Nemocdz 已修改,请校对。不好意思,拖了这么久。


## 声明自己的 Environment 键

SwiftUI environments 对内置键的数量是不限制的,你可以添加自己的键。除了 `foregroundColor` 之外,我们如果说想要添加一个自己的 environment 键,叫做 `fancyColor`。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我们如果说想要添加一个自己的 => 假设我们想添加一个

}
}
```
这样就可以了。我们可以开始使用我们的新 key 了。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我们可以开始使用我们的 => 可以开始使用


这个概念已经是相当棒而且强大了!但目前为止 environment key 仍需要有静态值的支持。它们需要通过 `.environment` 视图封装器(如果为空则是 `defaultValue` )来赋值到当前的 enviroment。

如果我们能够避免 `.environment(\.fancyColor, .red)` 这样的声明,然后基于其他 environment key 的值定义我们自己的 `fancyColor` 呢?这甚至可以**制定**如何从其他 key 值衍生的规则。**欢迎来到 [SwiftUI 规则](https://github.com/DirectToSwift/SwiftUIRules)**:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

去掉第一个”我们”

]
```

但它也可以被用得很高级,例如在一个工作流系统:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

但它也可以被用得很高级 => 但它也有一些高级用法

]

struct TaskView: View {
@Environment(\.view) var body // body derived from rules
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

翻译注释

}
```

因为 SwiftUI Views 也只是一个轻量的结构体,你可以构建动态的属性携带它们!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这段漏掉了很多内容,对照原文重新翻译。


# 总结

我们希望你喜欢它!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

漏掉一段

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这一段还是他们后面更新的 😂


我们希望你喜欢它!

更新:也同时检出我们的新版 [Direct to SwiftUI](http://www.alwaysrightinstitute.com/directtoswiftui/),它不管在什么时候都用 SwiftUI 构建了精美的数据库前端。
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. 检出?机翻?
  2. 读不通 修改

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants