-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make a TodoListViewModel and factor out logic
- Loading branch information
1 parent
e72fa96
commit 3eb1cff
Showing
5 changed files
with
105 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// Created by Christopher Trott on 12/9/15. | ||
// Copyright © 2015 twocentstudios. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol ListViewModelable { | ||
typealias ViewModelType | ||
|
||
var viewModels: [ViewModelType] { get } | ||
} | ||
|
||
extension ListViewModelable { | ||
func numberOfRowsInSection(section: Int) -> Int { | ||
if (section > 0) { return 0 } | ||
return viewModels.count | ||
} | ||
|
||
func viewModelAtIndexPath(indexPath: NSIndexPath) -> ViewModelType { | ||
precondition(indexPath.section == 0) | ||
return viewModels[indexPath.row] | ||
} | ||
} | ||
|
||
enum GroupChange { | ||
case Reload | ||
case NoOp | ||
} | ||
|
||
enum SingleChange { | ||
case Insert(NSIndexPath) | ||
case Delete(NSIndexPath) | ||
case Reload(NSIndexPath) | ||
case NoOp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// Created by Christopher Trott on 12/9/15. | ||
// Copyright © 2015 twocentstudios. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct TodoListViewModel: ListViewModelable { | ||
var viewModels = [TodoViewModel]() | ||
|
||
mutating func incorporateTodoViewModels(newViewModels: [TodoViewModel]) -> GroupChange { | ||
if (viewModels == newViewModels) { return .NoOp } | ||
viewModels = newViewModels | ||
return .Reload | ||
} | ||
|
||
mutating func incorporateTodoViewModel(newViewModel: TodoViewModel) -> SingleChange { | ||
if let index = viewModels.indexOf(newViewModel) { | ||
if (newViewModel.deleted) { | ||
// delete | ||
viewModels.removeAtIndex(index) | ||
return .Delete(NSIndexPath(forRow: index, inSection: 0)) | ||
} else { | ||
// replace | ||
viewModels[index] = newViewModel | ||
return .Reload(NSIndexPath(forRow: index, inSection: 0)) | ||
} | ||
} else { | ||
viewModels.insert(newViewModel, atIndex: 0) | ||
return .Insert(NSIndexPath(forRow: 0, inSection: 0)) | ||
} | ||
} | ||
} |