Skip to content

Commit

Permalink
Use 2 space indentation as per Swift style guides
Browse files Browse the repository at this point in the history
  • Loading branch information
GeordieP committed Nov 20, 2019
1 parent a77b008 commit 18e1538
Show file tree
Hide file tree
Showing 17 changed files with 374 additions and 374 deletions.
36 changes: 18 additions & 18 deletions SwiftShop/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ 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
}

// 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.
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
// 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.
}
}
14 changes: 7 additions & 7 deletions SwiftShop/Containers/ListPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
import SwiftUI

struct ListPage: View {
var body: some View {
VStack {
Text("List page")
}
var body: some View {
VStack {
Text("List page")
}
}
}

struct ListPage_Previews: PreviewProvider {
static var previews: some View {
ListPage()
}
static var previews: some View {
ListPage()
}
}
64 changes: 32 additions & 32 deletions SwiftShop/Containers/ProductsPage/FilterManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,36 @@
import Foundation

class FilterManager<T> : ObservableObject {
typealias Filter = (T) -> Bool
@Published var filters: [String: Filter]
init(defaultFilters: [String: Filter] = [String: Filter]()) {
filters = defaultFilters
}

/// Update a filter by name, or insert it if it does not exist.
///
/// - Parameters:
/// - name: Name of filter
/// - filterFn: Closure for filtering a T
func upsert(_ name: String, _ filterFn: @escaping Filter) {
filters.updateValue(filterFn, forKey: name)
}
/// Remove a filter by name.
///
/// - Parameters:
/// - name: Name of filter to remove
func remove(_ name: String) {
filters.removeValue(forKey: name)
}
/// Apply the current list of filters to a collection of T.
///
/// - Parameters:
/// - to: Collection to filter
/// - Returns: A filtered collection of T
func apply(to collection: [T]) -> [T] {
filters.values.reduce(collection, { $0.filter($1) })
}
typealias Filter = (T) -> Bool
@Published var filters: [String: Filter]

init(defaultFilters: [String: Filter] = [String: Filter]()) {
filters = defaultFilters
}
/// Update a filter by name, or insert it if it does not exist.
///
/// - Parameters:
/// - name: Name of filter
/// - filterFn: Closure for filtering a T
func upsert(_ name: String, _ filterFn: @escaping Filter) {
filters.updateValue(filterFn, forKey: name)
}

/// Remove a filter by name.
///
/// - Parameters:
/// - name: Name of filter to remove
func remove(_ name: String) {
filters.removeValue(forKey: name)
}

/// Apply the current list of filters to a collection of T.
///
/// - Parameters:
/// - to: Collection to filter
/// - Returns: A filtered collection of T
func apply(to collection: [T]) -> [T] {
filters.values.reduce(collection, { $0.filter($1) })
}
}
84 changes: 42 additions & 42 deletions SwiftShop/Containers/ProductsPage/ProductsPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,57 +10,57 @@ import SwiftUI
import SwiftDux

struct ProductsPage: View {
@MappedState private var props: Props
@MappedDispatch() private var dispatch
@ObservedObject private var filters = FilterManager<Product>()

func createProduct(name: String, price: Float) {
self.dispatch(ProductsAction.AddProduct(name: name, price: price))
}

func deleteProduct(in offset: IndexSet) {
self.dispatch(ProductsAction.RemoveProduct(at: offset))
}

var body: some View {
VStack() {
AddProductForm(onSubmit: createProduct)
.frame(height: 220.0)

ProductFilterBar(filterManager: filters)

List {
ForEach(filters.apply(to: props.products.values)) { product in
HStack {
Text(product.name)
Spacer()
Text(String(product.price))
}
}
.onDelete(perform: deleteProduct)
}
@MappedState private var props: Props
@MappedDispatch() private var dispatch
@ObservedObject private var filters = FilterManager<Product>()

func createProduct(name: String, price: Float) {
self.dispatch(ProductsAction.AddProduct(name: name, price: price))
}

func deleteProduct(in offset: IndexSet) {
self.dispatch(ProductsAction.RemoveProduct(at: offset))
}

var body: some View {
VStack() {
AddProductForm(onSubmit: createProduct)
.frame(height: 220.0)

ProductFilterBar(filterManager: filters)

List {
ForEach(filters.apply(to: props.products.values)) { product in
HStack {
Text(product.name)
Spacer()
Text(String(product.price))
}
}
.onDelete(perform: deleteProduct)
}
}
}
}

extension ProductsPage: Connectable {
struct Props {
var products: OrderedState<Product>
}
func map(state: AppState) -> Props? {
Props(products: state.products)
}
struct Props {
var products: OrderedState<Product>
}

func map(state: AppState) -> Props? {
Props(products: state.products)
}
}

struct ConnectedProductsPage: View {
var body: some View {
ProductsPage().connect()
}
var body: some View {
ProductsPage().connect()
}
}

struct ProductsPage_Previews: PreviewProvider {
static var previews: some View {
ConnectedProductsPage().provideStore(Store(state: AppState(), reducer: AppReducer()))
}
static var previews: some View {
ConnectedProductsPage().provideStore(Store(state: AppState(), reducer: AppReducer()))
}
}
60 changes: 30 additions & 30 deletions SwiftShop/Containers/ProductsPage/Views/AddProductForm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,39 @@ import SwiftUI
let DEFAULT_PRICE = "0.00"

struct AddProductForm: View {
var onSubmit: (String, Float) -> () // this is passed in from whatever renders us

@State private var name = ""
@State private var price = DEFAULT_PRICE

var body: some View {
Form {
Text("New Product").font(.headline)

TextField("Name", text: $name)
TextField("Price", text: $price)

Button(action: handleSubmit) {
Text("Submit")
}
}
var onSubmit: (String, Float) -> () // this is passed in from whatever renders us

@State private var name = ""
@State private var price = DEFAULT_PRICE

var body: some View {
Form {
Text("New Product").font(.headline)

TextField("Name", text: $name)
TextField("Price", text: $price)

Button(action: handleSubmit) {
Text("Submit")
}
}

func handleSubmit() {
guard let price = Float(price) else {
return
}

onSubmit(name, price)
self.name = ""
self.price = DEFAULT_PRICE
}

func handleSubmit() {
guard let price = Float(price) else {
return
}

onSubmit(name, price)
self.name = ""
self.price = DEFAULT_PRICE
}
}

struct AddProductForm_Previews: PreviewProvider {
static var previews: some View {
AddProductForm(onSubmit: mockOnSubmit)
}
static func mockOnSubmit(name: String, price: Float) {}
static var previews: some View {
AddProductForm(onSubmit: mockOnSubmit)
}

static func mockOnSubmit(name: String, price: Float) {}
}
46 changes: 23 additions & 23 deletions SwiftShop/Containers/ProductsPage/Views/ProductFilterBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@
import SwiftUI

struct ProductFilterBar: View {
var filterManager: FilterManager<Product>

var body: some View {
HStack {
Button(action: addSearchFilter) {
Text("Add ir filter")
}
Spacer()
Button(action: removeSearchFilter) {
Text("Remove ir filter")
}
}.padding()
}
func addSearchFilter() {
filterManager.upsert("SEARCH", { $0.name.contains("ir") })
}
func removeSearchFilter() {
filterManager.remove("SEARCH")
}
var filterManager: FilterManager<Product>
var body: some View {
HStack {
Button(action: addSearchFilter) {
Text("Add ir filter")
}

Spacer()

Button(action: removeSearchFilter) {
Text("Remove ir filter")
}
}.padding()
}

func addSearchFilter() {
filterManager.upsert("SEARCH", { $0.name.contains("ir") })
}

func removeSearchFilter() {
filterManager.remove("SEARCH")
}
}

//struct ProductFilterBar_Previews: PreviewProvider {
Expand Down
14 changes: 7 additions & 7 deletions SwiftShop/Containers/SettingsPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
import SwiftUI

struct SettingsPage: View {
var body: some View {
VStack {
Text("Settings page")
}
var body: some View {
VStack {
Text("Settings page")
}
}
}

struct SettingsPage_Previews: PreviewProvider {
static var previews: some View {
SettingsPage()
}
static var previews: some View {
SettingsPage()
}
}
Loading

0 comments on commit 18e1538

Please sign in to comment.