-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMeasurer.swift
99 lines (89 loc) · 3.53 KB
/
Measurer.swift
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
//
// Arranger.swift
// FastAdapter
//
// Created by Fabian Terhorst on 18.07.18.
// Copyright © 2018 everHome. All rights reserved.
//
open class Measurer<Itm: Item> {
public weak var fastAdapter: FastAdapter<Itm>?
public let interceptor: (CGFloat?, CGFloat?, ListView?) -> (CGFloat?, CGFloat?, Bool)
public init(interceptor: ((CGFloat?, CGFloat?, ListView?) -> (CGFloat?, CGFloat?, Bool))? = nil) {
self.interceptor = interceptor ?? MeasurerDefaults.defaultInterceptor
}
open func measureItems(items: [Itm]) -> [Itm] {
var measuredItems = [Itm]()
for item in items {
if measureItem(item: item) {
measuredItems.append(item)
}
}
return measuredItems
}
open func measureItem(item: Itm) -> Bool {
guard let listView = fastAdapter?.listView else {
return false
}
return measureItem(item: item, width: listView.frame.width, height: listView.frame.height)
}
open func measureItems(items: [Itm], width: CGFloat?, height: CGFloat?) -> [Itm] {
var arrangedItems = [Itm]()
for item in items {
if measureItem(item: item, width: width, height: height) {
arrangedItems.append(item)
}
}
return arrangedItems
}
open func measureItem(item: Itm, width: CGFloat?, height: CGFloat?) -> Bool {
if let fastAdapter = fastAdapter as? FastAdapter<Item> {
item.fastAdapter = fastAdapter
}
let (interceptedWidth, interceptedHeight, keep) = interceptor(width, height, self.fastAdapter?.listView)
if !keep {
return false
}
return item.onMeasure(width: interceptedWidth, height: interceptedHeight)
}
open func renew() {
if let listView = fastAdapter?.listView {
let frame = listView.frame
if let sections = fastAdapter?.adapter?.itemList.sections {
for section in sections {
if let header = section.header {
let _ = measureItem(item: header, width: frame.width, height: frame.height)
}
for item in section.items {
let _ = measureItem(item: item, width: frame.width, height: frame.height)
}
if let footer = section.footer {
let _ = measureItem(item: footer, width: frame.width, height: frame.height)
}
}
fastAdapter?.listView?.reloadData()
}
}
}
open func renew(width: CGFloat?, height: CGFloat?) {
if let sections = fastAdapter?.adapter?.itemList.sections {
for section in sections {
if let header = section.header {
let _ = measureItem(item: header, width: width, height: height)
}
for item in section.items {
let _ = measureItem(item: item, width: width, height: height)
}
if let footer = section.footer {
let _ = measureItem(item: footer, width: width, height: height)
}
}
fastAdapter?.listView?.reloadData()
}
}
}
class MeasurerDefaults {
public static let defaultInterceptor = {
(width: CGFloat?, height: CGFloat?, listView: ListView?) -> (CGFloat?, CGFloat?, Bool) in
return (width, nil, (width == nil && height == nil) ? false : true)
}
}