Skip to content

Instantly share code, notes, and snippets.

@luisburgos
Created August 11, 2017 14:51
Show Gist options
  • Save luisburgos/bf1670f5731115a64530236d48ac5f05 to your computer and use it in GitHub Desktop.
Save luisburgos/bf1670f5731115a64530236d48ac5f05 to your computer and use it in GitHub Desktop.
Example of a simple model decorator
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)
}
}
}
@luisburgos
Copy link
Author

Uses:

NotificationItemDecorator()
                .with(notification)
                .decorate(
                    image: mImageView,
                    title: titleLabel,
                    message: messageLabel,
                    hour: hourLabel,
                    date: dateLabel)
                .add(confirm: confirmButton)

or

let _ = NotificationItemDecorator()
            .with(notification)
            .decorate(
                image: cell.mImageView,
                title: cell.titleLabel,
                message: cell.messageLabel,
                hour: cell.hourLabel
            )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment