MESSENGER-5410 add toggle to room settings and refactor toggle view

This commit is contained in:
JanNiklas Grabowski
2023-12-22 17:16:35 +01:00
parent 2a875e3846
commit 866e27c1fd
3 changed files with 109 additions and 35 deletions
@@ -19,12 +19,29 @@ import SwiftUI
import Foundation
/// Helper class for making our SwiftUI view available to ObjectiveC
@objcMembers class TableViewCellWithLabelSubLabelAndSwitch<Content: View>: MXKTableViewCell {
@objcMembers class TableViewCellWithLabelSubLabelAndSwitch: MXKTableViewCell {
private var parentViewController: UIViewController?
private let hostingController = UIHostingController<Content?>(rootView: nil)
private var hostingController: UIHostingController<TableViewCellWithLabelSubLabelAndSwitchView>?
@Published var toggleValue: Bool = false
private var theme: Theme!
public func makeViewController(parent: UIViewController, rootView: TableViewCellWithLabelSubLabelAndSwitchView) {
hostingController = UIHostingController<TableViewCellWithLabelSubLabelAndSwitchView>(rootView: rootView)
setupView(parent: parent)
}
// support objective c vc
func makeViewController(parent: UIViewController, toggleText: String, subText: String, initalToggleValue: Bool,onValueChanged:((_ newValue: Bool)-> Void)?) {
self.toggleValue = initalToggleValue
hostingController = UIHostingController(rootView: TableViewCellWithLabelSubLabelAndSwitchView(toggleValue: Binding(get: {
return self.toggleValue
}, set: { newValue in
self.toggleValue = newValue
onValueChanged?(newValue)
}), toggleText: toggleText, subText: subText))
setupView(parent: parent)
}
override init!(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
@@ -33,27 +50,28 @@ import Foundation
fatalError("init(coder:) has not been implemented")
}
public func setupView(parent: UIViewController, rootView: Content) {
private func setupView(parent: UIViewController) {
self.parentViewController = parent
hostingController.rootView = rootView
let shouldControllerMove = hostingController.parent != parent
if let hostingController = hostingController {
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 !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)
}
if shouldControllerMove {
hostingController.didMove(toParent: parent)
}
registerThemeServiceDidChangeThemeNotification()
update(theme: ThemeService.shared().theme)
}
private func registerThemeServiceDidChangeThemeNotification() {
@@ -66,8 +84,9 @@ import Foundation
private func update(theme: Theme) {
self.theme = theme
hostingController.view.backgroundColor = theme.headerBackgroundColor
if let hostingController = hostingController {
hostingController.view.backgroundColor = theme.headerBackgroundColor
}
}
deinit {
@@ -75,32 +94,29 @@ import Foundation
}
private func removeController() {
hostingController.willMove(toParent: nil)
hostingController.view.removeFromSuperview()
hostingController.removeFromParent()
parentViewController = nil
if let hostingController = hostingController {
hostingController.willMove(toParent: nil)
hostingController.view.removeFromSuperview()
hostingController.removeFromParent()
parentViewController = nil
}
}
}
struct TableViewCellWithLabelSubLabelAndSwitchView: View {
@ObservedObject var viewModel: EnterNewRoomDetailsViewModel
@Binding var toggleValue: Bool
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) {
Toggle(isOn: $toggleValue) {
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))