92 lines
3.7 KiB
Swift
92 lines
3.7 KiB
Swift
//
|
|
// SubscriptionProductTableViewCell.swift
|
|
// Earthquake Network
|
|
//
|
|
// Created by Busi Andrea on 29/07/2020.
|
|
// Copyright © 2020 Earthquake Network. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import StoreKit
|
|
|
|
class SubscriptionProductTableViewCell: EQNBaseContainerTableViewCell {
|
|
|
|
override var isHeaderVisible: Bool { false }
|
|
override var isRightArrowVisbile: Bool { true }
|
|
|
|
// MARK: - UI
|
|
|
|
private lazy var productImageView: UIImageView = {
|
|
let imageView = UIImageView(frame: .zero)
|
|
imageView.translatesAutoresizingMaskIntoConstraints = false
|
|
imageView.contentMode = .scaleAspectFit
|
|
return imageView
|
|
}()
|
|
|
|
private lazy var productTitleLabel: UILabel = {
|
|
let label = UILabel()
|
|
label.translatesAutoresizingMaskIntoConstraints = false
|
|
label.textColor = AppTheme.shared.cardTextColor
|
|
label.font = .preferredFont(forTextStyle: .headline)
|
|
label.numberOfLines = 0
|
|
return label
|
|
}()
|
|
|
|
private lazy var productInfoLabel: UILabel = {
|
|
let label = UILabel()
|
|
label.translatesAutoresizingMaskIntoConstraints = false
|
|
label.textColor = AppTheme.Colors.red
|
|
label.font = .preferredFont(forTextStyle: .body)
|
|
label.numberOfLines = 0
|
|
return label
|
|
}()
|
|
|
|
// MARK: - Internal
|
|
|
|
override func setupUI() {
|
|
super.setupUI()
|
|
|
|
containerView.addSubview(productImageView)
|
|
containerView.addSubview(productTitleLabel)
|
|
containerView.addSubview(productInfoLabel)
|
|
|
|
productImageView.widthAnchor.constraint(equalToConstant: 80.0).isActive = true
|
|
productImageView.heightAnchor.constraint(equalToConstant: 40.0).isActive = true
|
|
|
|
productTitleLabel.topAnchor.constraint(equalTo: topView.bottomAnchor, constant: .cardVerticalSpacing).isActive = true
|
|
productTitleLabel.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: .cardPadding.negative).isActive = true
|
|
productImageView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: .cardPadding).isActive = true
|
|
productImageView.trailingAnchor.constraint(equalTo: productTitleLabel.leadingAnchor, constant: .cardPadding.negative).isActive = true
|
|
productImageView.centerYAnchor.constraint(equalTo: productTitleLabel.centerYAnchor).isActive = true
|
|
|
|
productInfoLabel.topAnchor.constraint(equalTo: productTitleLabel.bottomAnchor, constant: .cardVerticalSpacing).isActive = true
|
|
productInfoLabel.leadingAnchor.constraint(equalTo: productImageView.leadingAnchor).isActive = true
|
|
productInfoLabel.trailingAnchor.constraint(equalTo: productTitleLabel.trailingAnchor).isActive = true
|
|
productInfoLabel.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: .cardVerticalSpacing.negative).isActive = true
|
|
}
|
|
|
|
// MARK: - Public
|
|
|
|
func update(
|
|
category: EQNInAppProducts.Category,
|
|
availability: EQNPurchaseAvailability?
|
|
) {
|
|
productImageView.image = category.image
|
|
productTitleLabel.text = category.localizedTitle
|
|
|
|
let infoKey = category == .top100k ? "inapp_available_100k" : "inapp_available_10k"
|
|
let counter = availabilityCounter(for: category, availability: availability)
|
|
productInfoLabel.text = String(format: NSLocalizedString(infoKey, comment: ""), counter)
|
|
}
|
|
|
|
private func availabilityCounter(
|
|
for category: EQNInAppProducts.Category,
|
|
availability: EQNPurchaseAvailability?
|
|
) -> Int {
|
|
if category == .top100k {
|
|
return availability?.top100kAvailable ?? 0
|
|
}
|
|
return availability?.top10kAvailable ?? 0
|
|
}
|
|
}
|