Files
diehammer/DieHammer/Utilities/ViewUtils.swift
T

79 lines
2.1 KiB
Swift

//
// ViewUtiles.swift
// Bunch O' Dice
//
// Created by Rory Hinnen on 22/6/12.
//
import Foundation
import SwiftUI
class SheetState<State>: ObservableObject {
@Published var isShowing = false
@Published var state: State? {
didSet { isShowing = state != nil }
}
}
class SettingsSheet: SheetState<SettingsSheet.State> {
enum State {
case settings, history
}
}
extension CaseIterable where Self: Equatable {
mutating func next() {
let allCases = Self.allCases
// just a sanity check, as the possibility of a enum case to not be
// present in `allCases` is quite low
guard let selfIndex = allCases.firstIndex(of: self) else { return }
let nextIndex = Self.allCases.index(after: selfIndex)
self = allCases[nextIndex == allCases.endIndex ? allCases.startIndex : nextIndex]
}
}
extension View {
func disabledWithOpacity(_ condition: Bool) -> some View {
self.disabled(condition).opacity(condition ? 0.4 : 1.0)
}
}
struct ToggleChip: View {
let label: String
@Binding var isOn: Bool
var body: some View {
Button(action: { isOn.toggle() }) {
Text(label)
.fontWeight(isOn ? .semibold : .regular)
.padding(.horizontal, 12)
.padding(.vertical, 6)
.foregroundColor(isOn ? Color(UIColor.systemBackground) : .primary)
.background(isOn ? Color.primary : Color.clear)
.clipShape(Capsule())
.overlay(Capsule().stroke(Color.primary, lineWidth: 1))
}
}
}
struct StatStepper: View {
let top: String
let value: String
let bottom: String
let increment: () -> Void
let decrement: () -> Void
var body: some View {
VStack {
Text(top)
Group {
Text("▲").gesture(TapGesture().onEnded { increment() })
Text(value)
Text("▼").gesture(TapGesture().onEnded { decrement() })
}
.font(.system(size: 30))
Text(bottom)
}
}
}