|
|
|
|
@@ -425,11 +425,17 @@ final class FilesViewController: RefreshingTableViewController {
|
|
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
|
final class RepositoryDirectoryViewController: RefreshingTableViewController {
|
|
|
|
|
private enum Mode: Int { case files, history }
|
|
|
|
|
|
|
|
|
|
private let context: AppContext
|
|
|
|
|
private let owner: String
|
|
|
|
|
private let repository: String
|
|
|
|
|
private let path: String
|
|
|
|
|
private var mode = Mode.files
|
|
|
|
|
private var rows: [RepositoryContentRow] = []
|
|
|
|
|
private var history: CommitPage?
|
|
|
|
|
private var currentPage: UInt32 = 0
|
|
|
|
|
private var loadedFiles = false
|
|
|
|
|
|
|
|
|
|
init(context: AppContext, owner: String, repository: String, path: String, name: String) {
|
|
|
|
|
self.context = context
|
|
|
|
|
@@ -445,23 +451,47 @@ final class RepositoryDirectoryViewController: RefreshingTableViewController {
|
|
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
tableView.register(CommitCell.self, forCellReuseIdentifier: "commit")
|
|
|
|
|
navigationItem.prompt = title
|
|
|
|
|
let modeControl = UISegmentedControl(items: ["Files", "History"])
|
|
|
|
|
modeControl.selectedSegmentIndex = mode.rawValue
|
|
|
|
|
modeControl.addTarget(self, action: #selector(modeChanged(_:)), for: .valueChanged)
|
|
|
|
|
modeControl.accessibilityLabel = "Directory view"
|
|
|
|
|
navigationItem.titleView = modeControl
|
|
|
|
|
loadContent(refreshing: false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func loadContent(refreshing: Bool) {
|
|
|
|
|
switch mode {
|
|
|
|
|
case .files: loadFiles(refreshing: refreshing)
|
|
|
|
|
case .history: loadHistory(page: 1, refreshing: refreshing)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func loadMoreContent() {
|
|
|
|
|
guard mode == .history else { return }
|
|
|
|
|
loadHistory(page: currentPage + 1, refreshing: false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func loadFiles(refreshing: Bool) {
|
|
|
|
|
resetPagination()
|
|
|
|
|
beginLoading(refreshing: refreshing)
|
|
|
|
|
loadingTask?.cancel()
|
|
|
|
|
loadingTask = Task {
|
|
|
|
|
do {
|
|
|
|
|
rows = try await context.core.repositoryContents(
|
|
|
|
|
let rows = try await context.core.repositoryContents(
|
|
|
|
|
owner: owner,
|
|
|
|
|
repository: repository,
|
|
|
|
|
path: path
|
|
|
|
|
)
|
|
|
|
|
guard !Task.isCancelled, mode == .files else {
|
|
|
|
|
endLoading()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
self.rows = rows
|
|
|
|
|
loadedFiles = true
|
|
|
|
|
tableView.reloadData()
|
|
|
|
|
tableView.backgroundView = rows.isEmpty
|
|
|
|
|
? EmptyBackgroundView(title: "Empty folder", detail: "This folder does not contain any files.")
|
|
|
|
|
: nil
|
|
|
|
|
updateEmptyView()
|
|
|
|
|
} catch {
|
|
|
|
|
if !Task.isCancelled { show(error: error) }
|
|
|
|
|
}
|
|
|
|
|
@@ -469,45 +499,258 @@ final class RepositoryDirectoryViewController: RefreshingTableViewController {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func loadHistory(page requestedPage: UInt32, refreshing: Bool) {
|
|
|
|
|
if requestedPage == 1 {
|
|
|
|
|
resetPagination()
|
|
|
|
|
beginLoading(refreshing: refreshing)
|
|
|
|
|
}
|
|
|
|
|
loadingTask?.cancel()
|
|
|
|
|
loadingTask = Task {
|
|
|
|
|
do {
|
|
|
|
|
let history = try await context.core.commits(
|
|
|
|
|
owner: owner,
|
|
|
|
|
repository: repository,
|
|
|
|
|
branch: nil,
|
|
|
|
|
path: path,
|
|
|
|
|
pages: requestedPage
|
|
|
|
|
)
|
|
|
|
|
guard !Task.isCancelled, mode == .history else {
|
|
|
|
|
if requestedPage == 1 { endLoading() }
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
self.history = history
|
|
|
|
|
currentPage = requestedPage
|
|
|
|
|
finishPagination(hasMore: history.hasMore)
|
|
|
|
|
tableView.reloadData()
|
|
|
|
|
updateEmptyView()
|
|
|
|
|
} catch {
|
|
|
|
|
if !Task.isCancelled {
|
|
|
|
|
show(error: error)
|
|
|
|
|
failPagination()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if requestedPage == 1 { endLoading() }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
|
|
|
rows.count
|
|
|
|
|
mode == .files ? rows.count : history?.commits.count ?? 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func tableView(
|
|
|
|
|
_ tableView: UITableView,
|
|
|
|
|
cellForRowAt indexPath: IndexPath
|
|
|
|
|
) -> UITableViewCell {
|
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: "repository-content")
|
|
|
|
|
?? UITableViewCell(style: .subtitle, reuseIdentifier: "repository-content")
|
|
|
|
|
configureRepositoryContentCell(cell, row: rows[indexPath.row])
|
|
|
|
|
return cell
|
|
|
|
|
switch mode {
|
|
|
|
|
case .files:
|
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: "repository-content")
|
|
|
|
|
?? UITableViewCell(style: .subtitle, reuseIdentifier: "repository-content")
|
|
|
|
|
configureRepositoryContentCell(cell, row: rows[indexPath.row])
|
|
|
|
|
return cell
|
|
|
|
|
case .history:
|
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: "commit", for: indexPath) as! CommitCell
|
|
|
|
|
if let history {
|
|
|
|
|
cell.configure(history.commits[indexPath.row], laneCount: history.laneCount)
|
|
|
|
|
}
|
|
|
|
|
return cell
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
|
|
|
|
|
mode == .history ? 86 : UITableView.automaticDimension
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
|
tableView.deselectRow(at: indexPath, animated: true)
|
|
|
|
|
showRepositoryContent(
|
|
|
|
|
rows[indexPath.row],
|
|
|
|
|
context: context,
|
|
|
|
|
owner: owner,
|
|
|
|
|
repository: repository,
|
|
|
|
|
navigationController: navigationController
|
|
|
|
|
switch mode {
|
|
|
|
|
case .files:
|
|
|
|
|
showRepositoryContent(
|
|
|
|
|
rows[indexPath.row],
|
|
|
|
|
context: context,
|
|
|
|
|
owner: owner,
|
|
|
|
|
repository: repository,
|
|
|
|
|
navigationController: navigationController
|
|
|
|
|
)
|
|
|
|
|
case .history:
|
|
|
|
|
guard let commit = history?.commits[indexPath.row] else { return }
|
|
|
|
|
navigationController?.pushViewController(
|
|
|
|
|
FilesViewController(
|
|
|
|
|
context: context,
|
|
|
|
|
owner: owner,
|
|
|
|
|
repository: repository,
|
|
|
|
|
sha: commit.sha
|
|
|
|
|
),
|
|
|
|
|
animated: true
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@objc private func modeChanged(_ sender: UISegmentedControl) {
|
|
|
|
|
guard let mode = Mode(rawValue: sender.selectedSegmentIndex), mode != self.mode else { return }
|
|
|
|
|
loadingTask?.cancel()
|
|
|
|
|
endLoading()
|
|
|
|
|
self.mode = mode
|
|
|
|
|
resetPagination()
|
|
|
|
|
tableView.backgroundView = nil
|
|
|
|
|
tableView.reloadData()
|
|
|
|
|
switch mode {
|
|
|
|
|
case .files:
|
|
|
|
|
if loadedFiles { updateEmptyView() } else { loadFiles(refreshing: false) }
|
|
|
|
|
case .history:
|
|
|
|
|
if let history {
|
|
|
|
|
finishPagination(hasMore: history.hasMore)
|
|
|
|
|
updateEmptyView()
|
|
|
|
|
} else {
|
|
|
|
|
loadHistory(page: 1, refreshing: false)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func updateEmptyView() {
|
|
|
|
|
switch mode {
|
|
|
|
|
case .files:
|
|
|
|
|
tableView.backgroundView = loadedFiles && rows.isEmpty
|
|
|
|
|
? EmptyBackgroundView(title: "Empty folder", detail: "This folder does not contain any files.")
|
|
|
|
|
: nil
|
|
|
|
|
case .history:
|
|
|
|
|
tableView.backgroundView = history?.commits.isEmpty == true
|
|
|
|
|
? EmptyBackgroundView(title: "No commits", detail: "No commits affect this folder.")
|
|
|
|
|
: nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
|
final class RepositoryHistoryViewController: RefreshingTableViewController {
|
|
|
|
|
private let context: AppContext
|
|
|
|
|
private let owner: String
|
|
|
|
|
private let repository: String
|
|
|
|
|
private let path: String
|
|
|
|
|
private let emptyDetail: String
|
|
|
|
|
private let loadingIndicator = UIActivityIndicatorView(style: .medium)
|
|
|
|
|
private var page: CommitPage?
|
|
|
|
|
private var currentPage: UInt32 = 0
|
|
|
|
|
|
|
|
|
|
init(
|
|
|
|
|
context: AppContext,
|
|
|
|
|
owner: String,
|
|
|
|
|
repository: String,
|
|
|
|
|
path: String,
|
|
|
|
|
emptyDetail: String
|
|
|
|
|
) {
|
|
|
|
|
self.context = context
|
|
|
|
|
self.owner = owner
|
|
|
|
|
self.repository = repository
|
|
|
|
|
self.path = path
|
|
|
|
|
self.emptyDetail = emptyDetail
|
|
|
|
|
super.init()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@available(*, unavailable)
|
|
|
|
|
required init?(coder: NSCoder) { fatalError("init(coder:) is not supported") }
|
|
|
|
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
tableView.register(CommitCell.self, forCellReuseIdentifier: "commit")
|
|
|
|
|
loadContent(refreshing: false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func loadContent(refreshing: Bool) {
|
|
|
|
|
loadPage(1, refreshing: refreshing)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func loadMoreContent() {
|
|
|
|
|
loadPage(currentPage + 1, refreshing: false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func loadPage(_ requestedPage: UInt32, refreshing: Bool) {
|
|
|
|
|
if requestedPage == 1 {
|
|
|
|
|
resetPagination()
|
|
|
|
|
if !refreshing {
|
|
|
|
|
loadingIndicator.startAnimating()
|
|
|
|
|
tableView.backgroundView = loadingIndicator
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
loadingTask?.cancel()
|
|
|
|
|
loadingTask = Task {
|
|
|
|
|
do {
|
|
|
|
|
let page = try await context.core.commits(
|
|
|
|
|
owner: owner,
|
|
|
|
|
repository: repository,
|
|
|
|
|
branch: nil,
|
|
|
|
|
path: path,
|
|
|
|
|
pages: requestedPage
|
|
|
|
|
)
|
|
|
|
|
guard !Task.isCancelled else { return }
|
|
|
|
|
self.page = page
|
|
|
|
|
currentPage = requestedPage
|
|
|
|
|
finishPagination(hasMore: page.hasMore)
|
|
|
|
|
tableView.reloadData()
|
|
|
|
|
updateEmptyView()
|
|
|
|
|
} catch {
|
|
|
|
|
if !Task.isCancelled {
|
|
|
|
|
show(error: error)
|
|
|
|
|
failPagination()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if requestedPage == 1 {
|
|
|
|
|
loadingIndicator.stopAnimating()
|
|
|
|
|
refreshControl?.endRefreshing()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
|
|
|
|
page?.commits.count ?? 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func tableView(
|
|
|
|
|
_ tableView: UITableView,
|
|
|
|
|
cellForRowAt indexPath: IndexPath
|
|
|
|
|
) -> UITableViewCell {
|
|
|
|
|
let cell = tableView.dequeueReusableCell(withIdentifier: "commit", for: indexPath) as! CommitCell
|
|
|
|
|
if let page { cell.configure(page.commits[indexPath.row], laneCount: page.laneCount) }
|
|
|
|
|
return cell
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
|
|
|
|
|
86
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
|
|
|
|
tableView.deselectRow(at: indexPath, animated: true)
|
|
|
|
|
guard let commit = page?.commits[indexPath.row] else { return }
|
|
|
|
|
navigationController?.pushViewController(
|
|
|
|
|
FilesViewController(
|
|
|
|
|
context: context,
|
|
|
|
|
owner: owner,
|
|
|
|
|
repository: repository,
|
|
|
|
|
sha: commit.sha
|
|
|
|
|
),
|
|
|
|
|
animated: true
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func updateEmptyView() {
|
|
|
|
|
tableView.backgroundView = page?.commits.isEmpty == true
|
|
|
|
|
? EmptyBackgroundView(title: "No commits", detail: emptyDetail)
|
|
|
|
|
: nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
|
final class RepositoryFileViewController: UIViewController, QLPreviewControllerDataSource {
|
|
|
|
|
private enum MarkdownMode: Int { case preview, source }
|
|
|
|
|
|
|
|
|
|
private let context: AppContext
|
|
|
|
|
private let owner: String
|
|
|
|
|
private let repository: String
|
|
|
|
|
private let path: String
|
|
|
|
|
private let spinner = UIActivityIndicatorView(style: .medium)
|
|
|
|
|
private let modeControl = UISegmentedControl()
|
|
|
|
|
private var loadingTask: Task<Void, Never>?
|
|
|
|
|
private var page: RepositoryFilePage?
|
|
|
|
|
private var previewURL: URL?
|
|
|
|
|
private var markdownSource: String?
|
|
|
|
|
private var fileLanguage: String?
|
|
|
|
|
private var historyController: RepositoryHistoryViewController?
|
|
|
|
|
private var displayedController: UIViewController?
|
|
|
|
|
private var displayedView: UIView?
|
|
|
|
|
private weak var sourceScrollView: UIScrollView?
|
|
|
|
|
@@ -536,13 +779,11 @@ final class RepositoryFileViewController: UIViewController, QLPreviewControllerD
|
|
|
|
|
repository: repository,
|
|
|
|
|
path: path
|
|
|
|
|
)
|
|
|
|
|
guard !Task.isCancelled else { return }
|
|
|
|
|
self.page = page
|
|
|
|
|
title = page.name
|
|
|
|
|
fileLanguage = page.language.isEmpty ? nil : page.language
|
|
|
|
|
switch page.kind {
|
|
|
|
|
case "markdown": showMarkdown(page.text)
|
|
|
|
|
case "source": showSource(page.text)
|
|
|
|
|
default: try showQuickLook(page.data, name: page.name)
|
|
|
|
|
}
|
|
|
|
|
configureModes(for: page)
|
|
|
|
|
} catch {
|
|
|
|
|
if !Task.isCancelled { show(error: error) }
|
|
|
|
|
}
|
|
|
|
|
@@ -568,26 +809,62 @@ final class RepositoryFileViewController: UIViewController, QLPreviewControllerD
|
|
|
|
|
previewURL! as NSURL
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func showMarkdown(_ source: String) {
|
|
|
|
|
markdownSource = source
|
|
|
|
|
private func configureModes(for page: RepositoryFilePage) {
|
|
|
|
|
navigationItem.prompt = title
|
|
|
|
|
let modeControl = UISegmentedControl(items: ["Preview", "Source"])
|
|
|
|
|
modeControl.selectedSegmentIndex = MarkdownMode.preview.rawValue
|
|
|
|
|
modeControl.addTarget(self, action: #selector(markdownModeChanged(_:)), for: .valueChanged)
|
|
|
|
|
modeControl.accessibilityLabel = "Markdown view"
|
|
|
|
|
modeControl.removeAllSegments()
|
|
|
|
|
let modes = page.kind == "markdown"
|
|
|
|
|
? ["Preview", "Source", "History"]
|
|
|
|
|
: ["Content", "History"]
|
|
|
|
|
for (index, mode) in modes.enumerated() {
|
|
|
|
|
modeControl.insertSegment(withTitle: mode, at: index, animated: false)
|
|
|
|
|
}
|
|
|
|
|
modeControl.selectedSegmentIndex = 0
|
|
|
|
|
modeControl.addTarget(self, action: #selector(fileModeChanged(_:)), for: .valueChanged)
|
|
|
|
|
modeControl.accessibilityLabel = "File view"
|
|
|
|
|
navigationItem.titleView = modeControl
|
|
|
|
|
showMarkdownPreview(source)
|
|
|
|
|
showContent(page, mode: modes[0])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@objc private func markdownModeChanged(_ sender: UISegmentedControl) {
|
|
|
|
|
guard let mode = MarkdownMode(rawValue: sender.selectedSegmentIndex),
|
|
|
|
|
let source = markdownSource else { return }
|
|
|
|
|
@objc private func fileModeChanged(_ sender: UISegmentedControl) {
|
|
|
|
|
guard let page, let mode = sender.titleForSegment(at: sender.selectedSegmentIndex) else { return }
|
|
|
|
|
showContent(page, mode: mode)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func showContent(_ page: RepositoryFilePage, mode: String) {
|
|
|
|
|
switch mode {
|
|
|
|
|
case .preview: showMarkdownPreview(source)
|
|
|
|
|
case .source: showSource(source)
|
|
|
|
|
case "Preview": showMarkdownPreview(page.text)
|
|
|
|
|
case "Source": showSource(page.text)
|
|
|
|
|
case "Content" where page.kind == "source": showSource(page.text)
|
|
|
|
|
case "Content":
|
|
|
|
|
do {
|
|
|
|
|
try showQuickLook(page.data, name: page.name)
|
|
|
|
|
} catch {
|
|
|
|
|
show(error: error)
|
|
|
|
|
}
|
|
|
|
|
case "History": showHistory()
|
|
|
|
|
default: break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func showHistory() {
|
|
|
|
|
removeDisplayedView()
|
|
|
|
|
let history = historyController ?? RepositoryHistoryViewController(
|
|
|
|
|
context: context,
|
|
|
|
|
owner: owner,
|
|
|
|
|
repository: repository,
|
|
|
|
|
path: path,
|
|
|
|
|
emptyDetail: "No commits affect this file."
|
|
|
|
|
)
|
|
|
|
|
historyController = history
|
|
|
|
|
addChild(history)
|
|
|
|
|
history.view.frame = view.bounds
|
|
|
|
|
history.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
|
|
|
|
displayedController = history
|
|
|
|
|
displayedView = history.view
|
|
|
|
|
view.addSubview(history.view)
|
|
|
|
|
history.didMove(toParent: self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func showMarkdownPreview(_ source: String) {
|
|
|
|
|
removeDisplayedView()
|
|
|
|
|
let preview = UIHostingController(rootView: RepositoryMarkdownPreview(source: source))
|
|
|
|
|
@@ -683,15 +960,20 @@ final class RepositoryFileViewController: UIViewController, QLPreviewControllerD
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func showQuickLook(_ data: Data, name: String) throws {
|
|
|
|
|
let url = FileManager.default.temporaryDirectory
|
|
|
|
|
.appendingPathComponent("\(UUID().uuidString)-\(name)")
|
|
|
|
|
try data.write(to: url, options: .atomic)
|
|
|
|
|
previewURL = url
|
|
|
|
|
removeDisplayedView()
|
|
|
|
|
if previewURL == nil {
|
|
|
|
|
let url = FileManager.default.temporaryDirectory
|
|
|
|
|
.appendingPathComponent("\(UUID().uuidString)-\(name)")
|
|
|
|
|
try data.write(to: url, options: .atomic)
|
|
|
|
|
previewURL = url
|
|
|
|
|
}
|
|
|
|
|
let preview = QLPreviewController()
|
|
|
|
|
preview.dataSource = self
|
|
|
|
|
addChild(preview)
|
|
|
|
|
preview.view.frame = view.bounds
|
|
|
|
|
preview.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
|
|
|
|
displayedController = preview
|
|
|
|
|
displayedView = preview.view
|
|
|
|
|
view.addSubview(preview.view)
|
|
|
|
|
preview.didMove(toParent: self)
|
|
|
|
|
}
|
|
|
|
|
|