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

add action to a SettingToggle control #38

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
32 changes: 22 additions & 10 deletions Sources/Views/SettingToggle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,77 @@

import SwiftUI

/**
A simple toggle.
*/
public struct SettingToggle: View, Setting {
public var id: AnyHashable?
public var title: String
@Binding public var isOn: Bool
public var icon: SettingIcon?
public var horizontalSpacing = CGFloat(12)
public var verticalPadding = CGFloat(14)
public var horizontalPadding: CGFloat? = nil
public var horizontalPadding = CGFloat(16)
public var onChange: ((Bool) -> Void)? // Add onChange closure

public init(
id: AnyHashable? = nil,
icon: SettingIcon? = nil,
title: String,
isOn: Binding<Bool>,
horizontalSpacing: CGFloat = CGFloat(12),
verticalPadding: CGFloat = CGFloat(14),
horizontalPadding: CGFloat? = nil
horizontalPadding: CGFloat = CGFloat(16),
onChange: ((Bool) -> Void)? = nil // Initialize onChange closure
) {
self.id = id
self.icon = icon
self.title = title
self._isOn = isOn
self.horizontalSpacing = horizontalSpacing
self.verticalPadding = verticalPadding
self.horizontalPadding = horizontalPadding
self.onChange = onChange // Assign onChange closure
}

public var body: some View {
SettingToggleView(
icon: icon,
title: title,
isOn: $isOn,
horizontalSpacing: horizontalSpacing,
verticalPadding: verticalPadding,
horizontalPadding: horizontalPadding
horizontalPadding: horizontalPadding,
onChange: onChange // Pass onChange closure to SettingToggleView
)
}
}

struct SettingToggleView: View {
@Environment(\.edgePadding) var edgePadding

let icon: SettingIcon?
let title: String
@Binding var isOn: Bool

var horizontalSpacing = CGFloat(12)
var verticalPadding = CGFloat(14)
var horizontalPadding: CGFloat? = nil
var horizontalPadding = CGFloat(16)
var onChange: ((Bool) -> Void)? // Receive onChange closure

var body: some View {
HStack(spacing: horizontalSpacing) {
if let icon {
SettingIconView(icon: icon)
}

Text(title)
.fixedSize(horizontal: false, vertical: true)
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.vertical, verticalPadding)

Toggle("", isOn: $isOn)
.labelsHidden()
.onChange(of: isOn, perform: { newValue in
onChange?(newValue) // Call onChange closure
})
}
.padding(.horizontal, horizontalPadding ?? edgePadding)
.padding(.horizontal, horizontalPadding)
.accessibilityElement(children: .combine)
}
}