123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- import UIKit
- import NextcloudKit
- public protocol NCShareAccountsDelegate: AnyObject {
- func selected(url: String, user: String)
- }
- public extension NCShareAccountsDelegate {
- func selected(url: String, user: String) {}
- }
- class NCShareAccounts: UIViewController {
- @IBOutlet weak var titleLabel: UILabel!
- @IBOutlet weak var tableView: UITableView!
- @IBOutlet weak var progressView: UIProgressView!
- public var accounts: [NKDataAccountFile] = []
- public let heightCell: CGFloat = 60
- public var enableTimerProgress: Bool = true
- public var dismissDidEnterBackground: Bool = true
- public weak var delegate: NCShareAccountsDelegate?
- private var timer: Timer?
- private var time: Float = 0
- private let secondsAutoDismiss: Float = 3
-
- override func viewDidLoad() {
- super.viewDidLoad()
- titleLabel.text = NSLocalizedString("_account_select_to_add_", comment: "")
- tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1))
-
- view.backgroundColor = .secondarySystemBackground
- tableView.backgroundColor = .secondarySystemBackground
- progressView.trackTintColor = .clear
- progressView.progress = 1
- if enableTimerProgress {
- progressView.isHidden = false
- } else {
- progressView.isHidden = true
- }
- NotificationCenter.default.addObserver(self, selector: #selector(startTimer), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidBecomeActive), object: nil)
- NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
- }
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- }
- override func viewDidAppear(_ animated: Bool) {
- super.viewDidAppear(animated)
- let visibleCells = tableView.visibleCells
- if visibleCells.count == accounts.count {
- tableView.isScrollEnabled = false
- }
- }
- override func viewWillDisappear(_ animated: Bool) {
- super.viewWillDisappear(animated)
- timer?.invalidate()
- }
-
- @objc func applicationDidEnterBackground() {
- if dismissDidEnterBackground {
- dismiss(animated: false)
- }
- }
-
- @objc func startTimer() {
- if enableTimerProgress {
- time = 0
- timer?.invalidate()
- timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(updateProgress), userInfo: nil, repeats: true)
- progressView.isHidden = false
- } else {
- progressView.isHidden = true
- }
- }
- @objc func updateProgress() {
- time += 0.1
- if time >= secondsAutoDismiss {
- dismiss(animated: true)
- } else {
- progressView.progress = 1 - (time / secondsAutoDismiss)
- }
- }
- }
- extension NCShareAccounts: UITableViewDelegate {
- func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
- timer?.invalidate()
- progressView.progress = 0
- }
- func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- return heightCell
- }
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- dismiss(animated: true) {
- let account = self.accounts[indexPath.row]
- self.delegate?.selected(url: account.url, user: account.user)
- }
- }
- }
- extension NCShareAccounts: UITableViewDataSource {
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return accounts.count
- }
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
- cell.backgroundColor = tableView.backgroundColor
- let avatarImage = cell.viewWithTag(10) as? UIImageView
- let userLabel = cell.viewWithTag(20) as? UILabel
- let urlLabel = cell.viewWithTag(30) as? UILabel
- userLabel?.text = ""
- urlLabel?.text = ""
- let account = accounts[indexPath.row]
- if let avatarPath = account.avatar, !avatarPath.isEmpty, let avatarImage = avatarImage {
- do {
- let data = try Data(contentsOf: URL(fileURLWithPath: avatarPath))
- if let image = UIImage(data: data) {
- avatarImage.image = image
- }
- } catch { print("Error: \(error)") }
- }
- if let alias = account.alias, !alias.isEmpty {
- userLabel?.text = alias.uppercased() + " (\(account.user))"
- } else {
- userLabel?.text = account.user.uppercased()
- }
- urlLabel?.text = (URL(string: account.url)?.host ?? "")
- return cell
- }
- }
|