diff --git a/Yep/Services/YepService.swift b/Yep/Services/YepService.swift index a613101a..9219412f 100644 --- a/Yep/Services/YepService.swift +++ b/Yep/Services/YepService.swift @@ -1182,3 +1182,74 @@ func socialAccountWithProvider(provider: String, #failureHandler: ((Reason, Stri apiRequest({_ in}, baseURL, resource, defaultFailureHandler, completion) } } + +struct GithubWork { + + struct Repo { + let name: String + let language: String + let description: String + let stargazersCount: Int + let htmlURLString: String + } + + struct User { + let loginName: String + let avatarURLString: String + let htmlURLString: String + let followersCount: Int + let followingCount: Int + } + + let repos: [Repo] + let user: User +} + +func githubWorkOfUserWithUserID(userID: String, #failureHandler: ((Reason, String?) -> Void)?, #completion: GithubWork -> Void) { + + let parse: JSONDictionary -> GithubWork? = { data in + + if let reposData = data["repos"] as? [JSONDictionary], userInfo = data["user"] as? JSONDictionary { + + var repos = Array() + + for repoInfo in reposData { + if let + name = repoInfo["name"] as? String, + language = repoInfo["language"] as? String, + description = repoInfo["description"] as? String, + stargazersCount = repoInfo["stargazers_count"] as? Int, + htmlURLString = repoInfo["html_url"] as? String { + + let repo = GithubWork.Repo(name: name, language: language, description: description, stargazersCount: stargazersCount, htmlURLString: htmlURLString) + + repos.append(repo) + } + } + + if let + loginName = userInfo["login"] as? String, + avatarURLString = userInfo["avatar_url"] as? String, + htmlURLString = userInfo["html_url"] as? String, + followersCount = userInfo["followers"] as? Int, + followingCount = userInfo["following"] as? Int { + + let user = GithubWork.User(loginName: loginName, avatarURLString: avatarURLString, htmlURLString: htmlURLString, followersCount: followersCount, followingCount: followingCount) + + let githubWork = GithubWork(repos: repos, user: user) + + return githubWork + } + } + + return nil + } + + let resource = authJsonResource(path: "/api/v1/users/\(userID)/github", method: .GET, requestParameters: [:], parse: parse) + + if let failureHandler = failureHandler { + apiRequest({_ in}, baseURL, resource, failureHandler, completion) + } else { + apiRequest({_ in}, baseURL, resource, defaultFailureHandler, completion) + } +} diff --git a/Yep/ViewControllers/SocialWorks/SocialWorkGithubViewController.swift b/Yep/ViewControllers/SocialWorks/SocialWorkGithubViewController.swift index e38c8770..2af712be 100644 --- a/Yep/ViewControllers/SocialWorks/SocialWorkGithubViewController.swift +++ b/Yep/ViewControllers/SocialWorks/SocialWorkGithubViewController.swift @@ -22,6 +22,13 @@ class SocialWorkGithubViewController: UIViewController { let githubRepoCellIdentifier = "GithubRepoCell" + var githubUser: GithubWork.User? + var githubRepos = Array() { + didSet { + updateGithubTableView() + } + } + override func viewDidLoad() { super.viewDidLoad() @@ -40,10 +47,30 @@ class SocialWorkGithubViewController: UIViewController { githubTableView.registerNib(UINib(nibName: githubRepoCellIdentifier, bundle: nil), forCellReuseIdentifier: githubRepoCellIdentifier) githubTableView.rowHeight = 100 + + + // 获取 github Work + + if let userID = YepUserDefaults.userID.value { + + githubWorkOfUserWithUserID(userID, failureHandler: { (reason, errorMessage) -> Void in + defaultFailureHandler(reason, errorMessage) + + }, completion: { githubWork in + println("githubWork: \(githubWork)") + + self.githubUser = githubWork.user + self.githubRepos = githubWork.repos + }) + } } // MARK: Actions + func updateGithubTableView() { + githubTableView.reloadData() + } + func gotoUserGithubHome() { // TODO: gotoUserGithubHome } @@ -57,11 +84,18 @@ extension SocialWorkGithubViewController: UITableViewDataSource, UITableViewDele } func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { - return 15 + return githubRepos.count } func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier(githubRepoCellIdentifier) as! GithubRepoCell + + let repo = githubRepos[indexPath.row] + + cell.nameLabel.text = repo.name + cell.descriptionLabel.text = repo.description + cell.starCountLabel.text = "\(repo.stargazersCount)" + "★" + return cell } }