From df9303418a621f520b73603456b836c9a09f4a35 Mon Sep 17 00:00:00 2001 From: kyle Date: Wed, 25 Dec 2024 16:27:38 -0800 Subject: [PATCH 1/4] Support ProgressView label initializers --- .../SkipUI/Components/ProgressView.swift | 71 ++++++++++++------- 1 file changed, 46 insertions(+), 25 deletions(-) diff --git a/Sources/SkipUI/SkipUI/Components/ProgressView.swift b/Sources/SkipUI/SkipUI/Components/ProgressView.swift index 02090dc3..11eb5d53 100644 --- a/Sources/SkipUI/SkipUI/Components/ProgressView.swift +++ b/Sources/SkipUI/SkipUI/Components/ProgressView.swift @@ -5,7 +5,9 @@ // 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.size import androidx.compose.material3.CircularProgressIndicator import androidx.compose.material3.LinearProgressIndicator @@ -18,51 +20,48 @@ import androidx.compose.ui.unit.dp public struct ProgressView : View { let value: Double? let total: Double? + let label: (any View)? 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 = label() } - @available(*, unavailable) public init(_ titleKey: LocalizedStringKey, value: Double?, total: Double = 1.0) { self.value = value self.total = total + self.label = Text(titleKey) } - @available(*, unavailable) public init(_ title: String, value: Double?, total: Double = 1.0) { self.value = value self.total = total + self.label = Text(verbatim: title) } #if SKIP @@ -72,23 +71,45 @@ 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 { + Column(modifier: context.modifier, verticalArrangement: Arrangement.spacedBy(4.dp), horizontalAlignment: androidx.compose.ui.Alignment.Start) { + label.Compose(context: context.content()) + ComposeLinearProgress(context: context) + } } 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 { + Column(modifier: context.modifier, verticalArrangement: Arrangement.spacedBy(8.dp), horizontalAlignment: androidx.compose.ui.Alignment.CenterHorizontally) { + ComposeCircularProgress(context: context) + label.Compose(context: context.content()) } + } 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) } } } From 442f31421ee9bff6daa51b50e4e0724f1bb4f919 Mon Sep 17 00:00:00 2001 From: kyle Date: Thu, 26 Dec 2024 09:11:07 -0800 Subject: [PATCH 2/4] Update styling to better match SwiftUI --- Sources/SkipUI/SkipUI/Components/ProgressView.swift | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Sources/SkipUI/SkipUI/Components/ProgressView.swift b/Sources/SkipUI/SkipUI/Components/ProgressView.swift index 11eb5d53..9c6461c8 100644 --- a/Sources/SkipUI/SkipUI/Components/ProgressView.swift +++ b/Sources/SkipUI/SkipUI/Components/ProgressView.swift @@ -8,6 +8,7 @@ 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 @@ -72,7 +73,7 @@ public struct ProgressView : View { } if style == .linear { if let label, !EnvironmentValues.shared._labelsHidden { - Column(modifier: context.modifier, verticalArrangement: Arrangement.spacedBy(4.dp), horizontalAlignment: androidx.compose.ui.Alignment.Start) { + Column(modifier: context.modifier, verticalArrangement: Arrangement.spacedBy(3.dp), horizontalAlignment: androidx.compose.ui.Alignment.Start) { label.Compose(context: context.content()) ComposeLinearProgress(context: context) } @@ -81,9 +82,11 @@ public struct ProgressView : View { } } else { if let label, !EnvironmentValues.shared._labelsHidden { - Column(modifier: context.modifier, verticalArrangement: Arrangement.spacedBy(8.dp), horizontalAlignment: androidx.compose.ui.Alignment.CenterHorizontally) { + Column(modifier: context.modifier, verticalArrangement: Arrangement.spacedBy(4.dp), horizontalAlignment: androidx.compose.ui.Alignment.CenterHorizontally) { ComposeCircularProgress(context: context) - label.Compose(context: context.content()) + Row(modifier: context.modifier, horizontalArrangement: Arrangement.spacedBy(4.dp), verticalAlignment: androidx.compose.ui.Alignment.CenterVertically) { + label.Compose(context: context.content()) + } } } else { ComposeCircularProgress(context: context) From bee04203250cf304788e1b61b6815553b932a173 Mon Sep 17 00:00:00 2001 From: kyle Date: Thu, 26 Dec 2024 10:03:41 -0800 Subject: [PATCH 3/4] Refactor label to ComposeBuilder --- Sources/SkipUI/SkipUI/Components/ProgressView.swift | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Sources/SkipUI/SkipUI/Components/ProgressView.swift b/Sources/SkipUI/SkipUI/Components/ProgressView.swift index 9c6461c8..b52dd221 100644 --- a/Sources/SkipUI/SkipUI/Components/ProgressView.swift +++ b/Sources/SkipUI/SkipUI/Components/ProgressView.swift @@ -21,7 +21,7 @@ import androidx.compose.ui.unit.dp public struct ProgressView : View { let value: Double? let total: Double? - let label: (any View)? + let label: ComposeBuilder? public init() { self.value = nil @@ -50,19 +50,15 @@ public struct ProgressView : View { public init(value: Double?, total: Double = 1.0, @ViewBuilder label: () -> any View) { self.value = value self.total = total - self.label = label() + self.label = ComposeBuilder.from(label) } public init(_ titleKey: LocalizedStringKey, value: Double?, total: Double = 1.0) { - self.value = value - self.total = total - self.label = Text(titleKey) + self.init(value: value, total: total, label: { Text(titleKey) }) } public init(_ title: String, value: Double?, total: Double = 1.0) { - self.value = value - self.total = total - self.label = Text(verbatim: title) + self.init(value: value, total: total, label: { Text(verbatim: title) }) } #if SKIP From 7fad8169dc0eddd8e6cb88a1140045dc0c385743 Mon Sep 17 00:00:00 2001 From: kyle Date: Thu, 26 Dec 2024 10:04:34 -0800 Subject: [PATCH 4/4] Fix do not apply container modifiers to inner content --- Sources/SkipUI/SkipUI/Components/ProgressView.swift | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Sources/SkipUI/SkipUI/Components/ProgressView.swift b/Sources/SkipUI/SkipUI/Components/ProgressView.swift index b52dd221..d267f463 100644 --- a/Sources/SkipUI/SkipUI/Components/ProgressView.swift +++ b/Sources/SkipUI/SkipUI/Components/ProgressView.swift @@ -69,19 +69,21 @@ public struct ProgressView : View { } if style == .linear { 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: context.content()) - ComposeLinearProgress(context: context) + label.Compose(context: contentContext) + ComposeLinearProgress(context: contentContext) } } else { ComposeLinearProgress(context: context) } } else { 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: context) - Row(modifier: context.modifier, horizontalArrangement: Arrangement.spacedBy(4.dp), verticalAlignment: androidx.compose.ui.Alignment.CenterVertically) { - label.Compose(context: context.content()) + ComposeCircularProgress(context: contentContext) + Row(horizontalArrangement: Arrangement.spacedBy(4.dp), verticalAlignment: androidx.compose.ui.Alignment.CenterVertically) { + label.Compose(context: contentContext) } } } else {