Skip to content

Commit

Permalink
Use displayname instead of username (#273)
Browse files Browse the repository at this point in the history
* Implemented bookmark display name

* Fixed indentation

* Implemented different solution to use displayName

* adapt OCConnection init method to SDK changes

* prevent UI operation on main thread

* also get sure, that this is called on the main thread

* Adapt mocking OCConnection connect / disconnect

- OCConnection needs mocking for connect and disconnect
- moved mocking methods to UtilsTests class, make it accessible for other test classes
- added accessibility id for save button
- updated submodul, because newest SDK is needed
  • Loading branch information
mneuwert authored and hosy committed Mar 7, 2019
1 parent 203d600 commit 1a6ba33
Show file tree
Hide file tree
Showing 7 changed files with 291 additions and 169 deletions.
52 changes: 37 additions & 15 deletions ownCloud/Bookmarks/BookmarkViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ class BookmarkViewController: StaticTableViewController {

self.navigationItem.title = "Edit account".localized

saveBarButtonItem.accessibilityIdentifier = "account-save"
self.navigationItem.rightBarButtonItem = saveBarButtonItem
}

Expand Down Expand Up @@ -417,23 +418,44 @@ class BookmarkViewController: StaticTableViewController {
}

@objc func userActionSave() {
if isBookmarkComplete(bookmark: self.bookmark) {
self.bookmark?.authenticationDataStorage = .keychain // Commit auth changes to keychain

switch mode {
case .create:
// Add bookmark
OCBookmarkManager.shared.addBookmark(bookmark!)
OCBookmarkManager.shared.saveBookmarks()

case .edit:
// Update original bookmark
originalBookmark?.setValuesFrom(bookmark!)
OCBookmarkManager.shared.saveBookmarks()
OCBookmarkManager.shared.postChangeNotification()

guard let bookmark = self.bookmark else { return }

if isBookmarkComplete(bookmark: bookmark) {
bookmark.authenticationDataStorage = .keychain // Commit auth changes to keychain

if let connection = OCConnection(bookmark: bookmark) {
connection.connect { [weak self] (error, _) in
if let weakSelf = self {
if error == nil {
bookmark.displayName = connection.loggedInUser.displayName
connection.disconnect(completionHandler: {
switch weakSelf.mode {
case .create:
// Add bookmark
OCBookmarkManager.shared.addBookmark(bookmark)
OCBookmarkManager.shared.saveBookmarks()

case .edit:
// Update original bookmark
self?.originalBookmark?.setValuesFrom(bookmark)
OCBookmarkManager.shared.saveBookmarks()
OCBookmarkManager.shared.postChangeNotification()
}
OnMainThread {
weakSelf.presentingViewController?.dismiss(animated: true, completion: nil)
}

})
} else {
OnMainThread {
weakSelf.presentingViewController?.dismiss(animated: true, completion: nil)
}
}
}
}
}

self.presentingViewController?.dismiss(animated: true, completion: nil)
} else {
handleContinue()
}
Expand Down
47 changes: 33 additions & 14 deletions ownCloud/SDK Extensions/OCBookmark+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,56 @@
//

/*
* Copyright (C) 2018, ownCloud GmbH.
*
* This code is covered by the GNU Public License Version 3.
*
* For distribution utilizing Apple mechanisms please see https://owncloud.org/contribute/iOS-license-exception/
* You should have received a copy of this license along with this program. If not, see <http://www.gnu.org/licenses/gpl-3.0.en.html>.
*
*/
* Copyright (C) 2018, ownCloud GmbH.
*
* This code is covered by the GNU Public License Version 3.
*
* For distribution utilizing Apple mechanisms please see https://owncloud.org/contribute/iOS-license-exception/
* You should have received a copy of this license along with this program. If not, see <http://www.gnu.org/licenses/gpl-3.0.en.html>.
*
*/

import UIKit
import ownCloudSDK

extension OCBookmark {
static let OCBookmarkDisplayName : NSString = "OCBookmarkDisplayName"

var userName : String? {
if self.authenticationData != nil,
self.authenticationMethodIdentifier != nil,
let authenticationMethod = OCAuthenticationMethod.registeredAuthenticationMethod(forIdentifier: self.authenticationMethodIdentifier),
let userName = authenticationMethod.userName(fromAuthenticationData: self.authenticationData) {
return userName
self.authenticationMethodIdentifier != nil,
let authenticationMethod = OCAuthenticationMethod.registeredAuthenticationMethod(forIdentifier: self.authenticationMethodIdentifier),
let userName = authenticationMethod.userName(fromAuthenticationData: self.authenticationData) {
return userName
}

return nil
}

var displayName : String? {
get {
return self.userInfo.object(forKey: OCBookmark.OCBookmarkDisplayName) as? String
}

set {
self.userInfo[OCBookmark.OCBookmarkDisplayName] = newValue
}
}

var shortName: String {
if self.name != nil {
return self.name!
} else {
var userNamePrefix = ""

if let userName = self.userName {
userNamePrefix = userName + "@"
if let displayName = self.displayName {
userNamePrefix = displayName + "@"
}

if userNamePrefix.count == 0 {
if let userName = self.userName {
userNamePrefix = userName + "@"
}
}

if self.originURL?.host != nil {
Expand All @@ -50,4 +68,5 @@ extension OCBookmark {

return "bookmark"
}

}
154 changes: 78 additions & 76 deletions ownCloud/Server List/ServerListTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
//

/*
* Copyright (C) 2018, ownCloud GmbH.
*
* This code is covered by the GNU Public License Version 3.
*
* For distribution utilizing Apple mechanisms please see https://owncloud.org/contribute/iOS-license-exception/
* You should have received a copy of this license along with this program. If not, see <http://www.gnu.org/licenses/gpl-3.0.en.html>.
*
*/
* Copyright (C) 2018, ownCloud GmbH.
*
* This code is covered by the GNU Public License Version 3.
*
* For distribution utilizing Apple mechanisms please see https://owncloud.org/contribute/iOS-license-exception/
* You should have received a copy of this license along with this program. If not, see <http://www.gnu.org/licenses/gpl-3.0.en.html>.
*
*/

import UIKit
import ownCloudSDK
Expand Down Expand Up @@ -49,16 +49,16 @@ class ServerListTableViewController: UITableViewController, Themeable {
// TODO: Rebuild welcomeOverlayView in code
/*
override func loadView() {
super.loadView()
super.loadView()

welcomeOverlayView = UIView()
welcomeOverlayView.translatesAutoresizingMaskIntoConstraints = false
welcomeOverlayView = UIView()
welcomeOverlayView.translatesAutoresizingMaskIntoConstraints = false

welcomeTitleLabel = UILabel()
welcomeTitleLabel.font = UIFont.boldSystemFont(ofSize: 34)
welcomeTitleLabel.translatesAutoresizingMaskIntoConstraints = false
welcomeTitleLabel = UILabel()
welcomeTitleLabel.font = UIFont.boldSystemFont(ofSize: 34)
welcomeTitleLabel.translatesAutoresizingMaskIntoConstraints = false

welcomeAddServerButton = ThemeButton()
welcomeAddServerButton = ThemeButton()
}
*/

Expand Down Expand Up @@ -96,6 +96,8 @@ class ServerListTableViewController: UITableViewController, Themeable {
Theme.shared.register(client: self)

welcomeOverlayView.layoutSubviews()

self.tableView.reloadData()
}

override func viewDidAppear(_ animated: Bool) {
Expand Down Expand Up @@ -124,9 +126,9 @@ class ServerListTableViewController: UITableViewController, Themeable {
Log.log("Show beta warning: \(String(describing: VendorServices.classSetting(forOCClassSettingsKey: .showBetaWarning) as? Bool))")

if VendorServices.classSetting(forOCClassSettingsKey: .showBetaWarning) as? Bool == true,
let lastGitCommit = LastGitCommit(),
(lastBetaWarningCommit == nil) || (lastBetaWarningCommit != lastGitCommit) {
// Beta warning has never been shown before - or has last been shown for a different release
let lastGitCommit = LastGitCommit(),
(lastBetaWarningCommit == nil) || (lastBetaWarningCommit != lastGitCommit) {
// Beta warning has never been shown before - or has last been shown for a different release
let betaAlert = UIAlertController(with: "Beta Warning", message: "\nThis is a BETA release that may - and likely will - still contain bugs.\n\nYOU SHOULD NOT USE THIS BETA VERSION WITH PRODUCTION SYSTEMS, PRODUCTION DATA OR DATA OF VALUE. YOU'RE USING THIS BETA AT YOUR OWN RISK.\n\nPlease let us know about any issues that come up via the \"Send Feedback\" option in the settings.", okLabel: "Agree") {
OCAppIdentity.shared.userDefaults?.set(lastGitCommit, forKey: "LastBetaWarningCommit")
OCAppIdentity.shared.userDefaults?.set(NSDate(), forKey: "LastBetaWarningAcceptDate")
Expand Down Expand Up @@ -227,8 +229,8 @@ class ServerListTableViewController: UITableViewController, Themeable {

// Exit editing mode (unfortunately, self.isEditing = false will not do the trick as it leaves the left bar button unchanged as "Done")
if self.tableView.isEditing,
let target = self.navigationItem.leftBarButtonItem?.target,
let action = self.navigationItem.leftBarButtonItem?.action {
let target = self.navigationItem.leftBarButtonItem?.target,
let action = self.navigationItem.leftBarButtonItem?.action {
_ = target.perform(action, with: self)
}

Expand All @@ -242,7 +244,7 @@ class ServerListTableViewController: UITableViewController, Themeable {
}

@IBAction func settings() {
let viewController : SettingsViewController = SettingsViewController(style: .grouped)
let viewController : SettingsViewController = SettingsViewController(style: .grouped)

self.navigationController?.pushViewController(viewController, animated: true)
}
Expand All @@ -264,8 +266,8 @@ class ServerListTableViewController: UITableViewController, Themeable {
if let bookmark = OCBookmarkManager.shared.bookmark(at: UInt(indexPath.row)) {
if lockedBookmarks.contains(bookmark) {
let alertController = UIAlertController(title: NSString(format: "'%@' is currently locked".localized as NSString, bookmark.shortName as NSString) as String,
message: NSString(format: "An operation is currently performed that prevents connecting to '%@'. Please try again later.".localized as NSString, bookmark.shortName as NSString) as String,
preferredStyle: .alert)
message: NSString(format: "An operation is currently performed that prevents connecting to '%@'. Please try again later.".localized as NSString, bookmark.shortName as NSString) as String,
preferredStyle: .alert)

alertController.addAction(UIAlertAction(title: "OK".localized, style: .default, handler: { (_) in
// There was an error erasing the vault => re-add bookmark to give user another chance to delete its contents
Expand Down Expand Up @@ -303,7 +305,7 @@ class ServerListTableViewController: UITableViewController, Themeable {

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let bookmarkCell = self.tableView.dequeueReusableCell(withIdentifier: "bookmark-cell", for: indexPath) as? ServerListBookmarkCell else {
return ServerListBookmarkCell()
return ServerListBookmarkCell()
}

if let bookmark : OCBookmark = OCBookmarkManager.shared.bookmark(at: UInt(indexPath.row)) {
Expand All @@ -317,72 +319,72 @@ class ServerListTableViewController: UITableViewController, Themeable {

override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
return [
UITableViewRowAction(style: .destructive, title: "Delete".localized, handler: { (_, indexPath) in
if let bookmark = OCBookmarkManager.shared.bookmark(at: UInt(indexPath.row)) {
var presentationStyle: UIAlertController.Style = .actionSheet
if UIDevice.current.isIpad() {
presentationStyle = .alert
}

let alertController = UIAlertController(title: NSString(format: "Really delete '%@'?".localized as NSString, bookmark.shortName) as String,
message: "This will also delete all locally stored file copies.".localized,
preferredStyle: presentationStyle)

alertController.addAction(UIAlertAction(title: "Cancel".localized, style: .cancel, handler: nil))
UITableViewRowAction(style: .destructive, title: "Delete".localized, handler: { (_, indexPath) in
if let bookmark = OCBookmarkManager.shared.bookmark(at: UInt(indexPath.row)) {
var presentationStyle: UIAlertController.Style = .actionSheet
if UIDevice.current.isIpad() {
presentationStyle = .alert
}

alertController.addAction(UIAlertAction(title: "Delete".localized, style: .destructive, handler: { (_) in
let alertController = UIAlertController(title: NSString(format: "Really delete '%@'?".localized as NSString, bookmark.shortName) as String,
message: "This will also delete all locally stored file copies.".localized,
preferredStyle: presentationStyle)

self.lockedBookmarks.append(bookmark)
alertController.addAction(UIAlertAction(title: "Cancel".localized, style: .cancel, handler: nil))

OCCoreManager.shared.scheduleOfflineOperation({ (bookmark, completionHandler) in
let vault : OCVault = OCVault(bookmark: bookmark)
alertController.addAction(UIAlertAction(title: "Delete".localized, style: .destructive, handler: { (_) in

vault.erase(completionHandler: { (_, error) in
OnMainThread {
if error != nil {
// Inform user if vault couldn't be erased
let alertController = UIAlertController(title: NSString(format: "Deletion of '%@' failed".localized as NSString, bookmark.shortName as NSString) as String,
message: error?.localizedDescription,
preferredStyle: .alert)
self.lockedBookmarks.append(bookmark)

alertController.addAction(UIAlertAction(title: "OK".localized, style: .default, handler: nil))
OCCoreManager.shared.scheduleOfflineOperation({ (bookmark, completionHandler) in
let vault : OCVault = OCVault(bookmark: bookmark)

self.present(alertController, animated: true, completion: nil)
} else {
// Success! We can now remove the bookmark
self.ignoreServerListChanges = true
vault.erase(completionHandler: { (_, error) in
OnMainThread {
if error != nil {
// Inform user if vault couldn't be erased
let alertController = UIAlertController(title: NSString(format: "Deletion of '%@' failed".localized as NSString, bookmark.shortName as NSString) as String,
message: error?.localizedDescription,
preferredStyle: .alert)

OCBookmarkManager.shared.removeBookmark(bookmark)
alertController.addAction(UIAlertAction(title: "OK".localized, style: .default, handler: nil))

tableView.performBatchUpdates({
tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.fade)
}, completion: { (_) in
self.ignoreServerListChanges = false
})
self.present(alertController, animated: true, completion: nil)
} else {
// Success! We can now remove the bookmark
self.ignoreServerListChanges = true

self.updateNoServerMessageVisibility()
}
OCBookmarkManager.shared.removeBookmark(bookmark)

if let removeIndex = self.lockedBookmarks.index(of: bookmark) {
self.lockedBookmarks.remove(at: removeIndex)
}
tableView.performBatchUpdates({
tableView.deleteRows(at: [indexPath], with: UITableView.RowAnimation.fade)
}, completion: { (_) in
self.ignoreServerListChanges = false
})

completionHandler()
self.updateNoServerMessageVisibility()
}
})
}, for: bookmark)
}))

self.present(alertController, animated: true, completion: nil)
}
}),
if let removeIndex = self.lockedBookmarks.index(of: bookmark) {
self.lockedBookmarks.remove(at: removeIndex)
}

UITableViewRowAction(style: .normal, title: "Edit".localized, handler: { [weak self] (_, indexPath) in
if let bookmark = OCBookmarkManager.shared.bookmark(at: UInt(indexPath.row)) {
self?.showBookmarkUI(edit: bookmark)
}
})
]
completionHandler()
}
})
}, for: bookmark)
}))

self.present(alertController, animated: true, completion: nil)
}
}),

UITableViewRowAction(style: .normal, title: "Edit".localized, handler: { [weak self] (_, indexPath) in
if let bookmark = OCBookmarkManager.shared.bookmark(at: UInt(indexPath.row)) {
self?.showBookmarkUI(edit: bookmark)
}
})
]
}

override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
Expand Down
Loading

0 comments on commit 1a6ba33

Please sign in to comment.