This commit is contained in:
Ivan Vorobei
2019-06-26 22:12:35 +03:00
parent 2477f5d038
commit 48001a8e9a
1363 changed files with 6 additions and 3 deletions

View File

@@ -0,0 +1,41 @@
//
// AppDelegate.swift
// SwiftUIDemo
//
// Created by Thomas Ricouard on 04/06/2019.
// Copyright © 2019 Thomas Ricouarf. 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.
}
}

View File

@@ -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"
}
}

View File

@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}

View File

@@ -0,0 +1,22 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "user-image.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "user-image@2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View 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>

View 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>

View File

@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}

View File

@@ -0,0 +1,24 @@
//
// SceneDelegate.swift
// SwiftUIDemo
//
// Created by Thomas Ricouard on 04/06/2019.
// Copyright © 2019 Thomas Ricouarf. 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) {
let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = UIHostingController(rootView: TabbarView().environmentObject(store))
self.window = window
window.makeKeyAndVisible()
}
}

View File

@@ -0,0 +1,13 @@
//
// Action.swift
// SwiftUIDemo
//
// Created by Thomas Ricouard on 05/06/2019.
// Copyright © 2019 Thomas Ricouarf. All rights reserved.
//
import Foundation
protocol Action {
}

View File

@@ -0,0 +1,19 @@
//
// UsersAction.swift
// SwiftUIDemo
//
// Created by Thomas Ricouard on 05/06/2019.
// Copyright © 2019 Thomas Ricouarf. All rights reserved.
//
import Foundation
enum UserActions: Action {
case addUser
case deleteUser(index: Int)
case move(from: Int, to: Int)
case editUser(id: Int, name: String, username: String)
case testEditFirstUser
case startEditUser
case stopEditUser
}

View File

@@ -0,0 +1,20 @@
//
// User.swift
// SwiftUIDemo
//
// Created by Thomas Ricouard on 04/06/2019.
// Copyright © 2019 Thomas Ricouarf. All rights reserved.
//
import Foundation
import SwiftUI
struct User: Identifiable {
let id: Int
var name: String
var username: String
let imageName = "person"
}
let sampleData = [User(id: 0, name: "user 1", username: "@user1"),
User(id: 1, name: "user 2", username: "@user2")]

View File

@@ -0,0 +1,14 @@
//
// Reducer.swift
// SwiftUIDemo
//
// Created by Thomas Ricouard on 05/06/2019.
// Copyright © 2019 Thomas Ricouarf. All rights reserved.
//
import Foundation
protocol Reducer {
associatedtype StateType: FluxState
func reduce(state: StateType, action: Action) -> StateType
}

View File

@@ -0,0 +1,42 @@
//
// UsersStateReducer.swift
// SwiftUIDemo
//
// Created by Thomas Ricouard on 05/06/2019.
// Copyright © 2019 Thomas Ricouarf. All rights reserved.
//
import Foundation
struct UserStateReducer: Reducer {
func reduce(state: UsersState, action: Action) -> UsersState {
var state = state
switch action {
case UserActions.addUser:
state.users.append(User(id: state.users.count,
name: "New user \(state.users.count + 1)",
username: "@newuser\(state.users.count + 1)"))
case let UserActions.deleteUser(index):
state.users.remove(at: index)
case let UserActions.move(from, to):
let user = state.users.remove(at: from)
state.users.insert(user, at: to)
case let UserActions.editUser(id, name, username):
var user = state.users[id]
user.name = name
user.username = username
state.users[id] = user
case UserActions.testEditFirstUser:
if !state.users.isEmpty {
state.users[0] = User(id: 0, name: "user1", username: "u\ns\ne\nr\nn\na\nm\ne")
}
case UserActions.startEditUser:
state.isEditingUser = true
case UserActions.stopEditUser:
state.isEditingUser = false
default:
break
}
return state
}
}

View File

@@ -0,0 +1,34 @@
//
// AppStore.swift
// SwiftUIDemo
//
// Created by Thomas Ricouard on 05/06/2019.
// Copyright © 2019 Thomas Ricouarf. All rights reserved.
//
import Foundation
import SwiftUI
import Combine
final class AppState: BindableObject {
var didChange = PassthroughSubject<AppState, Never>()
var usersState: UsersState
init(usersState: UsersState = UsersState()) {
self.usersState = usersState
}
func dispatch(action: Action) {
usersState = UserStateReducer().reduce(state: usersState, action: action)
didChange.send(self)
}
}
let store = AppState()
#if DEBUG
let sampleStore = AppState(usersState: UsersState(users: sampleData))
#endif

View File

@@ -0,0 +1,11 @@
//
// FluxState.swift
// SwiftUIDemo
//
// Created by Thomas Ricouard on 05/06/2019.
// Copyright © 2019 Thomas Ricouarf. All rights reserved.
//
import Foundation
protocol FluxState { }

View File

@@ -0,0 +1,20 @@
//
// UsersStore.swift
// SwiftUIDemo
//
// Created by Thomas Ricouard on 04/06/2019.
// Copyright © 2019 Thomas Ricouarf. All rights reserved.
//
import Foundation
import SwiftUI
import Combine
struct UsersState: FluxState {
var users: [User]
var isEditingUser = false
init(users: [User] = []) {
self.users = users
}
}

View File

@@ -0,0 +1,32 @@
//
// MainView.swift
// SwiftUIDemo
//
// Created by Thomas Ricouard on 05/06/2019.
// Copyright © 2019 Thomas Ricouarf. All rights reserved.
//
import SwiftUI
struct TabbarView : View {
@EnvironmentObject var state: AppState
@State var selectedIndex: Int = 0
var body: some View {
TabbedView(selection: $selectedIndex) {
UsersListView()
.tabItemLabel(Text("Users"))
MapView()
.tabItemLabel(Text("Map"))
}
}
}
#if DEBUG
struct MainView_Previews : PreviewProvider {
static var previews: some View {
TabbarView(selectedIndex: 0).environmentObject(sampleStore)
}
}
#endif

View File

@@ -0,0 +1,36 @@
//
// MapVie.swift
// SwiftUIDemo
//
// Created by Thomas Ricouard on 05/06/2019.
// Copyright © 2019 Thomas Ricouarf. All rights reserved.
//
import SwiftUI
import UIKit
import MapKit
struct MapViewRepresentable: UIViewRepresentable {
func makeUIView(context: Context) -> MKMapView {
return MKMapView()
}
func updateUIView(_ uiView: MKMapView, context: Context) {
}
}
struct MapView : View {
var body: some View {
MapViewRepresentable()
}
}
#if DEBUG
struct MapVie_Previews : PreviewProvider {
static var previews: some View {
MapView()
}
}
#endif

View File

@@ -0,0 +1,53 @@
//
// UserDetailView.swift
// SwiftUIDemo
//
// Created by Thomas Ricouard on 04/06/2019.
// Copyright © 2019 Thomas Ricouarf. All rights reserved.
//
import SwiftUI
import Combine
struct UserDetailView : View {
@EnvironmentObject var state: AppState
let userId: Int
var editModal: Modal {
let user = state.usersState.users[userId]
return Modal(UserEditForm(userId: user.id, saveHandler: { saved in
self.state.dispatch(action: UserActions.stopEditUser)
}).environmentObject(state)) {
self.state.dispatch(action: UserActions.stopEditUser)
}
}
var body: some View {
let user = state.usersState.users[userId]
return VStack {
Image(systemName: user.imageName)
Text(user.name)
Text(user.username).lineLimit(0)
}
.navigationBarTitle(Text(user.name), displayMode: .inline)
.navigationBarItems(trailing:
Button(action: {
self.state.dispatch(action: UserActions.startEditUser)
}) {
Text("Edit user")
}
.presentation(self.state.usersState.isEditingUser ? self.editModal : nil))
}
}
#if DEBUG
struct UserDetailView_Previews : PreviewProvider {
static var previews: some View {
NavigationView {
UserDetailView(userId: 0).environmentObject(sampleStore)
}
}
}
#endif

View File

@@ -0,0 +1,88 @@
//
// UserEditForm.swift
// SwiftUIDemo
//
// Created by Thomas Ricouard on 05/06/2019.
// Copyright © 2019 Thomas Ricouarf. All rights reserved.
//
import SwiftUI
struct UserEditForm : View {
@EnvironmentObject var state: AppState
let userId: Int
let saveHandler: ((Bool) -> Swift.Void)?
@State var newUserName = ""
@State var newUserUsername = ""
@State var showSaved = false
@State var showError = false
var body: some View {
let user = state.usersState.users[userId]
return NavigationView {
VStack(alignment: .leading, spacing: 10) {
Text("User name")
TextField($newUserName, placeholder: Text("New name"))
.textFieldStyle(.roundedBorder)
Divider()
Text("Username")
TextField($newUserUsername, placeholder: Text("New username"))
.textFieldStyle(.roundedBorder)
}.padding(16)
Button(action: save) {
Text("Save")
.padding(8)
.foregroundColor(.white)
.background(Color.green)
.cornerRadius(8)
}
.navigationBarItems(trailing: Button(action: close) {
Text("Close")
})
.navigationBarTitle(Text("Edit \(user.name)"), displayMode: .inline)
Badge(text: "Saved successfully", color: .green, show: $showSaved)
Badge(text: "Missing username or name", color: .red, show: $showError)
}
}
func save() {
guard !newUserName.isEmpty && !newUserUsername.isEmpty else {
withAnimation{
showError = true
}
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.showError = false
}
return
}
withAnimation {
showSaved = true
}
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.showSaved = false
}
state.dispatch(action: UserActions.editUser(id: userId, name: newUserName, username: newUserUsername))
saveHandler?(true)
}
func close() {
saveHandler?(false)
}
}
#if DEBUG
struct UserEditForm_Previews : PreviewProvider {
static var previews: some View {
UserEditForm(userId: 0, saveHandler: nil).environmentObject(sampleStore)
}
}
#endif

View File

@@ -0,0 +1,66 @@
//
// ContentView.swift
// SwiftUIDemo
//
// Created by Thomas Ricouard on 04/06/2019.
// Copyright © 2019 Thomas Ricouarf. All rights reserved.
//
import SwiftUI
struct UsersListView : View {
@EnvironmentObject var state: AppState
var body: some View {
NavigationView {
List {
Section {
Button(action: addUser) {
Text("Add user")
}
Button(action: targetUpdate) {
Text("Update first")
}
}
Section {
ForEach(state.usersState.users) {user in
NavigationButton(destination: UserDetailView(userId: user.id)) {
UserRow(user: user)
}
}
.onDelete(perform: delete)
.onMove(perform: move)
}
}
.listStyle(.grouped)
.navigationBarTitle(Text("Users (\(state.usersState.users.count))"))
.navigationBarItems(trailing: EditButton())
}
}
func addUser() {
state.dispatch(action: UserActions.addUser)
}
func targetUpdate() {
state.dispatch(action: UserActions.testEditFirstUser)
}
func delete(at offset: IndexSet) {
state.dispatch(action: UserActions.deleteUser(index: offset.first!))
}
func move(from: IndexSet, to: Int) {
state.dispatch(action: UserActions.move(from: from.first!, to: to))
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
UsersListView().environmentObject(sampleStore)
}
}
#endif

View File

@@ -0,0 +1,32 @@
//
// GreenBadge.swift
// SwiftUIDemo
//
// Created by Thomas Ricouard on 06/06/2019.
// Copyright © 2019 Thomas Ricouarf. All rights reserved.
//
import SwiftUI
struct Badge : View {
let text: String
let color: Color
@Binding var show: Bool
var animation: Animation {
Animation
.spring(initialVelocity: 5)
.speed(2)
}
var body: some View {
Text(text)
.color(.white)
.padding()
.background(color)
.cornerRadius(8)
.scaleEffect(show ? 1: 0.5)
.opacity(show ? 1 : 0)
.animation(animation)
}
}

View File

@@ -0,0 +1,36 @@
//
// UserRw.swift
// SwiftUIDemo
//
// Created by Thomas Ricouard on 04/06/2019.
// Copyright © 2019 Thomas Ricouarf. All rights reserved.
//
import SwiftUI
struct UserRow : View {
let user: User
var body: some View {
VStack {
HStack {
Image(systemName: user.imageName)
VStack {
Text(user.name)
Text(user.username)
.color(.secondary)
.lineLimit(0)
}
}
}
}
}
#if DEBUG
struct UserRw_Previews : PreviewProvider {
static var previews: some View {
UserRow(user: sampleData[0])
}
}
#endif