Skip to content
This repository has been archived by the owner on Nov 24, 2021. It is now read-only.

Commit

Permalink
Run SwiftFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
3lvis committed Oct 17, 2016
1 parent 96d0a49 commit abaea90
Show file tree
Hide file tree
Showing 18 changed files with 122 additions and 105 deletions.
1 change: 1 addition & 0 deletions CollectionSwift/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class AppDelegate: UIResponder {
}

extension AppDelegate: UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
guard let window = self.window else { fatalError("Window not found") }
Expand Down
11 changes: 6 additions & 5 deletions CollectionSwift/CollectionController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CollectionController: UICollectionViewController {
let request: NSFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "User")
request.sortDescriptors = [
NSSortDescriptor(key: "name", ascending: true),
NSSortDescriptor(key: "firstLetterOfName", ascending: true)
NSSortDescriptor(key: "firstLetterOfName", ascending: true),
]

let dataSource = DATASource(collectionView: collectionView, cellIdentifier: CollectionCell.Identifier, fetchRequest: request, mainContext: self.dataStack.mainContext, sectionName: "firstLetterOfName", configuration: { cell, item, indexPath in
Expand All @@ -21,7 +21,7 @@ class CollectionController: UICollectionViewController {
})

return dataSource
}()
}()

init(layout: UICollectionViewLayout, dataStack: DATAStack) {
self.dataStack = dataStack
Expand Down Expand Up @@ -71,22 +71,23 @@ class CollectionController: UICollectionViewController {
func randomString() -> String {
let letters = "ABCDEFGHIJKL"
var string = ""
for _ in 0...5 {
for _ in 0 ... 5 {
let token = UInt32(letters.characters.count)
let letterIndex = Int(arc4random_uniform(token))
let firstChar = Array(letters.characters)[letterIndex]
string.append(firstChar)
}

return string
}
}

extension CollectionController {

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let object = self.dataSource.objectAtIndexPath(indexPath) else { return }

if let name = object.value(forKey: "name") as? String , name.characters.first == "A" {
if let name = object.value(forKey: "name") as? String, name.characters.first == "A" {
self.dataStack.performInNewBackgroundContext({ backgroundContext in
let backgroundObject = backgroundContext.object(with: object.objectID)
backgroundObject.setValue(name + "+", forKey: "name")
Expand Down
3 changes: 2 additions & 1 deletion DATASourceDelegateTableSwift/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ class AppDelegate: UIResponder {
}

extension AppDelegate: UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
guard let window = self.window else { fatalError("Window not found") }

let viewController = ViewController(dataStack: self.dataStack)
window.rootViewController = UINavigationController(rootViewController: viewController)
window.makeKeyAndVisible()

return true
}
}
3 changes: 2 additions & 1 deletion DATASourceDelegateTableSwift/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,20 @@ class ViewController: UITableViewController {
}

extension ViewController: DATASourceDelegate {

func dataSource(_ dataSource: DATASource, tableView: UITableView, canEditRowAtIndexPath indexPath: IndexPath) -> Bool {
return true
}

// This doesn't seem to be needed when implementing tableView(_:editActionsForRowAtIndexPath).
func dataSource(_ dataSource: DATASource, tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: IndexPath) {

}
}

// MARK: - UITableViewDelegate

extension ViewController {

override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
let delete = UITableViewRowAction(style: .default, title: "Delete") { action, indexPath in
let item = self.dataSource.objectAtIndexPath(indexPath)!
Expand Down
3 changes: 2 additions & 1 deletion InfiniteCollectionSwift/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class AppDelegate: UIResponder {
}

extension AppDelegate: UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
guard let window = self.window else { fatalError("Window not found") }
Expand All @@ -28,7 +29,7 @@ extension AppDelegate: UIApplicationDelegate {
let viewController = CollectionController(layout: layout, dataStack: self.dataStack)
window.rootViewController = UINavigationController(rootViewController: viewController)
window.makeKeyAndVisible()

return true
}
}
8 changes: 5 additions & 3 deletions InfiniteCollectionSwift/CollectionController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CollectionController: UICollectionViewController {
let request: NSFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "User")
request.sortDescriptors = [
NSSortDescriptor(key: "name", ascending: true),
NSSortDescriptor(key: "firstLetterOfName", ascending: true)
NSSortDescriptor(key: "firstLetterOfName", ascending: true),
]

let dataSource = DATASource(collectionView: collectionView, cellIdentifier: CollectionCell.Identifier, fetchRequest: request, mainContext: self.dataStack.mainContext, sectionName: "firstLetterOfName", configuration: { cell, item, indexPath in
Expand Down Expand Up @@ -57,6 +57,7 @@ class CollectionController: UICollectionViewController {
}

var loading = false

override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
guard let collectionView = self.collectionView else { return }
guard self.loading == false else { return }
Expand All @@ -80,7 +81,7 @@ class CollectionController: UICollectionViewController {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.dataStack.performInNewBackgroundContext { backgroundContext in
let entity = NSEntityDescription.entity(forEntityName: "User", in: backgroundContext)!
for i in initialIndex..<initialIndex + 18 {
for i in initialIndex ..< initialIndex + 18 {
let user = NSManagedObject(entity: entity, insertInto: backgroundContext)
user.setValue(String(format: "%04d", i), forKey: "name")

Expand All @@ -98,10 +99,11 @@ class CollectionController: UICollectionViewController {
}

extension CollectionController {

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let numberOfItems = self.collectionView?.numberOfItems(inSection: (indexPath as NSIndexPath).section) else { return }
var items = [NSManagedObject]()
for i in 0..<numberOfItems {
for i in 0 ..< numberOfItems {
let newIndexPath = IndexPath(row: i, section: (indexPath as NSIndexPath).section)
if let item = self.dataSource.objectAtIndexPath(newIndexPath) {
items.append(item)
Expand Down
1 change: 1 addition & 0 deletions InfiniteTableSwift/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class AppDelegate: UIResponder {
}

extension AppDelegate: UIApplicationDelegate {

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = UIWindow(frame: UIScreen.main.bounds)
guard let window = self.window else { fatalError("Window not found") }
Expand Down
6 changes: 4 additions & 2 deletions InfiniteTableSwift/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ViewController: UITableViewController {
let request: NSFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "User")
request.sortDescriptors = [
NSSortDescriptor(key: "name", ascending: true),
NSSortDescriptor(key: "firstLetterOfName", ascending: true)
NSSortDescriptor(key: "firstLetterOfName", ascending: true),
]

let dataSource = DATASource(tableView: self.tableView, cellIdentifier: ViewController.Identifier, fetchRequest: request, mainContext: self.dataStack!.mainContext, sectionName: "firstLetterOfName", configuration: { cell, item, indexPath in
Expand Down Expand Up @@ -49,6 +49,7 @@ class ViewController: UITableViewController {
}

var loading = false

override func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
guard let tableView = self.tableView else { return }
guard self.loading == false else { return }
Expand All @@ -72,7 +73,7 @@ class ViewController: UITableViewController {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.dataStack!.performInNewBackgroundContext { backgroundContext in
let entity = NSEntityDescription.entity(forEntityName: "User", in: backgroundContext)!
for i in initialIndex..<initialIndex + 18 {
for i in initialIndex ..< initialIndex + 18 {
let user = NSManagedObject(entity: entity, insertInto: backgroundContext)
user.setValue(String(format: "%04d", i), forKey: "name")

Expand All @@ -90,6 +91,7 @@ class ViewController: UITableViewController {
}

extension ViewController: DATASourceDelegate {

func sectionIndexTitlesForDataSource(_ dataSource: DATASource, tableView: UITableView) -> [String] {
return [String]()
}
Expand Down
12 changes: 6 additions & 6 deletions Library/InfiniteLoadingIndicator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class InfiniteLoadingIndicator: UIView {

UIView.animate(withDuration: 0.3, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: [.allowUserInteraction], animations: {
self.frame = originalFrame
}, completion: nil)
}, completion: nil)
}
}

Expand All @@ -58,11 +58,11 @@ class InfiniteLoadingIndicator: UIView {
newFrame.origin.y = self.parentController.view.frame.height + 10
UIView.animate(withDuration: 0.3, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: [.allowUserInteraction], animations: { () -> Void in
self.frame = newFrame
}, completion: { finished in
self.frame = originalFrame
if self.superview != nil {
self.removeFromSuperview()
}
}, completion: { finished in
self.frame = originalFrame
if self.superview != nil {
self.removeFromSuperview()
}
})
}
}
Expand Down
7 changes: 4 additions & 3 deletions Source/DATASource+NSFetchedResultsControllerDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import UIKit
import CoreData

extension DATASource: NSFetchedResultsControllerDelegate {

public func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
if let tableView = self.tableView {
tableView.beginUpdates()
} else if let _ = self.collectionView {
self.sectionChanges = [NSFetchedResultsChangeType : IndexSet]()
self.objectChanges = [NSFetchedResultsChangeType : Set<IndexPath>]()
self.sectionChanges = [NSFetchedResultsChangeType: IndexSet]()
self.objectChanges = [NSFetchedResultsChangeType: Set<IndexPath>]()
}
}

Expand Down Expand Up @@ -190,7 +191,7 @@ extension DATASource: NSFetchedResultsControllerDelegate {
collectionView.moveItem(at: fromIndexPath as IndexPath, to: toIndexPath as IndexPath)
}

}, completion: nil)
}, completion: nil)
}
}
self.delegate?.dataSourceDidChangeContent?(self)
Expand Down
3 changes: 2 additions & 1 deletion Source/DATASource+UICollectionViewDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import UIKit
import CoreData

extension DATASource: UICollectionViewDataSource {

public func numberOfSections(in collectionView: UICollectionView) -> Int {
return self.fetchedResultsController.sections?.count ?? 0
}
Expand Down Expand Up @@ -32,7 +33,7 @@ extension DATASource: UICollectionViewDataSource {

public func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
if let keyPath = self.fetchedResultsController.sectionNameKeyPath {
if self.cachedSectionNames.isEmpty || indexPath.section >= self.cachedSectionNames.count{
if self.cachedSectionNames.isEmpty || indexPath.section >= self.cachedSectionNames.count {
self.cacheSectionNames(using: keyPath)
}

Expand Down
66 changes: 34 additions & 32 deletions Source/DATASource+UITableViewDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import UIKit
import CoreData

extension DATASource: UITableViewDataSource {

public func numberOfSections(in tableView: UITableView) -> Int {
return self.fetchedResultsController.sections?.count ?? 0
}
Expand All @@ -17,14 +18,14 @@ extension DATASource: UITableViewDataSource {
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

var cellIdentifier = self.cellIdentifier

if let value = self.delegate?.cellIdentifier?(forIndexPath: indexPath) {
cellIdentifier = value
}
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)

let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)

self.configure(cell, indexPath: indexPath)

Expand All @@ -34,40 +35,41 @@ extension DATASource: UITableViewDataSource {
// Sections and Headers

#if os(iOS)
public func sectionIndexTitles(for tableView: UITableView) -> [String]? {
if let titles = self.delegate?.sectionIndexTitlesForDataSource?(self, tableView: tableView) {
return titles
} else if let keyPath = self.fetchedResultsController.sectionNameKeyPath {
let request = NSFetchRequest<NSFetchRequestResult>()
request.entity = self.fetchedResultsController.fetchRequest.entity
request.resultType = .dictionaryResultType
request.returnsDistinctResults = true
request.propertiesToFetch = [keyPath]
request.sortDescriptors = [NSSortDescriptor(key: keyPath, ascending: true)]
var names = [String]()
var objects: [NSDictionary]?

do {
objects = try self.fetchedResultsController.managedObjectContext.fetch(request) as? [NSDictionary]
} catch {
print("Error")
}

if let objects = objects {
for object in objects {
names.append(contentsOf: object.allValues as! [String])
public func sectionIndexTitles(for tableView: UITableView) -> [String]? {
if let titles = self.delegate?.sectionIndexTitlesForDataSource?(self, tableView: tableView) {
return titles
} else if let keyPath = self.fetchedResultsController.sectionNameKeyPath {
let request = NSFetchRequest<NSFetchRequestResult>()
request.entity = self.fetchedResultsController.fetchRequest.entity
request.resultType = .dictionaryResultType
request.returnsDistinctResults = true
request.propertiesToFetch = [keyPath]
request.sortDescriptors = [NSSortDescriptor(key: keyPath, ascending: true)]
var names = [String]()
var objects: [NSDictionary]?

do {
objects = try self.fetchedResultsController.managedObjectContext.fetch(request) as? [NSDictionary]
} catch {
print("Error")
}

if let objects = objects {
for object in objects {
names.append(contentsOf: object.allValues as! [String])
}
}

return names
}

return names
return nil
}

return nil
}

public func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
return self.delegate?.dataSource?(self, tableView: tableView, sectionForSectionIndexTitle: title, atIndex: index) ?? index
}
public func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
return self.delegate?.dataSource?(self, tableView: tableView, sectionForSectionIndexTitle: title, atIndex: index) ?? index
}
#endif

public func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
Expand Down
Loading

0 comments on commit abaea90

Please sign in to comment.