Skip to content

Commit

Permalink
Merge pull request #94 from ky-is/progressview-label
Browse files Browse the repository at this point in the history
Add ProgressView labels
  • Loading branch information
aabewhite authored Dec 26, 2024
2 parents fbda8d9 + 7fad816 commit 770905c
Showing 1 changed file with 51 additions and 29 deletions.
80 changes: 51 additions & 29 deletions Sources/SkipUI/SkipUI/Components/ProgressView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
// as published by the Free Software Foundation https://fsf.org

#if SKIP
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.size
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.LinearProgressIndicator
Expand All @@ -18,51 +21,44 @@ import androidx.compose.ui.unit.dp
public struct ProgressView : View {
let value: Double?
let total: Double?
let label: ComposeBuilder?

public init() {
self.value = nil
self.total = nil
self.label = nil
}

@available(*, unavailable)
public init(@ViewBuilder label: () -> any View) {
self.value = nil
self.total = nil
self.init(value: nil, label: label)
}

@available(*, unavailable)
public init(_ titleKey: LocalizedStringKey) {
self.value = nil
self.total = nil
self.init(titleKey, value: nil)
}

@available(*, unavailable)
public init(_ title: String) {
self.value = nil
self.total = nil
self.init(title, value: nil)
}

public init(value: Double?, total: Double = 1.0) {
self.value = value
self.total = total
self.label = nil
}

@available(*, unavailable)
public init(value: Double?, total: Double = 1.0, @ViewBuilder label: () -> any View) {
self.value = value
self.total = total
self.label = ComposeBuilder.from(label)
}

@available(*, unavailable)
public init(_ titleKey: LocalizedStringKey, value: Double?, total: Double = 1.0) {
self.value = value
self.total = total
self.init(value: value, total: total, label: { Text(titleKey) })
}

@available(*, unavailable)
public init(_ title: String, value: Double?, total: Double = 1.0) {
self.value = value
self.total = total
self.init(value: value, total: total, label: { Text(verbatim: title) })
}

#if SKIP
Expand All @@ -72,23 +68,49 @@ public struct ProgressView : View {
style = value == nil ? .circular : .linear
}
if style == .linear {
let modifier = Modifier.fillWidth().then(context.modifier)
let color = EnvironmentValues.shared._tint?.colorImpl() ?? ProgressIndicatorDefaults.linearColor
if value == nil || total == nil {
LinearProgressIndicator(modifier: modifier, color: color)
if let label, !EnvironmentValues.shared._labelsHidden {
let contentContext = context.content()
Column(modifier: context.modifier, verticalArrangement: Arrangement.spacedBy(3.dp), horizontalAlignment: androidx.compose.ui.Alignment.Start) {
label.Compose(context: contentContext)
ComposeLinearProgress(context: contentContext)
}
} else {
LinearProgressIndicator(progress: Float(value! / total!), modifier: modifier, color: color)
ComposeLinearProgress(context: context)
}
} else {
let color = EnvironmentValues.shared._tint?.colorImpl() ?? ProgressIndicatorDefaults.circularColor
// Reduce size to better match SwiftUI
let indicatorModifier = Modifier.size(20.dp)
Box(modifier: context.modifier, contentAlignment: androidx.compose.ui.Alignment.Center) {
if value == nil || total == nil {
CircularProgressIndicator(modifier: indicatorModifier, color: color)
} else {
CircularProgressIndicator(progress: Float(value! / total!), modifier: indicatorModifier, color: color)
if let label, !EnvironmentValues.shared._labelsHidden {
let contentContext = context.content()
Column(modifier: context.modifier, verticalArrangement: Arrangement.spacedBy(4.dp), horizontalAlignment: androidx.compose.ui.Alignment.CenterHorizontally) {
ComposeCircularProgress(context: contentContext)
Row(horizontalArrangement: Arrangement.spacedBy(4.dp), verticalAlignment: androidx.compose.ui.Alignment.CenterVertically) {
label.Compose(context: contentContext)
}
}
} else {
ComposeCircularProgress(context: context)
}
}
}

@Composable private func ComposeLinearProgress(context: ComposeContext) {
let modifier = Modifier.fillWidth().then(context.modifier)
let color = EnvironmentValues.shared._tint?.colorImpl() ?? ProgressIndicatorDefaults.linearColor
if value == nil || total == nil {
LinearProgressIndicator(modifier: modifier, color: color)
} else {
LinearProgressIndicator(progress: Float(value! / total!), modifier: modifier, color: color)
}
}

@Composable private func ComposeCircularProgress(context: ComposeContext) {
let color = EnvironmentValues.shared._tint?.colorImpl() ?? ProgressIndicatorDefaults.circularColor
// Reduce size to better match SwiftUI
let indicatorModifier = Modifier.size(20.dp)
Box(modifier: context.modifier, contentAlignment: androidx.compose.ui.Alignment.Center) {
if value == nil || total == nil {
CircularProgressIndicator(modifier: indicatorModifier, color: color)
} else {
CircularProgressIndicator(progress: Float(value! / total!), modifier: indicatorModifier, color: color)
}
}
}
Expand Down

0 comments on commit 770905c

Please sign in to comment.