Skip to content

Commit

Permalink
use swiftui core settings in emualtorviewcontroller
Browse files Browse the repository at this point in the history
Signed-off-by: Joseph Mattiello <git@joemattiello.com>
  • Loading branch information
JoeMatt committed Dec 5, 2024
1 parent d813aba commit 5247db4
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,38 @@ import Foundation
import PVSupport
import PVEmulatorCore
import PVCoreBridge
import SwiftUI

extension PVEmulatorViewController {
func showCoreOptions() {
var coreClass = type(of: core)
guard let coreClass = type(of: core) as? CoreOptional.Type else { return }

coreClass.coreClassName = core.coreIdentifier ?? ""
// Create the SwiftUI view
let coreOptionsView = CoreOptionsDetailView(
coreClass: coreClass,
title: "Core Options"
)

let optionsVC = CoreOptionsViewController(withCore: coreClass as! any CoreOptional.Type) // Assuming this initializer expects a PVEmulatorCore.Type
optionsVC.title = "Core Options"
let nav = UINavigationController(rootViewController: optionsVC)
// Create a hosting controller
let hostingController = UIHostingController(rootView: coreOptionsView)
let nav = UINavigationController(rootViewController: hostingController)

#if os(iOS)
optionsVC.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(dismissNav))
// Add done button for iOS
#if os(iOS)
hostingController.navigationItem.rightBarButtonItem = UIBarButtonItem(
barButtonSystemItem: .done,
target: self,
action: #selector(dismissNav)
)
// disable iOS 13 swipe to dismiss...
nav.isModalInPresentation = true
present(nav, animated: true, completion: nil)
#else
present(nav, animated: true)
#else
// Add menu button gesture for tvOS
let tap = UITapGestureRecognizer(target: self, action: #selector(self.dismissNav))
tap.allowedPressTypes = [.menu]
optionsVC.view.addGestureRecognizer(tap)
present(TVFullscreenController(rootViewController: nav), animated: true, completion: nil)
#endif
hostingController.view.addGestureRecognizer(tap)
present(TVFullscreenController(rootViewController: nav), animated: true)
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,63 @@ struct CoreOptionsDetailView: View {
/// State to track current values of options
@State private var optionValues: [String: Any] = [:]

private var groupedOptions: [(title: String, options: [CoreOption])] {
private struct IdentifiableOption: Identifiable {
let id = UUID()
let option: CoreOption
}

private struct OptionGroup: Identifiable {
let id = UUID()
let title: String
let options: [IdentifiableOption]

init(title: String, options: [CoreOption]) {
self.title = title
self.options = options.map { IdentifiableOption(option: $0) }
}
}

private var groupedOptions: [OptionGroup] {
var rootOptions = [CoreOption]()
var groups = [(title: String, options: [CoreOption])]()
var groups = [OptionGroup]()

// Process options into groups
coreClass.options.forEach { option in
switch option {
case let .group(display, subOptions):
groups.append((title: display.title, options: subOptions))
groups.append(OptionGroup(title: display.title, options: subOptions))
default:
rootOptions.append(option)
}
}

// Add root options as first group if any exist
if !rootOptions.isEmpty {
groups.insert((title: "General", options: rootOptions), at: 0)
groups.insert(OptionGroup(title: "General", options: rootOptions), at: 0)
}

return groups
}

var body: some View {
List {
ForEach(groupedOptions.indices, id: \.self) { sectionIndex in
let group = groupedOptions[sectionIndex]
Section(header: Text(group.title)) {
ForEach(group.options.indices, id: \.self) { optionIndex in
let option = group.options[optionIndex]
optionView(for: option)
ScrollView {
LazyVStack(spacing: 16) {
ForEach(groupedOptions) { group in
VStack(alignment: .leading, spacing: 8) {
Text(group.title)
.font(.headline)
.padding(.horizontal)

ForEach(group.options) { identifiableOption in
optionView(for: identifiableOption.option)
.padding(.horizontal)
}
}
.padding(.vertical, 8)
.background(Color(.secondarySystemGroupedBackground))
}
}
.padding(.vertical)
}
.navigationTitle(title)
.onAppear {
Expand All @@ -53,10 +77,10 @@ struct CoreOptionsDetailView: View {

private func loadOptionValues() {
for group in groupedOptions {
for option in group.options {
let value = getCurrentValue(for: option)
for identifiableOption in group.options {
let value = getCurrentValue(for: identifiableOption.option)
if let value = value {
optionValues[option.key] = value
optionValues[identifiableOption.option.key] = value
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
import SwiftUI

/// View that provides access to core-specific options
struct CoreOptionsView: View {
var body: some View {
public struct CoreOptionsView: View {

public init() {}

public var body: some View {
CoreOptionsListView()
}
}

0 comments on commit 5247db4

Please sign in to comment.