Playback speed
×
Share post
Share post at current time
0:00
/
0:00

💡 SwiftUI FYI #7 - TipKit Part 2

Conditional Tip: Show Tip when coin balance is positive

As a continuation of our previous post on TipKit, we present a method for displaying tips based on custom rules.

In this example, the tip will only be displayed when the coin balance is positive.

While we demonstrate a single rule in this example, you have the flexibility to combine multiple rules if required. For instance, you could show a tip if the balance is positive and this is the first time they are purchasing coins in our app.

🚨 Full code on Github

🚨 Find out more about TipKit in Apple’s Highlighting App features with TipKit documentation.

🤝 Find more about me on saidmarouf.com. I’m also on XThreads, and LinkedIn

Thanks for reading Your Weekly SwiftUI FYI 💡! Subscribe for free to receive new posts and support my work.


import SwiftUI
import TipKit

struct CoinsTip: Tip {
    
    @Parameter static var isCoinBalancePositive: Bool = false
    
    var title: Text {
        Text("Coin Balance")
    }
    
    var message: Text? {
        Text("Your Coin balance will appear here.")
    }
    
    var image: Image? {
        Image(systemName: "bitcoinsign.circle.fill")
    }
    
    var rules: [Rule] {
        [
            #Rule(Self.$isCoinBalancePositive) {
                $0 == true
            }
        ]
    }
}

struct ConditionalTipDemoView: View {
    
    let coinTip = CoinsTip()
    @State var coinCount: Int = 0
    
    var body: some View {

        NavigationStack {

            VStack {
                Button {
                    coinCount += 1
                } label: {
                   Label("Buy Coins", systemImage: "bitcoinsign.circle")
                        .font(.largeTitle)
                        .padding()
                }
                .buttonStyle(.borderedProminent)
                .tint(.orange)
            }
            .onChange(of: coinCount) { oldValue, newValue in
                CoinsTip.isCoinBalancePositive = newValue > 0
            }
            .task {
                do {
#if DEBUG
                    // clear store to force showing Tips every launch
                    try Tips.resetDatastore()
#endif
                    try Tips.configure()
                } catch {
                    print("TipKit error: \(error.localizedDescription)")
                }
            }
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    HStack(alignment: .bottom) {
                        Image(systemName: "bitcoinsign.circle.fill")
                        if coinCount > 0 {
                            Text("\(coinCount)")
                                .bold()
                        }
                    }
                    .foregroundStyle(.orange)
                    .font(.title)
                    .opacity(coinCount > 0 ? 1 : 0.2)
                    .popoverTip(coinTip, arrowEdge: .top)
                }
            }
        }        
    }
}


#Preview {
    ConditionalTipDemoView()
}

Discussion about this podcast