Skip to content

Commit

Permalink
Add toogle styles and refactor row
Browse files Browse the repository at this point in the history
  • Loading branch information
aromanov91 committed Dec 22, 2022
1 parent 11e1fc6 commit 4098388
Show file tree
Hide file tree
Showing 13 changed files with 478 additions and 266 deletions.
2 changes: 1 addition & 1 deletion Sources/OversizeUI/Controls/Badge/Bage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public struct Bage<Label: View>: View {
.padding(.horizontal, 6)
.background(
RoundedRectangle(cornerRadius: controlRadius,
style: .circular)
style: .continuous)
.fill(color.opacity(0.1))
.overlay(
RoundedRectangle(cornerRadius: controlRadius,
Expand Down
16 changes: 8 additions & 8 deletions Sources/OversizeUI/Controls/Button/Button.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public struct OversizeButtonStyle: ButtonStyle {
}

@ViewBuilder
func background(for role: ButtonRole?) -> some View {
private func background(for role: ButtonRole?) -> some View {
if type != .quaternary {
switch controlBorderShape {
case .capsule:
Expand All @@ -72,7 +72,7 @@ public struct OversizeButtonStyle: ButtonStyle {
}

/// Parameters
func backgroundColor(for role: ButtonRole?) -> Color {
private func backgroundColor(for role: ButtonRole?) -> Color {
switch type {
case .primary:
switch role {
Expand All @@ -94,7 +94,7 @@ public struct OversizeButtonStyle: ButtonStyle {
}
}

func foregroundColor(for role: ButtonRole?) -> Color {
private func foregroundColor(for role: ButtonRole?) -> Color {
switch type {
case .primary:
return Color.onPrimaryHighEmphasis
Expand Down Expand Up @@ -124,7 +124,7 @@ public struct OversizeButtonStyle: ButtonStyle {
}

@ViewBuilder
func loadingView(for role: ButtonRole?) -> some View {
private func loadingView(for role: ButtonRole?) -> some View {
if isLoading {
ProgressView()
.progressViewStyle(CircularProgressViewStyle(tint: foregroundColor(for: role)))
Expand All @@ -133,7 +133,7 @@ public struct OversizeButtonStyle: ButtonStyle {
}
}

var horizontalPadding: Space {
private var horizontalPadding: Space {
switch controlSize {
case .mini:
return .xxSmall
Expand All @@ -148,7 +148,7 @@ public struct OversizeButtonStyle: ButtonStyle {
}
}

var verticalPadding: Space {
private var verticalPadding: Space {
switch controlSize {
case .mini:
return .xxSmall
Expand All @@ -163,11 +163,11 @@ public struct OversizeButtonStyle: ButtonStyle {
}
}

var backgroundOpacity: CGFloat {
private var backgroundOpacity: CGFloat {
isEnabled ? 1 : 0.5
}

var foregroundOpacity: CGFloat {
private var foregroundOpacity: CGFloat {
isEnabled ? 1 : 0.7
}

Expand Down
70 changes: 70 additions & 0 deletions Sources/OversizeUI/Controls/Label/RowLabelStyle.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//
// Copyright © 2022 Alexander Romanov
// RowLabelStyle.swift
//

import SwiftUI

public struct RowLabelStyle: LabelStyle {
@Environment(\.sizeCategory) var sizeCategory
@Environment(\.multilineTextAlignment) var multilineTextAlignment

private let subtitle: String?

public init(subtitle: String? = nil) {
self.subtitle = subtitle
}

public func makeBody(configuration: Configuration) -> some View {
HStack(spacing: .xSmall) {
configuration.icon
.headline()
.foregroundColor(.onSurfaceHighEmphasis)

VStack(alignment: textAlignment, spacing: .xxxSmall) {
configuration.title
.headline(.semibold)
.foregroundColor(.onSurfaceHighEmphasis)

if let subtitle, !subtitle.isEmpty {
Text(subtitle)
.subheadline()
.foregroundColor(.onSurfaceMediumEmphasis)
}
}
.multilineTextAlignment(multilineTextAlignment)
}
.multilineTextAlignment(multilineTextAlignment)
}

private var textAlignment: HorizontalAlignment {
switch multilineTextAlignment {
case .leading:
return .leading
case .center:
return .center
case .trailing:
return .trailing
}
}
}

public extension LabelStyle where Self == RowLabelStyle {
static var row: RowLabelStyle { .init() }

static func row(_ subtitle: String?) -> RowLabelStyle {
RowLabelStyle(subtitle: subtitle)
}
}

struct RowLabelStyle_Previews: PreviewProvider {
static var previews: some View {
VStack(spacing: .large) {
Label("Text", systemImage: "sun.max")
.labelStyle(.row)

Label("Text", systemImage: "sun.max")
.labelStyle(.row("Subtitle"))
}
}
}
194 changes: 16 additions & 178 deletions Sources/OversizeUI/Controls/Row/Row.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,6 @@

import SwiftUI

public enum RowTrailingType {
case radio(isOn: Binding<Bool>)
case checkbox(isOn: Binding<Bool>)
case toggle(isOn: Binding<Bool>)
case toggleWithArrowButton(isOn: Binding<Bool>, action: (() -> Void)? = nil)
@available(watchOS, unavailable)
case timePicker(date: Binding<Date>)
case arrowIcon
case text(_ text: String)
case button(_ text: String, action: () -> Void)
}

public enum RowLeadingType {
case icon(_ name: IconsNames)
case iconOnSurface(_ name: IconsNames)
case image(_ image: Image, color: Color? = .onSurfaceHighEmphasis)
case imageOnSurface(_ image: Image, color: Color? = nil)
case systemImage(_ imageName: String)
case avatar(_ avatar: AvatarView)
case view(_ view: AnyView)
}

public enum RowClearIconStyle {
case `default`, onSurface
}
Expand Down Expand Up @@ -104,7 +82,7 @@ public struct Row: View {
VStack(alignment: .leading) {
HStack(spacing: .zero) {
if let leadingType {
leading(leadingType)
RowLeading(leadingType, isShowSubtitle: isShowSubtitle, iconBackgroundColor: iconBackgroundColor)
.padding(.trailing, .small)
}

Expand All @@ -128,7 +106,7 @@ public struct Row: View {
сlearButton

if let trallingType {
tralling(trallingType)
RowTrailing(trallingType, isPremiumOption: isPremiumOption)
.padding(.leading, .xxSmall)
}
}
Expand All @@ -137,6 +115,20 @@ public struct Row: View {
.padding(.horizontal, controlPadding.horizontal)
}

private var text: some View {
VStack(alignment: textAlignment, spacing: .xxxSmall) {
Text(title)
.headline(.semibold)
.foregroundColor(.onSurfaceHighEmphasis)
if let subtitle, !subtitle.isEmpty {
Text(subtitle)
.subheadline()
.foregroundColor(.onSurfaceMediumEmphasis)
}
}
.multilineTextAlignment(multilineTextAlignment)
}

@ViewBuilder
private var сlearButton: some View {
if сlearAction != nil {
Expand All @@ -155,146 +147,6 @@ public struct Row: View {
}
}

@ViewBuilder
private func leading(_ leadingType: RowLeadingType) -> some View {
switch leadingType {
case let .icon(icon):
Icon(icon)

case let .image(image, color):
image
.renderingMode(color != nil ? .template : .original)
.resizable()
.scaledToFill()
.foregroundColor(color)
.frame(width: isShowSubtitle ? 48 : 24, height: isShowSubtitle ? 48 : 24)
.cornerRadius(isShowSubtitle ? 4 : 2)

case let .avatar(avatar):
avatar

case let .iconOnSurface(icon):
Surface {
Icon(icon)
}
.surfaceStyle(.secondary)
.surfaceBackgroundColor(iconBackgroundColor)
.controlPadding(.xxSmall)

case let .imageOnSurface(image, color):
Surface {
image
.renderingMode(.template)
.foregroundColor(color)
}
.surfaceStyle(.secondary)
.surfaceBackgroundColor(iconBackgroundColor)
.controlPadding(.xxSmall)

case let .systemImage(systemImage):
Image(systemName: systemImage)
.foregroundColor(Color.onBackgroundHighEmphasis)
.font(.system(size: 24))
.frame(width: 24, height: 24, alignment: .center)

case let .view(view):
view
}
}

// swiftlint:disable function_body_length
@ViewBuilder
private func tralling(_ trallingType: RowTrailingType) -> some View {
switch trallingType {
case let .toggle(isOn):
Toggle(isOn: isOn) {}
.labelsHidden()
.disabled(isPremiumOption && premiumStatus == false)

case let .radio(isOn: isOn):
ZStack {
Circle()
.stroke(Color.onSurfaceDisabled, lineWidth: 4)
.frame(width: 24, height: 24)
.cornerRadius(12)
.opacity(isOn.wrappedValue ? 0 : 1)

Circle().fill(Color.accent)
.frame(width: 24, height: 24)
.cornerRadius(12)
.opacity(isOn.wrappedValue ? 1 : 0)

Circle().fill(Color.white).frame(width: 8, height: 8)
.cornerRadius(4)
.opacity(isOn.wrappedValue ? 1 : 0)
}

case let .checkbox(isOn: isOn):
ZStack {
RoundedRectangle(cornerRadius: Radius.small, style: .continuous)
.strokeBorder(Color.onSurfaceDisabled, lineWidth: 2.5)
.frame(width: 24, height: 24)
.opacity(isOn.wrappedValue ? 0 : 1)

RoundedRectangle(cornerRadius: Radius.small, style: .continuous).fill(Color.accent)
.frame(width: 24, height: 24)
.opacity(isOn.wrappedValue ? 1 : 0)

Image(systemName: "checkmark")
.font(.caption.weight(.black))
.foregroundColor(.onPrimaryHighEmphasis)
.opacity(isOn.wrappedValue ? 1 : 0)
}

case let .toggleWithArrowButton(isOn: isOn, action: action):
HStack {
Toggle(isOn: isOn) {}
.labelsHidden()

Button(action: action ?? {}, label: {
Icon(.chevronRight, color: .onSurfaceDisabled)
})
}
.disabled(isPremiumOption && premiumStatus == false)

case .arrowIcon:
Icon(.chevronRight, color: .onSurfaceDisabled)

case let .timePicker(date: date):
#if os(watchOS)
EmptyView()
#elseif os(iOS)
DatePicker("", selection: date, displayedComponents: .hourAndMinute)
.labelsHidden()
#endif
case let .text(text):
Text(text)
.subheadline()
.foregroundColor(.onSurfaceMediumEmphasis)

case let .button(text, action: action):
Button(text, action: action)
.buttonStyle(.tertiary)
.controlBorderShape(.capsule)
.controlSize(.small)
.disabled(isPremiumOption && premiumStatus == false)
}
}

private var text: some View {
VStack(alignment: textAlignment, spacing: .xxxSmall) {
Text(title)
.headline(.semibold)
.foregroundColor(.onSurfaceHighEmphasis)
if let subtitle, !subtitle.isEmpty {
Text(subtitle)
.subheadline()
.foregroundColor(.onSurfaceMediumEmphasis)
}
}
.multilineTextAlignment(multilineTextAlignment)
}

@ViewBuilder
private var premiumLabel: some View {
if isPremiumOption, premiumStatus == false {
Expand Down Expand Up @@ -368,20 +220,6 @@ public extension View {
}
}

public struct RowActionButtonStyle: ButtonStyle {
public func makeBody(configuration: Self.Configuration) -> some View {
configuration.label
.background(configuration.isPressed ? Color.surfaceSecondary : Color.clear)
.contentShape(Rectangle())
}
}

public extension ButtonStyle where Self == RowActionButtonStyle {
static var row: RowActionButtonStyle {
RowActionButtonStyle()
}
}

// swiftlint:disable all
struct ListRow_Previews: PreviewProvider {
static var previews: some View {
Expand Down
Loading

0 comments on commit 4098388

Please sign in to comment.