mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-22 01:22:46 +02:00
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:
+39
@@ -0,0 +1,39 @@
|
||||
//
|
||||
// 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
|
||||
|
||||
class MockRoomNotificationSettingsService: RoomNotificationSettingsServiceType {
|
||||
|
||||
static let example = MockRoomNotificationSettingsService(initialState: .all)
|
||||
|
||||
var listener: RoomNotificationStateCallback?
|
||||
var notificationState: RoomNotificationState
|
||||
|
||||
init(initialState: RoomNotificationState) {
|
||||
notificationState = initialState
|
||||
}
|
||||
|
||||
func observeNotificationState(listener: @escaping RoomNotificationStateCallback) {
|
||||
self.listener = listener
|
||||
}
|
||||
|
||||
func update(state: RoomNotificationState, completion: @escaping UpdateRoomNotificationStateCompletion) {
|
||||
self.notificationState = state
|
||||
completion()
|
||||
listener?(state)
|
||||
}
|
||||
}
|
||||
@@ -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 Foundation
|
||||
|
||||
enum RoomNotificationState: Int {
|
||||
case all
|
||||
case mentionsAndKeywordsOnly
|
||||
case mute
|
||||
}
|
||||
|
||||
extension RoomNotificationState: CaseIterable { }
|
||||
|
||||
extension RoomNotificationState: Identifiable {
|
||||
var id: Int { self.rawValue }
|
||||
}
|
||||
|
||||
extension RoomNotificationState {
|
||||
var title: String {
|
||||
switch self {
|
||||
case .all:
|
||||
return VectorL10n.roomNotifsSettingsAllMessages
|
||||
case .mentionsAndKeywordsOnly:
|
||||
return VectorL10n.roomNotifsSettingsMentionsAndKeywords
|
||||
case .mute:
|
||||
return VectorL10n.roomNotifsSettingsNone
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
//
|
||||
// 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
|
||||
|
||||
@available(iOS 14.0, *)
|
||||
struct FormItemButtonStyle: ButtonStyle {
|
||||
@Environment(\.theme) var theme: ThemeSwiftUI
|
||||
func makeBody(configuration: Self.Configuration) -> some View {
|
||||
configuration.label
|
||||
.background(configuration.isPressed ? theme.colors.system : theme.colors.background)
|
||||
.foregroundColor(theme.colors.primaryContent)
|
||||
.font(theme.fonts.body)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
//
|
||||
// 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 FormPickerItem: View {
|
||||
|
||||
typealias TapCallback = () -> Void
|
||||
|
||||
@Environment(\.theme) var theme: ThemeSwiftUI
|
||||
|
||||
var title: String
|
||||
var selected: Bool
|
||||
var onTap: TapCallback?
|
||||
|
||||
var body: some View {
|
||||
Button {
|
||||
onTap?()
|
||||
} label: {
|
||||
VStack {
|
||||
Spacer()
|
||||
HStack {
|
||||
Text(title)
|
||||
Spacer()
|
||||
if selected {
|
||||
Image("checkmark")
|
||||
.foregroundColor(theme.colors.accent)
|
||||
}
|
||||
}
|
||||
.padding(.trailing)
|
||||
Spacer()
|
||||
Divider()
|
||||
}
|
||||
.padding(.leading)
|
||||
}
|
||||
.buttonStyle(FormItemButtonStyle())
|
||||
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, idealHeight: 44, alignment: .leading)
|
||||
.fixedSize(horizontal: false, vertical: true)
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 14.0, *)
|
||||
struct FormPickerItem_Previews: PreviewProvider {
|
||||
|
||||
static let items = ["Item 1", "Item 2", "Item 3"]
|
||||
static var selected: String = items[0]
|
||||
static var previews: some View {
|
||||
VectorForm {
|
||||
ForEach(items, id: \.self) { item in
|
||||
FormPickerItem(title: item, selected: selected == item)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 FormSectionFooter: View {
|
||||
|
||||
@Environment(\.theme) var theme: ThemeSwiftUI
|
||||
var text: String
|
||||
|
||||
var body: some View {
|
||||
Text(text)
|
||||
.foregroundColor(theme.colors.secondaryContent)
|
||||
.padding(.top)
|
||||
.padding(.leading)
|
||||
.padding(.trailing)
|
||||
.font(theme.fonts.callout)
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 14.0, *)
|
||||
struct FormSectionFooter_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
VectorForm {
|
||||
SwiftUI.Section(footer: FormSectionFooter(text: "Please note that mentions & keyword notifications are not available in encrypted rooms on mobile.")) {
|
||||
FormPickerItem(title: "Item 1", selected: false)
|
||||
FormPickerItem(title: "Item 2", selected: false)
|
||||
FormPickerItem(title: "Item 3", selected: false)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
//
|
||||
// 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 FormSectionHeader: View {
|
||||
|
||||
@Environment(\.theme) var theme: ThemeSwiftUI
|
||||
var text: String
|
||||
|
||||
var body: some View {
|
||||
Text(text)
|
||||
.foregroundColor(theme.colors.secondaryContent)
|
||||
.padding(.top, 32)
|
||||
.padding(.leading)
|
||||
.padding(.bottom, 8)
|
||||
.font(theme.fonts.subheadline)
|
||||
.textCase(.uppercase)
|
||||
.frame(maxWidth: .infinity, alignment: .leading)
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 14.0, *)
|
||||
struct FormSectionHeader_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
VectorForm {
|
||||
SwiftUI.Section(header: FormSectionHeader(text: "Section Header")) {
|
||||
FormPickerItem(title: "Item 1", selected: false)
|
||||
FormPickerItem(title: "Item 2", selected: false)
|
||||
FormPickerItem(title: "Item 3", selected: false)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
//
|
||||
// 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.0, *)
|
||||
struct RoomNotificationSettings: View {
|
||||
|
||||
@ObservedObject var viewModel: RoomNotificationSettingsSwiftUIViewModel
|
||||
|
||||
let presentedModally: Bool
|
||||
|
||||
@ViewBuilder
|
||||
private var leftButton: some View {
|
||||
if presentedModally {
|
||||
Button(VectorL10n.cancel) {
|
||||
viewModel.process(viewAction: .cancel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private var rightButton: some View {
|
||||
Button(VectorL10n.save) {
|
||||
viewModel.process(viewAction: .save)
|
||||
}
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
VectorForm {
|
||||
if let avatarData = viewModel.viewState.avatarData as? AvatarInputType {
|
||||
RoomNotificationSettingsHeader(
|
||||
avatarData: avatarData,
|
||||
displayName: viewModel.viewState.displayName
|
||||
)
|
||||
}
|
||||
SwiftUI.Section(
|
||||
header: FormSectionHeader(text: VectorL10n.roomNotifsSettingsNotifyMeFor),
|
||||
footer: FormSectionFooter(text: viewModel.viewState.roomEncryptedString)
|
||||
) {
|
||||
ForEach(viewModel.viewState.notificationOptions) { option in
|
||||
FormPickerItem(title: option.title, selected: viewModel.viewState.notificationState == option) {
|
||||
viewModel.process(viewAction: .selectNotificationState(option))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.activityIndicator(show: viewModel.viewState.saving)
|
||||
.navigationBarTitle(VectorL10n.roomDetailsNotifs)
|
||||
.navigationBarItems(
|
||||
leading: leftButton,
|
||||
trailing: rightButton
|
||||
)
|
||||
.onAppear {
|
||||
viewModel.process(viewAction: .load)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 14.0, *)
|
||||
struct RoomNotificationSettings_Previews: PreviewProvider {
|
||||
|
||||
static let mockViewModel = RoomNotificationSettingsSwiftUIViewModel(
|
||||
roomNotificationService: MockRoomNotificationSettingsService.example,
|
||||
avatarData: MockAvatarInput.example,
|
||||
displayName: MockAvatarInput.example.displayName,
|
||||
roomEncrypted: true
|
||||
)
|
||||
|
||||
static var previews: some View {
|
||||
Group {
|
||||
NavigationView {
|
||||
RoomNotificationSettings(viewModel: mockViewModel, presentedModally: true)
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.addDependency(MockAvatarService.example)
|
||||
}
|
||||
NavigationView {
|
||||
RoomNotificationSettings(viewModel: mockViewModel, presentedModally: true)
|
||||
.navigationBarTitleDisplayMode(.inline)
|
||||
.theme(ThemeIdentifier.dark)
|
||||
.addDependency(MockAvatarService.example)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
//
|
||||
// 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 RoomNotificationSettingsHeader: View {
|
||||
|
||||
@Environment(\.theme) var theme: ThemeSwiftUI
|
||||
var avatarData: AvatarInputType
|
||||
var displayName: String?
|
||||
|
||||
var body: some View {
|
||||
HStack {
|
||||
Spacer()
|
||||
VStack(alignment: .center) {
|
||||
AvatarImage(avatarData: avatarData, size: .xxLarge)
|
||||
if let displayName = displayName {
|
||||
Text(displayName)
|
||||
.font(theme.fonts.title3SB)
|
||||
.foregroundColor(theme.colors.primaryContent)
|
||||
.textCase(nil)
|
||||
}
|
||||
}
|
||||
Spacer()
|
||||
}
|
||||
.padding(.top, 36)
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 14.0, *)
|
||||
struct RoomNotificationSettingsHeader_Previews: PreviewProvider {
|
||||
static let name = "Element"
|
||||
static var previews: some View {
|
||||
RoomNotificationSettingsHeader(avatarData: MockAvatarInput.example, displayName: name)
|
||||
.addDependency(MockAvatarService.example)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
//
|
||||
// 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 VectorForm<Content: View>: View {
|
||||
|
||||
@Environment(\.theme) var theme: ThemeSwiftUI
|
||||
var content: () -> Content
|
||||
|
||||
init(@ViewBuilder content: @escaping () -> Content) {
|
||||
self.content = content
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
ScrollView {
|
||||
VStack(alignment: .leading, spacing: 0, content: content)
|
||||
}
|
||||
.frame(
|
||||
minWidth: 0,
|
||||
maxWidth: .infinity,
|
||||
minHeight: 0,
|
||||
maxHeight: .infinity,
|
||||
alignment: .top
|
||||
)
|
||||
.background(theme.colors.system)
|
||||
.edgesIgnoringSafeArea(.bottom)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 14.0, *)
|
||||
struct VectorForm_Previews: PreviewProvider {
|
||||
|
||||
static var previews: some View {
|
||||
Group {
|
||||
VectorForm {
|
||||
SwiftUI.Section(header: FormSectionHeader(text: "Section Header")) {
|
||||
FormPickerItem(title: "Item 1", selected: true)
|
||||
FormPickerItem(title: "Item 2", selected: false)
|
||||
FormPickerItem(title: "Item 3", selected: false)
|
||||
}
|
||||
}
|
||||
VectorForm {
|
||||
FormPickerItem(title: "Item 1", selected: true)
|
||||
}
|
||||
.theme(ThemeIdentifier.dark)
|
||||
}
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// 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
|
||||
|
||||
typealias UpdateRoomNotificationStateCompletion = () -> Void
|
||||
typealias RoomNotificationStateCallback = (RoomNotificationState) -> Void
|
||||
|
||||
protocol RoomNotificationSettingsServiceType {
|
||||
|
||||
func observeNotificationState(listener: @escaping RoomNotificationStateCallback)
|
||||
func update(state: RoomNotificationState, completion: @escaping UpdateRoomNotificationStateCompletion)
|
||||
var notificationState: RoomNotificationState { get }
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
//
|
||||
// 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 Combine
|
||||
|
||||
@available(iOS 14.0, *)
|
||||
class RoomNotificationSettingsSwiftUIViewModel: RoomNotificationSettingsViewModel, ObservableObject {
|
||||
|
||||
@Published var viewState: RoomNotificationSettingsViewState
|
||||
|
||||
lazy var cancellables = Set<AnyCancellable>()
|
||||
|
||||
override init(roomNotificationService: RoomNotificationSettingsServiceType, initialState: RoomNotificationSettingsViewState) {
|
||||
self.viewState = initialState
|
||||
super.init(roomNotificationService: roomNotificationService, initialState: initialState)
|
||||
}
|
||||
|
||||
override func update(viewState: RoomNotificationSettingsViewState) {
|
||||
super.update(viewState: viewState)
|
||||
self.viewState = viewState
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
// File created from ScreenTemplate
|
||||
// $ createScreen.sh Room/NotificationSettings RoomNotificationSettings
|
||||
/*
|
||||
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
|
||||
/// RoomNotificationSettingsViewController view actions exposed to view model
|
||||
enum RoomNotificationSettingsViewAction {
|
||||
case load
|
||||
case selectNotificationState(RoomNotificationState)
|
||||
case save
|
||||
case cancel
|
||||
}
|
||||
+108
@@ -0,0 +1,108 @@
|
||||
// File created from ScreenTemplate
|
||||
// $ createScreen.sh Room/NotificationSettings RoomNotificationSettings
|
||||
/*
|
||||
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 Combine
|
||||
|
||||
class RoomNotificationSettingsViewModel: RoomNotificationSettingsViewModelType {
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
// MARK: Private
|
||||
|
||||
private let roomNotificationService: RoomNotificationSettingsServiceType
|
||||
var state: RoomNotificationSettingsViewState {
|
||||
willSet {
|
||||
update(viewState: newValue)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: Public
|
||||
|
||||
weak var viewDelegate: RoomNotificationSettingsViewModelViewDelegate?
|
||||
|
||||
weak var coordinatorDelegate: RoomNotificationSettingsViewModelCoordinatorDelegate?
|
||||
|
||||
// MARK: - Setup
|
||||
|
||||
init(
|
||||
roomNotificationService: RoomNotificationSettingsServiceType,
|
||||
initialState: RoomNotificationSettingsViewState
|
||||
) {
|
||||
self.roomNotificationService = roomNotificationService
|
||||
self.state = initialState
|
||||
|
||||
self.roomNotificationService.observeNotificationState { [weak self] state in
|
||||
guard let self = self else { return }
|
||||
self.state.notificationState = Self.mapNotificationStateOnRead(encrypted: self.state.roomEncrypted, state: state)
|
||||
}
|
||||
}
|
||||
|
||||
convenience init(
|
||||
roomNotificationService: RoomNotificationSettingsServiceType,
|
||||
avatarData: AvatarType?,
|
||||
displayName: String?,
|
||||
roomEncrypted: Bool
|
||||
) {
|
||||
let notificationState = Self.mapNotificationStateOnRead(encrypted: roomEncrypted, state: roomNotificationService.notificationState)
|
||||
|
||||
let initialState = RoomNotificationSettingsViewState(
|
||||
roomEncrypted: roomEncrypted,
|
||||
saving: false,
|
||||
notificationState: notificationState,
|
||||
avatarData: avatarData,
|
||||
displayName: displayName
|
||||
)
|
||||
self.init(roomNotificationService: roomNotificationService, initialState: initialState)
|
||||
}
|
||||
|
||||
// MARK: - Public
|
||||
|
||||
func process(viewAction: RoomNotificationSettingsViewAction) {
|
||||
switch viewAction {
|
||||
case .load:
|
||||
update(viewState: self.state)
|
||||
case .selectNotificationState(let state):
|
||||
self.state.notificationState = state
|
||||
case .save:
|
||||
self.state.saving = true
|
||||
roomNotificationService.update(state: state.notificationState) { [weak self] in
|
||||
guard let self = self else { return }
|
||||
self.state.saving = false
|
||||
self.coordinatorDelegate?.roomNotificationSettingsViewModelDidComplete(self)
|
||||
}
|
||||
case .cancel:
|
||||
coordinatorDelegate?.roomNotificationSettingsViewModelDidCancel(self)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private
|
||||
|
||||
private static func mapNotificationStateOnRead(encrypted: Bool, state: RoomNotificationState) -> RoomNotificationState {
|
||||
if encrypted, case .mentionsAndKeywordsOnly = state {
|
||||
// Notifications not supported on encrypted rooms, map mentionsOnly to mute on read
|
||||
return .mute
|
||||
} else {
|
||||
return state
|
||||
}
|
||||
}
|
||||
|
||||
func update(viewState: RoomNotificationSettingsViewState) {
|
||||
self.viewDelegate?.roomNotificationSettingsViewModel(self, didUpdateViewState: viewState)
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
// File created from ScreenTemplate
|
||||
// $ createScreen.sh Room/NotificationSettings RoomNotificationSettings
|
||||
/*
|
||||
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
|
||||
|
||||
protocol RoomNotificationSettingsViewModelViewDelegate: AnyObject {
|
||||
func roomNotificationSettingsViewModel(_ viewModel: RoomNotificationSettingsViewModelType, didUpdateViewState viewState: RoomNotificationSettingsViewStateType)
|
||||
}
|
||||
|
||||
protocol RoomNotificationSettingsViewModelCoordinatorDelegate: AnyObject {
|
||||
func roomNotificationSettingsViewModelDidComplete(_ viewModel: RoomNotificationSettingsViewModelType)
|
||||
func roomNotificationSettingsViewModelDidCancel(_ viewModel: RoomNotificationSettingsViewModelType)
|
||||
}
|
||||
|
||||
/// Protocol describing the view model used by `RoomNotificationSettingsViewController`
|
||||
protocol RoomNotificationSettingsViewModelType {
|
||||
|
||||
var viewDelegate: RoomNotificationSettingsViewModelViewDelegate? { get set }
|
||||
var coordinatorDelegate: RoomNotificationSettingsViewModelCoordinatorDelegate? { get set }
|
||||
|
||||
func process(viewAction: RoomNotificationSettingsViewAction)
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
// File created from ScreenTemplate
|
||||
// $ createScreen.sh Room/NotificationSettings RoomNotificationSettings
|
||||
/*
|
||||
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
|
||||
|
||||
/// RoomNotificationSettingsViewController view state
|
||||
struct RoomNotificationSettingsViewState: RoomNotificationSettingsViewStateType {
|
||||
let roomEncrypted: Bool
|
||||
var saving: Bool
|
||||
var notificationState: RoomNotificationState
|
||||
var avatarData: AvatarType?
|
||||
var displayName: String?
|
||||
}
|
||||
|
||||
extension RoomNotificationSettingsViewState {
|
||||
var notificationOptions: [RoomNotificationState] {
|
||||
if roomEncrypted {
|
||||
return [.all, .mute]
|
||||
} else {
|
||||
return RoomNotificationState.allCases
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protocol RoomNotificationSettingsViewStateType {
|
||||
var saving: Bool { get }
|
||||
var roomEncrypted: Bool { get }
|
||||
var notificationOptions: [RoomNotificationState] { get }
|
||||
var notificationState: RoomNotificationState { get }
|
||||
var avatarData: AvatarType? { get }
|
||||
var displayName: String? { get }
|
||||
}
|
||||
|
||||
extension RoomNotificationSettingsViewState {
|
||||
var roomEncryptedString: String {
|
||||
roomEncrypted ? VectorL10n.roomNotifsSettingsEncryptedRoomNotice : ""
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user