Advertisement
Guest User

Untitled

a guest
Dec 12th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.98 KB | None | 0 0
  1. //
  2. //  ProfileMapper.swift
  3. //  backoffice
  4. //
  5. //  Created by Sergey Butorin on 28/11/2018.
  6. //  Copyright © 2018 Eruditor Group. All rights reserved.
  7. //
  8.  
  9. final class ProfileMapper: ProfileMapperInput {
  10.    
  11.     func mapProfileScreen(from json: JsonDict) -> GeneralProfile? {
  12.         guard let header: ProfileHeaderSectionItem = mapProfileHeaderItem(json: json),
  13.             let itemsData = array(json, "items"),
  14.             let contactItem: ProfileContactSectionItem = mapProfileContactItem(json: json),
  15.             let linksData = array(json, "links")
  16.             else { return nil }
  17.        
  18.         var profile = GeneralProfile()
  19.        
  20.         var headerSection = ProfileSection()
  21.         headerSection.type = .header
  22.         headerSection.items = [header]
  23.         profile.sections.append(headerSection)
  24.  
  25.         let infoItems: [ProfileInfoSectionItem] = itemsData.compactMap { mapProfileInfoItem(itemData: $0) }
  26.         var infoSection = ProfileSection()
  27.         infoSection.type = .info
  28.         infoSection.items = infoItems
  29.         profile.sections.append(infoSection)
  30.  
  31.         var contactSection = ProfileSection()
  32.         contactSection.type = .contacts
  33.         contactSection.items = [contactItem]
  34.         profile.sections.append(contactSection)
  35.        
  36.         let linkItems: [ProfileLinkSectionItem] = linksData.compactMap { mapProfileLinkItem(itemData: $0) }
  37.         var linkSection = ProfileSection()
  38.         linkSection.type = .links
  39.         linkSection.items = linkItems
  40.         profile.sections.append(linkSection)
  41.        
  42.         return profile
  43.     }
  44.    
  45.     func mapProfileAboutScreen(from json: JsonDict) -> ProfileAbout? {
  46.         guard let itemsData = array(json, "items") else { return nil }
  47.        
  48.         var profileAbout = ProfileAbout()
  49.         profileAbout.items = itemsData.compactMap { mapProfileAboutItem(itemData: $0) }
  50.        
  51.         return profileAbout
  52.     }
  53.    
  54. }
  55.  
  56. // MARK: Element mappers
  57.  
  58. extension ProfileMapper {
  59.    
  60.     private func mapProfileHeaderItem(json: JsonDict) -> ProfileHeaderSectionItem? {
  61.         guard let name = string(json, "name"),
  62.             let rating = string(json, "rating"),
  63.             let reviews = string(json, "reviews"),
  64.             let balance = string(json, "balance")
  65.             else { return nil }
  66.        
  67.         var header = ProfileHeaderSectionItem()
  68.         header.name = name
  69.         header.rating = rating
  70.         header.reviewsCount = reviews
  71.         header.balance = balance
  72.        
  73.         if let avatarUrlString = string(json, "avatar"),
  74.             let avatarUrl = URL(string: avatarUrlString) {
  75.             header.avatarUrl = avatarUrl
  76.         }
  77.        
  78.         return header
  79.     }
  80.    
  81.     private func mapProfileInfoItem(itemData: JsonDict) -> ProfileInfoSectionItem? {
  82.         guard let code = string(itemData, "code"),
  83.             let type = GeneralProfileItemType(rawValue: code),
  84.             let title = string(itemData, "title"),
  85.             let description = string(itemData, "text")
  86.             else { return nil }
  87.        
  88.         var sectionItem = ProfileInfoSectionItem()
  89.         sectionItem.type = type
  90.         sectionItem.title = title
  91.         sectionItem.description = description
  92.        
  93.         return sectionItem
  94.     }
  95.    
  96.     private func mapProfileContactItem(json: JsonDict) -> ProfileContactSectionItem? {
  97.         guard let contactDescription = string(json, "videoText"),
  98.             let contactMail = string(json, "videoMail")
  99.             else { return nil }
  100.        
  101.         var contactItem = ProfileContactSectionItem()
  102.         contactItem.description = contactDescription
  103.         contactItem.mail = contactMail
  104.        
  105.         return contactItem
  106.     }
  107.    
  108.     private func mapProfileLinkItem(itemData: JsonDict) -> ProfileLinkSectionItem? {
  109.         guard let typeString = string(itemData, "type"),
  110.             let type = ProfileLinkType(rawValue: typeString),
  111.             let urlString = string(itemData, "target"),
  112.             let url = URL(string: urlString),
  113.             let title = string(itemData, "name")
  114.             else { return nil }
  115.        
  116.         var link = Link()
  117.         link.targetUrl = url
  118.         link.title = title
  119.        
  120.         var linkItem = ProfileLinkSectionItem()
  121.         linkItem.type = type
  122.         linkItem.link = link
  123.        
  124.         return linkItem
  125.     }
  126.    
  127.     private func mapProfileAboutItem(itemData: JsonDict) -> ProfileAboutItem? {
  128.         guard let typeString = string(itemData, "code"),
  129.             let type = ProfileAboutItemType(rawValue: typeString),
  130.             let title = string(itemData, "title"),
  131.             let text = string(itemData, "text"),
  132.             let placeholder = string(itemData, "placeholder")
  133.             else { return nil }
  134.        
  135.         var aboutItem = ProfileAboutItem()
  136.         aboutItem.type = type
  137.         aboutItem.title = title
  138.         aboutItem.text = text
  139.         aboutItem.placeholder = placeholder
  140.        
  141.         return aboutItem
  142.     }
  143.    
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement