From 87f308bd25fde0bff3a246ceea2c2d9c8d8e628a Mon Sep 17 00:00:00 2001 From: TSI-amrutwaghmare <96108296+TSI-amrutwaghmare@users.noreply.github.com> Date: Mon, 23 Oct 2023 14:24:51 +0530 Subject: [PATCH] NMC 1935 - Notification screen customisation --- .../NCNotificationText.swift | 85 ++++++++ .../Notification/NCNotification.storyboard | 185 ++++++++---------- iOSClient/Notification/NCNotification.swift | 91 ++++----- 3 files changed, 209 insertions(+), 152 deletions(-) create mode 100644 Tests/NextcloudUnitTests/NCNotificationText.swift 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 a9ff3bb549..b7a2785b92 100644 --- a/iOSClient/Notification/NCNotification.swift +++ b/iOSClient/Notification/NCNotification.swift @@ -48,6 +48,7 @@ class NCNotification: UITableViewController, NCNotificationCellDelegate, NCEmpty tableView.rowHeight = UITableView.automaticDimension tableView.estimatedRowHeight = 50.0 tableView.backgroundColor = .systemBackground + tableView.allowsSelection = false refreshControl?.addTarget(self, action: #selector(getNetwokingNotification), for: .valueChanged) @@ -69,11 +70,23 @@ class NCNotification: UITableViewController, NCNotificationCellDelegate, NCEmpty appDelegate.activeViewController = self navigationController?.setNavigationBarAppearance() 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) } @objc func viewClose() { self.dismiss(animated: true, completion: nil) } + + // MARK: - NotificationCenter + @objc func initialize() { + getNetwokingNotification() + } // MARK: - Empty @@ -99,19 +112,12 @@ class NCNotification: UITableViewController, NCNotificationCellDelegate, NCEmpty override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { - guard let notification = NCApplicationHandle().didSelectNotification(notifications[indexPath.row], viewController: self) else { return } + let notification = notifications[indexPath.row] - do { - if let subjectRichParameters = notification.subjectRichParameters, - let json = try JSONSerialization.jsonObject(with: subjectRichParameters, options: .mutableContainers) as? [String: Any], - let file = json["file"] as? [String: Any], - file["type"] as? String == "file" { - if let id = file["id"] { - NCActionCenter.shared.viewerFile(account: appDelegate.account, fileId: ("\(id)"), viewController: self) - } - } - } catch { - print("Something went wrong") + if notification.app == "files_sharing" { + NCActionCenter.shared.viewerFile(account: appDelegate.account, fileId: notification.objectId, viewController: self) + } else { + NCApplicationHandle().didSelectNotification(notification, viewController: self) } } @@ -132,30 +138,14 @@ class NCNotification: UITableViewController, NCNotificationCellDelegate, NCEmpty } if let image = image { - cell.icon.image = image.withTintColor(NCBrandColor.shared.brandElement, renderingMode: .alwaysOriginal) + cell.icon.image = image.withTintColor(NCBrandColor.shared.iconColor, renderingMode: .alwaysOriginal) } else { - cell.icon.image = utility.loadImage(named: "bell", color: NCBrandColor.shared.brandElement) + 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 = appDelegate.userBaseUrl + "-" + user + ".png" - let fileNameLocalPath = utilityFileSystem.directoryUserData + "/" + fileName - - if let image = UIImage(contentsOfFile: fileNameLocalPath) { - cell.avatar.image = image - } else if !FileManager.default.fileExists(atPath: fileNameLocalPath) { - cell.fileUser = user - NCNetworking.shared.downloadAvatar(user: user, dispalyName: json["user"]?["name"].string, fileName: fileName, cell: cell, view: tableView, cellImageView: cell.fileAvatarImageView) - } - } cell.date.text = DateFormatter.localizedString(from: notification.date as Date, dateStyle: .medium, timeStyle: .medium) cell.notification = notification @@ -171,28 +161,20 @@ class NCNotification: UITableViewController, NCNotificationCellDelegate, NCEmpty cell.primary.isEnabled = false cell.primary.isHidden = true cell.primary.titleLabel?.font = .systemFont(ofSize: 15) - cell.primary.layer.cornerRadius = 15 + cell.primary.setTitleColor(.white, for: .normal) + cell.primary.layer.cornerRadius = 10 cell.primary.layer.masksToBounds = true - cell.primary.layer.backgroundColor = NCBrandColor.shared.brandElement.cgColor - cell.primary.setTitleColor(NCBrandColor.shared.brandText, 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.brandElement.cgColor - cell.more.setTitleColor(NCBrandColor.shared.brandText, for: .normal) + 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 = UIColor.systemGray.cgColor - cell.secondary.layer.backgroundColor = UIColor.secondarySystemBackground.cgColor - cell.secondary.setTitleColor(.gray, for: .normal) + cell.secondary.layer.borderColor = NCBrandColor.shared.notificationAction.cgColor + cell.secondary.layer.backgroundColor = UIColor.clear.cgColor + cell.secondary.setTitleColor(NCBrandColor.shared.notificationAction, for: .normal) // Action if let actions = notification.actions, @@ -223,11 +205,17 @@ class NCNotification: UITableViewController, NCNotificationCellDelegate, NCEmpty 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) @@ -337,7 +325,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! @@ -385,10 +372,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 {