Skip to content

Instantly share code, notes, and snippets.

View RinniSwift's full-sized avatar
🔸
Swifting

Rinni Swift RinniSwift

🔸
Swifting
View GitHub Profile
enum Router {
case getSources
case getProductIds
case getProductInfo
var scheme: String {
switch self {
case .getSources, .getProductIds, .getProductInfo:
return "https"
enum Router {
// 1.
case getSources
case getProductIds
case getProductInfo
}
enum Router {
// ...
// 2.
var scheme: String {
switch self {
case .getSources, .getProductIds, .getProductInfo:
return "https"
}
}
enum Router {
// ...
// 7.
var method: String {
switch self {
case .getSources, .getProductIds, .getProductInfo:
return "GET"
}
}
class ServiceLayer {
// 1.
class func request<T: Codable>(router: Router, completion: @escaping (Result<[String: [T]], Error>) -> ()) {
// 2.
var components = URLComponents()
components.scheme = router.scheme
components.host = router.host
components.path = router.path
components.queryItems = router.parameters
// 3.

What will I refactor?

old code base line 40 to 110

  • The action sign in button.
    • to reduce amount of lines in code.
    • to reduce redundent code.

How will I refactor it?

  • create helper functions for independant tasks, and to reduce redundent code.
  • refactor the networking layer to push tasks into the background thread instead of pushing it to the background thread everytime I call the object.\
struct CollectionItem: Codable {
let title: String
let id: Int
}
struct CollectionItemId: Codable {
let productId: Int
enum CodingKeys: String, CodingKey {
case productId = "product_id"
}
}
struct CollectionInfo: Codable {
let title: String
let productType: String
let variants: [VariantsInfo]
let image: ImageInfo
enum CodingKeys: String, CodingKey {
case title
case productType = "product_type"
case variants
ServiceLayer.request(router: Router.getProductInfo) { (result: Result<[String : [CollectionItem]], Error>) in
switch result {
case .success:
print(result)
case .failure:
print(result)
}
}