123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- //
- // NCListCell.swift
- // Nextcloud
- //
- // Created by Marino Faggiana on 24/10/2018.
- // Copyright © 2018 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
- class NCListCell: UICollectionViewCell, UIGestureRecognizerDelegate, NCCellProtocol {
- @IBOutlet weak var imageItem: UIImageView!
- @IBOutlet weak var imageSelect: UIImageView!
- @IBOutlet weak var imageStatus: UIImageView!
- @IBOutlet weak var imageFavorite: UIImageView!
- @IBOutlet weak var imageFavoriteBackground: UIImageView!
- @IBOutlet weak var imageLocal: UIImageView!
- @IBOutlet weak var labelTitle: UILabel!
- @IBOutlet weak var labelInfo: UILabel!
- @IBOutlet weak var labelSubinfo: UILabel!
- @IBOutlet weak var imageShared: UIImageView!
- @IBOutlet weak var buttonShared: UIButton!
- @IBOutlet weak var imageMore: UIImageView!
- @IBOutlet weak var buttonMore: UIButton!
- @IBOutlet weak var separator: UIView!
- @IBOutlet weak var tag0: UILabel!
- @IBOutlet weak var tag1: UILabel!
- @IBOutlet weak var imageItemLeftConstraint: NSLayoutConstraint!
- @IBOutlet weak var separatorHeightConstraint: NSLayoutConstraint!
- @IBOutlet weak var titleTrailingConstraint: NSLayoutConstraint!
- @IBOutlet weak var subInfoTrailingConstraint: NSLayoutConstraint!
- var ocId = ""
- var ocIdTransfer = ""
- var user = ""
- weak var listCellDelegate: NCListCellDelegate?
- var fileAvatarImageView: UIImageView? {
- return imageShared
- }
- var fileOcId: String? {
- get { return ocId }
- set { ocId = newValue ?? "" }
- }
- var fileOcIdTransfer: String? {
- get { return ocIdTransfer }
- set { ocIdTransfer = newValue ?? "" }
- }
- var filePreviewImageView: UIImageView? {
- get { return imageItem }
- set { imageItem = newValue }
- }
- var fileUser: String? {
- get { return user }
- set { user = newValue ?? "" }
- }
- var fileTitleLabel: UILabel? {
- get { return labelTitle }
- set { labelTitle = newValue }
- }
- var fileInfoLabel: UILabel? {
- get { return labelInfo }
- set { labelInfo = newValue }
- }
- var fileSubinfoLabel: UILabel? {
- get { return labelSubinfo }
- set { labelSubinfo = newValue }
- }
- var fileStatusImage: UIImageView? {
- get { return imageStatus }
- set { imageStatus = newValue }
- }
- var fileLocalImage: UIImageView? {
- get { return imageLocal }
- set { imageLocal = newValue }
- }
- var fileFavoriteImage: UIImageView? {
- get { return imageFavorite }
- set { imageFavorite = newValue }
- }
- var fileSharedImage: UIImageView? {
- get { return imageShared }
- set { imageShared = newValue }
- }
- var fileMoreImage: UIImageView? {
- get { return imageMore }
- set { imageMore = newValue }
- }
- var cellSeparatorView: UIView? {
- get { return separator }
- set { separator = newValue }
- }
- override func awakeFromNib() {
- super.awakeFromNib()
- initCell()
- }
- override func prepareForReuse() {
- super.prepareForReuse()
- initCell()
- }
- func initCell() {
- accessibilityHint = nil
- accessibilityLabel = nil
- accessibilityValue = nil
- isAccessibilityElement = true
- imageItem.image = nil
- imageItem.layer.cornerRadius = 6
- imageItem.layer.masksToBounds = true
- imageStatus.image = nil
- imageFavorite.image = nil
- imageFavoriteBackground.isHidden = true
- imageLocal.image = nil
- labelTitle.text = ""
- labelInfo.text = ""
- labelSubinfo.text = ""
- imageShared.image = nil
- imageMore.image = nil
- separatorHeightConstraint.constant = 0.5
- tag0.text = ""
- tag1.text = ""
- titleInfoTrailingDefault()
- let longPressedGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPress(gestureRecognizer:)))
- longPressedGesture.minimumPressDuration = 0.5
- longPressedGesture.delegate = self
- longPressedGesture.delaysTouchesBegan = true
- self.addGestureRecognizer(longPressedGesture)
- }
- override func snapshotView(afterScreenUpdates afterUpdates: Bool) -> UIView? {
- return nil
- }
- @IBAction func touchUpInsideShare(_ sender: Any) {
- listCellDelegate?.tapShareListItem(with: ocId, ocIdTransfer: ocIdTransfer, sender: sender)
- }
- @IBAction func touchUpInsideMore(_ sender: Any) {
- listCellDelegate?.tapMoreListItem(with: ocId, ocIdTransfer: ocIdTransfer, image: imageItem.image, sender: sender)
- }
- @objc func longPress(gestureRecognizer: UILongPressGestureRecognizer) {
- listCellDelegate?.longPressListItem(with: ocId, ocIdTransfer: ocIdTransfer, gestureRecognizer: gestureRecognizer)
- }
- fileprivate func setA11yActions() {
- self.accessibilityCustomActions = [
- UIAccessibilityCustomAction(
- name: NSLocalizedString("_share_", comment: ""),
- target: self,
- selector: #selector(touchUpInsideShare)),
- UIAccessibilityCustomAction(
- name: NSLocalizedString("_more_", comment: ""),
- target: self,
- selector: #selector(touchUpInsideMore))
- ]
- }
- func titleInfoTrailingFull() {
- titleTrailingConstraint.constant = 10
- subInfoTrailingConstraint.constant = 10
- }
- func titleInfoTrailingDefault() {
- titleTrailingConstraint.constant = 90
- subInfoTrailingConstraint.constant = 90
- }
- func setButtonMore(image: UIImage) {
- imageMore.image = image
- setA11yActions()
- }
- func hideButtonMore(_ status: Bool) {
- imageMore.isHidden = status
- buttonMore.isHidden = status
- }
- func hideButtonShare(_ status: Bool) {
- imageShared.isHidden = status
- buttonShared.isHidden = status
- }
- func selected(_ status: Bool, isEditMode: Bool) {
- if isEditMode {
- imageItemLeftConstraint.constant = 45
- imageSelect.isHidden = false
- imageShared.isHidden = true
- imageMore.isHidden = true
- buttonShared.isHidden = true
- buttonMore.isHidden = true
- accessibilityCustomActions = nil
- } else {
- imageItemLeftConstraint.constant = 10
- imageSelect.isHidden = true
- imageShared.isHidden = false
- imageMore.isHidden = false
- buttonShared.isHidden = false
- buttonMore.isHidden = false
- backgroundView = nil
- setA11yActions()
- }
- if status {
- var blurEffectView: UIView?
- blurEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .systemMaterial))
- blurEffectView?.backgroundColor = .lightGray
- blurEffectView?.frame = self.bounds
- blurEffectView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
- imageSelect.image = NCImageCache.shared.getImageCheckedYes()
- backgroundView = blurEffectView
- separator.isHidden = true
- } else {
- imageSelect.image = NCImageCache.shared.getImageCheckedNo()
- backgroundView = nil
- separator.isHidden = false
- }
- }
- func writeInfoDateSize(date: NSDate, size: Int64) {
- labelInfo.text = NCUtility().dateDiff(date as Date)
- labelSubinfo.text = " · " + NCUtilityFileSystem().transformedSize(size)
- }
- func setAccessibility(label: String, value: String) {
- accessibilityLabel = label
- accessibilityValue = value
- }
- func setTags(tags: [String]) {
- if tags.isEmpty {
- tag0.isHidden = true
- tag1.isHidden = true
- labelInfo.isHidden = false
- labelSubinfo.isHidden = false
- } else {
- tag0.isHidden = false
- tag1.isHidden = true
- labelInfo.isHidden = true
- labelSubinfo.isHidden = true
- if let tag = tags.first {
- tag0.text = tag
- if tags.count > 1 {
- tag1.isHidden = false
- tag1.text = "+\(tags.count - 1)"
- }
- }
- }
- }
- func setIconOutlines() {
- imageFavoriteBackground.isHidden = fileFavoriteImage?.image == nil
- if imageStatus.image != nil {
- imageStatus.makeCircularBackground(withColor: .systemBackground)
- } else {
- imageStatus.backgroundColor = .clear
- }
- if imageLocal.image != nil {
- imageLocal.makeCircularBackground(withColor: .systemBackground)
- } else {
- imageLocal.backgroundColor = .clear
- }
- }
- }
- protocol NCListCellDelegate: AnyObject {
- func tapShareListItem(with ocId: String, ocIdTransfer: String, sender: Any)
- func tapMoreListItem(with ocId: String, ocIdTransfer: String, image: UIImage?, sender: Any)
- func longPressListItem(with ocId: String, ocIdTransfer: String, gestureRecognizer: UILongPressGestureRecognizer)
- }
- // MARK: - List Layout
- class NCListLayout: UICollectionViewFlowLayout {
- var itemHeight: CGFloat = 60
- override init() {
- super.init()
- sectionHeadersPinToVisibleBounds = false
- minimumInteritemSpacing = 0
- minimumLineSpacing = 1
- self.scrollDirection = .vertical
- self.sectionInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
- }
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- override var itemSize: CGSize {
- get {
- if let collectionView = collectionView {
- let itemWidth: CGFloat = collectionView.frame.width
- return CGSize(width: itemWidth, height: self.itemHeight)
- }
- return CGSize(width: 100, height: 100)
- }
- set {
- super.itemSize = newValue
- }
- }
- override func targetContentOffset(forProposedContentOffset proposedContentOffset: CGPoint) -> CGPoint {
- return proposedContentOffset
- }
- }
|