mirror of
https://github.com/ivanvorobei/SwiftUI.git
synced 2026-03-25 10:51:45 +01:00
Add examples project
This commit is contained in:
41
Examples/Example To-Do App/SwiftUITodo/AppDelegate.swift
Executable file
41
Examples/Example To-Do App/SwiftUITodo/AppDelegate.swift
Executable file
@@ -0,0 +1,41 @@
|
||||
//
|
||||
// AppDelegate.swift
|
||||
// SwiftUITodo
|
||||
//
|
||||
// Created by Suyeol Jeon on 03/06/2019.
|
||||
// Copyright © 2019 Suyeol Jeon. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
@UIApplicationMain
|
||||
class AppDelegate: UIResponder, UIApplicationDelegate {
|
||||
|
||||
|
||||
|
||||
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
|
||||
// Override point for customization after application launch.
|
||||
return true
|
||||
}
|
||||
|
||||
func applicationWillTerminate(_ application: UIApplication) {
|
||||
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
|
||||
}
|
||||
|
||||
// MARK: UISceneSession Lifecycle
|
||||
|
||||
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
|
||||
// Called when a new scene session is being created.
|
||||
// Use this method to select a configuration to create the new scene with.
|
||||
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
|
||||
}
|
||||
|
||||
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
|
||||
// Called when the user discards a scene session.
|
||||
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
|
||||
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
{
|
||||
"images" : [
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "20x20",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "20x20",
|
||||
"scale" : "3x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "29x29",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "29x29",
|
||||
"scale" : "3x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "40x40",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "40x40",
|
||||
"scale" : "3x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "60x60",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "iphone",
|
||||
"size" : "60x60",
|
||||
"scale" : "3x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "20x20",
|
||||
"scale" : "1x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "20x20",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "29x29",
|
||||
"scale" : "1x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "29x29",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "40x40",
|
||||
"scale" : "1x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "40x40",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "76x76",
|
||||
"scale" : "1x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "76x76",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ipad",
|
||||
"size" : "83.5x83.5",
|
||||
"scale" : "2x"
|
||||
},
|
||||
{
|
||||
"idiom" : "ios-marketing",
|
||||
"size" : "1024x1024",
|
||||
"scale" : "1x"
|
||||
}
|
||||
],
|
||||
"info" : {
|
||||
"version" : 1,
|
||||
"author" : "xcode"
|
||||
}
|
||||
}
|
||||
6
Examples/Example To-Do App/SwiftUITodo/Assets.xcassets/Contents.json
Executable file
6
Examples/Example To-Do App/SwiftUITodo/Assets.xcassets/Contents.json
Executable file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"info" : {
|
||||
"version" : 1,
|
||||
"author" : "xcode"
|
||||
}
|
||||
}
|
||||
25
Examples/Example To-Do App/SwiftUITodo/Base.lproj/LaunchScreen.storyboard
Executable file
25
Examples/Example To-Do App/SwiftUITodo/Base.lproj/LaunchScreen.storyboard
Executable file
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13122.16" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
|
||||
<dependencies>
|
||||
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13104.12"/>
|
||||
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
|
||||
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
|
||||
</dependencies>
|
||||
<scenes>
|
||||
<!--View Controller-->
|
||||
<scene sceneID="EHf-IW-A2E">
|
||||
<objects>
|
||||
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
|
||||
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
|
||||
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
|
||||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
|
||||
<color key="backgroundColor" xcode11CocoaTouchSystemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
|
||||
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
|
||||
</view>
|
||||
</viewController>
|
||||
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
|
||||
</objects>
|
||||
<point key="canvasLocation" x="53" y="375"/>
|
||||
</scene>
|
||||
</scenes>
|
||||
</document>
|
||||
62
Examples/Example To-Do App/SwiftUITodo/Info.plist
Executable file
62
Examples/Example To-Do App/SwiftUITodo/Info.plist
Executable file
@@ -0,0 +1,62 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>$(DEVELOPMENT_LANGUAGE)</string>
|
||||
<key>CFBundleExecutable</key>
|
||||
<string>$(EXECUTABLE_NAME)</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundleName</key>
|
||||
<string>$(PRODUCT_NAME)</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.0</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1</string>
|
||||
<key>LSRequiresIPhoneOS</key>
|
||||
<true/>
|
||||
<key>UIApplicationSceneManifest</key>
|
||||
<dict>
|
||||
<key>UIApplicationSupportsMultipleScenes</key>
|
||||
<false/>
|
||||
<key>UISceneConfigurations</key>
|
||||
<dict>
|
||||
<key>UIWindowSceneSessionRoleApplication</key>
|
||||
<array>
|
||||
<dict>
|
||||
<key>UILaunchStoryboardName</key>
|
||||
<string>LaunchScreen</string>
|
||||
<key>UISceneConfigurationName</key>
|
||||
<string>Default Configuration</string>
|
||||
<key>UISceneDelegateClassName</key>
|
||||
<string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
|
||||
</dict>
|
||||
</array>
|
||||
</dict>
|
||||
</dict>
|
||||
<key>UILaunchStoryboardName</key>
|
||||
<string>LaunchScreen</string>
|
||||
<key>UIRequiredDeviceCapabilities</key>
|
||||
<array>
|
||||
<string>armv7</string>
|
||||
</array>
|
||||
<key>UISupportedInterfaceOrientations</key>
|
||||
<array>
|
||||
<string>UIInterfaceOrientationPortrait</string>
|
||||
<string>UIInterfaceOrientationLandscapeLeft</string>
|
||||
<string>UIInterfaceOrientationLandscapeRight</string>
|
||||
</array>
|
||||
<key>UISupportedInterfaceOrientations~ipad</key>
|
||||
<array>
|
||||
<string>UIInterfaceOrientationPortrait</string>
|
||||
<string>UIInterfaceOrientationPortraitUpsideDown</string>
|
||||
<string>UIInterfaceOrientationLandscapeLeft</string>
|
||||
<string>UIInterfaceOrientationLandscapeRight</string>
|
||||
</array>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"info" : {
|
||||
"version" : 1,
|
||||
"author" : "xcode"
|
||||
}
|
||||
}
|
||||
61
Examples/Example To-Do App/SwiftUITodo/SceneDelegate.swift
Executable file
61
Examples/Example To-Do App/SwiftUITodo/SceneDelegate.swift
Executable file
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// SceneDelegate.swift
|
||||
// SwiftUITodo
|
||||
//
|
||||
// Created by Suyeol Jeon on 03/06/2019.
|
||||
// Copyright © 2019 Suyeol Jeon. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
import SwiftUI
|
||||
|
||||
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
|
||||
|
||||
var window: UIWindow?
|
||||
|
||||
|
||||
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).
|
||||
|
||||
// Use a UIHostingController as window root view controller
|
||||
let window = UIWindow(frame: UIScreen.main.bounds)
|
||||
window.rootViewController = UIHostingController(rootView: NavigationView {
|
||||
TaskListView().environmentObject(UserData())
|
||||
})
|
||||
self.window = window
|
||||
window.makeKeyAndVisible()
|
||||
}
|
||||
|
||||
func sceneDidDisconnect(_ scene: UIScene) {
|
||||
// Called as the scene is being released by the system.
|
||||
// This occurs shortly after the scene enters the background, or when its session is discarded.
|
||||
// Release any resources associated with this scene that can be re-created the next time the scene connects.
|
||||
// The scene may re-connect later, as its session was not neccessarily discarded (see `application:didDiscardSceneSessions` instead).
|
||||
}
|
||||
|
||||
func sceneDidBecomeActive(_ scene: UIScene) {
|
||||
// Called when the scene has moved from an inactive state to an active state.
|
||||
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
|
||||
}
|
||||
|
||||
func sceneWillResignActive(_ scene: UIScene) {
|
||||
// Called when the scene will move from an active state to an inactive state.
|
||||
// This may occur due to temporary interruptions (ex. an incoming phone call).
|
||||
}
|
||||
|
||||
func sceneWillEnterForeground(_ scene: UIScene) {
|
||||
// Called as the scene transitions from the background to the foreground.
|
||||
// Use this method to undo the changes made on entering the background.
|
||||
}
|
||||
|
||||
func sceneDidEnterBackground(_ scene: UIScene) {
|
||||
// Called as the scene transitions from the foreground to the background.
|
||||
// Use this method to save data, release shared resources, and store enough scene-specific state information
|
||||
// to restore the scene back to its current state.
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
21
Examples/Example To-Do App/SwiftUITodo/Task.swift
Executable file
21
Examples/Example To-Do App/SwiftUITodo/Task.swift
Executable file
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// Task.swift
|
||||
// SwiftUITodo
|
||||
//
|
||||
// Created by Suyeol Jeon on 03/06/2019.
|
||||
// Copyright © 2019 Suyeol Jeon. All rights reserved.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct Task: Equatable, Hashable, Codable, Identifiable {
|
||||
let id: UUID
|
||||
var title: String
|
||||
var isDone: Bool
|
||||
|
||||
init(title: String, isDone: Bool) {
|
||||
self.id = UUID()
|
||||
self.title = title
|
||||
self.isDone = isDone
|
||||
}
|
||||
}
|
||||
52
Examples/Example To-Do App/SwiftUITodo/TaskEditView.swift
Executable file
52
Examples/Example To-Do App/SwiftUITodo/TaskEditView.swift
Executable file
@@ -0,0 +1,52 @@
|
||||
//
|
||||
// TaskEditView.swift
|
||||
// SwiftUITodo
|
||||
//
|
||||
// Created by Suyeol Jeon on 03/06/2019.
|
||||
// Copyright © 2019 Suyeol Jeon. All rights reserved.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct TaskEditView: View {
|
||||
@EnvironmentObject var userData: UserData
|
||||
private let task: Task
|
||||
private var draftTitle: State<String>
|
||||
|
||||
init(task: Task) {
|
||||
self.task = task
|
||||
self.draftTitle = .init(initialValue: task.title)
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
let inset = EdgeInsets(top: -8, leading: -10, bottom: -7, trailing: -10)
|
||||
return VStack(alignment: .leading, spacing: 0) {
|
||||
TextField(
|
||||
self.draftTitle.binding,
|
||||
placeholder: Text("Enter New Title..."),
|
||||
onEditingChanged: { _ in self.updateTask() },
|
||||
onCommit: {}
|
||||
)
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: 5)
|
||||
.fill(Color.clear)
|
||||
.border(Color(red: 0.7, green: 0.7, blue: 0.7), width: 1 / UIScreen.main.scale, cornerRadius: 5)
|
||||
.padding(inset)
|
||||
)
|
||||
.padding(EdgeInsets(
|
||||
top: 15 - inset.top,
|
||||
leading: 20 - inset.leading,
|
||||
bottom: 15 - inset.bottom,
|
||||
trailing: 20 - inset.trailing
|
||||
))
|
||||
|
||||
Spacer()
|
||||
}
|
||||
.navigationBarTitle(Text("Edit Task 📝"))
|
||||
}
|
||||
|
||||
private func updateTask() {
|
||||
guard let index = self.userData.tasks.firstIndex(of: self.task) else { return }
|
||||
self.userData.tasks[index].title = self.draftTitle.value
|
||||
}
|
||||
}
|
||||
52
Examples/Example To-Do App/SwiftUITodo/TaskItemView.swift
Executable file
52
Examples/Example To-Do App/SwiftUITodo/TaskItemView.swift
Executable file
@@ -0,0 +1,52 @@
|
||||
//
|
||||
// TaskItemView.swift
|
||||
// SwiftUITodo
|
||||
//
|
||||
// Created by Suyeol Jeon on 03/06/2019.
|
||||
// Copyright © 2019 Suyeol Jeon. All rights reserved.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct TaskItemView: View {
|
||||
@EnvironmentObject var userData: UserData
|
||||
|
||||
let task: Task
|
||||
@Binding var isEditing: Bool
|
||||
|
||||
var body: some View {
|
||||
return HStack {
|
||||
if self.isEditing {
|
||||
Image(systemName: "minus.circle")
|
||||
.foregroundColor(.red)
|
||||
.tapAction(count: 1) {
|
||||
self.delete()
|
||||
}
|
||||
NavigationButton(destination: TaskEditView(task: task).environmentObject(self.userData)) {
|
||||
Text(task.title)
|
||||
}
|
||||
} else {
|
||||
Button(action: { self.toggleDone() }) {
|
||||
Text(self.task.title)
|
||||
}
|
||||
Spacer()
|
||||
if task.isDone {
|
||||
Image(systemName: "checkmark").foregroundColor(.green)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func toggleDone() {
|
||||
guard !self.isEditing else { return }
|
||||
guard let index = self.userData.tasks.firstIndex(where: { $0.id == self.task.id }) else { return }
|
||||
self.userData.tasks[index].isDone.toggle()
|
||||
}
|
||||
|
||||
private func delete() {
|
||||
self.userData.tasks.removeAll(where: { $0.id == self.task.id })
|
||||
if self.userData.tasks.isEmpty {
|
||||
self.isEditing = false
|
||||
}
|
||||
}
|
||||
}
|
||||
38
Examples/Example To-Do App/SwiftUITodo/TaskListView.swift
Executable file
38
Examples/Example To-Do App/SwiftUITodo/TaskListView.swift
Executable file
@@ -0,0 +1,38 @@
|
||||
//
|
||||
// TaskListView.swift
|
||||
// SwiftUITodo
|
||||
//
|
||||
// Created by Suyeol Jeon on 03/06/2019.
|
||||
// Copyright © 2019 Suyeol Jeon. All rights reserved.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct TaskListView: View {
|
||||
@EnvironmentObject var userData: UserData
|
||||
@State var draftTitle: String = ""
|
||||
@State var isEditing: Bool = false
|
||||
|
||||
var body: some View {
|
||||
List {
|
||||
TextField($draftTitle, placeholder: Text("Create a New Task..."), onCommit: self.createTask)
|
||||
ForEach(self.userData.tasks) { task in
|
||||
TaskItemView(task: task, isEditing: self.$isEditing)
|
||||
}
|
||||
}
|
||||
.navigationBarItem(title: Text("Tasks 👀"))
|
||||
.navigationBarItems(trailing: Button(action: { self.isEditing.toggle() }) {
|
||||
if !self.isEditing {
|
||||
Text("Edit")
|
||||
} else {
|
||||
Text("Done").bold()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private func createTask() {
|
||||
let newTask = Task(title: self.draftTitle, isDone: false)
|
||||
self.userData.tasks.insert(newTask, at: 0)
|
||||
self.draftTitle = ""
|
||||
}
|
||||
}
|
||||
26
Examples/Example To-Do App/SwiftUITodo/UserData.swift
Executable file
26
Examples/Example To-Do App/SwiftUITodo/UserData.swift
Executable file
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// UserData.swift
|
||||
// SwiftUITodo
|
||||
//
|
||||
// Created by Suyeol Jeon on 03/06/2019.
|
||||
// Copyright © 2019 Suyeol Jeon. All rights reserved.
|
||||
//
|
||||
|
||||
import Combine
|
||||
import SwiftUI
|
||||
|
||||
private let defaultTasks: [Task] = [
|
||||
Task(title: "Read SwiftUI Documentation 📚", isDone: false),
|
||||
Task(title: "Watch WWDC19 Keynote 🎉", isDone: true),
|
||||
]
|
||||
|
||||
final class UserData: BindableObject {
|
||||
let didChange = PassthroughSubject<UserData, Never>()
|
||||
|
||||
@UserDefaultValue(key: "Tasks", defaultValue: defaultTasks)
|
||||
var tasks: [Task] {
|
||||
didSet {
|
||||
didChange.send(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
28
Examples/Example To-Do App/SwiftUITodo/UserDefaultsValue.swift
Executable file
28
Examples/Example To-Do App/SwiftUITodo/UserDefaultsValue.swift
Executable file
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// UserDefaultsValue.swift
|
||||
// SwiftUITodo
|
||||
//
|
||||
// Created by kemchenj on 2019/6/5.
|
||||
// Copyright © 2019 Suyeol Jeon. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
@propertyDelegate
|
||||
struct UserDefaultValue<Value: Codable> {
|
||||
|
||||
let key: String
|
||||
let defaultValue: Value
|
||||
|
||||
var value: Value {
|
||||
get {
|
||||
let data = UserDefaults.standard.data(forKey: key)
|
||||
let value = data.flatMap { try? JSONDecoder().decode(Value.self, from: $0) }
|
||||
return value ?? defaultValue
|
||||
}
|
||||
set {
|
||||
let data = try? JSONEncoder().encode(newValue)
|
||||
UserDefaults.standard.set(data, forKey: key)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user