Created
May 24, 2024 09:01
-
-
Save ynagatomo/735bdd3ee94c662201297f185ca9dc6d to your computer and use it in GitHub Desktop.
A sample code that uses TipKit in visionOS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// TipKitTestApp.swift | |
// TipKitTest | |
// | |
// Created by Yasuhito Nagatomo on 2024/05/24. | |
// | |
// A sample code that uses TipKit in visionOS. | |
// | |
// References: | |
// - Article: Swift with Majid, Discovering app features with TipKit. Basics., 07 May 2024 | |
// https://swiftwithmajid.com/2024/05/07/discovering-app-features-with-tipkit-basics/ | |
import SwiftUI | |
import TipKit | |
@main | |
struct TipKitTestApp: App { | |
init() { | |
try? Tips.configure([ | |
.displayFrequency(.weekly) | |
// stored into UserDefault by default | |
// , .datastoreLocation(.applicationDefault) | |
]) | |
} | |
var body: some Scene { | |
WindowGroup { | |
ContentView() | |
} | |
} | |
} | |
enum FeedTip: Tip { | |
case add | |
case remove | |
var title: Text { | |
switch self { | |
case .add: | |
Text("Add more items.") | |
case .remove: | |
Text("Remove the item.") | |
} | |
} | |
var message: Text? { | |
switch self { | |
case .add: | |
Text("You can add more items to the feed here.") | |
default: | |
nil | |
} | |
} | |
var image: Image? { | |
switch self { | |
case .add: Image(systemName: "plus") | |
default: nil | |
} | |
} | |
var actions: [Action] { | |
switch self { | |
case .add: [Action(id: "add", title: "Add")] | |
default: [] | |
} | |
} | |
} | |
struct Item: Identifiable { | |
let name: String | |
var id: String { | |
name | |
} | |
var description: String { | |
name | |
} | |
} | |
struct FeedStore { | |
let items: [Item] | |
func addItem() { } | |
} | |
struct FeedView: View { | |
let feed: FeedStore | |
var body: some View { | |
NavigationStack { | |
List { | |
// TipView(FeedTip.add, arrowEdge: .trailing) | |
ForEach(feed.items) { item in | |
Text(item.description) | |
} | |
Button("Add", systemImage: "plus", action: feed.addItem) | |
.popoverTip(FeedTip.add) { action in | |
if action.id == "add" { | |
feed.addItem() | |
} | |
} | |
} | |
.navigationTitle("Feed") | |
} | |
} | |
} | |
struct ContentView: View { | |
@State private var showing = false | |
let feedStore = FeedStore(items: [Item(name: "apple"), | |
Item(name: "orange")]) | |
var body: some View { | |
FeedView(feed: feedStore) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment