diff --git a/elm-todo/Todo.elm b/elm-todo/Todo.elm index 3a07b24..c970a13 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,8 +71,6 @@ to them. -} type Msg = UpdateNewEntryField String - | EditingEntry Int Bool - | UpdateEntry Int String | Add | Delete Int | DeleteAllCompleted @@ -102,29 +95,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 = - if t.id == id then - { t | description = task } - else - t - in - { model | entries = List.map updateEntry model.entries } - Check id isCompleted -> let updateEntry t = @@ -141,11 +111,8 @@ update msg model = DeleteAllCompleted -> { model | entries = List.filter (not << .completed) model.entries } - - -- VIEW - view : Model -> Html Msg view model = div @@ -229,7 +196,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,7 +207,7 @@ viewEntry todo = ] [] , label - [ onDoubleClick (EditingEntry todo.id True) ] + [] [ text todo.description ] , button [ class "destroy" @@ -248,16 +215,6 @@ viewEntry todo = ] [] ] - , 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,8 +270,8 @@ viewControlsClear entriesCompleted = infoFooter : Html msg infoFooter = footer [ class "info" ] - [ p [] [ text "Double-click to edit a todo" ] - , p [] +-- [p [] [ text "You can not edit todos. To change a todo, delete it and add a new todo." ] + [p [] [ text "Written by " , a [ href "https://github.com/evancz" ] [ text "Evan Czaplicki" ] ] diff --git a/ruby-todo/Gemfile.lock b/ruby-todo/Gemfile.lock index 8b2b027..15835e1 100644 --- a/ruby-todo/Gemfile.lock +++ b/ruby-todo/Gemfile.lock @@ -14,6 +14,7 @@ GEM PLATFORMS ruby + x64-mingw32 DEPENDENCIES minitest diff --git a/ruby-todo/lib/engine.rb b/ruby-todo/lib/engine.rb index a257c20..4d990d9 100644 --- a/ruby-todo/lib/engine.rb +++ b/ruby-todo/lib/engine.rb @@ -5,8 +5,8 @@ def self.run(*args) def self.run_with_history(model, messages) messages.map do |msg| - msg.apply_to(model) - model + model = msg.apply_to(model) + end end end diff --git a/ruby-todo/lib/messages.rb b/ruby-todo/lib/messages.rb index abe85de..b732395 100644 --- a/ruby-todo/lib/messages.rb +++ b/ruby-todo/lib/messages.rb @@ -2,13 +2,13 @@ module Msg class Add def apply_to(model) + entriesList = model.entries.dup unless model.new_entry_field.blank? - model.entries << Entry.new( - description: model.new_entry_field, - id: model.next_id) + entriesList< [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..0361194 100644 --- a/swift-todo/Sources/Todo/Message.swift +++ b/swift-todo/Sources/Todo/Message.swift @@ -14,30 +14,39 @@ enum Message { case delete(Int) case deleteAllCompleted - func apply(to model: Model) { + func apply(to model: Model) -> Model { switch(self) { case .add: + var entriesentriesList = model.entries if !model.newEntryField.isBlank() { - model.entries.append(Entry(id: model.nextID, description: model.newEntryField)) + entriesList.append(Entry(id: model.nextID, description: model.newEntryField)) } - model.nextID += 1 - model.newEntryField = "" - + return Model(nextID: model.nextID + 1, newEntryField: "", entries: entriesList) + case .updateNewEntryField(let str): - model.newEntryField = str - + return Model(nextID:model.nextID, newEntryField: str, entries: model.entries) + case .check(let id, let isCompleted): + var entriesList = [Entry]() + for entry in model.entries { if(entry.id == id) { - entry.completed = isCompleted + entriesList.append(Entry(id: entry.id, description: entry.description, completed: isCompleted)) } } + + return Model(nextID:model.nextID, newEntryField: model.newEntryField, entries: entriesList) case .delete(let id): - model.entries.remove { $0.id == id } + var entriesList = model.entries + entriesList.remove { $0.id == id } + return Model(nextID:model.nextID, newEntryField: model.newEntryField, entries: entriesList) case .deleteAllCompleted: - model.entries.remove { $0.completed } + var entriesList = model.entries + entriesList.remove { $0.completed } + return Model(nextID:model.nextID, newEntryField: model.newEntryField, entries: entriesList) } } } + 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..7f35d24 100644 --- a/swift-todo/Tests/TodoTests/TodoTests.swift +++ b/swift-todo/Tests/TodoTests/TodoTests.swift @@ -77,7 +77,6 @@ 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 +144,4 @@ class TodoTests: XCTestCase { XCTAssertEqual(expected, actual, "History mismatch at step \(index)") } } -*/ - }