refactor: Migarate active subscription and description to code

This commit is contained in:
Andrea Busi
2024-06-14 15:05:16 +02:00
parent d8612e33a3
commit b0d1cde42b
5 changed files with 85 additions and 170 deletions
@@ -1156,8 +1156,8 @@
children = (
DC3BA11024D1A9C90062EE7F /* SubscriptionsViewController.swift */,
DCC23DEB24D281CE003A2404 /* SubscriptionsActiveTableViewCell.swift */,
DCBB267924D1E7F500F04559 /* SubscriptionsHeaderTableViewCell.swift */,
65AD23CD261B03D400E3B57C /* SubscriptionsDescriptionTableViewCell.swift */,
DCBB267924D1E7F500F04559 /* SubscriptionsHeaderTableViewCell.swift */,
DCBB267D24D1EA2000F04559 /* SubscriptionProductTableViewCell.swift */,
DCBB267F24D1ECE200F04559 /* SubscriptionDetailViewController.swift */,
);
@@ -9,34 +9,60 @@
import UIKit
import StoreKit
class SubscriptionsActiveTableViewCell: EQNBaseTableViewCell {
var product: SKProduct? {
didSet {
updateUI()
}
}
class SubscriptionsActiveTableViewCell: EQNBaseContainerTableViewCell {
override var headerText: String { NSLocalizedString("inapp_active", comment: "") }
// MARK: - UI
private lazy var noSubscriptionsLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = AppTheme.shared.cardTextColor
label.font = .preferredFont(forTextStyle: .body)
label.textAlignment = .center
label.numberOfLines = 0
return label
}()
@IBOutlet private weak var headerLabel: UILabel!
@IBOutlet private weak var noSubscriptionsLabel: UILabel!
@IBOutlet private weak var activeSubscriptionImageView: UIImageView!
private lazy var activeSubscriptionImageView: UIImageView = {
let imageView = UIImageView(frame: .zero)
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.contentMode = .scaleAspectFit
return imageView
}()
// MARK: - Internal
// MARK: - View Lifecycle
override func awakeFromNib() {
super.awakeFromNib()
override func setupUI() {
super.setupUI()
localizeUI()
let stackView = UIStackView(arrangedSubviews: [ activeSubscriptionImageView, noSubscriptionsLabel ])
stackView.translatesAutoresizingMaskIntoConstraints = false
stackView.alignment = .center
stackView.distribution = .equalSpacing
stackView.axis = .vertical
containerView.addSubview(stackView)
activeSubscriptionImageView.widthAnchor.constraint(equalToConstant: 150.0).isActive = true
activeSubscriptionImageView.heightAnchor.constraint(equalToConstant: 50.0).isActive = true
stackView.topAnchor.constraint(equalTo: topView.bottomAnchor, constant: Self.DefaultVerticalSpacing).isActive = true
stackView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: Self.DefaultPadding).isActive = true
stackView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -Self.DefaultPadding).isActive = true
stackView.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: -Self.DefaultVerticalSpacing).isActive = true
}
// MARK: - Private
private func localizeUI() {
headerLabel.text = NSLocalizedString("inapp_active", comment: "")
override func updateUI() {
super.updateUI()
noSubscriptionsLabel.text = NSLocalizedString("inapp_nosub", comment: "")
}
private func updateUI() {
// MARK: - Public
func update(with product: SKProduct?) {
if let productIdentifier = product?.productIdentifier {
noSubscriptionsLabel.isHidden = true
activeSubscriptionImageView.isHidden = false
@@ -8,21 +8,40 @@
import UIKit
class SubscriptionsDescriptionTableViewCell: EQNBaseTableViewCell {
@IBOutlet private weak var headerLabel: UILabel!
@IBOutlet private weak var descriptionLabel: UILabel!
class SubscriptionsDescriptionTableViewCell: EQNBaseContainerTableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
override var headerText: String { NSLocalizedString("inapp_list", comment: "") }
// MARK: - UI
private lazy var descriptionLabel: UILabel = {
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.textColor = AppTheme.shared.cardTextColor
label.font = .preferredFont(forTextStyle: .body)
label.textAlignment = .center
label.numberOfLines = 0
label.textAlignment = .justified
return label
}()
// MARK: - Internal
override func setupUI() {
super.setupUI()
localizeUI()
containerView.addSubview(descriptionLabel)
descriptionLabel.topAnchor.constraint(equalTo: topView.bottomAnchor, constant: Self.DefaultVerticalSpacing).isActive = true
descriptionLabel.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: Self.DefaultPadding).isActive = true
descriptionLabel.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: -Self.DefaultPadding).isActive = true
descriptionLabel.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: -Self.DefaultVerticalSpacing).isActive = true
}
// MARK: - Private
private func localizeUI() {
headerLabel.text = NSLocalizedString("inapp_list", comment: "")
override func updateUI() {
super.updateUI()
descriptionLabel.text = NSLocalizedString("inapp_description", comment: "")
}
}
@@ -14,7 +14,6 @@ import Shogun
class SubscriptionsViewController: UITableViewController {
private static let SegueIdentifierSubscriptionDetail = "ShowSubscriptionDetail"
private static let CellHeightDescription: CGFloat = 320.0
// sezioni
private enum TableSection: CaseIterable {
@@ -103,7 +102,9 @@ class SubscriptionsViewController: UITableViewController {
}
tableView.rowHeight = UITableView.automaticDimension
tableView.estimatedRowHeight = Self.CellHeightDescription;
tableView.estimatedRowHeight = 600.0
tableView.registerCell(for: SubscriptionsActiveTableViewCell.self)
tableView.registerCell(for: SubscriptionsDescriptionTableViewCell.self)
}
private func updateUI() {
@@ -256,15 +257,6 @@ class SubscriptionsViewController: UITableViewController {
}
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
let tableSection = sections[indexPath.section]
if tableSection == .description {
// autolayout in description doesn't work 🤷
return Self.CellHeightDescription
}
return UITableView.automaticDimension
}
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
let tableSection = sections[indexPath.section]
if tableSection == .active || tableSection == .description {
@@ -295,12 +287,14 @@ class SubscriptionsViewController: UITableViewController {
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let tableSection = sections[indexPath.section]
if tableSection == .active {
let cell = tableView.dequeueReusableCell(withIdentifier: "ActiveSubscriptionsCell", for: indexPath) as! SubscriptionsActiveTableViewCell
cell.product = subscribedProduct
let cell = tableView.dequeueReusableCell(cellIdentifiable: SubscriptionsActiveTableViewCell.self, for: indexPath)
cell.selectionStyle = .none
cell.update(with: subscribedProduct)
return cell
}
if tableSection == .description {
let cell = tableView.dequeueReusableCell(withIdentifier: "DescriptionCell", for: indexPath) as! SubscriptionsDescriptionTableViewCell
let cell = tableView.dequeueReusableCell(cellIdentifiable: SubscriptionsDescriptionTableViewCell.self, for: indexPath)
cell.selectionStyle = .none
return cell
}
@@ -923,129 +923,8 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" systemColor="systemGroupedBackgroundColor"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="none" indentationWidth="10" reuseIdentifier="ActiveSubscriptionsCell" rowHeight="140" id="MQJ-yM-iDt" customClass="SubscriptionsActiveTableViewCell" customModule="Earthquake_Network" customModuleProvider="target">
<rect key="frame" x="0.0" y="50" width="414" height="140"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="MQJ-yM-iDt" id="POW-zi-4UN">
<rect key="frame" x="0.0" y="0.0" width="414" height="140"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Qua-Xe-GWL">
<rect key="frame" x="8" y="8" width="398" height="124"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Active subscriptions" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="c9L-RP-VQh" customClass="EQNEdgeInsetLabel" customModule="Earthquake_Network" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="158.5" height="26"/>
<color key="backgroundColor" name="Light blue"/>
<constraints>
<constraint firstAttribute="height" constant="26" id="ErK-An-J5r"/>
</constraints>
<fontDescription key="fontDescription" type="system" weight="medium" pointSize="17"/>
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
</label>
<stackView opaque="NO" contentMode="scaleToFill" axis="vertical" alignment="center" spacing="4" translatesAutoresizingMaskIntoConstraints="NO" id="g2D-09-dmu">
<rect key="frame" x="48" y="34" width="322" height="62"/>
<subviews>
<imageView hidden="YES" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" image="top_10k" translatesAutoresizingMaskIntoConstraints="NO" id="ZQI-Db-Pb4">
<rect key="frame" x="96" y="0.0" width="130" height="50"/>
<constraints>
<constraint firstAttribute="width" constant="130" id="fZ7-02-Ezz"/>
<constraint firstAttribute="height" constant="50" id="qsx-s4-NiD"/>
</constraints>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="No active subscriptions" textAlignment="center" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="axa-WN-rP3">
<rect key="frame" x="72" y="0.0" width="178.5" height="62"/>
<fontDescription key="fontDescription" style="UICTFontTextStyleBody"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
</stackView>
</subviews>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<constraints>
<constraint firstAttribute="trailing" secondItem="c9L-RP-VQh" secondAttribute="trailing" priority="250" constant="255.5" id="14i-ZH-FKD"/>
<constraint firstItem="g2D-09-dmu" firstAttribute="centerX" secondItem="Qua-Xe-GWL" secondAttribute="centerX" constant="10" id="AN3-jv-Jda"/>
<constraint firstItem="g2D-09-dmu" firstAttribute="top" secondItem="c9L-RP-VQh" secondAttribute="bottom" constant="8" id="IwZ-uA-q9y"/>
<constraint firstAttribute="bottomMargin" secondItem="g2D-09-dmu" secondAttribute="bottom" constant="20" id="asX-RQ-r3l"/>
<constraint firstItem="c9L-RP-VQh" firstAttribute="leading" secondItem="Qua-Xe-GWL" secondAttribute="leading" id="fHZ-h9-ypi"/>
<constraint firstItem="g2D-09-dmu" firstAttribute="leading" secondItem="Qua-Xe-GWL" secondAttribute="leadingMargin" constant="40" id="hJl-t8-8gM"/>
<constraint firstItem="c9L-RP-VQh" firstAttribute="top" secondItem="Qua-Xe-GWL" secondAttribute="top" id="qMw-jv-AFa"/>
<constraint firstAttribute="trailingMargin" secondItem="g2D-09-dmu" secondAttribute="trailing" constant="20" id="xp5-qM-pn7"/>
</constraints>
</view>
</subviews>
<constraints>
<constraint firstAttribute="bottom" secondItem="Qua-Xe-GWL" secondAttribute="bottom" constant="8" id="2dd-ZU-S09"/>
<constraint firstItem="Qua-Xe-GWL" firstAttribute="leading" secondItem="POW-zi-4UN" secondAttribute="leading" constant="8" id="IPJ-On-GUz"/>
<constraint firstAttribute="trailing" secondItem="Qua-Xe-GWL" secondAttribute="trailing" constant="8" id="pIw-hF-VaG"/>
<constraint firstItem="Qua-Xe-GWL" firstAttribute="top" secondItem="POW-zi-4UN" secondAttribute="top" constant="8" id="yNm-sR-xl1"/>
</constraints>
</tableViewCellContentView>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<connections>
<outlet property="activeSubscriptionImageView" destination="ZQI-Db-Pb4" id="bf8-ua-1AW"/>
<outlet property="containerView" destination="Qua-Xe-GWL" id="0KS-N4-2AR"/>
<outlet property="headerLabel" destination="c9L-RP-VQh" id="ZLx-CA-yeJ"/>
<outlet property="noSubscriptionsLabel" destination="axa-WN-rP3" id="4yQ-qa-GLH"/>
</connections>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="none" indentationWidth="10" reuseIdentifier="DescriptionCell" rowHeight="400" id="5xb-5o-0fS" customClass="SubscriptionsDescriptionTableViewCell" customModule="Earthquake_Network" customModuleProvider="target">
<rect key="frame" x="0.0" y="190" width="414" height="400"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="5xb-5o-0fS" id="gqq-Lx-ead">
<rect key="frame" x="0.0" y="0.0" width="414" height="400"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="kCf-AE-JEH">
<rect key="frame" x="8" y="8" width="398" height="384"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Priority service" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="ZXY-1S-WBT" customClass="EQNEdgeInsetLabel" customModule="Earthquake_Network" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="117.5" height="26"/>
<color key="backgroundColor" name="Light blue"/>
<constraints>
<constraint firstAttribute="height" constant="26" id="3bq-w5-2GF"/>
</constraints>
<fontDescription key="fontDescription" type="system" weight="medium" pointSize="17"/>
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" textAlignment="justified" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="BNz-EW-lXI">
<rect key="frame" x="16" y="34" width="366" height="342"/>
<string key="text">Un'allerta è inviata agli utenti dell'app ogni volta che un sisma è rilevato in tempo reale. Allertare tutti può richiedere fino a 30 secondi in quanto non è tecnicamente fattibile farlo istantaneamente. Ora puoi entrare a far parte delle liste di priorità delle prime 10'000 o 100'000 persone allertate. L'ordine di allerta è il seguente: prima tutti gli utenti con servizio TOP 10K, successivamente tutti gli utenti con servizio TOP 100K ed infine tutti gli altri utenti. A parità di servizio, l'ordine di allerta si basa sulla distanza dall'epicentro.</string>
<fontDescription key="fontDescription" style="UICTFontTextStyleCallout"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<constraints>
<constraint firstAttribute="trailing" secondItem="ZXY-1S-WBT" secondAttribute="trailing" priority="250" constant="297" id="7tO-mq-Pwc"/>
<constraint firstAttribute="trailingMargin" secondItem="BNz-EW-lXI" secondAttribute="trailing" constant="8" id="CFv-s3-cyE"/>
<constraint firstItem="BNz-EW-lXI" firstAttribute="top" secondItem="ZXY-1S-WBT" secondAttribute="bottom" constant="8" id="Coo-ku-J4a"/>
<constraint firstItem="BNz-EW-lXI" firstAttribute="leading" secondItem="kCf-AE-JEH" secondAttribute="leadingMargin" constant="8" id="PIQ-wu-11u"/>
<constraint firstItem="ZXY-1S-WBT" firstAttribute="top" secondItem="kCf-AE-JEH" secondAttribute="top" id="cG1-7Y-qjf"/>
<constraint firstAttribute="bottom" secondItem="BNz-EW-lXI" secondAttribute="bottom" constant="8" id="okG-4A-tj3"/>
<constraint firstItem="ZXY-1S-WBT" firstAttribute="leading" secondItem="kCf-AE-JEH" secondAttribute="leading" id="z3e-uS-566"/>
</constraints>
</view>
</subviews>
<constraints>
<constraint firstAttribute="bottom" secondItem="kCf-AE-JEH" secondAttribute="bottom" constant="8" id="GzR-SY-AP4"/>
<constraint firstItem="kCf-AE-JEH" firstAttribute="leading" secondItem="gqq-Lx-ead" secondAttribute="leading" constant="8" id="Ik0-zJ-lf3"/>
<constraint firstAttribute="trailing" secondItem="kCf-AE-JEH" secondAttribute="trailing" constant="8" id="fQU-k1-G2e"/>
<constraint firstItem="kCf-AE-JEH" firstAttribute="top" secondItem="gqq-Lx-ead" secondAttribute="top" constant="8" id="q22-Yy-szu"/>
</constraints>
</tableViewCellContentView>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<connections>
<outlet property="containerView" destination="kCf-AE-JEH" id="2fg-BQ-t5L"/>
<outlet property="descriptionLabel" destination="BNz-EW-lXI" id="bjA-4M-6Ej"/>
<outlet property="headerLabel" destination="ZXY-1S-WBT" id="0g5-nu-fz8"/>
</connections>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="none" indentationWidth="10" reuseIdentifier="SectionHeaderCell" id="urG-ON-XcB" customClass="SubscriptionsHeaderTableViewCell" customModule="Earthquake_Network" customModuleProvider="target">
<rect key="frame" x="0.0" y="590" width="414" height="44"/>
<rect key="frame" x="0.0" y="50" width="414" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="urG-ON-XcB" id="SrE-iI-Nig">
<rect key="frame" x="0.0" y="0.0" width="414" height="44"/>
@@ -1083,7 +962,7 @@
</connections>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" accessoryType="disclosureIndicator" indentationWidth="10" reuseIdentifier="SubscriptionCell" rowHeight="140" id="ltf-er-wHX" customClass="SubscriptionProductTableViewCell" customModule="Earthquake_Network" customModuleProvider="target">
<rect key="frame" x="0.0" y="634" width="414" height="140"/>
<rect key="frame" x="0.0" y="94" width="414" height="140"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" preservesSuperviewLayoutMargins="YES" insetsLayoutMarginsFromSafeArea="NO" tableViewCell="ltf-er-wHX" id="ohD-ot-UaH">
<rect key="frame" x="0.0" y="0.0" width="383.5" height="140"/>
@@ -1677,9 +1556,6 @@ Sisma rilevato da 10 smartphone</string>
<namedColor name="Gray (dark)">
<color red="0.10999999940395355" green="0.13300000131130219" blue="0.14900000393390656" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</namedColor>
<namedColor name="Light blue">
<color red="0.50599998235702515" green="0.75300002098083496" blue="0.98000001907348633" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</namedColor>
<namedColor name="Mercalli 100">
<color red="0.79199999570846558" green="0.15299999713897705" blue="0.14900000393390656" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</namedColor>