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

Solid Counter #121

Merged
merged 7 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
55 changes: 42 additions & 13 deletions Sources/Compound/List/ListRowDetails.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,48 +14,77 @@ import SwiftUI
public struct ListRowDetails<Icon: View> {
var title: String?
var icon: Icon?
var counter: Int?

var isWaiting = false

// MARK: - Initialisers

public static func label(title: String,
icon: Icon,
counter: Int? = nil,
isWaiting: Bool = false) -> Self {
ListRowDetails(title: title, icon: icon, isWaiting: isWaiting)
ListRowDetails(title: title,
icon: icon,
counter: counter,
isWaiting: isWaiting)
}

public static func label(title: String,
icon: KeyPath<CompoundIcons, Image>,
counter: Int? = nil,
isWaiting: Bool = false) -> Self where Icon == CompoundIcon {
ListRowDetails(title: title, icon: CompoundIcon(icon), isWaiting: isWaiting)
ListRowDetails(title: title,
icon: CompoundIcon(icon),
counter: counter,
isWaiting: isWaiting)
}

public static func label(title: String,
systemIcon: SFSymbol,
counter: Int? = nil,
isWaiting: Bool = false) -> Self where Icon == Image {
ListRowDetails(title: title, icon: Image(systemSymbol: systemIcon), isWaiting: isWaiting)
ListRowDetails(title: title,
icon: Image(systemSymbol: systemIcon),
counter: counter,
isWaiting: isWaiting)
}

public static func icon(_ icon: Icon, isWaiting: Bool = false) -> Self {
ListRowDetails(icon: icon, isWaiting: isWaiting)
public static func icon(_ icon: Icon,
counter: Int? = nil,
isWaiting: Bool = false) -> Self {
ListRowDetails(icon: icon,
counter: counter,
isWaiting: isWaiting)
}

public static func icon(_ icon: KeyPath<CompoundIcons, Image>, isWaiting: Bool = false) -> Self where Icon == CompoundIcon {
ListRowDetails(icon:CompoundIcon(icon), isWaiting: isWaiting)
public static func icon(_ icon: KeyPath<CompoundIcons, Image>,
counter: Int? = nil,
isWaiting: Bool = false) -> Self where Icon == CompoundIcon {
ListRowDetails(icon:CompoundIcon(icon),
counter: counter,
isWaiting: isWaiting)
}

public static func systemIcon(_ systemIcon: SFSymbol, isWaiting: Bool = false) -> Self where Icon == Image {
ListRowDetails(icon: Image(systemSymbol: systemIcon), isWaiting: isWaiting)
public static func systemIcon(_ systemIcon: SFSymbol,
counter: Int? = nil,
isWaiting: Bool = false) -> Self where Icon == Image {
ListRowDetails(icon: Image(systemSymbol: systemIcon),
counter: counter,
isWaiting: isWaiting)
}
}

public extension ListRowDetails where Icon == Image {
static func title(_ title: String, isWaiting: Bool = false) -> Self {
ListRowDetails(title: title, isWaiting: isWaiting)
static func title(_ title: String,
counter: Int? = nil,
isWaiting: Bool = false) -> Self {
ListRowDetails(title: title,
counter: counter,
isWaiting: isWaiting)
}

static func isWaiting(_ isWaiting: Bool) -> Self {
ListRowDetails(isWaiting: isWaiting)
static func isWaiting(counter: Int? = nil, _ isWaiting: Bool) -> Self {
ListRowDetails(counter: counter, isWaiting: isWaiting)
Velin92 marked this conversation as resolved.
Show resolved Hide resolved
}
}
32 changes: 26 additions & 6 deletions Sources/Compound/List/ListRowTrailingSection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,22 @@ private struct ListRowDetailsLabelStyle: LabelStyle {
/// The view shown to the right of the `ListRowLabel` inside of a `ListRow`.
/// This consists of both the `ListRowDetails` and the `ListRowAccessory`.
public struct ListRowTrailingSection<Icon: View>: View {
var title: String?
var icon: Icon?

var isWaiting = false
var accessory: ListRowAccessory?
@Environment(\.isEnabled) private var isEnabled
Velin92 marked this conversation as resolved.
Show resolved Hide resolved

private var title: String?
private var icon: Icon?
private var counter: Int?
private var isWaiting = false
private var accessory: ListRowAccessory?

@ScaledMetric private var iconSize = 24
private var hideAccessory: Bool { isWaiting && accessory?.kind == .unselected }

init(_ details: ListRowDetails<Icon>?, accessory: ListRowAccessory? = nil) {
init(_ details: ListRowDetails<Icon>?, solidCounter: Int? = nil, accessory: ListRowAccessory? = nil) {
Velin92 marked this conversation as resolved.
Show resolved Hide resolved
title = details?.title
icon = details?.icon
isWaiting = details?.isWaiting ?? false
counter = details?.counter
self.accessory = accessory
}

Expand All @@ -59,6 +62,16 @@ public struct ListRowTrailingSection<Icon: View>: View {
.labelStyle(ListRowDetailsLabelStyle())
}

if let counter {
Text("\(counter)")
.font(.compound.bodyXSSemibold)
.foregroundStyle(.compound.textOnSolidPrimary)
.padding(.horizontal, 6)
.padding(.vertical, 2)
.background { Capsule().fill(isEnabled ? .compound.iconSuccessPrimary : .compound.iconDisabled) }
}


if let accessory, !hideAccessory {
accessory
}
Expand Down Expand Up @@ -96,6 +109,10 @@ struct ListRowTrailingSection_Previews: PreviewProvider, PrefireProvider {
ListRowTrailingSection(.title("Hello"))

ListRowTrailingSection(someCondition ? .isWaiting(true) : otherCondition ? .systemIcon(.checkmark) : .title("Hello"))

ListRowTrailingSection(.title("Hello"), solidCounter: 1)
ListRowTrailingSection(.title("Hello"), solidCounter: 1)
.disabled(true)
}
}

Expand All @@ -111,6 +128,9 @@ struct ListRowTrailingSection_Previews: PreviewProvider, PrefireProvider {
// The checkmark's space should be reserved.
ListRowTrailingSection(.isWaiting(false), accessory: .selection(false))
.border(.purple)

ListRowTrailingSection(.isWaiting(false), solidCounter: 1, accessory: .navigationLink)
.border(.purple)
}
}
}
Loading