Skip to content

Instantly share code, notes, and snippets.

@TungVuDuc2805
Created June 7, 2020 07:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TungVuDuc2805/e36456045151bd992567c011b248076c to your computer and use it in GitHub Desktop.
Save TungVuDuc2805/e36456045151bd992567c011b248076c to your computer and use it in GitHub Desktop.
protocol ProfileLoader {
func load() -> [ProfileSectionControllerProtocol]
}
class RemoteProfileLoader: ProfileLoader {
func load() -> [ProfileSectionControllerProtocol] {
var items = [ProfileSectionControllerProtocol]()
guard let data = dataFromFile("ServerData"), let profile = Profile(data: data) else {
return []
}
if let name = profile.fullName, let pictureUrl = profile.pictureUrl {
let nameAndPictureItem = ProfileViewModelNamePictureItem(name: name, pictureUrl: pictureUrl)
let sectionItem = ProfileSectionController(title: "Main Info", cells: [ProfileNamePictureCellController(dataModel: nameAndPictureItem)])
items.append(sectionItem)
}
if let about = profile.about {
let aboutItem = ProfileViewModelAboutItem(about: about)
let sectionItem = ProfileSectionController(title: "About", cells: [ProfileAboutCellController(dataModel: aboutItem)])
items.append(sectionItem)
}
if let email = profile.email {
let dobItem = ProfileViewModelEmailItem(email: email)
let sectionItem = ProfileSectionController(title: "Email", cells: [ProfileEmailCellController(dataModel: dobItem)])
items.append(sectionItem)
}
var attributeCells = [ProfileAttributesCellController]()
let attributes = profile.profileAttributes
for attribute in attributes {
let attributesItem = ProfileViewModeAttributeItem(attribute: attribute)
attributeCells.append(ProfileAttributesCellController(dataModel: attributesItem))
}
let attributeSectionItem = ProfileSectionController(title: "Attributes", cells: attributeCells)
items.append(attributeSectionItem)
var friendCells = [ProfileFriendsCellController]()
let friends = profile.friends
for friend in friends {
let friendsItem = ProfileViewModeFriendsItem(friend: friend)
friendCells.append(ProfileFriendsCellController(dataModel: friendsItem))
}
let sectionItem = ProfileSectionController(title: "Friends", cells: friendCells)
items.append(sectionItem)
return items
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment