Files
GitHawk/Classes/Notifications/Notification+NotificationViewModel.swift
Ryan Nystrom eb7069f6f7 Refactor date formatting from String (#1229)
* refactor date formatting from String

* undo new file
2017-12-16 22:16:01 -05:00

88 lines
2.6 KiB
Swift

//
// Notification+NotificationViewModel.swift
// Freetime
//
// Created by Ryan Nystrom on 5/14/17.
// Copyright © 2017 Ryan Nystrom. All rights reserved.
//
import UIKit
extension String {
var notificationIdentifier: NotificationViewModel.Identifier? {
let split = components(separatedBy: "/")
guard split.count > 2,
let identifier = split.last
else { return nil }
if split[split.count - 2] == "commits" {
return .hash(identifier)
} else {
return .number((identifier as NSString).integerValue)
}
}
}
func CreateViewModels(
containerWidth: CGFloat,
notifications: [NotificationResponse]) -> [NotificationViewModel] {
var viewModels = [NotificationViewModel]()
for notification in notifications {
guard let type = NotificationType(rawValue: notification.subject.type),
let date = notification.updated_at.githubDate,
let identifier = notification.subject.url.notificationIdentifier
else { continue }
let model = NotificationViewModel(
id: notification.id,
title: notification.subject.title,
type: type,
date: date,
read: !notification.unread,
owner: notification.repository.owner.login,
repo: notification.repository.name,
identifier: identifier,
containerWidth: containerWidth
)
viewModels.append(model)
}
return viewModels
}
func CreateNotificationViewModels(
containerWidth: CGFloat,
notifications: [NotificationResponse],
completion: @escaping ([NotificationViewModel]) -> Void
) {
DispatchQueue.global().async {
var viewModels = [NotificationViewModel]()
for notification in notifications {
guard let type = NotificationType(rawValue: notification.subject.type),
let date = notification.updated_at.githubDate,
let identifier = notification.subject.url.notificationIdentifier
else { continue }
let model = NotificationViewModel(
id: notification.id,
title: notification.subject.title,
type: type,
date: date,
read: !notification.unread,
owner: notification.repository.owner.login,
repo: notification.repository.name,
identifier: identifier,
containerWidth: containerWidth
)
viewModels.append(model)
}
DispatchQueue.main.async {
completion(viewModels)
}
}
}