diff --git a/elm-todo/Todo.elm b/elm-todo/Todo.elm index 3a07b24..5e3d72c 100755 --- a/elm-todo/Todo.elm +++ b/elm-todo/Todo.elm @@ -1,13 +1,10 @@ port module Todo exposing (..) {-| TodoMVC implemented in Elm, using plain HTML and CSS for rendering. - This application is broken up into three key parts: - 1. Model - a full definition of the application's state 2. Update - a way to step the application state forward 3. View - a way to visualize our application state with HTML - This clean division of concerns is a core part of Elm. You can read more about this in -} @@ -45,7 +42,6 @@ type alias Model = type alias Entry = { description : String , completed : Bool - , editing : Bool , id : Int } @@ -62,7 +58,6 @@ newEntry : String -> Int -> Entry newEntry desc id = { description = desc , completed = False - , editing = False , id = id } @@ -76,7 +71,6 @@ to them. -} type Msg = UpdateNewEntryField String - | EditingEntry Int Bool | UpdateEntry Int String | Add | Delete Int @@ -102,19 +96,6 @@ update msg model = UpdateNewEntryField str -> { model | newEntryField = str } - EditingEntry id isEditing -> - let - updateEntry t = - if t.id == id then - { t | editing = isEditing } - else - t - - focus = - Dom.focus ("todo-" ++ toString id) - in - { model | entries = List.map updateEntry model.entries } - UpdateEntry id task -> let updateEntry t = @@ -229,7 +210,7 @@ viewKeyedEntry todo = viewEntry : Entry -> Html Msg viewEntry todo = li - [ classList [ ( "completed", todo.completed ), ( "editing", todo.editing ) ] ] + [ classList [ ( "completed", todo.completed ) ] ] [ div [ class "view" ] [ input @@ -240,24 +221,14 @@ viewEntry todo = ] [] , label - [ onDoubleClick (EditingEntry todo.id True) ] + [] [ text todo.description ] , button [ class "destroy" , onClick (Delete todo.id) ] [] - ] - , input - [ class "edit" - , value todo.description - , name "title" - , id ("todo-" ++ toString todo.id) - , onInput (UpdateEntry todo.id) - , onBlur (EditingEntry todo.id False) - , onEnter (EditingEntry todo.id False) - ] - [] + ] ] @@ -313,7 +284,7 @@ viewControlsClear entriesCompleted = infoFooter : Html msg infoFooter = footer [ class "info" ] - [ p [] [ text "Double-click to edit a todo" ] + [ p [] [ text "Double-clicking won't save you now!" ] , p [] [ text "Written by " , a [ href "https://github.com/evancz" ] [ text "Evan Czaplicki" ] @@ -322,4 +293,4 @@ infoFooter = [ text "Part of " , a [ href "http://todomvc.com" ] [ text "TodoMVC" ] ] - ] + ] \ No newline at end of file diff --git a/ruby-todo/lib/engine.rb b/ruby-todo/lib/engine.rb index a257c20..a639cc3 100644 --- a/ruby-todo/lib/engine.rb +++ b/ruby-todo/lib/engine.rb @@ -4,9 +4,8 @@ def self.run(*args) end def self.run_with_history(model, messages) - messages.map do |msg| - msg.apply_to(model) - model + messages.map do |msg| + model = msg.apply_to(model) end end end diff --git a/ruby-todo/lib/messages.rb b/ruby-todo/lib/messages.rb index abe85de..be64769 100644 --- a/ruby-todo/lib/messages.rb +++ b/ruby-todo/lib/messages.rb @@ -2,13 +2,17 @@ module Msg class Add def apply_to(model) + new_entries = model.entries.dup + new_id = model.next_id unless model.new_entry_field.blank? - model.entries << Entry.new( + new_entries << Entry.new( description: model.new_entry_field, id: model.next_id) end - model.next_id += 1 - model.new_entry_field = "" + return Model.new( + entries: new_entries, + new_entry_field: "", + next_id: new_id += 1) end end @@ -20,7 +24,10 @@ def initialize(str) attr_reader :str def apply_to(model) - model.new_entry_field = str + return Model.new( + entries: model.entries.dup, + new_entry_field: str, + next_id: model.next_id) end end @@ -32,11 +39,20 @@ def initialize(id, is_completed) attr_reader :id, :is_completed def apply_to(model) - model.entries.each do |entry| + new_entries = model.entries.dup + new_entries.each do |entry| if entry.id == id - entry.completed = is_completed + updated_entry = Entry.new( + description: entry.description, + completed: is_completed, + id: entry.id) + new_entries[new_entries.index(entry)] = updated_entry end end + return Model.new( + entries: new_entries, + new_entry_field: model.new_entry_field, + next_id: model.next_id) end end @@ -48,13 +64,23 @@ def initialize(id) attr_reader :id def apply_to(model) - model.entries.reject! { |e| e.id == id } + new_entries = model.entries.dup + new_entries.reject! { |e| e.id == id } + return Model.new( + entries: new_entries, + new_entry_field: model.new_entry_field, + next_id: model.next_id) end end class DeleteAllCompleted def apply_to(model) - model.entries.reject!(&:completed) + new_entries = model.entries.dup + new_entries.reject!(&:completed) + return Model.new( + entries: new_entries, + new_entry_field: model.new_entry_field, + next_id: model.next_id) end end diff --git a/ruby-todo/lib/model.rb b/ruby-todo/lib/model.rb index 75a6085..e64ca6a 100644 --- a/ruby-todo/lib/model.rb +++ b/ruby-todo/lib/model.rb @@ -5,7 +5,7 @@ def initialize(entries: [], new_entry_field: "", next_id: nil) (entries.lazy.map(&:id).max || -1) + 1 end - attr_accessor :entries, :new_entry_field, :next_id + attr_reader :entries, :new_entry_field, :next_id def ==(other) !other.nil? && @@ -20,7 +20,7 @@ def initialize(id:, description:, completed: false) @description, @id, @completed = description, id, completed end - attr_accessor :description, :completed, :id + attr_reader :description, :completed, :id def ==(other) !other.nil? && diff --git a/ruby-todo/test/todo_test.rb b/ruby-todo/test/todo_test.rb index f7436c7..d3351dc 100644 --- a/ruby-todo/test/todo_test.rb +++ b/ruby-todo/test/todo_test.rb @@ -91,7 +91,7 @@ end it "supports time travel" do - skip "Mutable model does not support time travel" + # skip "Mutable model does not support time travel" actual_history = Engine.run_with_history(Model.new, [ Msg::UpdateNewEntryField.new("go forward in time"), diff --git a/swift-todo/Sources/Todo/Engine.swift b/swift-todo/Sources/Todo/Engine.swift index e3b14a7..c717840 100644 --- a/swift-todo/Sources/Todo/Engine.swift +++ b/swift-todo/Sources/Todo/Engine.swift @@ -13,9 +13,10 @@ struct Engine { } static func runWithHistory(on model: Model, applying messages: [Message]) -> [Model] { + var newModel = model return messages.map { message in - message.apply(to: model) - return model + newModel = message.apply(to: newModel) + return newModel } } } diff --git a/swift-todo/Sources/Todo/Message.swift b/swift-todo/Sources/Todo/Message.swift index 92dd868..e100a46 100644 --- a/swift-todo/Sources/Todo/Message.swift +++ b/swift-todo/Sources/Todo/Message.swift @@ -4,7 +4,6 @@ // // Created by Paul on 2018/4/4. // - import Foundation enum Message { @@ -13,31 +12,44 @@ enum Message { case check(Int, Bool) case delete(Int) case deleteAllCompleted - - func apply(to model: Model) { + + func apply(to model: Model) -> Model { switch(self) { - case .add: - if !model.newEntryField.isBlank() { - model.entries.append(Entry(id: model.nextID, description: model.newEntryField)) + case .add: + var newModel = model + if !model.newEntryField.isBlank() { + newModel.entries.append(Entry(id: model.nextID, description: model.newEntryField)) + } + newModel.nextID += 1 + newModel.newEntryField = "" + return newModel + + case .updateNewEntryField(let str): + var newModel = model + newModel.newEntryField = str + return newModel + + case .check(let id, let isCompleted): + var newModel = model + for i in model.entries.indices { + if(model.entries[i].id == id) { + var newEntry = model.entries[i] + newEntry.completed = isCompleted + newModel.entries[i] = newEntry + break } - model.nextID += 1 - model.newEntryField = "" - - case .updateNewEntryField(let str): - model.newEntryField = str - - case .check(let id, let isCompleted): - for entry in model.entries { - if(entry.id == id) { - entry.completed = isCompleted - } - } - - case .delete(let id): - model.entries.remove { $0.id == id } - - case .deleteAllCompleted: - model.entries.remove { $0.completed } + } + return newModel + + case .delete(let id): + var newModel = model + newModel.entries.remove { $0.id == id } + return newModel + + case .deleteAllCompleted: + var newModel = model + newModel.entries.remove { $0.completed } + return newModel } } } diff --git a/swift-todo/Sources/Todo/Model.swift b/swift-todo/Sources/Todo/Model.swift index 1cca801..d50d863 100644 --- a/swift-todo/Sources/Todo/Model.swift +++ b/swift-todo/Sources/Todo/Model.swift @@ -1,4 +1,4 @@ -class Model { +struct Model { var entries: [Entry] var newEntryField: String var nextID: Int @@ -11,7 +11,7 @@ class Model { } } -class Entry { +struct Entry { var id: Int var description: String var completed: Bool diff --git a/swift-todo/Tests/TodoTests/TodoTests.swift b/swift-todo/Tests/TodoTests/TodoTests.swift index 9220517..e19fddd 100644 --- a/swift-todo/Tests/TodoTests/TodoTests.swift +++ b/swift-todo/Tests/TodoTests/TodoTests.swift @@ -77,7 +77,7 @@ class TodoTests: XCTestCase { XCTAssertEqual([11], newModel.entries.map { $0.id }) } -/* + func testTimeTravel() { let actualHistory = Engine.runWithHistory(on: Model(), applying: [ .updateNewEntryField("go forward in time"), @@ -145,6 +145,4 @@ class TodoTests: XCTestCase { XCTAssertEqual(expected, actual, "History mismatch at step \(index)") } } -*/ - }