Finish extraction

- Moves SwiftUI code out of Riot and into RiotSwiftUI which has no dependency on Matrix SDK.
- Git wasn't smart enough to see the file moves. Most feature function has remain unchanged. 1 change I did make was remove NotificationSettingsViewModel's dependence on MxPushRule, so that the view model could be moved into RiotSwiftUI.
- Add LocaleProvider to abstract VectorL10n's use of Matrix SDK language so it can be used in RiotSwiftUI.
- Split Theme into UKit/SwiftUI version to remove RiotSwiftUI's dependence on ThemeService and ThemeV1.
- Migrated from ThemeObserver to ThemePublisher. We push updates to ThemePublisher so that we can remove ThemeService as dependency.
- Add .DS_Store to .gitignore
This commit is contained in:
David Langley
2021-09-01 12:34:38 +01:00
parent 200fb69993
commit f4cb404df0
93 changed files with 921 additions and 259 deletions
@@ -0,0 +1,123 @@
//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import SwiftUI
/**
A bordered style of text input as defined in:
https://www.figma.com/file/X4XTH9iS2KGJ2wFKDqkyed/Compound?node-id=2039%3A26415
*/
@available(iOS 14.0, *)
struct BorderedInputFieldStyle: TextFieldStyle {
@Environment(\.theme) var theme: ThemeSwiftUI
@Environment(\.isEnabled) var isEnabled: Bool
var isEditing: Bool = false
var isError: Bool = false
private var borderColor: Color {
if !isEnabled {
return theme.colors.quinaryContent
} else if isError {
return theme.colors.alert
} else if isEditing {
return theme.colors.accent
}
return theme.colors.quarterlyContent
}
private var accentColor: Color {
if isError {
return theme.colors.alert
}
return theme.colors.accent
}
private var textColor: Color {
if !isEnabled {
return theme.colors.quarterlyContent
}
return theme.colors.primaryContent
}
private var backgroundColor: Color {
if !isEnabled && (theme.identifier == ThemeIdentifier.dark) {
return theme.colors.quinaryContent
}
return theme.colors.background
}
private var borderWidth: CGFloat {
return isEditing || isError ? 2 : 1.5
}
func _body(configuration: TextField<_Label>) -> some View {
let rect = RoundedRectangle(cornerRadius: 8)
return configuration
.font(theme.fonts.callout)
.foregroundColor(textColor)
.accentColor(accentColor)
.frame(height: 48)
.padding(.horizontal, 8)
.background(backgroundColor)
.clipShape(rect)
.overlay(rect.stroke(borderColor, lineWidth: borderWidth))
}
}
@available(iOS 14.0, *)
struct BorderedInputFieldStyle_Previews: PreviewProvider {
static var previews: some View {
Group {
VStack {
TextField("Placeholder", text: .constant(""))
.textFieldStyle(BorderedInputFieldStyle())
TextField("Placeholder", text: .constant(""))
.textFieldStyle(BorderedInputFieldStyle(isEditing: true))
TextField("Placeholder", text: .constant("Web"))
.textFieldStyle(BorderedInputFieldStyle())
TextField("Placeholder", text: .constant("Web"))
.textFieldStyle(BorderedInputFieldStyle(isEditing: true))
TextField("Placeholder", text: .constant("Web"))
.textFieldStyle(BorderedInputFieldStyle())
.disabled(true)
TextField("Placeholder", text: .constant("Web"))
.textFieldStyle(BorderedInputFieldStyle(isEditing: true, isError: true))
}
.padding()
VStack {
TextField("Placeholder", text: .constant(""))
.textFieldStyle(BorderedInputFieldStyle())
TextField("Placeholder", text: .constant(""))
.textFieldStyle(BorderedInputFieldStyle(isEditing: true))
TextField("Placeholder", text: .constant("Web"))
.textFieldStyle(BorderedInputFieldStyle())
TextField("Placeholder", text: .constant("Web"))
.textFieldStyle(BorderedInputFieldStyle(isEditing: true))
TextField("Placeholder", text: .constant("Web"))
.textFieldStyle(BorderedInputFieldStyle())
.disabled(true)
TextField("Placeholder", text: .constant("Web"))
.textFieldStyle(BorderedInputFieldStyle(isEditing: true, isError: true))
}
.padding()
.theme(ThemeIdentifier.dark)
}
}
}
@@ -0,0 +1,75 @@
//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import SwiftUI
/**
A single rounded rect chip to be rendered within `Chips` collection
*/
@available(iOS 14.0, *)
struct Chip: View {
@Environment(\.isEnabled) var isEnabled
@Environment(\.theme) var theme: ThemeSwiftUI
let title: String
let onDelete: () -> Void
var backgroundColor: Color {
if !isEnabled {
return theme.colors.quinaryContent
}
return theme.colors.accent
}
var foregroundColor: Color {
if !isEnabled {
return theme.colors.tertiaryContent
}
return Color.white
}
var body: some View {
HStack {
Text(title)
.font(theme.fonts.body)
.lineLimit(1)
Button(action: onDelete) {
Image(systemName: "xmark.circle.fill")
.frame(width: 16, height: 16, alignment: .center)
}
}
.padding(.leading, 12)
.padding(.top, 6)
.padding(.bottom, 6)
.padding(.trailing, 8)
.background(backgroundColor)
.foregroundColor(foregroundColor)
.cornerRadius(20)
}
}
@available(iOS 14.0, *)
struct Chip_Previews: PreviewProvider {
static var previews: some View {
Group {
Chip(title: "My great chip", onDelete: { })
Chip(title: "My great chip", onDelete: { })
.theme(.dark)
}
}
}
@@ -0,0 +1,91 @@
//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import SwiftUI
/**
Renders multiple chips in a flow layout.
*/
@available(iOS 14.0, *)
struct Chips: View {
@State private var frame: CGRect = CGRect.zero
let titles: [String]
let didDeleteChip: (String) -> Void
let verticalSpacing: CGFloat = 16
let horizontalSpacing: CGFloat = 12
var body: some View {
Group {
VStack {
var x = CGFloat.zero
var y = CGFloat.zero
GeometryReader { geo in
ZStack(alignment: .topLeading, content: {
ForEach(titles, id: \.self) { chip in
Chip(title: chip) {
didDeleteChip(chip)
}
.alignmentGuide(.leading) { dimension in
// Align with leading side and move vertically down to next line
// if chip does not fit on trailing side.
if abs(x - dimension.width) > geo.size.width {
x = 0
y -= dimension.height + verticalSpacing
}
let result = x
if chip == titles.last {
// Reset x if it's the last.
x = 0
} else {
// Align next chip to the end of the current one.
x -= dimension.width + horizontalSpacing
}
return result
}
.alignmentGuide(.top) { dimension in
// Use next y value and reset if its the last.
let result = y
if chip == titles.last {
y = 0
}
return result
}
}
})
.background(ViewFrameReader(frame: $frame))
}
}
.frame(height: frame.size.height)
}
}
}
@available(iOS 14.0, *)
struct Chips_Previews: PreviewProvider {
static var chips: [String] = ["Chip1", "Chip2", "Chip3", "Chip4", "Chip5", "Chip6"]
static var previews: some View {
Group {
Chips(titles: chips, didDeleteChip: { _ in })
Chips(titles: chips, didDeleteChip: { _ in })
.theme(.dark)
}
}
}
@@ -0,0 +1,67 @@
//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import SwiftUI
/**
Renders an input field and a collection of chips
with callbacks for addition and deletion.
*/
@available(iOS 14.0, *)
struct ChipsInput: View {
@Environment(\.theme) var theme: ThemeSwiftUI
@Environment(\.isEnabled) var isEnabled
@State private var chipText: String = ""
let titles: [String]
let didAddChip: (String) -> Void
let didDeleteChip: (String) -> Void
var placeholder: String = ""
var body: some View {
VStack(spacing: 16) {
TextField(placeholder, text: $chipText, onCommit: {
didAddChip(chipText)
chipText = ""
})
.disabled(!isEnabled)
.disableAutocorrection(true)
.autocapitalization(.none)
.textFieldStyle(FormInputFieldStyle())
Chips(titles: titles, didDeleteChip: didDeleteChip)
.padding(.horizontal)
}
}
}
@available(iOS 14.0, *)
struct ChipsInput_Previews: PreviewProvider {
static var chips = Set<String>(["Website", "Element", "Design", "Matrix/Element"])
static var previews: some View {
ChipsInput(titles: Array(chips)) { chip in
chips.insert(chip)
} didDeleteChip: { chip in
chips.remove(chip)
}
.disabled(true)
}
}
@@ -0,0 +1,44 @@
//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import SwiftUI
@available(iOS 14.0, *)
struct DefaultNotificationSettings: View {
@ObservedObject var viewModel: NotificationSettingsViewModel
var body: some View {
NotificationSettings(viewModel: viewModel)
.navigationBarTitle(VectorL10n.settingsDefault)
}
}
@available(iOS 14.0, *)
struct DefaultNotifications_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
DefaultNotificationSettings(
viewModel: NotificationSettingsViewModel(
notificationSettingsService: MockNotificationSettingsService.example,
ruleIds: NotificationSettingsScreen.defaultNotifications.pushRules
)
)
.navigationBarTitleDisplayMode(.inline)
}
}
}
@@ -0,0 +1,84 @@
//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import SwiftUI
/**
An input field for forms.
*/
@available(iOS 14.0, *)
struct FormInputFieldStyle: TextFieldStyle {
@Environment(\.theme) var theme: ThemeSwiftUI
@Environment(\.isEnabled) var isEnabled
private var textColor: Color {
if !isEnabled {
return theme.colors.quarterlyContent
}
return theme.colors.primaryContent
}
private var backgroundColor: Color {
if !isEnabled && theme.identifier == .dark {
return theme.colors.quinaryContent
}
return theme.colors.background
}
func _body(configuration: TextField<_Label>) -> some View {
configuration
.font(theme.fonts.callout)
.foregroundColor(textColor)
.frame(minHeight: 48)
.padding(.horizontal)
.background(backgroundColor)
}
}
@available(iOS 14.0, *)
struct FormInputFieldStyle_Previews: PreviewProvider {
static var previews: some View {
Group {
VectorForm {
TextField("Placeholder", text: .constant(""))
.textFieldStyle(FormInputFieldStyle())
TextField("Placeholder", text: .constant("Web"))
.textFieldStyle(FormInputFieldStyle())
TextField("Placeholder", text: .constant("Web"))
.textFieldStyle(FormInputFieldStyle())
.disabled(true)
}
.padding()
VectorForm {
TextField("Placeholder", text: .constant(""))
.textFieldStyle(FormInputFieldStyle())
TextField("Placeholder", text: .constant("Web"))
.textFieldStyle(FormInputFieldStyle())
TextField("Placeholder", text: .constant("Web"))
.textFieldStyle(FormInputFieldStyle())
.disabled(true)
}
.padding()
.theme(ThemeIdentifier.dark)
}
}
}
@@ -0,0 +1,54 @@
//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import SwiftUI
@available(iOS 14.0, *)
struct MentionsAndKeywordNotificationSettings: View {
@ObservedObject var viewModel: NotificationSettingsViewModel
var keywordSection: some View {
SwiftUI.Section(
header: FormSectionHeader(text: VectorL10n.settingsYourKeywords),
footer: FormSectionFooter(text: VectorL10n.settingsMentionsAndKeywordsEncryptionNotice)
) {
NotificationSettingsKeywords(viewModel: viewModel)
}
}
var body: some View {
NotificationSettings(
viewModel: viewModel,
bottomSection: keywordSection
)
.navigationTitle(VectorL10n.settingsMentionsAndKeywords)
}
}
@available(iOS 14.0, *)
struct MentionsAndKeywords_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
MentionsAndKeywordNotificationSettings(
viewModel: NotificationSettingsViewModel(
notificationSettingsService: MockNotificationSettingsService.example,
ruleIds: NotificationSettingsScreen.mentionsAndKeywords.pushRules
)
)
.navigationBarTitleDisplayMode(.inline)
}
}
}
@@ -0,0 +1,73 @@
//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import SwiftUI
/**
Renders the push rule settings that can be enabled/disable.
Also renders an optional bottom section
(used in the case of keywords, for the keyword chips and input).
*/
@available(iOS 14.0, *)
struct NotificationSettings<BottomSection: View>: View {
@ObservedObject var viewModel: NotificationSettingsViewModel
var bottomSection: BottomSection?
var body: some View {
VectorForm {
SwiftUI.Section(
header: FormSectionHeader(text: VectorL10n.settingsNotifyMeFor)
) {
ForEach(viewModel.viewState.ruleIds) { ruleId in
let checked = viewModel.viewState.selectionState[ruleId] ?? false
FormPickerItem(title: ruleId.title, selected: checked) {
viewModel.update(ruleID: ruleId, isChecked: !checked)
}
}
}
bottomSection
}
.activityIndicator(show: viewModel.viewState.saving)
}
}
@available(iOS 14.0, *)
extension NotificationSettings where BottomSection == EmptyView {
init(viewModel: NotificationSettingsViewModel) {
self.init(viewModel: viewModel, bottomSection: nil)
}
}
@available(iOS 14.0, *)
struct NotificationSettings_Previews: PreviewProvider {
static var previews: some View {
Group {
ForEach(NotificationSettingsScreen.allCases) { screen in
NavigationView {
NotificationSettings(
viewModel: NotificationSettingsViewModel(
notificationSettingsService: MockNotificationSettingsService.example,
ruleIds: screen.pushRules
)
)
.navigationBarTitleDisplayMode(.inline)
}
}
}
}
}
@@ -0,0 +1,46 @@
//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import SwiftUI
/**
Renders the keywords input, driven by 'NotificationSettingsViewModel'.
*/
@available(iOS 14.0, *)
struct NotificationSettingsKeywords: View {
@ObservedObject var viewModel: NotificationSettingsViewModel
var body: some View {
ChipsInput(
titles: viewModel.viewState.keywords,
didAddChip: viewModel.add(keyword:),
didDeleteChip: viewModel.remove(keyword:),
placeholder: VectorL10n.settingsNewKeyword
)
.disabled(!(viewModel.viewState.selectionState[.keywords] ?? false))
}
}
@available(iOS 14.0, *)
struct Keywords_Previews: PreviewProvider {
static let viewModel = NotificationSettingsViewModel(
notificationSettingsService: MockNotificationSettingsService.example,
ruleIds: NotificationSettingsScreen.mentionsAndKeywords.pushRules
)
static var previews: some View {
NotificationSettingsKeywords(viewModel: viewModel)
}
}
@@ -0,0 +1,42 @@
//
// Copyright 2021 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import SwiftUI
@available(iOS 14.0, *)
struct OtherNotificationSettings: View {
@ObservedObject var viewModel: NotificationSettingsViewModel
var body: some View {
NotificationSettings(viewModel: viewModel)
.navigationTitle(VectorL10n.settingsOther)
}
}
@available(iOS 14.0, *)
struct OtherNotifications_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
DefaultNotificationSettings(
viewModel: NotificationSettingsViewModel(
notificationSettingsService: MockNotificationSettingsService.example,
ruleIds: NotificationSettingsScreen.other.pushRules
)
)
.navigationBarTitleDisplayMode(.inline)
}
}
}