Created
August 11, 2017 14:51
-
-
Save luisburgos/bf1670f5731115a64530236d48ac5f05 to your computer and use it in GitHub Desktop.
Example of a simple model decorator
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
class NotificationItemDecorator { | |
var notification: Notification? | |
func with(_ notification: Notification) -> NotificationItemDecorator { | |
self.notification = notification | |
return self | |
} | |
func add(confirm: UIButton?) { | |
if let confirm = confirm, let currentNotification = notification { | |
if let data = currentNotification.data { | |
confirm.isHidden = data.isConfirmed | |
} else { | |
confirm.isHidden = true | |
} | |
} | |
} | |
func decorate(image: UIImageView?, title: UILabel?, message: UILabel?, hour: UILabel?, date: UILabel? = nil) -> NotificationItemDecorator { | |
if let notification = self.notification { | |
if let data = notification.data { | |
self.decorate(with: data, type: notification.type, image: image, title: title) | |
} else { | |
title?.text = notification.title | |
} | |
self.commonDecorate(message: message, hour: hour, date: date) | |
} | |
return self | |
} | |
private func commonDecorate(message: UILabel?, hour: UILabel?, date: UILabel? = nil) { | |
if let message = message { | |
message.text = self.notification?.message | |
} | |
if let hour = hour, let dateString = self.notification?.date { | |
hour.setup(date: dateString) | |
if let date = date { | |
date.setup(date: dateString, type: .verbose) | |
} | |
} | |
} | |
private func decorate(with data: NotificationData, type: NotificationType, image: UIImageView?, title: UILabel?) { | |
if let image = image, let imageUrl = data.imageUrl { | |
image.loadImageFromUrl(imageUrl, cacheIdentifier: CacheConstants.UserProfileImage.rawValue, circular: true, bordered: false, rounded: false) | |
} | |
if let title = title { | |
title.text = type == .fall ? NotificationsHelper.fall(withName: data.elderName) : NotificationsHelper.cameOut(withName: data.elderName) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uses:
or