Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize ncshareaccounts #3112

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
138 changes: 56 additions & 82 deletions iOSClient/Account Request/NCAccountRequest.swift
Original file line number Diff line number Diff line change
@@ -1,26 +1,3 @@
//
// 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 NextcloudKit

Expand All @@ -41,8 +18,9 @@ class NCAccountRequest: UIViewController {
public var enableAddAccount: Bool = false
public var dismissDidEnterBackground: Bool = false
public weak var delegate: NCAccountRequestDelegate?

let utility = NCUtility()
private var timer: Timer?
private weak var timer: Timer?
private var time: Float = 0
private let secondsAutoDismiss: Float = 3

Expand All @@ -51,40 +29,44 @@ class NCAccountRequest: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

setupUI()
setupObservers()
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

adjustTableViewScroll()
}

override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)

invalidateTimer()
}

// MARK: - Setup Functions

private func setupUI() {
titleLabel.text = NSLocalizedString("_account_select_", comment: "")
tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1))
tableView.separatorStyle = UITableViewCell.SeparatorStyle.none

tableView.separatorStyle = .none
view.backgroundColor = .secondarySystemBackground
tableView.backgroundColor = .secondarySystemBackground

progressView.trackTintColor = .clear
progressView.progress = 1
if enableTimerProgress {
progressView.isHidden = false
} else {
progressView.isHidden = true
}
progressView.isHidden = !enableTimerProgress
}

private func setupObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(startTimer), name: UIApplication.didBecomeActiveNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
}

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 viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)

timer?.invalidate()
private func adjustTableViewScroll() {
let numAccounts = enableAddAccount ? accounts.count + 1 : accounts.count
tableView.isScrollEnabled = tableView.visibleCells.count < numAccounts
}

// MARK: - NotificationCenter
Expand All @@ -95,17 +77,23 @@ class NCAccountRequest: UIViewController {
}
}

// MARK: - Progress
// MARK: - Timer Functions

@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
guard enableTimerProgress else {
progressView.isHidden = true
return
}

time = 0
invalidateTimer()
timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(updateProgress), userInfo: nil, repeats: true)
progressView.isHidden = false
}

private func invalidateTimer() {
timer?.invalidate()
timer = nil
}

@objc func updateProgress() {
Expand All @@ -120,7 +108,7 @@ class NCAccountRequest: UIViewController {

extension NCAccountRequest: UITableViewDelegate {
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
timer?.invalidate()
invalidateTimer()
progressView.progress = 0
}

Expand All @@ -147,16 +135,19 @@ extension NCAccountRequest: UITableViewDelegate {

extension NCAccountRequest: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if enableAddAccount {
return accounts.count + 1
} else {
return accounts.count
}
return enableAddAccount ? accounts.count + 1 : accounts.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.backgroundColor = tableView.backgroundColor

configureCell(cell, at: indexPath)

return cell
}

private func configureCell(_ cell: UITableViewCell, at indexPath: IndexPath) {
let avatarImage = cell.viewWithTag(10) as? UIImageView
let userLabel = cell.viewWithTag(20) as? UILabel
let urlLabel = cell.viewWithTag(30) as? UILabel
Expand All @@ -166,36 +157,19 @@ extension NCAccountRequest: UITableViewDataSource {
urlLabel?.text = ""

if indexPath.row == accounts.count {

avatarImage?.image = utility.loadImage(named: "plus", colors: [.systemBlue])
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 = utility.loadUserImage(for: account.user, displayName: account.displayName, userBaseUrl: account)

userLabel?.text = account.alias.isEmpty ? account.user.uppercased() : account.alias.uppercased()
urlLabel?.text = account.alias.isEmpty ? (URL(string: account.urlBase)?.host ?? "") : nil

avatarImage?.image = utility.loadUserImage(
for: account.user,
displayName: account.displayName,
userBaseUrl: account)

if account.alias.isEmpty {
userLabel?.text = account.user.uppercased()
urlLabel?.text = (URL(string: account.urlBase)?.host ?? "")
} else {
userLabel?.text = account.alias.uppercased()
}

if account.active {
activeImage?.image = utility.loadImage(named: "checkmark", colors: [.systemBlue])
} else {
activeImage?.image = nil
}
activeImage?.image = account.active ? utility.loadImage(named: "checkmark", colors: [.systemBlue]) : nil
}

return cell
}
}
48 changes: 28 additions & 20 deletions iOSClient/Account Request/NCShareAccounts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,27 @@ class NCShareAccounts: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

titleLabel.text = NSLocalizedString("_account_select_to_add_", comment: "")
setupUI()
}

tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1))
tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

adjustTableViewScroll()
}

// MARK: - UI Setup

private func setupUI() {
titleLabel.text = NSLocalizedString("_account_select_to_add_", comment: "")
tableView.tableFooterView = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 1))
tableView.separatorStyle = .none
view.backgroundColor = .secondarySystemBackground
tableView.backgroundColor = .secondarySystemBackground
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

let visibleCells = tableView.visibleCells
if visibleCells.count == accounts.count {
tableView.isScrollEnabled = false
}
private func adjustTableViewScroll() {
tableView.isScrollEnabled = tableView.visibleCells.count < accounts.count
}
}

Expand All @@ -75,7 +80,6 @@ extension NCShareAccounts: UITableViewDelegate {
}

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)
Expand All @@ -90,10 +94,15 @@ extension NCShareAccounts: UITableViewDataSource {
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.backgroundColor = tableView.backgroundColor

configureCell(cell, at: indexPath)

return cell
}

private func configureCell(_ cell: UITableViewCell, at indexPath: IndexPath) {
let avatarImage = cell.viewWithTag(10) as? UIImageView
let userLabel = cell.viewWithTag(20) as? UILabel
let urlLabel = cell.viewWithTag(30) as? UILabel
Expand All @@ -103,17 +112,16 @@ extension NCShareAccounts: UITableViewDataSource {

let account = accounts[indexPath.row]

if let image = account.image {
avatarImage?.image = image
}
avatarImage?.image = account.image
userLabel?.text = accountDisplayName(for: account)
urlLabel?.text = (URL(string: account.url)?.host ?? "")
}

private func accountDisplayName(for account: NKShareAccounts.DataAccounts) -> String {
if let name = account.name, !name.isEmpty {
userLabel?.text = name.uppercased() + " (\(account.user))"
return name.uppercased() + " (\(account.user))"
} else {
userLabel?.text = account.user.uppercased()
return account.user.uppercased()
}
urlLabel?.text = (URL(string: account.url)?.host ?? "")

return cell
}
}