Skip to content

Commit

Permalink
Remove AnyView and use ViewBuilder instead
Browse files Browse the repository at this point in the history
  • Loading branch information
twodayslate committed Oct 22, 2022
1 parent 736ac5d commit 0d7569d
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 114 deletions.
202 changes: 97 additions & 105 deletions Sources/SwipeCell/SwipeCellViewModifier1.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,41 +85,35 @@ struct SwipeCellModifier: ViewModifier {
self.initialStatusResetDelay = initialStatusResetDelay
}

func buttonView(_ slot: SwipeCellSlot, _ i: Int) -> some View {
func emptyView(_ button: SwipeCellButton) -> some View {
Text("nil").foregroundColor(button.titleColor)
}

@ViewBuilder func buttonView(_ slot: SwipeCellSlot, _ i: Int) -> some View {
let button = slot.slots[i]
let viewStyle = button.buttonStyle

func emptyView() -> AnyView {
AnyView(
Text("nil").foregroundColor(button.titleColor)
)
}
let emptyView = emptyView(button)

switch viewStyle {
case .image:
guard let image = button.systemImage else {
return emptyView()
}
return AnyView(
if let image = button.systemImage {
Image(systemName: image)
.font(.system(size: 23))
.foregroundColor(button.imageColor)
)
case .title:
guard let title = button.title else {
return emptyView()
} else {
emptyView
}
return AnyView(
case .title:
if let title = button.title {
Text(title)
.font(.callout)
.bold()
.foregroundColor(button.titleColor)
)
case .titleAndImage:
guard let title = button.title, let image = button.systemImage else {
return emptyView()
} else {
emptyView
}
return AnyView(
case .titleAndImage:
if let title = button.title, let image = button.systemImage {
VStack(spacing: 5) {
Image(systemName: image)
.font(.system(size: 23))
Expand All @@ -129,13 +123,15 @@ struct SwipeCellModifier: ViewModifier {
.bold()
.foregroundColor(button.titleColor)
}
)

} else {
emptyView
}
case .view:
guard let view = button.view else {
return emptyView()
if let view = button.view {
view()
} else {
emptyView
}
return view()
}
}

Expand Down Expand Up @@ -235,13 +231,13 @@ struct SwipeCellModifier: ViewModifier {
}
}

func loadButtons(_ slot: SwipeCellSlot, position: SwipeCellSlotPosition, frame: CGRect)
-> some View
@ViewBuilder func loadButtons(_ slot: SwipeCellSlot, position: SwipeCellSlotPosition, frame: CGRect)
-> some View
{
let buttons = slot.slots

if slot.slotStyle == .destructive && leftOffset == -10000 && position == .left {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
let _ = DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
leftOffset = cellOffset(
i: buttons.count - 1,
count: buttons.count,
Expand All @@ -253,7 +249,7 @@ struct SwipeCellModifier: ViewModifier {
}

if slot.slotStyle == .destructive && rightOffset == 10000 && position == .right {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
let _ = DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
rightOffset = cellOffset(
i: buttons.count - 1,
count: buttons.count,
Expand All @@ -265,99 +261,96 @@ struct SwipeCellModifier: ViewModifier {
}

if slot.slotStyle == .destructive {
//单button的销毁按钮
if buttons.count == 1 {
return AnyView(
slotView(slot: slot, i: 0, position: position)
destructiveButtons(slot, position: position, frame: frame)
}
else {
ZStack {
ForEach(0..<buttons.count, id: \.self) { i in
if slot.slotStyle == .destructiveDelay && i == buttons.count - 1 {
slotView(slot: slot, i: i, position: position)
.offset(
x: showDalayButtonWith != 0
? showDalayButtonWith
: cellOffset(
i: i,
count: buttons.count,
position: position,
width: frame.width,
slot: slot
)
)
.zIndex(Double(i))
}
else {
slotView(slot: slot, i: i, position: position)
.offset(
x: cellOffset(
i: i,
count: buttons.count,
position: position,
width: frame.width,
slot: slot
)
)
.zIndex(Double(i))
}
}
}
}
}

@ViewBuilder private func destructiveButtons(_ slot: SwipeCellSlot, position: SwipeCellSlotPosition, frame: CGRect) -> some View {
let buttons = slot.slots
//单button的销毁按钮
if buttons.count == 1 {
slotView(slot: slot, i: 0, position: position)
.offset(
x: cellOffset(
i: 0,
count: buttons.count,
position: position,
width: frame.width,
slot: slot
)
)
}
else {
ZStack {
ForEach(0..<buttons.count - 1, id: \.self) { i in
slotView(slot: slot, i: i, position: position)
.offset(
x: cellOffset(
i: 0,
i: i,
count: buttons.count,
position: position,
width: frame.width,
slot: slot
)
)
)
}
else {
return AnyView(
ZStack {
ForEach(0..<buttons.count - 1, id: \.self) { i in
slotView(slot: slot, i: i, position: position)
.offset(
x: cellOffset(
i: i,
count: buttons.count,
position: position,
width: frame.width,
slot: slot
)
)
.zIndex(Double(i))
}
//销毁按钮
if slot.slotStyle == .destructive && position == .left {
slotView(slot: slot, i: buttons.count - 1, position: .left)
.zIndex(10)
.offset(x: leftOffset)

}
.zIndex(Double(i))
}
//销毁按钮
if slot.slotStyle == .destructive && position == .left {
slotView(slot: slot, i: buttons.count - 1, position: .left)
.zIndex(10)
.offset(x: leftOffset)

if slot.slotStyle == .destructive && position == .right {
slotView(slot: slot, i: buttons.count - 1, position: .right)
.offset(x: rightOffset)
.zIndex(10)
}

}
}
)
}
}
else {
if slot.slotStyle == .destructive && position == .right {
slotView(slot: slot, i: buttons.count - 1, position: .right)
.offset(x: rightOffset)
.zIndex(10)

return AnyView(
ZStack {
ForEach(0..<buttons.count, id: \.self) { i in
if slot.slotStyle == .destructiveDelay && i == buttons.count - 1 {
slotView(slot: slot, i: i, position: position)
.offset(
x: showDalayButtonWith != 0
? showDalayButtonWith
: cellOffset(
i: i,
count: buttons.count,
position: position,
width: frame.width,
slot: slot
)
)
.zIndex(Double(i))
}
else {
slotView(slot: slot, i: i, position: position)
.offset(
x: cellOffset(
i: i,
count: buttons.count,
position: position,
width: frame.width,
slot: slot
)
)
.zIndex(Double(i))
}
}
}
)
}
}
}

func offsetForSingleDestructiveButton(slot: SwipeCellSlot, position: SwipeCellSlotPosition) {
if slot.slotStyle == .destructive && slot.slots.count == 1 {
switch position {
case .left:
print("left")
DispatchQueue.main.async {
spaceWidth = 0
}
Expand Down Expand Up @@ -509,5 +502,4 @@ struct SwipeCellModifier: ViewModifier {
}
}
}

}
14 changes: 5 additions & 9 deletions Sources/SwipeCell/ViewExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,17 @@ extension View {
}

extension View {
public func _hidden(_ condition: Bool) -> some View {
@ViewBuilder public func _hidden(_ condition: Bool) -> some View {
Group {
if condition {
AnyView(self)
self
}
else {
AnyView(EmptyView())
EmptyView()
}
}
}

func toAnyView() -> AnyView {
AnyView(self)
}


@ViewBuilder func ifIs<T>(_ condition: Bool, transform: (Self) -> T) -> some View
where T: View {
if condition {
Expand All @@ -70,7 +66,7 @@ extension View {
self
}
}

func doSomething(_ action: () -> Void) -> some View {
action()
return self
Expand Down

0 comments on commit 0d7569d

Please sign in to comment.