1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 22:00:31 +05:30
yattee/Shared/Settings/MultiselectRow.swift

51 lines
1.4 KiB
Swift
Raw Normal View History

2022-08-13 19:42:45 +05:30
import SwiftUI
struct MultiselectRow: View {
let title: String
2022-08-14 22:32:36 +05:30
var selected: Bool
var disabled = false
2022-08-13 19:42:45 +05:30
var action: (Bool) -> Void
@State private var toggleChecked = false
var body: some View {
Button(action: { action(!selected) }) {
HStack {
#if os(macOS)
Toggle(isOn: $toggleChecked) {
Text(self.title)
Spacer()
}
.onAppear {
2022-08-14 22:32:36 +05:30
guard !disabled else { return }
2022-08-13 19:42:45 +05:30
toggleChecked = selected
}
.onChange(of: toggleChecked) { new in
action(new)
}
#else
Text(self.title)
Spacer()
if selected {
Image(systemName: "checkmark")
#if os(iOS)
.foregroundColor(.accentColor)
#endif
}
#endif
}
.contentShape(Rectangle())
}
2022-08-14 22:32:36 +05:30
.disabled(disabled)
2022-08-13 19:42:45 +05:30
#if !os(tvOS)
2022-08-14 22:32:36 +05:30
.buttonStyle(.plain)
2022-08-13 19:42:45 +05:30
#endif
}
}
struct MultiselectRow_Previews: PreviewProvider {
static var previews: some View {
2022-08-25 22:39:55 +05:30
MultiselectRow(title: "Title", selected: false) { _ in }
2022-08-13 19:42:45 +05:30
}
}