-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentsTableViewModel.swift
More file actions
72 lines (67 loc) · 3.81 KB
/
CommentsTableViewModel.swift
File metadata and controls
72 lines (67 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// CommentsTableViewModel.swift
// Real News
//
// Created by PC on 4/1/17.
// Copyright © 2017 PC. All rights reserved.
//
import RxSwift
import RxCocoa
import Foundation
import UIKit
struct CommentViewModel{
let comment:Comment
let authorAttributedText:NSAttributedString
let commentAttributedText:NSAttributedString
let timeAttributedText:NSAttributedString
let replyAttributedText:NSAttributedString
let level:Int
var rowHeight:CGFloat
var collapsedRowHeight:CGFloat
var collapsed:Bool
var completeCollapsed:Bool
var expanded:Bool
}
func newComment(cellWidth:CGFloat,story:Story)->Observable<CommentViewModel?>?{
guard let observable = createCommentsObservable(kids: [story.id],isRoot:true,level:0) else{
return nil
}
return observable.map({wrappedComment in
guard let comment = wrappedComment else{
return nil
}
guard let authorString = comment.author,let commentString = comment.text else{
return nil
}
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = padding.paragraphLineSpacing.rawValue
let authorFont = UIFont.detail
let commentFont = UIFont.standard
let authorAttributes = [NSFontAttributeName:authorFont,NSForegroundColorAttributeName:UIColor.appTextColor]
let commentAttributes = [NSFontAttributeName:commentFont,NSParagraphStyleAttributeName:paragraphStyle,NSForegroundColorAttributeName:UIColor.appTextColor]
let indentation = CGFloat(comment.level)*10
let contentViewWidth = cellWidth - ((padding.cell.rawValue * 2) + (padding.genericDouble.rawValue) + indentation)
let size = CGSize(width: contentViewWidth, height: CGFloat.greatestFiniteMagnitude)
let commentAttributedText = commentString.htmlAttributedString()!
commentAttributedText.addAttributes(commentAttributes, range:NSMakeRange(0, commentAttributedText.length))
let authorAttributedText = NSAttributedString(string: authorString,attributes:authorAttributes)
let commentDetailAttributes = [NSFontAttributeName:commentFont,NSForegroundColorAttributeName:UIColor.lightAppTextColor]
let time = Calendar.current.dateComponents([Calendar.Component.hour,Calendar.Component.minute], from: Date(timeIntervalSince1970: TimeInterval(comment.time!)), to:Date())
var timeText = "・"
timeText += time.hour! == 0 ? String(describing:time.minute!)+"m ago" : String(describing:time.hour!)+"h ago"
let timeAttributedText = NSAttributedString(string: timeText, attributes: commentDetailAttributes)
var replyString = "・"
if comment.kids == nil {
replyString += "0 reply"
}else{
replyString += String(comment.kids!.count)
replyString += comment.kids!.count > 1 ? " replies" : " reply"
}
let replyAttributedString = NSAttributedString(string: replyString, attributes: commentDetailAttributes)
let authorHeight = authorAttributedText.boundingRect(with: size, options: .usesLineFragmentOrigin, context: nil).height
let commentHeight = commentAttributedText.boundingRect(with: size, options: .usesLineFragmentOrigin, context: nil).height
let rowHeight = authorHeight + commentHeight + padding.generic.rawValue + padding.generic.rawValue + padding.interLabel.rawValue + (padding.cell.rawValue * 2)
let collapsedRowHeight = rowHeight - commentHeight - padding.generic.rawValue
return CommentViewModel(comment:comment,authorAttributedText: authorAttributedText, commentAttributedText:commentAttributedText,timeAttributedText:timeAttributedText,replyAttributedText:replyAttributedString,level:comment.level,rowHeight:rowHeight,collapsedRowHeight:collapsedRowHeight,collapsed:false,completeCollapsed:false,expanded:true)
})
}