Skip to content

Commit

Permalink
Merge pull request #8 from yonekawa/watchos_tvos
Browse files Browse the repository at this point in the history
Add watchOS support
  • Loading branch information
yonekawa committed Nov 23, 2015
2 parents 22bc0c0 + c88f4a3 commit 276aa7e
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 47 deletions.
23 changes: 14 additions & 9 deletions DemoApp/TodoAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,33 @@ import SwiftFlux
import Result

class TodoAction {
class Fetch: Action {
struct Fetch: Action {
typealias Payload = [Todo]
func invoke(dispatcher: Dispatcher) {
let todos = [
Todo(title: "Default ToDo 1"),
Todo(title: "Default ToDo 2"),
Todo(title: "Default ToDo 3")
Todo(title: "ToDo 1"),
Todo(title: "ToDo 2"),
Todo(title: "ToDo 3")
]
dispatcher.dispatch(self, result: Result(value: todos))
}
}

class Create: Action {
struct Create: Action {
typealias Payload = Todo
let title: String

private var title: String = ""
init(title: String) {
self.title = title
func invoke(dispatcher: Dispatcher) {
dispatcher.dispatch(self, result: Result(value: Todo(title: title)))
}
}

struct Delete: Action {
typealias Payload = Int
let index: Int

func invoke(dispatcher: Dispatcher) {
dispatcher.dispatch(self, result: Result(value: Todo(title: self.title)))
dispatcher.dispatch(self, result: Result(value: index))
}
}
}
15 changes: 11 additions & 4 deletions DemoApp/TodoListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,22 @@ class TodoListViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()

self.todoStore.eventEmitter.listen(TodoStore.Event.Fetched) { () -> Void in
self.todoStore.eventEmitter.listen(TodoStore.Event.Fetched) { () in
self.tableView.reloadData()
}
self.todoStore.eventEmitter.listen(TodoStore.Event.Created) { () -> Void in
self.todoStore.eventEmitter.listen(TodoStore.Event.Created) { () in
self.tableView.reloadData()
}
self.todoStore.eventEmitter.listen(TodoStore.Event.Deleted) { () in
self.tableView.reloadData()
}
ActionCreator.invoke(TodoAction.Fetch())
}

@IBAction func createTodo() {
ActionCreator.invoke(TodoAction.Create(title: "New ToDo"))
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.todoStore.list.count
}
Expand All @@ -34,8 +41,8 @@ class TodoListViewController: UITableViewController {
return cell
}

@IBAction func createTodo() {
ActionCreator.invoke(TodoAction.Create(title: "New ToDo"))
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
ActionCreator.invoke(TodoAction.Delete(index: indexPath.row))
}
}

15 changes: 13 additions & 2 deletions DemoApp/TodoStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class TodoStore : Store {
enum TodoEvent {
case Fetched
case Created
case Deleted
}
typealias Event = TodoEvent

Expand All @@ -25,7 +26,7 @@ class TodoStore : Store {
}

init() {
ActionCreator.dispatcher.register(TodoAction.Fetch.self) { (result) -> Void in
ActionCreator.dispatcher.register(TodoAction.Fetch.self) { (result) in
switch result {
case .Success(let box):
self.todos = box
Expand All @@ -35,7 +36,7 @@ class TodoStore : Store {
}
}

ActionCreator.dispatcher.register(TodoAction.Create.self) { (result) -> Void in
ActionCreator.dispatcher.register(TodoAction.Create.self) { (result) in
switch result {
case .Success(let box):
self.todos.insert(box, atIndex: 0)
Expand All @@ -44,5 +45,15 @@ class TodoStore : Store {
break;
}
}

ActionCreator.dispatcher.register(TodoAction.Delete.self) { (result) in
switch result {
case .Success(let box):
self.todos.removeAtIndex(box)
self.eventEmitter.emit(TodoEvent.Deleted)
case .Failure(_):
break;
}
}
}
}
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ It provides concept of "one-way data flow" with **type-safe** modules by Swift l

# Requirements

- Swift 2.0 (If you need to use with Swift 1.2, Use v0.0.3)
- Swift 2.0 or later
- iOS 8.0 or later
- Mac OS 10.9 or later
- watch OS 2.0 or later

## Installation

Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ end
namespace :carthage do
desc 'Update depndencies by carthage'
task :update do |t|
run 'carthage update --use-submodules --platform Mac,iOS'
run 'carthage update --use-submodules --platform iOS,Mac,watchOS --verbose'
end
end

Expand Down
1 change: 1 addition & 0 deletions SwiftFlux.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Pod::Spec.new do |s|

s.ios.deployment_target = "8.0"
s.osx.deployment_target = "10.9"
s.watchos.deployment_target = "2.0"

s.source = {
git: "https://github.com/yonekawa/SwiftFlux.git",
Expand Down
Loading

0 comments on commit 276aa7e

Please sign in to comment.