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

Commit

Permalink
Merge pull request #53 from 3lvis/example/infinite-sectioned-collection
Browse files Browse the repository at this point in the history
Example: infinite sectioned collection
  • Loading branch information
3lvis committed Jan 5, 2016
2 parents e836739 + 63a1b4d commit e19fe7f
Show file tree
Hide file tree
Showing 25 changed files with 132 additions and 260 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ language: objective-c
cache: cocoapods
before_install: gem install xcpretty cocoapods obcd slather -N
podfile: Podfile
script: xcodebuild -workspace Demo.xcworkspace -scheme Tests -sdk iphonesimulator build test GCC_INSTRUMENT_PROGRAM_FLOW_ARCS=YES GCC_GENERATE_TEST_COVERAGE_FILES=YES clean test | xcpretty -c && exit ${PIPESTATUS[0]}
script: xcodebuild -workspace Demo.xcworkspace -scheme Tests -sdk iphonesimulator build test -destination 'platform=iOS Simulator,name=iPhone 6' GCC_INSTRUMENT_PROGRAM_FLOW_ARCS=YES GCC_GENERATE_TEST_COVERAGE_FILES=YES clean test | xcpretty -c && exit ${PIPESTATUS[0]}
after_success: slather
9 changes: 0 additions & 9 deletions CollectionObjC/CollectionCell.h

This file was deleted.

37 changes: 0 additions & 37 deletions CollectionObjC/CollectionCell.m

This file was deleted.

8 changes: 4 additions & 4 deletions CollectionObjC/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@import CoreData;

#import "FooterExampleView.h"
#import "CollectionCell.h"
#import "CollectionObjC-Swift.h"

@interface ViewController () <NSFetchedResultsControllerDelegate, DATASourceDelegate>

Expand Down Expand Up @@ -33,13 +33,13 @@ - (DATASource *)dataSource {
request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"firstLetterOfName" ascending:YES], [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]];

_dataSource = [[DATASource alloc] initWithCollectionView:self.collectionView
cellIdentifier:CollectionCellIdentifier
cellIdentifier:[CollectionCell Identifier]
fetchRequest:request
mainContext:self.dataStack.mainContext
sectionName:@"firstLetterOfName"
configuration:^(UICollectionViewCell * _Nonnull cell, NSManagedObject * _Nonnull item, NSIndexPath * _Nonnull indexPath) {
CollectionCell *collectionCell = (CollectionCell *)cell;
[collectionCell updateWithText:[item valueForKey:@"name"]];
collectionCell.textLabel.text = [item valueForKey:@"name"];
}];
_dataSource.delegate = self;

Expand All @@ -49,7 +49,7 @@ - (DATASource *)dataSource {
- (void)viewDidLoad {
[super viewDidLoad];

[self.collectionView registerClass:[CollectionCell class] forCellWithReuseIdentifier:CollectionCellIdentifier];
[self.collectionView registerClass:[CollectionCell class] forCellWithReuseIdentifier:[CollectionCell Identifier]];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(addAction)];
Expand Down
20 changes: 10 additions & 10 deletions CollectionSwift/CollectionController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import DATASource
import CoreData

class CollectionController: UICollectionViewController {
var dataStack: DATAStack?
unowned var dataStack: DATAStack

lazy var dataSource: DATASource = {
guard let collectionView = self.collectionView, mainContext = self.dataStack?.mainContext else { fatalError("CollectionView is nil") }
guard let collectionView = self.collectionView else { fatalError("CollectionView is nil") }

let request: NSFetchRequest = NSFetchRequest(entityName: "User")
request.sortDescriptors = [
NSSortDescriptor(key: "name", ascending: true),
NSSortDescriptor(key: "firstLetterOfName", ascending: true)
]

let dataSource = DATASource(collectionView: collectionView, cellIdentifier: CollectionCell.Identifier, fetchRequest: request, mainContext: mainContext, sectionName: "firstLetterOfName", configuration: { cell, item, indexPath in
let dataSource = DATASource(collectionView: collectionView, cellIdentifier: CollectionCell.Identifier, fetchRequest: request, mainContext: self.dataStack.mainContext, sectionName: "firstLetterOfName", configuration: { cell, item, indexPath in
let collectionCell = cell as! CollectionCell
collectionCell.textLabel.text = item.valueForKey("name") as? String
})
Expand All @@ -25,9 +25,9 @@ class CollectionController: UICollectionViewController {
}()

init(layout: UICollectionViewLayout, dataStack: DATAStack) {
super.init(collectionViewLayout: layout)

self.dataStack = dataStack

super.init(collectionViewLayout: layout)
}

required init?(coder aDecoder: NSCoder) {
Expand All @@ -48,7 +48,7 @@ class CollectionController: UICollectionViewController {
}

func saveAction() {
self.dataStack?.performInNewBackgroundContext { backgroundContext in
self.dataStack.performInNewBackgroundContext { backgroundContext in
if let entity = NSEntityDescription.entityForName("User", inManagedObjectContext: backgroundContext) {
let user = NSManagedObject(entity: entity, insertIntoManagedObjectContext: backgroundContext)

Expand All @@ -65,7 +65,7 @@ class CollectionController: UICollectionViewController {
fatalError()
}

self.dataStack?.persistWithCompletion({ })
self.dataStack.persistWithCompletion({ })
} else {
print("Oh no")
}
Expand All @@ -91,17 +91,17 @@ extension CollectionController {
guard let object = self.dataSource.objectAtIndexPath(indexPath) else { return }

if let name = object.valueForKey("name") as? String where name.characters.first == "A" {
self.dataStack?.performInNewBackgroundContext({ backgroundContext in
self.dataStack.performInNewBackgroundContext({ backgroundContext in
let backgroundObject = backgroundContext.objectWithID(object.objectID)
backgroundObject.setValue(name + "+", forKey: "name")
try! backgroundContext.save()
})
} else {
self.dataStack?.performInNewBackgroundContext({ backgroundContext in
self.dataStack.performInNewBackgroundContext({ backgroundContext in
let backgroundObject = backgroundContext.objectWithID(object.objectID)
backgroundContext.deleteObject(backgroundObject)
try! backgroundContext.save()
})
}
}
}
}
Loading

0 comments on commit e19fe7f

Please sign in to comment.