Skip to content

Commit

Permalink
FIX: TupleView Issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Mercen-Lee committed Jul 10, 2023
1 parent c7376eb commit 474164a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 40 deletions.
27 changes: 0 additions & 27 deletions Sources/OpenTDS/Component/TabView/TabView+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,9 @@ import SwiftUI

@available(macOS 11, iOS 14, *)
public extension View {
@ViewBuilder func tossTransition(_ id: Int, _ selected: Int) -> some View {
self
.offset(x: { () -> CGFloat in
if id == selected {
return 0
} else if id < selected {
return -10
} else {
return 10
}
}())
.opacity(id == selected ? 1 : 0)
}

@ViewBuilder func tossTabItem(_ title: String, _ image: Image) -> TossTabItemView<Self> {
TossTabItemView(title: title, image: image) {
self
}
}
}

@available(macOS 11, iOS 14, *)
public struct TossTransition: ViewModifier {
let offset: CGFloat
let opacity: CGFloat

public func body(content: Content) -> some View {
content
.offset(x: offset)
.opacity(opacity)
.clipped()
}
}
9 changes: 4 additions & 5 deletions Sources/OpenTDS/Component/TabView/TabView+Item.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@ public struct TossTabItemView<Content: View>: TossTabItemViewProtocol {

public let title: String
public let image: Image
let content: Content
public let content: AnyView

public init(title: String,
image: Image,
@ViewBuilder content: () -> Content)
{
self.title = title
self.image = image
self.content = content()
self.content = AnyView(content())
}

public var body: some View {
content
}
public var body: some View { EmptyView() }
}

@available(macOS 11, iOS 14, *)
Expand All @@ -35,4 +33,5 @@ public struct TossTabItemModifier: ViewModifier {
public protocol TossTabItemViewProtocol: View {
var title: String { get }
var image: Image { get }
var content: AnyView { get }
}
35 changes: 27 additions & 8 deletions Sources/OpenTDS/Component/TabView/TabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,40 @@ import SwiftUI
}
*/
@available(macOS 11, iOS 14, *)
public struct TossTabView<Content: TossTabItemViewProtocol>: View {
public struct TossTabView: View {

@State var selected: Int = 0
let content: [Content]
let content: [any TossTabItemViewProtocol]

public init(@ViewBuilder content: @escaping () -> TupleView<(Content, Content)>)
public init<C0: TossTabItemViewProtocol,
C1: TossTabItemViewProtocol>(@ViewBuilder content: @escaping () -> TupleView<(C0, C1)>)
{
let cv = content().value
self.content = [cv.0, cv.1]
}

public init(@ViewBuilder content: @escaping () -> TupleView<(Content, Content, Content)>)
public init<C0: TossTabItemViewProtocol,
C1: TossTabItemViewProtocol,
C2: TossTabItemViewProtocol>(@ViewBuilder content: @escaping () -> TupleView<(C0, C1, C2)>)
{
let cv = content().value
self.content = [cv.0, cv.1, cv.2]
}

public init(@ViewBuilder content: @escaping () -> TupleView<(Content, Content, Content, Content)>)
public init<C0: TossTabItemViewProtocol,
C1: TossTabItemViewProtocol,
C2: TossTabItemViewProtocol,
C3: TossTabItemViewProtocol>(@ViewBuilder content: @escaping () -> TupleView<(C0, C1, C2, C3)>)
{
let cv = content().value
self.content = [cv.0, cv.1, cv.2, cv.3]
}

public init(@ViewBuilder content: @escaping () -> TupleView<(Content, Content, Content, Content, Content)>)
public init<C0: TossTabItemViewProtocol,
C1: TossTabItemViewProtocol,
C2: TossTabItemViewProtocol,
C3: TossTabItemViewProtocol,
C4: TossTabItemViewProtocol>(@ViewBuilder content: @escaping () -> TupleView<(C0, C1, C2, C3, C4)>)
{
let cv = content().value
self.content = [cv.0, cv.1, cv.2, cv.3, cv.4]
Expand All @@ -48,8 +58,17 @@ public struct TossTabView<Content: TossTabItemViewProtocol>: View {
ZStack(alignment: .bottom) {
ZStack {
ForEach(0..<content.count, id: \.self) { idx in
content[idx]
.tossTransition(idx, selected)
content[idx].content
.offset(x: { () -> CGFloat in
if idx == selected {
return 0
} else if idx < selected {
return -10
} else {
return 10
}
}())
.opacity(idx == selected ? 1 : 0)
}

}
Expand Down

0 comments on commit 474164a

Please sign in to comment.