-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Refactor RunTest view to improve input and nettest status handling
Signed-off-by: Norbel AMBANUMBEN <[email protected]>
- Loading branch information
Showing
8 changed files
with
215 additions
and
235 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,43 @@ | ||
// | ||
// InputTableViewCell.swift | ||
// ooniprobe | ||
// | ||
// Created by Norbel Ambanumben on 26/07/2024. | ||
// Copyright © 2024 OONI. All rights reserved. | ||
// | ||
import SwiftUI | ||
|
||
import Foundation | ||
// MARK: - Input views and TableCell | ||
|
||
/// A SwiftUI view that represents an input in the table view. | ||
struct InputTableView: View { | ||
var item: String | ||
|
||
var body: some View { | ||
HStack { | ||
Text(item).font(.callout) | ||
Spacer() | ||
} | ||
} | ||
} | ||
|
||
/// A UITableViewCell subclass that displays an input in the table view. | ||
class InputTableViewCell: UITableViewCell { | ||
private var hostingController: UIHostingController<InputTableView>? | ||
|
||
/// Configures the cell with the specified data. | ||
/// - Parameter data: The input string. | ||
func configure(with data: String) { | ||
|
||
let toggleCellView = InputTableView(item:data) | ||
|
||
if let hostingController = hostingController { | ||
hostingController.rootView = toggleCellView | ||
} else { | ||
hostingController = UIHostingController(rootView: toggleCellView) | ||
if let hostingView = hostingController?.view { | ||
hostingView.translatesAutoresizingMaskIntoConstraints = false | ||
contentView.addSubview(hostingView) | ||
NSLayoutConstraint.activate([ | ||
hostingView.topAnchor.constraint(equalTo: contentView.topAnchor), | ||
hostingView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor), | ||
hostingView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), | ||
hostingView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor), | ||
]) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
// | ||
// NettestStatus.swift | ||
// ooniprobe | ||
// | ||
// Created by Norbel Ambanumben on 26/07/2024. | ||
// Copyright © 2024 OONI. All rights reserved. | ||
// | ||
// MARK: - NettestStatus | ||
|
||
import Foundation | ||
/// A struct that represents the status of a Nettest. | ||
class NettestStatus : ObservableObject { | ||
var nettest: Nettest | ||
@Published var isSelected: Bool = false | ||
@Published var isExpanded: Bool = false | ||
|
||
init(nettest: Nettest) { | ||
self.nettest = nettest | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,78 @@ | ||
// | ||
// NettestTableViewCell.swift | ||
// ooniprobe | ||
// | ||
// Created by Norbel Ambanumben on 26/07/2024. | ||
// Copyright © 2024 OONI. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
/// A SwiftUI toggle style that uses a checkbox. | ||
struct iOSCheckboxToggleStyle: ToggleStyle { | ||
func makeBody(configuration: Configuration) -> some View { | ||
Button(action: { | ||
configuration.isOn.toggle() | ||
}, label: { | ||
HStack { | ||
configuration.label | ||
Spacer() | ||
Image(systemName: configuration.isOn ? "checkmark.square" : "square") | ||
.padding() | ||
} | ||
}).foregroundColor(.black) | ||
} | ||
} | ||
|
||
// MARK: - Nettests views and TableCell | ||
|
||
/// A SwiftUI view that represents a section in the table view. | ||
struct NettestTableCell: View { | ||
var item: NettestStatus | ||
@Binding var isSelected: Bool | ||
|
||
var body: some View { | ||
HStack { | ||
Text(LocalizationUtility.getNameForTest(item.nettest.name)) | ||
.font(.callout) | ||
.lineLimit(1) | ||
.layoutPriority(1) | ||
if let inputs = item.nettest.inputs, !inputs.isEmpty { | ||
Image(systemName: item.isExpanded ? "chevron.up" : "chevron.down") | ||
} else { | ||
Spacer() | ||
} | ||
Toggle(isOn: $isSelected) {}.toggleStyle(iOSCheckboxToggleStyle()) | ||
} | ||
} | ||
} | ||
|
||
/// A UITableViewCell subclass that displays a section in the table view. | ||
class NettestTableViewCell: UITableViewCell { | ||
private var hostingController: UIHostingController<NettestTableCell>? | ||
|
||
/// Configures the cell with the specified data. | ||
/// - Parameters: | ||
/// - data: The NettestStatus object. | ||
/// - onToggleChange: A closure that is called when the toggle is changed. | ||
func configure(with data: NettestStatus, onToggleChange: @escaping (Bool) -> Void) { | ||
// Create a binding to pass the data to the SwiftUI view | ||
let binding = Binding<Bool>( | ||
get: { data.isSelected }, | ||
set: { newValue in | ||
data.isSelected = newValue | ||
onToggleChange(newValue) | ||
} | ||
) | ||
|
||
let toggleCellView = NettestTableCell(item: data, isSelected: binding) | ||
|
||
if let hostingController = hostingController { | ||
hostingController.rootView = toggleCellView | ||
} else { | ||
hostingController = UIHostingController(rootView: toggleCellView) | ||
if let hostingView = hostingController?.view { | ||
hostingView.translatesAutoresizingMaskIntoConstraints = false | ||
contentView.addSubview(hostingView) | ||
NSLayoutConstraint.activate([ | ||
hostingView.topAnchor.constraint(equalTo: contentView.topAnchor), | ||
hostingView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor), | ||
hostingView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), | ||
hostingView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor), | ||
]) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.