1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 23:00:32 +05:30
yattee/Shared/Settings/MultiselectRow.swift
Arkadiusz Fal 611bcde238 Fix #338
2022-12-04 13:32:24 +01:00

49 lines
1.3 KiB
Swift

import SwiftUI
struct MultiselectRow: View {
let title: String
var selected: Bool
var disabled = false
var action: (Bool) -> Void
@State private var toggleChecked = false
var body: some View {
#if os(macOS)
Toggle(title, isOn: $toggleChecked)
.toggleStyle(.checkbox)
.onAppear {
guard !disabled else { return }
toggleChecked = selected
}
.onChange(of: toggleChecked) { new in
action(new)
}
#else
Button(action: { action(!selected) }) {
HStack {
Text(self.title)
Spacer()
if selected {
Image(systemName: "checkmark")
#if os(iOS)
.foregroundColor(.accentColor)
#endif
}
}
.contentShape(Rectangle())
}
.disabled(disabled)
#if !os(tvOS)
.buttonStyle(.plain)
#endif
#endif
}
}
struct MultiselectRow_Previews: PreviewProvider {
static var previews: some View {
MultiselectRow(title: "Title", selected: false) { _ in }
}
}