|
| 1 | +// |
| 2 | +// HistoryGraphQLToPathCommitModel.swift |
| 3 | +// Freetime |
| 4 | +// |
| 5 | +// Created by Ryan Nystrom on 10/20/18. |
| 6 | +// Copyright © 2018 Ryan Nystrom. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import Foundation |
| 10 | +import StyledTextKit |
| 11 | + |
| 12 | +extension RepoFileHistoryQuery.Data { |
| 13 | + |
| 14 | + func commits( |
| 15 | + width: CGFloat, |
| 16 | + contentSizeCategory: UIContentSizeCategory |
| 17 | + ) -> [PathCommitModel] { |
| 18 | + guard let nodes = repository?.object?.asCommit?.history.nodes |
| 19 | + else { return [] } |
| 20 | + |
| 21 | + return nodes.compactMap { |
| 22 | + guard let model = $0, |
| 23 | + let author = model.author?.user?.login, |
| 24 | + let date = model.committedDate.githubDate, |
| 25 | + let url = URL(string: model.url) |
| 26 | + else { return nil } |
| 27 | + |
| 28 | + let paragraphStyle = NSMutableParagraphStyle() |
| 29 | + paragraphStyle.paragraphSpacing = 12 |
| 30 | + paragraphStyle.lineSpacing = 2 |
| 31 | + let attributes: [NSAttributedStringKey: Any] = [ |
| 32 | + .foregroundColor: Styles.Colors.Gray.dark.color, |
| 33 | + .paragraphStyle: paragraphStyle, |
| 34 | + .backgroundColor: UIColor.white |
| 35 | + ] |
| 36 | + |
| 37 | + let builder = StyledTextBuilder(styledText: StyledText( |
| 38 | + style: Styles.Text.bodyBold.with(attributes: attributes) |
| 39 | + )) |
| 40 | + .add(text: "\(model.message.firstLine)\n") |
| 41 | + .add(style: Styles.Text.secondary.with(foreground: Styles.Colors.Gray.medium.color)) |
| 42 | + .save() |
| 43 | + .add(text: author, traits: [.traitBold]) |
| 44 | + .restore() |
| 45 | + |
| 46 | + if let committer = model.committer?.user?.login { |
| 47 | + builder.add(text: NSLocalizedString(" authored and ", comment: "")) |
| 48 | + .save() |
| 49 | + .add(text: committer, traits: [.traitBold]) |
| 50 | + .restore() |
| 51 | + } |
| 52 | + |
| 53 | + builder.add(text: NSLocalizedString(" committed ", comment: "")) |
| 54 | + .save() |
| 55 | + .add(styledText: StyledText( |
| 56 | + text: model.oid.hashDisplay, |
| 57 | + style: Styles.Text.secondaryCodeBold.with(foreground: Styles.Colors.Blue.medium.color) |
| 58 | + )) |
| 59 | + .restore() |
| 60 | + .add(text: " \(date.agoString(.long))") |
| 61 | + |
| 62 | + return PathCommitModel( |
| 63 | + oid: model.oid, |
| 64 | + text: StyledTextRenderer( |
| 65 | + string: builder.build(), |
| 66 | + contentSizeCategory: contentSizeCategory, |
| 67 | + inset: PathCommitCell.inset, |
| 68 | + backgroundColor: .white |
| 69 | + ).warm(width: width), |
| 70 | + commitURL: url |
| 71 | + ) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments