// // NotificationCellAdapter.swift // PrivadoVPN // // Created by Zhandos Bolatbekov on 21.01.2021. // Copyright © 2021 Privado LLC. All rights reserved. // import Foundation import SwiftyMarkdown import UIKit enum NotificationCellType { case ipad case iphone } typealias NotificationCellLinkAction = (URL) -> Void struct NotificationCellAdapter { private enum Constants { static let dateFormat = "dd/MM/yyyy" enum IPhone { static let bodyColor = UIColor(rgb: 0x7a86be) static let bodyFontName = "SFProText-Regular" static let bodyFontSize: CGFloat = 16.7 } enum IPad { static let titleLineSpacing: CGFloat = 8 static let bodyColor = Colors.Notification.accent.color static let bodyFontName = "Spartan-Regular" static let bodyFontSize: CGFloat = 10 } } let type: NotificationCellType let title: String? var attrTitle: NSAttributedString? let body: NSAttributedString? let date: String? let linkAction: NotificationCellLinkAction? init(with type: NotificationCellType, notification: PrivadoNotification, linkAction: NotificationCellLinkAction?) { self.type = type self.title = notification.payload.subject self.body = Self.getFormattedMarkdown(from: notification.payload.body, cellType: type) self.date = Self.getFormattedDate(from: notification.payload.sentDate) self.linkAction = linkAction guard let str = notification.payload.subject else { return } let style = NSMutableParagraphStyle() style.lineSpacing = Constants.IPad.titleLineSpacing self.attrTitle = NSAttributedString(string: str, attributes: [.paragraphStyle: style]) } private static func getFormattedMarkdown(from body: String, cellType: NotificationCellType) -> NSAttributedString { let md = SwiftyMarkdown(string: body) switch cellType { case .ipad: md.body.color = Constants.IPad.bodyColor md.body.fontName = Constants.IPad.bodyFontName md.body.fontSize = Constants.IPad.bodyFontSize md.link.color = UIColor.white md.link.fontName = Constants.IPad.bodyFontName md.link.fontSize = Constants.IPad.bodyFontSize md.underlineLinks = true case .iphone: md.body.color = Constants.IPhone.bodyColor md.body.fontName = Constants.IPhone.bodyFontName md.body.fontSize = Constants.IPhone.bodyFontSize } return md.attributedString() } private static func getFormattedDate(from dateString: String) -> String? { guard let standardDate = DateFormatter.iso8601Full.date(from: dateString) else { return nil } let dateFormatter = DateFormatter() dateFormatter.dateFormat = Constants.dateFormat return dateFormatter.string(from: standardDate) } }