Add NotificationSettings and Keywords UI and VM

This commit is contained in:
David Langley
2021-08-18 09:33:56 +01:00
parent 42801a3ca1
commit 343d5a7017
28 changed files with 822 additions and 373 deletions
@@ -0,0 +1,52 @@
//
// 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 Chip: View {
@Environment(\.theme) var theme: Theme
let titleKey: String
let onClose: () -> Void
var body: some View {
HStack {
Text(titleKey)
.font(Font(theme.fonts.body))
.lineLimit(1)
Image(systemName: "xmark.circle.fill")
.frame(width: 16, height: 16, alignment: .center)
.onTapGesture(perform: onClose)
}
.padding(.leading, 12)
.padding(.top, 6)
.padding(.bottom, 6)
.padding(.trailing, 8)
.background(Color(theme.tintColor))
.foregroundColor(Color.white)
.cornerRadius(20)
}
}
@available(iOS 14.0, *)
struct Chip_Previews: PreviewProvider {
static var previews: some View {
Chip(titleKey: "My great chip", onClose: { })
}
}
@@ -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
@available(iOS 14.0, *)
struct Chips: View {
var chips: [String]
var body: some View {
var width = CGFloat.zero
var height = CGFloat.zero
return GeometryReader { geo in
ZStack(alignment: .topLeading, content: {
ForEach(chips, id: \.self) { chip in
Chip(titleKey: chip) {
}
.padding(.all, 5)
.alignmentGuide(.leading) { dimension in
if abs(width - dimension.width) > geo.size.width {
width = 0
height -= dimension.height
}
let result = width
if chip == chips.last {
width = 0
} else {
width -= dimension.width
}
return result
}
.alignmentGuide(.top) { dimension in
let result = height
if chip == chips.last {
height = 0
}
return result
}
}
})
}.padding(.all, 10)
}
}
@available(iOS 14.0, *)
struct Chips_Previews: PreviewProvider {
static var previews: some View {
Chips(chips: ["Chip1", "Chip2", "Chip3", "Chip4", "Chip5", "Chip6"])
.frame(width: .infinity, height: 400, alignment: .leading)
}
}
@@ -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
@available(iOS 14.0, *)
struct DefaultNotifications: View {
@ObservedObject var viewModel: NotificationSettingsViewModel
var body: some View {
NotificationSettings(
viewModel: viewModel,
footer: EmptyView()
)
.navigationBarTitle(VectorL10n.settingsDefault)
}
}
@available(iOS 14.0, *)
struct DefaultNotifications_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
DefaultNotifications(
viewModel: NotificationSettingsViewModel(
rules: NotificationSettingsScreen.defaultNotificaitons.pushRules
)
)
.navigationBarTitleDisplayMode(.inline)
}
}
}
@@ -0,0 +1,38 @@
//
// 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 Keywords: View {
@ObservedObject var viewModel: KeywordsViewModel
@State var keywordText: String = ""
var body: some View {
VStack {
TextField("New Keyword", text: $keywordText)
Chips(chips: viewModel.keywords)
}
}
}
@available(iOS 14.0, *)
struct Keywords_Previews: PreviewProvider {
static var previews: some View {
Keywords(viewModel: KeywordsViewModel())
}
}
@@ -0,0 +1,47 @@
//
// 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 MentionsAndKeywords: View {
@ObservedObject var keywordsViewModel: KeywordsViewModel
@ObservedObject var viewModel: NotificationSettingsViewModel
var body: some View {
NotificationSettings(
viewModel: viewModel,
footer: Keywords(viewModel: keywordsViewModel)
)
.navigationTitle(VectorL10n.settingsMentionsAndKeywords)
}
}
@available(iOS 14.0, *)
struct MentionsAndKeywords_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
MentionsAndKeywords(
keywordsViewModel: KeywordsViewModel(),
viewModel: NotificationSettingsViewModel(
rules: NotificationSettingsScreen.mentionsAndKeywords.pushRules
)
)
.navigationBarTitleDisplayMode(.inline)
}
}
}
@@ -0,0 +1,71 @@
//
// 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 NotificationSettings<Footer: View>: View {
@ObservedObject var viewModel: NotificationSettingsViewModel
var footer: Footer
@ViewBuilder
private var rightButton: some View {
Button(VectorL10n.save) {
viewModel.process(viewAction: .save)
}
}
var body: some View {
VectorForm {
SwiftUI.Section(
header: FormSectionHeader(text: VectorL10n.roomNotifsSettingsNotifyMeFor),
footer: footer
) {
ForEach(viewModel.viewState.selectionState) { item in
FormPickerItem(title: item.title ?? "", selected: item.selected) {
viewModel.process(viewAction: .selectNotification(item.ruleId, !item.selected))
}
}
}
}
.activityIndicator(show: viewModel.viewState.saving)
.navigationBarItems(
trailing: rightButton
)
.onAppear {
viewModel.process(viewAction: .load)
}
}
}
@available(iOS 14.0, *)
struct NotificationSettings_Previews: PreviewProvider {
static var previews: some View {
Group {
ForEach(NotificationSettingsScreen.allCases) { screen in
NavigationView {
NotificationSettings(
viewModel: NotificationSettingsViewModel(rules: screen.pushRules),
footer: EmptyView()
)
.navigationBarTitleDisplayMode(.inline)
}
}
}
}
}
@@ -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 OtherNotifications: View {
@ObservedObject var viewModel: NotificationSettingsViewModel
var body: some View {
NotificationSettings(
viewModel: viewModel,
footer: EmptyView()
)
.navigationTitle(VectorL10n.settingsOther)
}
}
@available(iOS 14.0, *)
struct OtherNotifications_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
DefaultNotifications(
viewModel: NotificationSettingsViewModel(
rules: NotificationSettingsScreen.other.pushRules
)
)
.navigationBarTitleDisplayMode(.inline)
}
}
}