-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomeTableViewController.swift
More file actions
100 lines (81 loc) · 3.12 KB
/
HomeTableViewController.swift
File metadata and controls
100 lines (81 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// HomeTableViewController.swift
// SideMenuControllerExample
//
// Created by JE on 15.07.19.
// Copyright © 2019 JE. All rights reserved.
//
import UIKit
import JESideMenuController
final class HomeTableViewController: UIViewController {
private struct Constants: Sendable {
static let identifier = String(describing: UITableViewCell.self)
}
nonisolated
private struct Item: Hashable, Sendable {
let text: String
let hasImage: Bool
private let identifier = UUID()
static func == (lhs: Item, rhs: Item) -> Bool {
lhs.identifier == rhs.identifier
}
func hash(into hasher: inout Hasher) {
hasher.combine(identifier)
}
}
// MARK: - Private Properties
private lazy var tableView: UITableView = {
let tableView = UITableView()
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.allowsSelection = false
tableView.separatorInset = .zero
return tableView
}()
private var dataSource: UITableViewDiffableDataSource<Int, Item>?
// MARK: - ViewController Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
setupView()
setupDataSource()
try? loadData()
}
// MARK: - Private Methods
private func setupView() {
view.addSubview(tableView)
tableView.register(UITableViewCell.self, forCellReuseIdentifier: Constants.identifier)
let action = UIAction { [weak self] _ in
self?.sideMenuController?.toggle()
}
let bbi = UIBarButtonItem(
image: UIImage(systemName: "line.3.horizontal"),
primaryAction: action
)
bbi.tintColor = .label
navigationItem.leftBarButtonItem = bbi
NSLayoutConstraint.activate([
tableView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
tableView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
tableView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
tableView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor)
])
}
private func setupDataSource() {
dataSource = UITableViewDiffableDataSource<Int, Item>(tableView: tableView) { tableView, indexPath, item in
let cell = tableView.dequeueReusableCell(withIdentifier: Constants.identifier, for: indexPath)
var configuration = cell.messageConfiguration()
configuration.text = item.text
configuration.hasImage = item.hasImage
cell.contentConfiguration = configuration
return cell
}
}
private func loadData() throws {
let dataLoader = DataLoader()
let data: [Message] = try dataLoader.loadData()
let items = data.map { Item(text: $0.text, hasImage: $0.hasImage ?? false) }
var snapshot = NSDiffableDataSourceSnapshot<Int, Item>()
snapshot.appendSections([0])
snapshot.appendItems(items)
dataSource?.apply(snapshot, animatingDifferences: false)
}
}