2021-07-21 21:11:39 -07:00
|
|
|
//
|
|
|
|
// ShareViewDelegate.swift
|
|
|
|
// Share Extension
|
|
|
|
//
|
|
|
|
// Created by David Sinclair on 2021-07-18.
|
|
|
|
// Copyright © 2021 NewsBlur. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
|
|
class ShareViewDelegate: NSObject {
|
|
|
|
@IBOutlet weak var viewController: ShareViewController!
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ShareViewDelegate: UITableViewDelegate {
|
|
|
|
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
|
2021-12-23 16:18:26 -07:00
|
|
|
if viewController.mode == .add, indexPath.section == 0 {
|
|
|
|
viewController.selectedFolderIndexPath = indexPath
|
|
|
|
tableView.reloadData()
|
|
|
|
}
|
|
|
|
|
2021-07-21 21:11:39 -07:00
|
|
|
viewController.updateSaveButtonState()
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
|
|
|
|
viewController.updateSaveButtonState()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ShareViewDelegate: UITableViewDataSource {
|
2021-12-23 16:18:26 -07:00
|
|
|
func numberOfSections(in tableView: UITableView) -> Int {
|
|
|
|
if viewController.mode == .add {
|
|
|
|
return 2
|
|
|
|
} else {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
|
|
|
|
if viewController.mode == .add, section == 1 {
|
|
|
|
return "Add new sub-folder:"
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-21 21:11:39 -07:00
|
|
|
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
2021-12-23 16:18:26 -07:00
|
|
|
switch viewController.mode {
|
|
|
|
case .save:
|
2021-07-21 21:11:39 -07:00
|
|
|
return viewController.tags.count + 1
|
2021-12-23 16:18:26 -07:00
|
|
|
case .share:
|
2021-07-21 21:11:39 -07:00
|
|
|
return 1
|
2021-12-23 16:18:26 -07:00
|
|
|
case .add:
|
|
|
|
if section == 0 {
|
|
|
|
return viewController.folders.count
|
|
|
|
} else {
|
|
|
|
return 1
|
|
|
|
}
|
2021-07-21 21:11:39 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
|
|
|
if viewController.mode == .save {
|
|
|
|
if indexPath.item < viewController.tags.count {
|
|
|
|
guard let cell = tableView.dequeueReusableCell(withIdentifier: ShareSaveTagCell.reuseIdentifier, for: indexPath) as? ShareSaveTagCell else {
|
|
|
|
preconditionFailure("Expected to dequeue a ShareSaveTagCell")
|
|
|
|
}
|
|
|
|
|
2022-03-09 22:04:31 -07:00
|
|
|
let tag = viewController.tags[indexPath.item]
|
|
|
|
|
|
|
|
cell.tagLabel.text = tag.name
|
|
|
|
cell.countLabel.text = "\(tag.count)"
|
2021-07-21 21:11:39 -07:00
|
|
|
|
|
|
|
return cell
|
|
|
|
} else {
|
|
|
|
guard let cell = tableView.dequeueReusableCell(withIdentifier: ShareSaveNewCell.reuseIdentifier, for: indexPath) as? ShareSaveNewCell else {
|
|
|
|
preconditionFailure("Expected to dequeue a ShareSaveNewCell")
|
|
|
|
}
|
|
|
|
|
|
|
|
cell.tagField.text = ""
|
2021-12-23 16:18:26 -07:00
|
|
|
cell.tagField.placeholder = "new tag"
|
|
|
|
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
} else if viewController.mode == .add {
|
|
|
|
if indexPath.section == 0 {
|
|
|
|
guard let cell = tableView.dequeueReusableCell(withIdentifier: ShareSaveTagCell.reuseIdentifier, for: indexPath) as? ShareSaveTagCell else {
|
|
|
|
preconditionFailure("Expected to dequeue a ShareSaveTagCell")
|
|
|
|
}
|
|
|
|
|
|
|
|
let components = viewController.folders[indexPath.item].components(separatedBy: " ▸ ")
|
|
|
|
|
2022-03-09 22:04:31 -07:00
|
|
|
cell.countLabel.text = ""
|
|
|
|
|
2021-12-23 16:18:26 -07:00
|
|
|
if components.first == "everything" {
|
|
|
|
cell.tagLabel.text = "🗃 Top Level"
|
|
|
|
} else {
|
|
|
|
cell.tagLabel.text = "\(String(repeating: " ", count: components.count))📁 \(components.last ?? "?")"
|
|
|
|
}
|
|
|
|
|
|
|
|
cell.accessoryType = indexPath == viewController.selectedFolderIndexPath ? .checkmark : .none
|
|
|
|
|
|
|
|
return cell
|
|
|
|
} else {
|
|
|
|
guard let cell = tableView.dequeueReusableCell(withIdentifier: ShareSaveNewCell.reuseIdentifier, for: indexPath) as? ShareSaveNewCell else {
|
|
|
|
preconditionFailure("Expected to dequeue a ShareSaveNewCell")
|
|
|
|
}
|
|
|
|
|
|
|
|
cell.tagField.text = viewController.newFolder
|
|
|
|
cell.tagField.placeholder = "new folder title"
|
2021-07-21 21:11:39 -07:00
|
|
|
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
guard let cell = tableView.dequeueReusableCell(withIdentifier: ShareCommentCell.reuseIdentifier, for: indexPath) as? ShareCommentCell else {
|
|
|
|
preconditionFailure("Expected to dequeue a ShareCommentCell")
|
|
|
|
}
|
|
|
|
|
|
|
|
cell.commentTextView.text = ""
|
|
|
|
cell.commentTextView.delegate = self
|
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
cell.commentTextView.becomeFirstResponder()
|
|
|
|
}
|
|
|
|
|
|
|
|
return cell
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension ShareViewDelegate: UITextViewDelegate {
|
|
|
|
func textViewDidChange(_ textView: UITextView) {
|
|
|
|
viewController.comments = textView.text
|
|
|
|
}
|
|
|
|
}
|