Created
March 28, 2020 23:27
-
-
Save wbbernardes/870c607fc58040fe71ac7d85b08ae471 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
// | |
// HomeController.swift | |
// javapop | |
// | |
// Created by Wesley Brito on 22/03/20. | |
// Copyright © 2020 Wesley Brito. All rights reserved. | |
// | |
import Foundation | |
class HomeController { | |
var githubItens: [Item] = [] | |
// MARK: - Api Request | |
func loadRepos(pageCount: Int = 1, onCompletion: @escaping (Bool, RequestErrors?) -> Void) { | |
ServiceConnection().makeHTTPGetRequest(ServiceRepository.baseUrl(page: pageCount), Github.self) { (result, error) in | |
if let _ = error { | |
onCompletion(false, .unexpectedError) | |
return | |
} | |
self.githubItens += result?.items ?? [] | |
onCompletion(true, nil) | |
} | |
} | |
// MARK: - Get Infors | |
func getAvatarUrl(_ index: Int) -> URL? { | |
if let avatar = githubItens[index].owner?.avatarURL, | |
let image = URL(string: avatar) { | |
return image | |
} | |
return nil | |
} | |
func getUsername(_ index: Int) -> String { | |
if let username = githubItens[index].owner?.login { | |
return username | |
} | |
return "" | |
} | |
func getName(_ index: Int) -> String { | |
return githubItens[index].name | |
} | |
func getDescription(_ index: Int) -> String { | |
return githubItens[index].itemDescription | |
} | |
func getForkCount(_ index: Int) -> String { | |
return "\(githubItens[index].forksCount)" | |
} | |
func getStarsCount(_ index: Int) -> String { | |
return "\(githubItens[index].stargazersCount)" | |
} | |
func getFullName(_ index: Int) -> String { | |
return githubItens[index].fullName | |
} | |
func getHtmlUrl(_ index: Int) -> String { | |
return githubItens[index].htmlURL | |
} | |
func getItensCount() -> Int { | |
return githubItens.count | |
} | |
// MARK: - Filter | |
func filterRepo(searchText: String = "", sort: SortType = .Star) { | |
if !searchText.isEmpty { | |
let filtered = githubItens.filter{ | |
($0.name.range(of: searchText, options: .caseInsensitive) != nil) || | |
($0.owner!.login.range(of: searchText, options: .caseInsensitive) != nil) | |
} | |
self.githubItens = [] | |
self.githubItens = filtered | |
} else if sort == .Star { | |
let filtered = githubItens.sorted { $0.stargazersCount > $1.stargazersCount } | |
self.githubItens = [] | |
self.githubItens = filtered | |
} else { | |
let filtered = githubItens.sorted { $0.name.lowercased() < $1.name.lowercased() } | |
self.githubItens = [] | |
self.githubItens = filtered | |
} | |
} | |
func searchTextIsValid(text: String) -> Bool { | |
if text.count < 30 { | |
return true | |
} else { | |
return false | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment