Skip to content

Commit

Permalink
💄 :: [#496] LogHistoryViewController
Browse files Browse the repository at this point in the history
  • Loading branch information
baekteun committed Apr 23, 2024
1 parent baef00c commit da290d0
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 7 deletions.
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
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
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
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

0 comments on commit da290d0

Please sign in to comment.