Skip to content

Commit

Permalink
iOS: Badges wrapping view (#1116)
Browse files Browse the repository at this point in the history
^ALTAPPS-769
  • Loading branch information
ivan-magda authored Jul 24, 2024
1 parent 40e71ff commit c2e7bda
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ struct ProjectSelectionListGridCellBadgesView: View {
}

var body: some View {
if isEmpty {
EmptyView()
} else {
HStack(spacing: LayoutInsets.smallInset) {
if !isEmpty {
FlowLayoutCompatibility(
configuration: .init(
spacing: LayoutInsets.smallInset,
fallbackLayout: .horizontal()
)
) {
if isSelected {
BadgeView.selected()
}
Expand All @@ -40,24 +43,25 @@ struct ProjectSelectionListGridCellBadgesView: View {
}
}

struct ProjectSelectionListGridCellBadgesView_Previews: PreviewProvider {
static var previews: some View {
ProjectSelectionListGridCellBadgesView(
isSelected: true,
isIdeRequired: true,
isBeta: true,
isBestRated: true,
isFastestToComplete: true
)
.padding()

ProjectSelectionListGridCellBadgesView(
isSelected: true,
isIdeRequired: true,
isBeta: true,
isBestRated: true,
isFastestToComplete: true
)
.preferredColorScheme(.dark)
}
#if DEBUG
#Preview {
ProjectSelectionListGridCellBadgesView(
isSelected: true,
isIdeRequired: true,
isBeta: true,
isBestRated: true,
isFastestToComplete: true
)
}

#Preview {
ProjectSelectionListGridCellBadgesView(
isSelected: true,
isIdeRequired: true,
isBeta: true,
isBestRated: true,
isFastestToComplete: true
)
.preferredColorScheme(.dark)
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,25 @@ struct StepQuizCodeBlanksSuggestionsView: View {
let onSuggestionTap: (Suggestion) -> Void

var body: some View {
let views = ForEach(suggestions, id: \.self) { suggestion in
Button(
action: {
onSuggestionTap(suggestion)
},
label: {
StepQuizCodeBlanksOptionView(text: suggestion.text, isActive: true)
}
FlowLayoutCompatibility(
configuration: .init(
spacing: LayoutInsets.defaultInset,
fallbackLayout: .vertical()
)
.buttonStyle(BounceButtonStyle())
}

if #available(iOS 16.0, *) {
FlowLayout(spacing: LayoutInsets.defaultInset) {
views
}
.padding(LayoutInsets.defaultInset)
.frame(minHeight: 72)
} else {
VStack(alignment: .leading, spacing: LayoutInsets.defaultInset) {
views
) {
ForEach(suggestions, id: \.self) { suggestion in
Button(
action: {
onSuggestionTap(suggestion)
},
label: {
StepQuizCodeBlanksOptionView(text: suggestion.text, isActive: true)
}
)
.buttonStyle(BounceButtonStyle())
}
.padding(LayoutInsets.defaultInset)
.frame(minHeight: 72)
}
.padding(LayoutInsets.defaultInset)
.frame(minHeight: 72)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ struct StudyPlanSectionItemBadgesView: View {
}

var body: some View {
if isEmpty {
EmptyView()
} else {
HStack(spacing: LayoutInsets.smallInset) {
if !isEmpty {
FlowLayoutCompatibility(
configuration: .init(
spacing: LayoutInsets.smallInset,
fallbackLayout: .horizontal()
)
) {
if let formattedProgress {
BadgeView(text: formattedProgress, style: .green)
}
Expand All @@ -26,11 +29,11 @@ struct StudyPlanSectionItemBadgesView: View {
}
}

struct StudyPlanSectionItemBadgesView_Previews: PreviewProvider {
static var previews: some View {
StudyPlanSectionItemBadgesView(
formattedProgress: "50%",
isIdeRequired: true
)
}
#if DEBUG
#Preview {
StudyPlanSectionItemBadgesView(
formattedProgress: "50%",
isIdeRequired: true
)
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ struct TrackSelectionListGridCellBadgesView: View {
}

var body: some View {
if isEmpty {
EmptyView()
} else {
HStack(spacing: LayoutInsets.smallInset) {
if !isEmpty {
FlowLayoutCompatibility(
configuration: .init(
spacing: LayoutInsets.smallInset,
fallbackLayout: .horizontal()
)
) {
if isSelected {
BadgeView.selected()
}
Expand All @@ -35,23 +38,25 @@ struct TrackSelectionListGridCellBadgesView: View {
}
}

struct TrackSelectionListGridCellBadgesView_Previews: PreviewProvider {
static var previews: some View {
TrackSelectionListGridCellBadgesView(
isSelected: true,
isIdeRequired: true,
isBeta: true,
isCompleted: true
)
.padding()

TrackSelectionListGridCellBadgesView(
isSelected: true,
isIdeRequired: true,
isBeta: true,
isCompleted: true
)
.padding()
.preferredColorScheme(.dark)
}
#if DEBUG
#Preview {
TrackSelectionListGridCellBadgesView(
isSelected: true,
isIdeRequired: true,
isBeta: true,
isCompleted: true
)
.padding()
}

#Preview {
TrackSelectionListGridCellBadgesView(
isSelected: true,
isIdeRequired: true,
isBeta: true,
isCompleted: true
)
.padding()
.preferredColorScheme(.dark)
}
#endif
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
import SwiftUI

@available(iOS, deprecated: 16.0)
struct FlowLayoutCompatibility<Content>: View where Content: View {
let configuration: Configuration

let content: Content

init(
configuration: Configuration,
@ViewBuilder content: () -> Content
) {
self.configuration = configuration
self.content = content()
}

var body: some View {
if #available(iOS 16.0, *) {
FlowLayout(spacing: configuration.spacing) { content }
} else {
switch configuration.fallbackLayout {
case .vertical(let alignment):
VStack(alignment: alignment, spacing: configuration.spacing) { content }
case .horizontal(let alignment):
HStack(alignment: alignment, spacing: configuration.spacing) { content }
}
}
}

struct Configuration {
var spacing: CGFloat = LayoutInsets.smallInset

let fallbackLayout: FallbackLayout

enum FallbackLayout {
case vertical(alignment: HorizontalAlignment = .leading)
case horizontal(alignment: VerticalAlignment = .center)
}
}
}

@available(iOS 16.0, *)
struct FlowLayout: Layout {
var spacing: CGFloat = LayoutInsets.smallInset
Expand Down

0 comments on commit c2e7bda

Please sign in to comment.