123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- //
- // NCAccountRequest.swift
- // Nextcloud
- //
- // Created by Marino Faggiana on 26/02/21.
- // Copyright © 2021 Marino Faggiana. All rights reserved.
- //
- // Author Marino Faggiana <marino.faggiana@nextcloud.com>
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- //
- import UIKit
- import NCCommunication
- public protocol NCAccountRequestDelegate {
- func accountRequestAddAccount()
- func changeAccountRequestAddAccount(account: String)
- }
- // optional func
- public extension NCAccountRequestDelegate {
- func accountRequestAddAccount() {}
- func changeAccountRequestAddAccount(account: String) {}
- }
- class NCAccountRequest: UIViewController {
- @IBOutlet weak var titleLabel: UILabel!
- @IBOutlet weak var closeButton: UIButton!
- @IBOutlet weak var tableView: UITableView!
- @IBOutlet weak var progressView: UIProgressView!
-
- public var accounts: [tableAccount] = []
- public var activeAccount: tableAccount?
- public let heightCell: CGFloat = 60
- public var enableTimerProgress: Bool = true
- public var enableAddAccount: Bool = false
- public var dismissDidEnterBackground: Bool = false
- public var delegate: NCAccountRequestDelegate?
- private var timer: Timer?
- private var time: Float = 0
- private let secondsAutoDismiss: Float = 3
-
- // MARK: - View Life Cycle
- override func viewDidLoad() {
- super.viewDidLoad()
-
- titleLabel.text = NSLocalizedString("_account_select_", comment: "")
- closeButton.setImage(NCUtility.shared.loadImage(named: "xmark", color: NCBrandColor.shared.label), for: .normal)
-
- tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1))
- tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
- 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
- var numAccounts = accounts.count
- if enableAddAccount { numAccounts += 1 }
-
- if visibleCells.count == numAccounts {
- tableView.isScrollEnabled = false
- }
- }
-
- override func viewWillDisappear(_ animated: Bool) {
- super.viewWillDisappear(animated)
-
- timer?.invalidate()
- }
-
- @IBAction func actionClose(_ sender: UIButton) {
- dismiss(animated: true)
- }
-
- // MARK: - NotificationCenter
- @objc func applicationDidEnterBackground() {
-
- if dismissDidEnterBackground {
- dismiss(animated: false)
- }
- }
- // MARK: - Progress
-
- @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 NCAccountRequest: UITableViewDelegate {
-
- func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
-
- timer?.invalidate()
- progressView.progress = 0
- }
-
- func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
- if decelerate {
- // startTimer()
- }
- }
-
- func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
- // startTimer()
- }
-
- func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- return heightCell
- }
-
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
-
- if indexPath.row == accounts.count {
-
- dismiss(animated: true)
- delegate?.accountRequestAddAccount()
-
- } else {
-
- let account = accounts[indexPath.row]
- if account.account != activeAccount?.account {
- dismiss(animated: true) {
- self.delegate?.changeAccountRequestAddAccount(account: account.account)
- }
- } else {
- dismiss(animated: true)
- }
- }
- }
- }
- extension NCAccountRequest: UITableViewDataSource {
-
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- if enableAddAccount {
- return accounts.count + 1
- } else {
- return accounts.count
- }
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
-
- let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
-
- let avatarImage = cell.viewWithTag(10) as? UIImageView
- let userLabel = cell.viewWithTag(20) as? UILabel
- let urlLabel = cell.viewWithTag(30) as? UILabel
- let activeImage = cell.viewWithTag(40) as? UIImageView
- userLabel?.text = ""
- urlLabel?.text = ""
-
- if indexPath.row == accounts.count {
-
- avatarImage?.image = NCUtility.shared.loadImage(named: "plus").image(color: .systemBlue, size: 15)
- avatarImage?.contentMode = .center
- userLabel?.text = NSLocalizedString("_add_account_", comment: "")
- userLabel?.textColor = .systemBlue
- userLabel?.font = UIFont.systemFont(ofSize: 15)
- } else {
-
- let account = accounts[indexPath.row]
- avatarImage?.image = NCUtility.shared.loadImage(named: "person.crop.circle")
-
- let fileNamePath = String(CCUtility.getDirectoryUserData()) + "/" + String(CCUtility.getStringUser(account.user, urlBase: account.urlBase)) + "-" + account.user + ".png"
-
- if let image = UIImage(contentsOfFile: fileNamePath) {
- avatarImage?.image = NCUtility.shared.createAvatar(image: image, size: 40)
- }
-
- if account.alias != "" {
- userLabel?.text = account.alias.uppercased()
- } else {
- userLabel?.text = account.user.uppercased()
- urlLabel?.text = (URL(string: account.urlBase)?.host ?? "")
- }
- if account.active {
- activeImage?.image = NCUtility.shared.loadImage(named: "checkmark").image(color: .systemBlue, size: 30)
- } else {
- activeImage?.image = nil
- }
- }
-
- return cell
- }
- }
|