apptekstudios / ASCollectionView

A SwiftUI collection view with support for custom layouts, preloading, and more.
MIT License
1.36k stars 160 forks source link

The CardView created works well independently but not with ASCollectionView #167

Closed gorbat-o closed 3 years ago

gorbat-o commented 4 years ago

Hey guys!

Describe the bug So I am currently trying to have a CardView container. It seems to work in the preview and pretty much everywhere independently, but as soon as I use it with ASCollectionView, it seems to break and do not show the labels.

To Reproduce

TradeView

struct TradeView: View {
    @ObservedObject var viewModel: TradeViewModel

    var body: some View {
        NavigationView {
            ASCollectionView(data: viewModel.tradeCellViewModels, dataID: \.self) { tradeCellViewModel, _ in
                TradeCellView(viewModel: tradeCellViewModel)
            }
            .layout {
                .grid(layoutMode: .adaptive(withMinItemSize: 300),
                      itemSpacing: 8,
                      lineSpacing: 8,
                      itemSize: .estimated(600))
            }
            .navigationBarTitle("Trade")
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

#if DEBUG
struct TradeView_Previews: PreviewProvider {
    static var previews: some View {
        Group {
            TradeView(viewModel: TradeViewModel(with: Provider()))
        }
    }
}
#endif

TradeCellView

struct TradeCellView: View {
    @ObservedObject private(set) var viewModel: TradeCellViewModel

    var body: some View {
        let binding = Binding(
            get: {
                self.viewModel.isAuto
        },
            set: {
                self.viewModel.isAuto = $0
                self.viewModel.setAuto()
        })

        let cardView = CardView(content: {
            VStack {
                Text(self.viewModel.symbol)
                    .font(.title)
                    .bold()
                Text("$\(self.viewModel.marketValue)")
                    .font(.title)
                BarChartView(candles: self.viewModel.candleViewModels)
                    .frame(width: nil, height: 200)
                Text("Qty: \(self.viewModel.qty)")
                    .font(.body)
                Text("Avg Entry Price: $\(self.viewModel.avgEntryPrice)")
                    .font(.body)
                Text("Current Value: $\(self.viewModel.currentPrice)")
                    .font(.body)
                Text("Total Return %: \(self.viewModel.unrealizedPlpc)")
                    .font(.body)
                Text("Today Return %: \(self.viewModel.changeToday)")
                    .font(.body)
            }
        })

        return cardView
    }
}

#if DEBUG
struct TradeCellView_Previews: PreviewProvider {
    static var previews: some View {
        TradeCellView(viewModel: TradeCellViewModel(Position(), bars: [], isAuto: false))
            .previewLayout(PreviewLayout.sizeThatFits)
            .padding()
    }
}
#endif

Container

public struct CardView<Content: View>: View {
    let content: () -> Content

    public init(@ViewBuilder content: @escaping () -> Content) {
        self.content = content
    }

    public var body: some View {
        VStack {
            self.content()
        }
        .clipped()
        .padding()
        .overlay(
            RoundedRectangle(cornerRadius: 10)
                .stroke(Color.black, lineWidth: 0))
            .background(
                RoundedRectangle(cornerRadius: 10)
                    .foregroundColor(Color.white)
                    .shadow(color: Color.black, radius: 0.5, x: 0, y: 0)
        )
    }
}

struct CardView_Previews: PreviewProvider {
    static var previews: some View {
        CardView {
            VStack {
                Text("Symbol")
                    .font(.title)
                    .bold()
                Text("$500")
                    .font(.title)
                BarChartView(candles: [
                    CandleViewModel(with: Bar()) // X16
                ])
                    .frame(width: nil, height: 200)
                Text("Qty: 500")
                    .font(.body)
                Text("Avg Entry Price: $400")
                    .font(.body)
                Text("Current Value: $500")
                    .font(.body)
                Text("Total Return %: +2%")
                    .font(.body)
                Text("Today Return %: +24%")
                    .font(.body)
            }
        }.frame(height: 400)
        .padding()
    }
}

Current Behaviour image

Expected behaviour image image

Xcode Version:

Simulator, Device, Both?

Thank you for any future help 👍

apptekstudios commented 3 years ago

It is likely you need to add a fixedSize modifier to the Text items. Otherwise SwiftUI will allow them to be collapsed when it determines the smallest fitting size 👍

gorbat-o commented 3 years ago

Will try that thank you!