Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly showing selected state in SelectionView #826

Merged
merged 3 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions openHAB/OpenHABSitemapViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class OpenHABSitemapViewController: OpenHABViewController, GenericUITableViewCel
private let search = UISearchController(searchResultsController: nil)
private var isUserInteracting = false
private var isWaitingToReload = false
private let logger = Logger(subsystem: "org.openhab.app", category: "OpenHABSitemapViewController")

var relevantPage: OpenHABSitemapPage? {
if isFiltering {
Expand Down Expand Up @@ -777,16 +778,13 @@ extension OpenHABSitemapViewController: UITableViewDelegate, UITableViewDataSour
newViewController.openHABRootUrl = openHABRootUrl
navigationController?.pushViewController(newViewController, animated: true)
} else if widget?.type == .selection {
os_log("Selected selection widget", log: .viewCycle, type: .info)
selectedWidgetRow = indexPath.row
let selectedWidget: OpenHABWidget? = relevantWidget(indexPath: indexPath)
let selectionItemState = selectedWidget?.item?.state
logger.info("Selected selection widget in status: \(selectionItemState ?? "unknown")")
let hostingController = UIHostingController(rootView: SelectionView(
mappings: selectedWidget?.mappingsOrItemOptions ?? [],
selectionItem:
Binding(
get: { selectedWidget?.item },
set: { selectedWidget?.item = $0 }
),
selectionItemState: selectionItemState,
onSelection: { selectedMappingIndex in
let selectedWidget: OpenHABWidget? = self.relevantPage?.widgets[self.selectedWidgetRow]
let selectedMapping: OpenHABWidgetMapping? = selectedWidget?.mappingsOrItemOptions[selectedMappingIndex]
Expand Down
19 changes: 11 additions & 8 deletions openHAB/SelectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,42 @@ import SwiftUI

struct SelectionView: View {
var mappings: [OpenHABWidgetMapping] // List of mappings (instead of AnyHashable, we use a concrete type)
@Binding var selectionItem: OpenHABItem? // Binding to track the selected item state
@State var selectionItemState: String? // To track the selected item state
var onSelection: (Int) -> Void // Closure to handle selection

private let logger = Logger(subsystem: "org.openhab.app", category: "SelectionView")

var body: some View {
List(0 ..< mappings.count, id: \.self) { index in
let mapping = mappings[index]
HStack {
Text(mapping.label)
Spacer()
if selectionItem?.state == mapping.command {
if selectionItemState == mapping.command {
Image(systemSymbol: .checkmark)
.foregroundColor(.blue)
}
}
.contentShape(Rectangle()) // Ensures entire row is tappable
.contentShape(.interaction, Rectangle()) // Ensures entire row is tappable
.onTapGesture {
os_log("Selected mapping %d", log: .viewCycle, type: .info, index)
selectionItemState = mappings[index].command
logger.info("Selected mapping \(index)")
onSelection(index)
}
.accessibilityElement(children: .combine)
.accessibilityAddTraits(.isButton)
}
.navigationTitle("Select Mapping") // Navigation title
}
}

#Preview {
let selectedItem: OpenHABItem? = OpenHABItem(name: "", type: "", state: "command2", link: "", label: nil, groupType: nil, stateDescription: nil, commandDescription: nil, members: [], category: nil, options: nil)

return SelectionView(
SelectionView(
mappings: [
OpenHABWidgetMapping(command: "command1", label: "Option 1"),
OpenHABWidgetMapping(command: "command2", label: "Option 2")
],
selectionItem: .constant(selectedItem)
selectionItemState: "command2"
) { selectedMappingIndex in
print("Selected mapping at index \(selectedMappingIndex)")
}
Expand Down
Loading