diff --git a/Tests/NextcloudUnitTests/NCNotificationText.swift b/Tests/NextcloudUnitTests/NCNotificationText.swift new file mode 100644 index 0000000000..1a7e0b2345 --- /dev/null +++ b/Tests/NextcloudUnitTests/NCNotificationText.swift @@ -0,0 +1,85 @@ +// +// NCNotificationText.swift +// NextcloudUnitTests +// +// Created by Amrut Waghmare on 18/10/23. +// Copyright © 2023 Marino Faggiana. All rights reserved. +// + +@testable import Nextcloud +import XCTest +import NextcloudKit + +class NCNotificationText: XCTestCase { + var viewController : NCNotification! + + override func setUpWithError() throws { + // Step 1. Create an instance of UIStoryboard + let storyboard = UIStoryboard(name: "NCNotification", bundle: nil) + // Step 2. Instantiate UIViewController with Storyboard ID + viewController = storyboard.instantiateViewController(withIdentifier: "NCNotification.storyboard") as? NCNotification + + // Step 3. Make the viewDidLoad() execute. + viewController.loadViewIfNeeded() + } + + override func tearDownWithError() throws { + viewController = nil + } + + //Test that a cell with the correct reuse identifier is dequeued + func testTableViewCellDequeue() { + let notification = NKNotifications() + viewController.notifications = [notification] + let tableView = UITableView() + tableView.register(NCNotificationCell.self, forCellReuseIdentifier: "Cell") + let indexPath = IndexPath(row: 0, section: 0) + let cell = viewController.tableView(tableView, cellForRowAt: indexPath) as? NCNotificationCell + XCTAssertNotNil(cell) + XCTAssertEqual(cell?.reuseIdentifier, "Cell") + } + + //Test that the cell's icon is set image + func testTableViewCellIcon() { + let notification = NKNotifications() + viewController.notifications = [notification] + let tableView = UITableView() + tableView.register(NCNotificationCell.self, forCellReuseIdentifier: "Cell") + let indexPath = IndexPath(row: 0, section: 0) + let cell = viewController.tableView(tableView, cellForRowAt: indexPath) as? NCNotificationCell + XCTAssertNotNil(cell?.icon.image) + } + + //Test that the cell's primary and secondary buttons are set up correctly + func testTableViewCellButtons() { + let notification = NKNotifications() + notification.actions = Data("[{\"label\":\"OK\",\"primary\":true},{\"label\":\"Cancel\",\"primary\":false}]".utf8) + viewController.notifications = [notification] + let tableView = UITableView() + tableView.register(NCNotificationCell.self, forCellReuseIdentifier: "Cell") + let indexPath = IndexPath(row: 0, section: 0) + let cell = viewController.tableView(tableView, cellForRowAt: indexPath) as? NCNotificationCell + XCTAssertEqual(cell?.primary.title(for: .normal), "OK") + XCTAssertEqual(cell?.secondary.title(for: .normal), "Cancel") + } + + //Test that the cell's date label is set correctly + func testTableViewCellDate() { + let notification = NKNotifications() + notification.date = NSDate() + viewController.notifications = [notification] + let tableView = UITableView() + tableView.register(NCNotificationCell.self, forCellReuseIdentifier: "Cell") + let indexPath = IndexPath(row: 0, section: 0) + let cell = viewController.tableView(tableView, cellForRowAt: indexPath) as? NCNotificationCell + XCTAssertEqual(cell?.date.text, "less than a minute ago") + } + + //Test with a color that is image not nil + func testImageNotNil() { + let color = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1.0) + let image = UIImage().imageColor(color) + XCTAssertNotNil(image, "Image should not be nil.") + + } +} diff --git a/iOSClient/Notification/NCNotification.storyboard b/iOSClient/Notification/NCNotification.storyboard index a06f8023db..a89ba02d24 100644 --- a/iOSClient/Notification/NCNotification.storyboard +++ b/iOSClient/Notification/NCNotification.storyboard @@ -1,174 +1,163 @@ - + - + - + - - + + - - + + - - + + - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - + + + + + + + + + + + - - + + - - + + + - + - + + diff --git a/iOSClient/Notification/NCNotification.swift b/iOSClient/Notification/NCNotification.swift index 1e86e5f6a7..f35d54fdd7 100644 --- a/iOSClient/Notification/NCNotification.swift +++ b/iOSClient/Notification/NCNotification.swift @@ -45,6 +45,7 @@ class NCNotification: UITableViewController, NCNotificationCellDelegate { tableView.rowHeight = UITableView.automaticDimension tableView.estimatedRowHeight = 50.0 tableView.backgroundColor = .systemBackground + tableView.allowsSelection = false refreshControl?.addTarget(self, action: #selector(getNetwokingNotification), for: .valueChanged) @@ -65,6 +66,13 @@ class NCNotification: UITableViewController, NCNotificationCellDelegate { super.viewDidAppear(animated) getNetwokingNotification() + NotificationCenter.default.addObserver(self, selector: #selector(initialize), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterInitialize), object: nil) + } + + override func viewWillDisappear(_ animated: Bool) { + super.viewWillDisappear(animated) + + NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterInitialize), object: nil) } override func viewWillDisappear(_ animated: Bool) { @@ -77,6 +85,11 @@ class NCNotification: UITableViewController, NCNotificationCellDelegate { @objc func viewClose() { self.dismiss(animated: true, completion: nil) } + + // MARK: - NotificationCenter + @objc func initialize() { + getNetwokingNotification() + } // MARK: - Table @@ -119,32 +132,13 @@ class NCNotification: UITableViewController, NCNotificationCellDelegate { if let image = image { cell.icon.image = image.withTintColor(NCBrandColor.shared.getElement(account: session.account), renderingMode: .alwaysOriginal) + } else { + cell.icon.image = utility.loadImage(named: "bell", color: NCBrandColor.shared.iconColor) } // Avatar cell.avatar.isHidden = true cell.avatarLeadingMargin.constant = 10 - if let subjectRichParameters = notification.subjectRichParameters, - let json = JSON(subjectRichParameters).dictionary, - let user = json["user"]?["id"].stringValue { - cell.avatar.isHidden = false - cell.avatarLeadingMargin.constant = 50 - - let fileName = NCSession.shared.getFileName(urlBase: session.urlBase, user: user) - let results = NCManageDatabase.shared.getImageAvatarLoaded(fileName: fileName) - - if results.image == nil { - cell.fileAvatarImageView?.image = utility.loadUserImage(for: user, displayName: json["user"]?["name"].string, urlBase: session.urlBase) - } else { - cell.fileAvatarImageView?.image = results.image - } - - if !(results.tableAvatar?.loaded ?? false), - NCNetworking.shared.downloadAvatarQueue.operations.filter({ ($0 as? NCOperationDownloadAvatar)?.fileName == fileName }).isEmpty { - NCNetworking.shared.downloadAvatarQueue.addOperation(NCOperationDownloadAvatar(user: user, fileName: fileName, account: session.account, view: tableView)) - } - } - cell.date.text = DateFormatter.localizedString(from: notification.date as Date, dateStyle: .medium, timeStyle: .medium) cell.notification = notification cell.date.text = utility.dateDiff(notification.date as Date) @@ -159,23 +153,15 @@ class NCNotification: UITableViewController, NCNotificationCellDelegate { cell.primary.isEnabled = false cell.primary.isHidden = true cell.primary.titleLabel?.font = .systemFont(ofSize: 15) - cell.primary.layer.cornerRadius = 15 - cell.primary.layer.masksToBounds = true - cell.primary.layer.backgroundColor = NCBrandColor.shared.getElement(account: session.account).cgColor cell.primary.setTitleColor(.white, for: .normal) - - cell.more.isEnabled = false - cell.more.isHidden = true - cell.more.titleLabel?.font = .systemFont(ofSize: 15) - cell.more.layer.cornerRadius = 15 - cell.more.layer.masksToBounds = true - cell.more.layer.backgroundColor = NCBrandColor.shared.getElement(account: session.account).cgColor - cell.more.setTitleColor(.white, for: .normal) + cell.primary.layer.cornerRadius = 10 + cell.primary.layer.masksToBounds = true + cell.primary.layer.backgroundColor = NCBrandColor.shared.notificationAction.cgColor cell.secondary.isEnabled = false cell.secondary.isHidden = true cell.secondary.titleLabel?.font = .systemFont(ofSize: 15) - cell.secondary.layer.cornerRadius = 15 + cell.secondary.layer.cornerRadius = 10 cell.secondary.layer.masksToBounds = true cell.secondary.layer.borderWidth = 1 cell.secondary.layer.borderColor = NCBrandColor.shared.iconImageColor2.cgColor @@ -211,11 +197,17 @@ class NCNotification: UITableViewController, NCNotificationCellDelegate { cell.secondary.setTitle(label, for: .normal) } } - } else if jsonActions.count >= 3 { + } + + let widthPrimary = cell.primary.intrinsicContentSize.width + 48; + let widthSecondary = cell.secondary.intrinsicContentSize.width + 48; - cell.more.isEnabled = true - cell.more.isHidden = false - cell.more.setTitle("…", for: .normal) + if widthPrimary > widthSecondary { + cell.primaryWidth.constant = widthPrimary + cell.secondaryWidth.constant = widthPrimary + } else { + cell.primaryWidth.constant = widthSecondary + cell.secondaryWidth.constant = widthSecondary } var buttonWidth = max(cell.primary.intrinsicContentSize.width, cell.secondary.intrinsicContentSize.width) @@ -325,7 +317,6 @@ class NCNotificationCell: UITableViewCell, NCCellProtocol { @IBOutlet weak var remove: UIButton! @IBOutlet weak var primary: UIButton! @IBOutlet weak var secondary: UIButton! - @IBOutlet weak var more: UIButton! @IBOutlet weak var avatarLeadingMargin: NSLayoutConstraint! @IBOutlet weak var primaryWidth: NSLayoutConstraint! @IBOutlet weak var secondaryWidth: NSLayoutConstraint! @@ -373,10 +364,6 @@ class NCNotificationCell: UITableViewCell, NCCellProtocol { delegate?.tapAction(with: notification, label: label) } - @IBAction func touchUpInsideMore(_ sender: Any) { - guard let notification = notification else { return } - delegate?.tapMore(with: notification) - } } protocol NCNotificationCellDelegate: AnyObject {