Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Browse commit history of repositories, directories, and files #2321

Merged
merged 2 commits into from
Oct 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions Classes/History/Client+History.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// Client+History.swift
// Freetime
//
// Created by Ryan Nystrom on 10/20/18.
// Copyright © 2018 Ryan Nystrom. All rights reserved.
//

import Foundation
import GitHubAPI

extension Client {

func fetchHistory(
owner: String,
repo: String,
branch: String,
path: String?,
cursor: String?,
width: CGFloat,
contentSizeCategory: UIContentSizeCategory,
completion: @escaping (Result<([PathCommitModel], String?)>) -> Void
) {
query(
RepoFileHistoryQuery(
owner: owner,
name: repo,
branch: branch,
path: path,
after: cursor,
page_size: 20
),
result: { $0 },
completion: { result in
switch result {
case .failure(let error):
completion(.error(error))
case .success(let data):
let commits = data.commits(width: width, contentSizeCategory: contentSizeCategory)
let nextPage: String?
if let pageInfo = data.repository?.object?.asCommit?.history.pageInfo,
pageInfo.hasNextPage {
nextPage = pageInfo.endCursor
} else {
nextPage = nil
}
completion(.success((commits, nextPage)))
}
})
}

}
56 changes: 56 additions & 0 deletions Classes/History/PathCommitCell.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// PathCommitCell.swift
// Freetime
//
// Created by Ryan Nystrom on 10/20/18.
// Copyright © 2018 Ryan Nystrom. All rights reserved.
//

import UIKit
import SnapKit

final class PathCommitCell: SelectableCell {

static let inset = UIEdgeInsets(
top: Styles.Sizes.rowSpacing,
left: Styles.Sizes.gutter,
bottom: Styles.Sizes.rowSpacing,
right: Styles.Sizes.gutter + Styles.Sizes.columnSpacing + Styles.Sizes.icon.width
)

private let textView = MarkdownStyledTextView()
private let disclosureImageView = UIImageView(image: UIImage(named: "chevron-right")?.withRenderingMode(.alwaysTemplate))

override init(frame: CGRect) {
super.init(frame: frame)

backgroundColor = .white
contentView.addSubview(textView)

disclosureImageView.tintColor = Styles.Colors.Gray.light.color
contentView.addSubview(disclosureImageView)

disclosureImageView.snp.makeConstraints { make in
make.right.equalTo(-Styles.Sizes.gutter)
make.centerY.equalToSuperview()
}

addBorder(.bottom, left: Styles.Sizes.gutter)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func layoutSubviews() {
super.layoutSubviews()
textView.reposition(for: contentView.bounds.width)
}

// MARK: Public API

func configure(with model: PathCommitModel) {
textView.configure(with: model.text, width: contentView.bounds.width)
}

}
30 changes: 30 additions & 0 deletions Classes/History/PathCommitModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// PathCommitModel.swift
// Freetime
//
// Created by Ryan Nystrom on 10/20/18.
// Copyright © 2018 Ryan Nystrom. All rights reserved.
//

import Foundation
import IGListKit
import StyledTextKit

struct PathCommitModel: ListSwiftDiffable {

let oid: String
let text: StyledTextRenderer
let commitURL: URL

// MARK: ListSwiftDiffable

var identifier: String {
return oid
}

func isEqual(to value: ListSwiftDiffable) -> Bool {
guard let value = value as? PathCommitModel else { return false }
return text.string == value.text.string
}

}
37 changes: 37 additions & 0 deletions Classes/History/PathCommitSectionController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// PathCommitSectionController.swift
// Freetime
//
// Created by Ryan Nystrom on 10/20/18.
// Copyright © 2018 Ryan Nystrom. All rights reserved.
//

import Foundation
import IGListKit

final class PathCommitSectionController: ListSwiftSectionController<PathCommitModel> {

override func createBinders(from value: PathCommitModel) -> [ListBinder] {
return [
binder(
value,
cellType: ListCellType.class(PathCommitCell.self),
size: {
return CGSize(
width: $0.collection.containerSize.width,
height: $0.value.text.viewSize(in: $0.collection.insetContainerSize.width).height
)
},
configure: {
$0.configure(with: $1.value)
},
didSelect: { [weak self] context in
guard let `self` = self else { return }
context.deselect(animated: true)
self.viewController?.presentSafari(url: context.value.commitURL)
})
]
}

}

75 changes: 75 additions & 0 deletions Classes/History/PathHistoryViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//
// PathHistoryViewController.swift
// Freetime
//
// Created by Ryan Nystrom on 10/20/18.
// Copyright © 2018 Ryan Nystrom. All rights reserved.
//

import UIKit
import IGListKit
import Squawk

final class PathHistoryViewController: BaseListViewController2<String>,
BaseListViewController2DataSource {

private let viewModel: PathHistoryViewModel
private var models = [PathCommitModel]()

init(viewModel: PathHistoryViewModel) {
self.viewModel = viewModel
super.init(emptyErrorMessage: NSLocalizedString("Cannot load history.", comment: ""))
dataSource = self
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {
super.viewDidLoad()

let titleView = NavigationTitleDropdownView(chevronVisible: false)
titleView.configure(
title: NSLocalizedString("History", comment: ""),
subtitle: viewModel.path?.path
)
navigationItem.titleView = titleView
}

override func fetch(page: String?) {
// assumptions here, but the collectionview may not have been laid out or content size found
// assume the collectionview is pinned to the view's bounds
let contentInset = feed.collectionView.contentInset
let width = view.bounds.width - contentInset.left - contentInset.right

viewModel.client.client.fetchHistory(
owner: viewModel.owner,
repo: viewModel.repo,
branch: viewModel.branch,
path: viewModel.path?.path,
cursor: page,
width: width,
contentSizeCategory: UIApplication.shared.preferredContentSizeCategory
) { [weak self] result in
switch result {
case .error(let error):
Squawk.show(error: error)
case .success(let commits, let nextPage):
if page == nil {
self?.models = commits
} else {
self?.models += commits
}
self?.update(page: nextPage, animated: trueUnlessReduceMotionEnabled)
}
}
}

// MARK: BaseListViewController2DataSource

func models(adapter: ListSwiftAdapter) -> [ListSwiftPair] {
return models.map { ListSwiftPair.pair($0, { PathCommitSectionController() }) }
}

}
19 changes: 19 additions & 0 deletions Classes/History/PathHistoryViewModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// PathHistoryViewModel.swift
// Freetime
//
// Created by Ryan Nystrom on 10/20/18.
// Copyright © 2018 Ryan Nystrom. All rights reserved.
//

import Foundation

struct PathHistoryViewModel {

let owner: String
let repo: String
let client: GithubClient
let branch: String
let path: FilePath?

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//
// HistoryGraphQLToPathCommitModel.swift
// Freetime
//
// Created by Ryan Nystrom on 10/20/18.
// Copyright © 2018 Ryan Nystrom. All rights reserved.
//

import Foundation
import StyledTextKit

extension RepoFileHistoryQuery.Data {

func commits(
width: CGFloat,
contentSizeCategory: UIContentSizeCategory
) -> [PathCommitModel] {
guard let nodes = repository?.object?.asCommit?.history.nodes
else { return [] }

return nodes.compactMap {
guard let model = $0,
let author = model.author?.user?.login,
let date = model.committedDate.githubDate,
let url = URL(string: model.url)
else { return nil }

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.paragraphSpacing = 12
paragraphStyle.lineSpacing = 2
let attributes: [NSAttributedStringKey: Any] = [
.foregroundColor: Styles.Colors.Gray.dark.color,
.paragraphStyle: paragraphStyle,
.backgroundColor: UIColor.white
]

let builder = StyledTextBuilder(styledText: StyledText(
style: Styles.Text.bodyBold.with(attributes: attributes)
))
.add(text: "\(model.message.firstLine)\n")
.add(style: Styles.Text.secondary.with(foreground: Styles.Colors.Gray.medium.color))
.save()
.add(text: author, traits: [.traitBold])
.restore()

if let committer = model.committer?.user?.login {
builder.add(text: NSLocalizedString(" authored and ", comment: ""))
.save()
.add(text: committer, traits: [.traitBold])
.restore()
}

builder.add(text: NSLocalizedString(" committed ", comment: ""))
.save()
.add(styledText: StyledText(
text: model.oid.hashDisplay,
style: Styles.Text.secondaryCodeBold.with(foreground: Styles.Colors.Blue.medium.color)
))
.restore()
.add(text: " \(date.agoString(.long))")

return PathCommitModel(
oid: model.oid,
text: StyledTextRenderer(
string: builder.build(),
contentSizeCategory: contentSizeCategory,
inset: PathCommitCell.inset,
backgroundColor: .white
).warm(width: width),
commitURL: url
)
}
}

}
37 changes: 37 additions & 0 deletions Classes/History/UIViewController+HistoryAction.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// UIViewController+HistoryAction.swift
// Freetime
//
// Created by Ryan Nystrom on 10/20/18.
// Copyright © 2018 Ryan Nystrom. All rights reserved.
//

import UIKit

extension UIViewController {
func viewHistoryAction(
owner: String,
repo: String,
branch: String,
client: GithubClient,
path: FilePath? = nil
) -> UIAlertAction {
return UIAlertAction(
title: NSLocalizedString("View History", comment: ""),
style: .default
) { [weak self] _ in
self?.navigationController?.pushViewController(
PathHistoryViewController(
viewModel: PathHistoryViewModel(
owner: owner,
repo: repo,
client: client,
branch: branch,
path: path
)
),
animated: trueUnlessReduceMotionEnabled
)
}
}
}
1 change: 0 additions & 1 deletion Classes/Issues/IssuesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import UIKit
import IGListKit
import TUSafariActivity
import SafariServices
import SnapKit
import FlatCache
import MessageViewController
Expand Down
Loading