Files
GitHawk/Classes/Notifications/NotificationViewModel.swift
Ryan Nystrom 5bae4ba89d [WIP] Add FlatCache lib and use for Notifications (#632)
* init FlatCache lib

* tests for all other cases

* install flatcache to freeitme project, move VM creation into client

* refactor and split up inbox design

* fixup flatcache pod
2017-10-22 00:08:42 -04:00

99 lines
2.4 KiB
Swift

//
// NotificationViewModel.swift
// Freetime
//
// Created by Ryan Nystrom on 5/12/17.
// Copyright © 2017 Ryan Nystrom. All rights reserved.
//
import Foundation
import IGListKit
import FlatCache
final class NotificationViewModel: ListDiffable, Cachable {
enum Identifier {
case number(Int)
case hash(String)
}
let id: String
let title: NSAttributedStringSizing
let type: NotificationType
let date: Date
let read: Bool
let owner: String
let repo: String
let identifier: Identifier
init(
id: String,
title: NSAttributedStringSizing,
type: NotificationType,
date: Date,
read: Bool,
owner: String,
repo: String,
identifier: Identifier
) {
self.id = id
self.title = title
self.type = type
self.date = date
self.read = read
self.owner = owner
self.repo = repo
self.identifier = identifier
}
convenience init(
id: String,
title: String,
type: NotificationType,
date: Date,
read: Bool,
owner: String,
repo: String,
identifier: Identifier,
containerWidth: CGFloat
) {
let attributes = [
NSAttributedStringKey.font: Styles.Fonts.body,
NSAttributedStringKey.foregroundColor: Styles.Colors.Gray.dark.color
]
let title = NSAttributedStringSizing(
containerWidth: containerWidth,
attributedText: NSAttributedString(string: title, attributes: attributes),
inset: NotificationCell.labelInset
)
self.init(
id: id,
title: title,
type: type,
date: date,
read: read,
owner: owner,
repo: repo,
identifier: identifier
)
}
// MARK: ListDiffable
func diffIdentifier() -> NSObjectProtocol {
return id as NSObjectProtocol
}
func isEqual(toDiffableObject object: ListDiffable?) -> Bool {
if self === object { return true }
guard let object = object as? NotificationViewModel else { return false }
return read == object.read
&& type == object.type
&& date == object.date
&& repo == object.repo
&& owner == object.owner
&& title.attributedText.string == object.title.attributedText.string
}
}