Skip to content

Commit

Permalink
Properly showing selected state in SelectionView
Browse files Browse the repository at this point in the history
  • Loading branch information
timbms committed Sep 16, 2024
1 parent 8637c46 commit 4514c56
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
9 changes: 3 additions & 6 deletions openHAB/OpenHABSitemapViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -776,16 +776,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

0 comments on commit 4514c56

Please sign in to comment.