Skip to content
Snippets Groups Projects
AvatarView.swift 2.49 KiB
Newer Older
Bruno Muniz's avatar
Bruno Muniz committed
import UIKit

public final class AvatarView: UIView {
    public enum Size {
        case small
        case medium
        case large
    }
Bruno Muniz's avatar
Bruno Muniz committed

    let imageView = UIImageView()
    let monogramLabel = UILabel()
    let iconImageView = UIImageView()
Bruno Muniz's avatar
Bruno Muniz committed

    public init() {
        super.init(frame: .zero)

        layer.masksToBounds = true
        backgroundColor = Asset.brandPrimary.color
Bruno Muniz's avatar
Bruno Muniz committed

Ahmed Shehata's avatar
Ahmed Shehata committed
        iconImageView.contentMode = .center
        imageView.contentMode = .scaleAspectFill
        monogramLabel.textColor = Asset.neutralWhite.color

        addSubview(monogramLabel)
Ahmed Shehata's avatar
Ahmed Shehata committed
        addSubview(iconImageView)
        addSubview(imageView)

        imageView.snp.makeConstraints {
            $0.edges.equalToSuperview()
        }

        monogramLabel.snp.makeConstraints {
            $0.center.equalToSuperview()
        }

        iconImageView.snp.makeConstraints {
            $0.center.equalToSuperview()
        }
Bruno Muniz's avatar
Bruno Muniz committed
    }

    required init?(coder: NSCoder) { nil }

Bruno Muniz's avatar
Bruno Muniz committed
    public func prepareForReuse() {
        imageView.image = nil
        monogramLabel.text = nil
        iconImageView.image = nil
Bruno Muniz's avatar
Bruno Muniz committed
    }

    public func setupProfile(title: String, image: Data?, size: AvatarView.Size) {
        iconImageView.image = nil
        monogramLabel.text = title
            .trimmingCharacters(in: .whitespacesAndNewlines)
            .replacingOccurrences(of: " ", with: "")
            .prefix(2)
            .uppercased()

        monogramLabel.text = "\(title.prefix(2))".uppercased()
Bruno Muniz's avatar
Bruno Muniz committed

        // TODO: What are the font sizes and corner radius for small/medium avatars?
Bruno Muniz's avatar
Bruno Muniz committed

        switch size {
        case .small:
            layer.cornerRadius = 13.0
            monogramLabel.font = Fonts.Mulish.semiBold.font(size: 14.0)
        case .medium:
            layer.cornerRadius = 13.0
            monogramLabel.font = Fonts.Mulish.semiBold.font(size: 14.0)
        case .large:
            layer.cornerRadius = 18.0
            monogramLabel.font = Fonts.Mulish.semiBold.font(size: 16.0)
        }
Bruno Muniz's avatar
Bruno Muniz committed

        guard let image = image else {
            imageView.image = nil
            return
        }
Bruno Muniz's avatar
Bruno Muniz committed

        imageView.image = UIImage(data: image)
Bruno Muniz's avatar
Bruno Muniz committed
    }

    public func setupGroup(size: AvatarView.Size) {
        switch size {
        case .small:
            layer.cornerRadius = 13.0
        case .medium:
            layer.cornerRadius = 13.0
        case .large:
            layer.cornerRadius = 18.0
Bruno Muniz's avatar
Bruno Muniz committed
        }

        imageView.image = nil
        monogramLabel.text = nil
        iconImageView.image = Asset.sharedGroup.image
Bruno Muniz's avatar
Bruno Muniz committed
    }
}