Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
folium-app
GitHub Repository: folium-app/Folium
Path: blob/a-new-beginning/Folium-iOS/Classes/Cells/FileCell.swift
2 views
//
//  FileCell.swift
//  Folium-iOS
//
//  Created by Jarrod Norwell on 15/7/2025.
//

import FilesManager
import UIKit

class FileCell : BlurredCollectionViewCell {
    var label: UILabel? = nil,
        secondaryLabel: UILabel? = nil,
        tertiaryLabel: UILabel? = nil
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        clipsToBounds = true
        layer.cornerCurve = .continuous
        
        tertiaryLabel = .init()
        guard let tertiaryLabel else {
            return
        }
        tertiaryLabel.translatesAutoresizingMaskIntoConstraints = false
        tertiaryLabel.font = .bold(.body)
        visualEffectView.contentView.addSubview(tertiaryLabel)
        
        tertiaryLabel.centerYAnchor.constraint(equalTo: visualEffectView.contentView.safeAreaLayoutGuide.centerYAnchor).isActive = true
        tertiaryLabel.trailingAnchor.constraint(equalTo: visualEffectView.contentView.safeAreaLayoutGuide.trailingAnchor, constant: -20).isActive = true
        
        secondaryLabel = .init()
        guard let secondaryLabel else {
            return
        }
        secondaryLabel.translatesAutoresizingMaskIntoConstraints = false
        secondaryLabel.font = .preferredFont(forTextStyle: .body)
        secondaryLabel.textColor = .secondaryLabel
        visualEffectView.contentView.addSubview(secondaryLabel)
        
        secondaryLabel.centerYAnchor.constraint(equalTo: visualEffectView.contentView.safeAreaLayoutGuide.centerYAnchor).isActive = true
        secondaryLabel.trailingAnchor.constraint(equalTo: tertiaryLabel.safeAreaLayoutGuide.leadingAnchor, constant: -8).isActive = true
        
        label = .init()
        guard let label else {
            return
        }
        label.translatesAutoresizingMaskIntoConstraints = false
        label.font = .bold(.body)
        if #available(iOS 18, *) {
            label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
            label.setContentHuggingPriority(.defaultLow, for: .horizontal)
            label.setContentHuggingPriority(.required, for: .vertical)
            label.setContentCompressionResistancePriority(.required, for: .vertical)
            label.adjustsFontForContentSizeCategory = true
            label.setValuesForKeys([
                "marqueeEnabled" : true,
                "marqueeRunning" : true,
                "marqueeRepeatCount" : 0,
                "marqueeLoopPadding" : 40,
                "marqueeUpdatable" : true
            ])
        }
        visualEffectView.contentView.addSubview(label)
        
        label.centerYAnchor.constraint(equalTo: visualEffectView.contentView.safeAreaLayoutGuide.centerYAnchor).isActive = true
        label.leadingAnchor.constraint(equalTo: visualEffectView.contentView.safeAreaLayoutGuide.leadingAnchor, constant: 20).isActive = true
        label.trailingAnchor.constraint(lessThanOrEqualTo: secondaryLabel.safeAreaLayoutGuide.leadingAnchor, constant: -20).isActive = true
        
        heightAnchor.constraint(equalToConstant: 50).isActive = true
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        layer.cornerRadius = .largeCornerRadius + 8
    }
    
    func set(file: File) {
        guard let label, let secondaryLabel, let tertiaryLabel else {
            return
        }
        
        label.text = file.nameWithoutExtension
        secondaryLabel.text = file.`extension`.uppercased()
        tertiaryLabel.text = file.core
    }
}