Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // ProfileMapper.swift
- // backoffice
- //
- // Created by Sergey Butorin on 28/11/2018.
- // Copyright © 2018 Eruditor Group. All rights reserved.
- //
- final class ProfileMapper: ProfileMapperInput {
- func mapProfileScreen(from json: JsonDict) -> GeneralProfile? {
- guard let header: ProfileHeaderSectionItem = mapProfileHeaderItem(json: json),
- let itemsData = array(json, "items"),
- let contactItem: ProfileContactSectionItem = mapProfileContactItem(json: json),
- let linksData = array(json, "links")
- else { return nil }
- var profile = GeneralProfile()
- var headerSection = ProfileSection()
- headerSection.type = .header
- headerSection.items = [header]
- profile.sections.append(headerSection)
- let infoItems: [ProfileInfoSectionItem] = itemsData.compactMap { mapProfileInfoItem(itemData: $0) }
- var infoSection = ProfileSection()
- infoSection.type = .info
- infoSection.items = infoItems
- profile.sections.append(infoSection)
- var contactSection = ProfileSection()
- contactSection.type = .contacts
- contactSection.items = [contactItem]
- profile.sections.append(contactSection)
- let linkItems: [ProfileLinkSectionItem] = linksData.compactMap { mapProfileLinkItem(itemData: $0) }
- var linkSection = ProfileSection()
- linkSection.type = .links
- linkSection.items = linkItems
- profile.sections.append(linkSection)
- return profile
- }
- func mapProfileAboutScreen(from json: JsonDict) -> ProfileAbout? {
- guard let itemsData = array(json, "items") else { return nil }
- var profileAbout = ProfileAbout()
- profileAbout.items = itemsData.compactMap { mapProfileAboutItem(itemData: $0) }
- return profileAbout
- }
- }
- // MARK: Element mappers
- extension ProfileMapper {
- private func mapProfileHeaderItem(json: JsonDict) -> ProfileHeaderSectionItem? {
- guard let name = string(json, "name"),
- let rating = string(json, "rating"),
- let reviews = string(json, "reviews"),
- let balance = string(json, "balance")
- else { return nil }
- var header = ProfileHeaderSectionItem()
- header.name = name
- header.rating = rating
- header.reviewsCount = reviews
- header.balance = balance
- if let avatarUrlString = string(json, "avatar"),
- let avatarUrl = URL(string: avatarUrlString) {
- header.avatarUrl = avatarUrl
- }
- return header
- }
- private func mapProfileInfoItem(itemData: JsonDict) -> ProfileInfoSectionItem? {
- guard let code = string(itemData, "code"),
- let type = GeneralProfileItemType(rawValue: code),
- let title = string(itemData, "title"),
- let description = string(itemData, "text")
- else { return nil }
- var sectionItem = ProfileInfoSectionItem()
- sectionItem.type = type
- sectionItem.title = title
- sectionItem.description = description
- return sectionItem
- }
- private func mapProfileContactItem(json: JsonDict) -> ProfileContactSectionItem? {
- guard let contactDescription = string(json, "videoText"),
- let contactMail = string(json, "videoMail")
- else { return nil }
- var contactItem = ProfileContactSectionItem()
- contactItem.description = contactDescription
- contactItem.mail = contactMail
- return contactItem
- }
- private func mapProfileLinkItem(itemData: JsonDict) -> ProfileLinkSectionItem? {
- guard let typeString = string(itemData, "type"),
- let type = ProfileLinkType(rawValue: typeString),
- let urlString = string(itemData, "target"),
- let url = URL(string: urlString),
- let title = string(itemData, "name")
- else { return nil }
- var link = Link()
- link.targetUrl = url
- link.title = title
- var linkItem = ProfileLinkSectionItem()
- linkItem.type = type
- linkItem.link = link
- return linkItem
- }
- private func mapProfileAboutItem(itemData: JsonDict) -> ProfileAboutItem? {
- guard let typeString = string(itemData, "code"),
- let type = ProfileAboutItemType(rawValue: typeString),
- let title = string(itemData, "title"),
- let text = string(itemData, "text"),
- let placeholder = string(itemData, "placeholder")
- else { return nil }
- var aboutItem = ProfileAboutItem()
- aboutItem.type = type
- aboutItem.title = title
- aboutItem.text = text
- aboutItem.placeholder = placeholder
- return aboutItem
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement