Last active
July 23, 2016 15:10
-
-
Save andersio/0ab044bdc3b4426ee87b4337c7e16bb3 to your computer and use it in GitHub Desktop.
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
// View model for each tweets. | |
class TweetViewModel { | |
let username: AnyProperty<String> | |
let content: AnyProperty<String> | |
let date: AnyProperty<String> | |
init(tweet: tweet, controller: controller) { | |
let dateFormatter = { … } | |
username = AnyProperty(tweet.property(forKeyPath: #keyPath(Tweet.name))) | |
content = AnyProperty(tweet.property(forKeyPath: #keyPath(Tweet.content))) | |
date = tweet.property(forKeyPath: #keyPath(Tweet.date), type: Date.self) | |
.map(dateFormatter) | |
} | |
} | |
// View model for a timeline of tweets. | |
class TimelineViewModel { | |
let tweets: ViewModelSet<TweetViewModel> | |
init(controller: TimelineController) { | |
let tweetObjects = Tweet.query(controller.mainContext) | |
.group(by: .descending(keyPath: "date")) | |
.sort(by: .descending(keyPath: "datetime")) | |
.makeObjectSet() | |
tweets = ViewModelSet(tweetsObjects) { tweet in | |
return TweetViewModel(tweet: tweet, controller: controller) | |
} | |
} | |
} | |
// The view controller for the timeline. | |
class TimelineViewController: UITableViewController { | |
// setter injection. | |
var viewModel: TimelineViewModel! | |
func viewDidLoad { | |
TableViewAdapter(set: viewModel.tweets) | |
.register(for: …) { … cell.viewModel.value = $0 … } | |
.bind(tableView) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment