Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
heiberg committed Jun 19, 2018
0 parents commit 5c921ec
Show file tree
Hide file tree
Showing 64 changed files with 5,879 additions and 0 deletions.
38 changes: 38 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Mac OS X
*.DS_Store

# Xcode
*.pbxuser
*.mode1v3
*.mode2v3
*.perspectivev3
*.xcuserstate
project.xcworkspace/
xcuserdata/
*.xccheckout

# Playgrounds
timeline.xctimeline

# Generated files
build/
#*.[oa]
*.pyc
.cache

# Backup files
*~.nib
*.orig
~*

# Localizer script
*.strings.old
*.strings.new

# Temporary files
*.sqlite-journal

!Carthage/Build/

Pods/
Podfile.lock
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
osx_image: xcode9.4
language: swift
script:
- pod install --project-directory=Examples
- set -o pipefail && xcodebuild test -enableCodeCoverage YES -workspace Examples/SimpleSourceExample.xcworkspace -scheme SimpleSource-Unit-Tests -destination 'name=iPhone 8' ONLY_ACTIVE_ARCH=YES | xcpretty
- pod lib lint
after_success:
- bash <(curl -s https://codecov.io/bash) -J 'SimpleSource'
74 changes: 74 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Contributor Covenant Code of Conduct

## Our Pledge

In the interest of fostering an open and welcoming environment, we as
contributors and maintainers pledge to making participation in our project and
our community a harassment-free experience for everyone, regardless of age, body
size, disability, ethnicity, gender identity and expression, level of experience,
nationality, personal appearance, race, religion, or sexual identity and
orientation.

## Our Standards

Examples of behavior that contributes to creating a positive environment
include:

* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery and unwelcome sexual attention or
advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic
address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting

## Our Responsibilities

Project maintainers are responsible for clarifying the standards of acceptable
behavior and are expected to take appropriate and fair corrective action in
response to any instances of unacceptable behavior.

Project maintainers have the right and responsibility to remove, edit, or
reject comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct, or to ban temporarily or
permanently any contributor for other behaviors that they deem inappropriate,
threatening, offensive, or harmful.

## Scope

This Code of Conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community. Examples of
representing a project or community include using an official project e-mail
address, posting via an official social media account, or acting as an appointed
representative at an online or offline event. Representation of a project may be
further defined and clarified by project maintainers.

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project team at [[email protected]](mailto:[email protected]). All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Further details of specific enforcement policies may be posted separately.

Project maintainers who do not follow or enforce the Code of Conduct in good
faith may face temporary or permanent repercussions as determined by other
members of the project's leadership.

## Attribution

This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
available at [http://contributor-covenant.org/version/1/4][version]

[homepage]: http://contributor-covenant.org
[version]: http://contributor-covenant.org/version/1/4/
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import UIKit
import SimpleSource
import PlaygroundSupport

/*:
- Important:
Make sure to open this playground via `SimpleSourceExample.xcworkspace`. And remember to run `pod install` in the `Examples` directory to install the necessary prerequisites.
*/
/*:
# SimpleSource Basic Usage

The goal here is to show a bit of data in a table view using SimpleSource.
*/
/*:
## The Data

We begin by defining our model object, which must conform to `Equatable`. In this case we'll just use `String`, but you can use anything `Equatable` for the items.
*/
typealias Item = String
/*:
We want to organize our `Item` objects into sections. A section is anything conforming to the `SectionType` protocol.

A section only has to provide an `items` array. But we are free to add more properties to a section, such as a title (or anything else we need) to properly configure section headers etc.

To illustrate this, let's also add a title to our custom section type, to make it a little richer.
*/
struct Section: SectionType {
typealias ItemType = Item
var title: String
var items: [ItemType]
}
/*:
- Note:
If you want automatic animated updates when your data mutates, your sections must also
conform to `IdentifiableSection`. That way SimpleSource will be able to tell which sections have changed,
and create the necessary animations for you when the data changes.

Finally, let's create an array of `Section`s containing some `Item`s. This `[Section]` array will be our data.
*/
let sections: [Section] = [
Section(title: "First Section", items: ["First item", "Second item"]),
Section(title: "Second Section", items: ["First item in second section"])
]
/*:
We are ready to create the data source, which will hold the above data for use in SimpleSource.

- Note:
For explicit data (basic objects or values arranged in arrays), SimpleSource provides `BasicDataSource`. This is what we will use here. If your data is stored in Core Data you can use `CoreDataSource` instead.

A `BasicDataSource` exposes a mutable `.sections` property. This can be set to a new value whenever you wish, triggering an update to all views backed by this data source.
*/
let dataSource = BasicDataSource(sections: sections)
/*:
## The Views

Let's create a `UIViewController` with a `UITableView` to render our data:
*/
let vc = UITableViewController(style: .grouped)
/*:
To get data into the `UITableView` we will need something which conforms to `UITableViewDataSource`: SimpleSource provides `TableViewDataSource` (to drive table views) and `CollectionViewDataSource` (to drive collection views).

For our first example here let's create a `TableViewDataSource`.

Looking at the initializer for `TableViewDataSource` we see that it needs three different things:

- Some kind of data source from which to get the items
- A view factory from which to get the cells
- A way to incorporate changes in the data into the view

We already have the data source from before, so the next step is to create a view factory. This will be responsible for emitting cells for the table.

- Note:
A view factory is initialized with a closure to provide a `reuseIdentifier` for a given item in a given view. Returning different values from this closure based on the item will allow you to mix different cell types in the same view. In our case we only have one cell type, so we only ever return one reuse identifier.
*/
let viewFactory = TableViewFactory<Item> { item, view in
return "Cell"
}
/*:
With SimpleSource we use closures to configure cells as they are dequeued for display.

The convenient thing about these closures is that they are given both a correctly typed cell and a correctly typed model object, which we then use to configure the cell.

In this simple case we use vanilla `UITableViewCell`s, so that it what the closure gets. But if you have custom cell subclasses then that is what SimpleSource will send to your closure. No need for type casting.
*/
let configureCell = { (cell: UITableViewCell, item: Item, indexPath: IndexPath) -> Void in
cell.textLabel?.text = item
}
/*:
Now we need to register the cell types we want to display with the view factory.

This teaches the view factory how to dequeue the correct cells from the table view.

The `registerCell` method takes:

- A cell instantiation method (you can use nibs, class-based or storyboard prototypes).
- A reuse identifier.
- The view to register the cell in.
- The closure to configure the cell before display.

- Note:
If you want to mix multiple cells types in your table, there's two steps to it: In the closure passed to the `ViewFactory` initializer, you inspect the item and return the reuse identifier for the cell type you wish to display. Then call `registerCell` on the view factory for each possible reuse identifier that might be emitted from the above closure.
*/
viewFactory.registerCell(
method: .classBased(UITableViewCell.self),
reuseIdentifier: "Cell",
in: vc.tableView,
configuration: configureCell
)
/*:
For good measure, let's also add some headers to show off the title properties of our sections.
*/
viewFactory.registerHeaderText(in: vc.tableView) { section in
return dataSource.sections[section].title
}
/*:
Now we are ready to create the `UITableViewDataSource` for our table view. This is going to be an instance of `TableViewDataSource`.

We previously noted that `TableViewDataSource` needs you to provide a way to incorporate changes into the view. Most often you probably want to use one of the built-in row animations for table views, and use `performBatchUpdates` for collection views.

For table views SimpleSource defines `UITableView.defaultViewUpdate()` which does this animated update for you. If you prefer an unanimated update you can use `UITableView.unanimatedViewUpdate`. Or you can create your own. It's just a closure! You can also pass your favorite `UITableViewRowAnimation` to `defaultViewUpdate()` to customize it.

For collection views the built-in view updaters are called `UICollectionView.defaultViewUpdate` and `UICollectionView.unanimatedViewUpdate`.

Time to create the `TableViewDataSource`!
*/
let tableDataSource = TableViewDataSource(dataSource: dataSource, viewFactory: viewFactory, viewUpdate: vc.tableView.defaultViewUpdate())
/*:
- Important: It is your responsibility to keep a reference to your `TableViewDataSource`. For example in an instance variable of the `UIViewController` using it. This is important because `UITableView` does not retain its `.dataSource` property. – If you want to, you can forget about the `BasicDataSource` and the `TableViewFactory` after you have created your `TableViewDataSource`. They will be retained for as long as they are needed.

You are free to let one `BasicDataSource` back multiple `TableViewDataSource` or `CollectionViewDataSource` at the same time. All the views will automatically update if you change the data stored in `.sections` (or when the database changes if you are using `CoreDataSource`).
*/
/*:
## The Result

Now let's see the table view, rendering our data:
*/
vc.view.bounds.size = CGSize(width: 250, height: 300)
vc.tableView.dataSource = tableDataSource
PlaygroundPage.current.liveView = vc.view
/*:
Press the play button to run this playground (if it's not already running) and open the timeline using Xcode's assistant editor to see the live table view, populated with our data.
*/
/*:
## What's Next?

Run the example app in this workspace to see all this in action. Or dig into the documentation provided by the `README.md` file.
*/
2 changes: 2 additions & 0 deletions Examples/Playground.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='6.0' target-platform='ios' display-mode='rendered'/>

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Examples/Podfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
platform :ios, '9.0'

target 'SimpleSourceExample' do
use_frameworks!

pod 'SimpleSource', :path => '..', :testspecs => ['Tests']
pod 'SwiftyJSON'
end

# This makes the SimpleSource symbols available to the playground.
# It became necessary after the release of CocoaPods 1.5.0.
# See also https://github.com/CocoaPods/CocoaPods/issues/7606
post_install do |installer|
installer.pods_project.build_configurations.each do |config|
config.build_settings.delete('CODE_SIGNING_ALLOWED')
config.build_settings.delete('CODE_SIGNING_REQUIRED')
end
end
Loading

0 comments on commit 5c921ec

Please sign in to comment.