Skip to content

Commit

Permalink
IOS-9445 Hide last divider (#326)
Browse files Browse the repository at this point in the history
  • Loading branch information
pbartolome committed Dec 22, 2023
1 parent 74fce28 commit 9058967
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 51 deletions.
16 changes: 10 additions & 6 deletions MisticaCatalog/Source/Catalog/CatalogList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,21 @@ struct CatalogList: View {
Cell(
style: .fullwidth,
title: row.title,
subtitle: nil,
description: nil,
assetType: .smallIcon(
Image(uiImage: row.icon),
foregroundColor: nil
),
presetView: { CellNavigationPreset() }
presetView: { CellNavigationPreset() },
headlineView: {},
destinationView: {
componentView(for: row)
.navigationTitle(row.title)
.navigationBarTitleDisplayMode(.inline)
}
)
.navigationLink {
componentView(for: row)
.navigationTitle(row.title)
.navigationBarTitleDisplayMode(.inline)
}
.shouldShowDivider(row != rows.last)
}
}
.misticaListStyle()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,19 @@ struct ListCatalogView: View {

NavigationLink("Show List") {
List {
ForEach(0 ..< 50) { _ in
let range = 0 ..< 20
ForEach(range, id: \.self) { count in
Cell(
style: styles[selectedStyleIndex],
title: title,
subtitle: subtitle.isEmpty ? nil : subtitle,
description: description.isEmpty ? nil : description,
assetType: assetTypes[selectedAssetTypeIndex],
presetView: { presetView },
headlineView: { headlineView }
headlineView: { headlineView },
destinationView: { Text("Detail view \(count)") }
)
.navigationLink(to: { Text("Detail view") })
.shouldShowDivider(count != range.last)
}
}
.misticaListStyle()
Expand Down
1 change: 1 addition & 0 deletions MisticaCatalog/Source/ColorsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct ColorsView: View {
description: item.color.hexString.uppercased(),
assetType: .roundImage(circle(with: item.color))
)
.shouldShowDivider(item.name != colors.last?.name)
}
}
.misticaListStyle()
Expand Down
54 changes: 12 additions & 42 deletions Sources/MisticaSwiftUI/Components/List/Cell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public struct Cell<PresetView: View, HeadlineView: View, Destination: View>: Vie
private var backgroundColor = Color.backgroundContainer
private var pressedBackgroundColor = Color.neutralLow.opacity(0.8)
private var allowsPressing = true
private var shouldShowDivider = true

@State var isPressed = false

Expand Down Expand Up @@ -90,7 +91,9 @@ public struct Cell<PresetView: View, HeadlineView: View, Destination: View>: Vie

case .fullwidth:
coreView
.overlay(divider, alignment: .bottom)
.if(shouldShowDivider) {
$0.overlay(divider, alignment: .bottom)
}
.if(allowsPressing) {
$0.overlay(cellLink)
}
Expand Down Expand Up @@ -195,27 +198,6 @@ public struct Cell<PresetView: View, HeadlineView: View, Destination: View>: Vie
}
}

// MARK: Utils

public extension Cell {
/// Sets a NavigationLink as a background with no opacity. This behaves as a workaround to avoid
/// NavigationLinks to add extra views to the Cell inside List. Otherwise, the system will add the default chevron.
func navigationLink<T: View>(to destination: () -> T) -> Cell<PresetView, HeadlineView, T> {
var cell = Cell<PresetView, HeadlineView, T>(
style: style,
title: title,
subtitle: subtitle,
description: description,
assetType: assetType,
presetView: { presetView },
headlineView: { headlineView },
destinationView: destination
)
cell.allowsPressing = true
return cell
}
}

// MARK: Cell with no preset nor headline

public extension Cell where Destination == EmptyView {
Expand Down Expand Up @@ -394,11 +376,17 @@ public extension Cell {
cell.allowsPressing = allowsPressing
return cell
}

func shouldShowDivider(_ shouldShowDivider: Bool) -> Cell {
var cell = self
cell.shouldShowDivider = shouldShowDivider
return cell
}
}

// MARK: Helpers

struct ListSeparatorHiddenModifieriOS15: ViewModifier {
struct ListSeparatorHiddenModifier: ViewModifier {
public func body(content: Content) -> some View {
if #available(iOS 15.0, *) {
content
Expand All @@ -418,27 +406,9 @@ struct ListSeparatorHiddenModifieriOS15: ViewModifier {
}
}

struct ListSeparatorHiddenModifier: ViewModifier {
public func body(content: Content) -> some View {
content
.listRowInsets(EdgeInsets())
.overlay(
VStack {
Divider(color: .background)
Spacer()
Divider(color: .background)
}
)
}
}

public extension View {
func listSeparatorHidden() -> some View {
conditionalModifier(
.iOS15,
then: { $0.modifier(ListSeparatorHiddenModifieriOS15()) },
else: { $0.modifier(ListSeparatorHiddenModifier()) }
)
modifier(ListSeparatorHiddenModifier())
}
}

Expand Down
11 changes: 11 additions & 0 deletions Sources/MisticaSwiftUI/Components/List/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ NavigationView {
}
```

The last cell on a Mistica List should not have a divider. Use the modifier (`shouldShowDivider(<condition to match last item>)`) to hide it. E.g.:

```swift
ForEach(rows) { row in
Cell(
...
)
.shouldShowDivider(row != rows.last)
}
```

#### Control Customization

`Cell` can be configured with a custom control like a `Toggle`.
Expand Down

0 comments on commit 9058967

Please sign in to comment.