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

Update SettingToggle to support custom icon #37

Open
wants to merge 1 commit 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
33 changes: 32 additions & 1 deletion Sources/Views/SettingToggle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import SwiftUI
*/
public struct SettingToggle: View, Setting {
public var id: AnyHashable?
public var icon: SettingIcon?

public var title: String
@Binding public var isOn: Bool
public var horizontalSpacing = CGFloat(12)
Expand All @@ -21,13 +23,16 @@ public struct SettingToggle: View, Setting {

public init(
id: AnyHashable? = nil,
icon: SettingIcon? = nil,

title: String,
isOn: Binding<Bool>,
horizontalSpacing: CGFloat = CGFloat(12),
verticalPadding: CGFloat = CGFloat(14),
horizontalPadding: CGFloat? = nil
) {
self.id = id
self.icon = icon
self.title = title
self._isOn = isOn
self.horizontalSpacing = horizontalSpacing
Expand All @@ -37,6 +42,7 @@ public struct SettingToggle: View, Setting {

public var body: some View {
SettingToggleView(
icon: icon,
title: title,
isOn: $isOn,
horizontalSpacing: horizontalSpacing,
Expand All @@ -48,7 +54,7 @@ public struct SettingToggle: View, Setting {

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

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

Expand All @@ -58,6 +64,9 @@ struct SettingToggleView: View {

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)
Expand All @@ -70,3 +79,25 @@ struct SettingToggleView: View {
.accessibilityElement(children: .combine)
}
}

public extension SettingToggle {
func icon(_ icon: String, color: Color = .blue) -> SettingToggle {
var toggle = self
toggle.icon = .system(icon: icon, backgroundColor: color)
return toggle
}

func icon(_ icon: String, foregroundColor: Color = .white, backgroundColor: Color = .blue) -> SettingToggle {
var toggle = self
toggle.icon = .system(icon: icon, foregroundColor: foregroundColor, backgroundColor: backgroundColor)
return toggle
}

func icon(icon: SettingIcon) -> SettingToggle {
var toggle = self
toggle.icon = icon
return toggle
}


}