-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
💄 :: [#496] LogHistoryViewController
- Loading branch information
Showing
4 changed files
with
170 additions
and
7 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
Projects/Modules/LogManager/Sources/LogHistory/LogHistoryCell.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#if DEBUG | ||
import UIKit | ||
|
||
final class LogHistoryCell: UITableViewCell { | ||
static let reuseIdentifier = String(describing: LogHistoryCell.self) | ||
|
||
private let logStackView: UIStackView = { | ||
let stackView = UIStackView() | ||
stackView.axis = .vertical | ||
stackView.spacing = 8 | ||
stackView.alignment = .leading | ||
stackView.translatesAutoresizingMaskIntoConstraints = false | ||
return stackView | ||
}() | ||
|
||
private let logTitleLabel: UILabel = { | ||
let label = UILabel() | ||
label.font = .systemFont(ofSize: 20) | ||
label.numberOfLines = 0 | ||
return label | ||
}() | ||
|
||
private let logParametersLabel: UILabel = { | ||
let label = UILabel() | ||
label.font = .systemFont(ofSize: 16) | ||
label.textColor = .gray | ||
label.numberOfLines = 0 | ||
return label | ||
}() | ||
|
||
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { | ||
super.init(style: style, reuseIdentifier: reuseIdentifier) | ||
addView() | ||
setLayout() | ||
} | ||
|
||
@available(*, unavailable) | ||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
func configure(log: any AnalyticsLogType) { | ||
logTitleLabel.text = log.name | ||
logParametersLabel.text = log.params | ||
.filter { !($0.key == "date" || $0.key == "timestamp") } | ||
.map { "- \($0) : \($1)" } | ||
.joined(separator: "\n") | ||
} | ||
} | ||
|
||
private extension LogHistoryCell { | ||
func addView() { | ||
contentView.addSubview(logStackView) | ||
logStackView.addArrangedSubview(logTitleLabel) | ||
logStackView.addArrangedSubview(logParametersLabel) | ||
} | ||
|
||
func setLayout() { | ||
NSLayoutConstraint.activate([ | ||
logStackView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 8), | ||
logStackView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor), | ||
logStackView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor), | ||
logStackView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -8) | ||
]) | ||
} | ||
} | ||
|
||
#endif |
17 changes: 17 additions & 0 deletions
17
Projects/Modules/LogManager/Sources/LogHistory/LogHistorySectionItem.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#if DEBUG | ||
import Foundation | ||
|
||
struct LogHistorySectionItem: Hashable, Equatable { | ||
let index: Int | ||
let log: any AnalyticsLogType | ||
|
||
func hash(into hasher: inout Hasher) { | ||
hasher.combine(index) | ||
hasher.combine(log.name) | ||
} | ||
|
||
static func == (lhs: LogHistorySectionItem, rhs: LogHistorySectionItem) -> Bool { | ||
lhs.index == rhs.index && lhs.log.name == rhs.log.name | ||
} | ||
} | ||
#endif |
14 changes: 7 additions & 7 deletions
14
Projects/Modules/LogManager/Sources/LogHistory/LogHistoryStorage.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
#if DEBUG | ||
import Foundation | ||
import Foundation | ||
|
||
final class LogHistoryStorage { | ||
static let shared = LogHistoryStorage() | ||
final class LogHistoryStorage { | ||
static let shared = LogHistoryStorage() | ||
|
||
private(set) var logHistory: [any AnalyticsLogType] = [] | ||
private(set) var logHistory: [any AnalyticsLogType] = [] | ||
|
||
func appendHistory(log: any AnalyticsLogType) { | ||
logHistory.append(log) | ||
func appendHistory(log: any AnalyticsLogType) { | ||
logHistory.append(log) | ||
} | ||
} | ||
} | ||
|
||
#endif |
78 changes: 78 additions & 0 deletions
78
Projects/Modules/LogManager/Sources/LogHistory/LogHistoryViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#if DEBUG | ||
import UIKit | ||
|
||
public final class LogHistoryViewController: UIViewController { | ||
private let logHistoryTableView: UITableView = { | ||
let tableView = UITableView() | ||
tableView.separatorInset = .init(top: 8, left: 0, bottom: 8, right: 0) | ||
tableView.register(LogHistoryCell.self, forCellReuseIdentifier: LogHistoryCell.reuseIdentifier) | ||
return tableView | ||
}() | ||
|
||
private lazy var logHistoryTableViewDiffableDataSource = UITableViewDiffableDataSource< | ||
Int, | ||
LogHistorySectionItem | ||
>( | ||
tableView: logHistoryTableView | ||
) { tableView, indexPath, itemIdentifier in | ||
guard let cell = tableView.dequeueReusableCell( | ||
withIdentifier: LogHistoryCell.reuseIdentifier, | ||
for: indexPath | ||
) as? LogHistoryCell | ||
else { | ||
return UITableViewCell() | ||
} | ||
cell.configure(log: itemIdentifier.log) | ||
return cell | ||
} | ||
|
||
public init() { | ||
super.init(nibName: nil, bundle: nil) | ||
} | ||
|
||
@available(*, unavailable) | ||
required init?(coder: NSCoder) { | ||
fatalError("init(coder:) has not been implemented") | ||
} | ||
|
||
override public func viewDidLoad() { | ||
super.viewDidLoad() | ||
addView() | ||
setLayout() | ||
view.backgroundColor = .white | ||
|
||
let logs = LogHistoryStorage.shared.logHistory | ||
.enumerated() | ||
.map { LogHistorySectionItem(index: $0.offset, log: $0.element) } | ||
|
||
var snapshot = logHistoryTableViewDiffableDataSource.snapshot() | ||
snapshot.appendSections([0]) | ||
snapshot.appendItems(logs, toSection: 0) | ||
|
||
logHistoryTableViewDiffableDataSource.apply(snapshot, animatingDifferences: true) | ||
} | ||
} | ||
|
||
private extension LogHistoryViewController { | ||
func addView() { | ||
view.addSubview(logHistoryTableView) | ||
logHistoryTableView.translatesAutoresizingMaskIntoConstraints = false | ||
} | ||
|
||
func setLayout() { | ||
NSLayoutConstraint.activate([ | ||
logHistoryTableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 16), | ||
logHistoryTableView.leadingAnchor.constraint( | ||
equalTo: view.safeAreaLayoutGuide.leadingAnchor, | ||
constant: 16 | ||
), | ||
logHistoryTableView.trailingAnchor.constraint( | ||
equalTo: view.safeAreaLayoutGuide.trailingAnchor, | ||
constant: -16 | ||
), | ||
logHistoryTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -16) | ||
]) | ||
} | ||
} | ||
|
||
#endif |