Skip to content

Instantly share code, notes, and snippets.

@DaisukeNagata
Created December 19, 2019 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DaisukeNagata/720e7a29c7f7dae9b692747167c1a28b to your computer and use it in GitHub Desktop.
Save DaisukeNagata/720e7a29c7f7dae9b692747167c1a28b to your computer and use it in GitHub Desktop.
SwiftUI_SampleList
import SwiftUI
struct ContentView: View {
var body: some View {
let first = Restaurant(id: 0, name: "0")
let second = Restaurant(id: 1, name: "1")
let third = Restaurant(id: 2, name: "2")
let restaurants = [first, second, third]
return List(restaurants) { restaurant in
RestaurantRow(restaurant: restaurant)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct Restaurant: Identifiable {
var id = Int()
var name: String
}
struct RestaurantRow: View {
var restaurant: Restaurant
@EnvironmentObject var model: OrientationModel
var body: some View {
Text("API Something \(self.model.showingAlert[self.restaurant.id])")
.onTapGesture {
// API Something
if self.restaurant.id == 0 {
self.model.count_0 += 1
self.model.showingAlert[self.restaurant.id] = self.model.count_0
} else if self.restaurant.id == 1 {
self.model.count_1 += 1
self.model.showingAlert[self.restaurant.id] = self.model.count_1
} else if self.restaurant.id == 2 {
self.model.count_2 += 1
self.model.showingAlert[self.restaurant.id] = self.model.count_2
}
}
}
}
final class OrientationModel: ObservableObject {
@Published var count_0 = 0
@Published var count_1 = 1
@Published var count_2 = 2
@Published var showingAlert = [0,1,2]
}
SceneDelegate
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
// Create the SwiftUI view that provides the window contents.
let contentView = ContentView()
let settings = OrientationModel()
// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView.environmentObject(settings))
self.window = window
window.makeKeyAndVisible()
}
}
@DaisukeNagata
Copy link
Author

gifMovie

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment