// /* * Copyright (c) 2023 BWI GmbH * * 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 import Foundation /// Helper class for making our SwiftUI view available to ObjectiveC @objcMembers class TableViewCellWithLabelSubLabelAndSwitch: MXKTableViewCell { private var parentViewController: UIViewController? private let hostingController = UIHostingController(rootView: nil) private var theme: Theme! override init!(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } public func setupView(parent: UIViewController, rootView: Content) { self.parentViewController = parent hostingController.rootView = rootView let shouldControllerMove = hostingController.parent != parent if shouldControllerMove { removeController() parent.addChild(hostingController) } if !self.contentView.subviews.contains(hostingController.view) { self.contentView.addSubview(hostingController.view) self.contentView.vc_addSubViewMatchingParentSafeArea(hostingController.view) } if shouldControllerMove { hostingController.didMove(toParent: parent) } registerThemeServiceDidChangeThemeNotification() update(theme: ThemeService.shared().theme) } private func registerThemeServiceDidChangeThemeNotification() { NotificationCenter.default.addObserver(self, selector: #selector(themeDidChange), name: .themeServiceDidChangeTheme, object: nil) } @objc private func themeDidChange() { update(theme: ThemeService.shared().theme) } private func update(theme: Theme) { self.theme = theme hostingController.view.backgroundColor = theme.headerBackgroundColor } deinit { removeController() } private func removeController() { hostingController.willMove(toParent: nil) hostingController.view.removeFromSuperview() hostingController.removeFromParent() parentViewController = nil } } struct TableViewCellWithLabelSubLabelAndSwitchView: View { @ObservedObject var viewModel: EnterNewRoomDetailsViewModel var toggleText: String var subText: String @Environment(\.theme) private var theme var onValueChanged: ((_: Bool) -> Void)? var body: some View { VStack(alignment: .leading) { HStack { Toggle(isOn: $viewModel.serverACLToggleValue) { Text(toggleText) .lineLimit(nil) } .toggleStyle(SwitchToggleStyle(tint: Color(ThemeService.shared().theme.tintColor))) .onChange(of: viewModel.serverACLToggleValue) { newToggleValue in onValueChanged?(newToggleValue) } } Text(subText) .font(.system(size: 15)) .foregroundColor(theme.colors.secondaryContent) .lineLimit(nil) } .padding([.leading, .trailing], 16) .padding([.top, .bottom], 8) } }