-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathUIView+AssociatedObjects.swift
94 lines (78 loc) · 3.85 KB
/
UIView+AssociatedObjects.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
//
// Copyright SkeletonView. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// UIView+AssociatedObjects.swift
//
// Created by Juanpe Catalán on 18/8/21.
import UIKit
// codebeat:disable[TOO_MANY_IVARS]
enum ViewAssociatedKeys {
static var skeletonable: StaticString = "skeletonable"
static var hiddenWhenSkeletonIsActive: StaticString = "hiddenWhenSkeletonIsActive"
static var status: StaticString = "status"
static var skeletonLayer: StaticString = "layer"
static var flowDelegate: StaticString = "flowDelegate"
static var isSkeletonAnimated: StaticString = "isSkeletonAnimated"
static var viewState: StaticString = "viewState"
static var labelViewState: StaticString = "labelViewState"
static var imageViewState: StaticString = "imageViewState"
static var buttonViewState: StaticString = "buttonViewState"
static var headerFooterViewState: StaticString = "headerFooterViewState"
static var currentSkeletonConfig: StaticString = "currentSkeletonConfig"
static var skeletonCornerRadius: StaticString = "skeletonCornerRadius"
static var disabledWhenSkeletonIsActive: StaticString = "disabledWhenSkeletonIsActive"
static var delayedShowSkeletonWorkItem: StaticString = "delayedShowSkeletonWorkItem"
}
// codebeat:enable[TOO_MANY_IVARS]
extension UIView {
enum SkeletonStatus {
case on
case off
}
var _flowDelegate: SkeletonFlowDelegate? {
get { return ao_get(pkey: &ViewAssociatedKeys.flowDelegate) as? SkeletonFlowDelegate }
set { ao_setOptional(newValue, pkey: &ViewAssociatedKeys.flowDelegate) }
}
var _skeletonLayer: SkeletonLayer? {
get { return ao_get(pkey: &ViewAssociatedKeys.skeletonLayer) as? SkeletonLayer }
set { ao_setOptional(newValue, pkey: &ViewAssociatedKeys.skeletonLayer) }
}
var _currentSkeletonConfig: SkeletonConfig? {
get { return ao_get(pkey: &ViewAssociatedKeys.currentSkeletonConfig) as? SkeletonConfig }
set { ao_setOptional(newValue, pkey: &ViewAssociatedKeys.currentSkeletonConfig) }
}
var _status: SkeletonStatus {
get { return ao_get(pkey: &ViewAssociatedKeys.status) as? SkeletonStatus ?? .off }
set { ao_set(newValue, pkey: &ViewAssociatedKeys.status) }
}
var _isSkeletonAnimated: Bool {
get { return ao_get(pkey: &ViewAssociatedKeys.isSkeletonAnimated) as? Bool ?? false }
set { ao_set(newValue, pkey: &ViewAssociatedKeys.isSkeletonAnimated) }
}
var _delayedShowSkeletonWorkItem: DispatchWorkItem? {
get { return ao_get(pkey: &ViewAssociatedKeys.delayedShowSkeletonWorkItem) as? DispatchWorkItem }
set { ao_setOptional(newValue, pkey: &ViewAssociatedKeys.delayedShowSkeletonWorkItem) }
}
var _skeletonable: Bool {
get { return ao_get(pkey: &ViewAssociatedKeys.skeletonable) as? Bool ?? false }
set { ao_set(newValue, pkey: &ViewAssociatedKeys.skeletonable) }
}
var _hiddenWhenSkeletonIsActive: Bool {
get { return ao_get(pkey: &ViewAssociatedKeys.hiddenWhenSkeletonIsActive) as? Bool ?? false }
set { ao_set(newValue, pkey: &ViewAssociatedKeys.hiddenWhenSkeletonIsActive) }
}
var _disabledWhenSkeletonIsActive: Bool {
get { return ao_get(pkey: &ViewAssociatedKeys.disabledWhenSkeletonIsActive) as? Bool ?? true }
set { ao_set(newValue, pkey: &ViewAssociatedKeys.disabledWhenSkeletonIsActive) }
}
var _skeletonableCornerRadius: Float {
get { return ao_get(pkey: &ViewAssociatedKeys.skeletonCornerRadius) as? Float ?? SkeletonViewAppearance.shared.skeletonCornerRadius }
set { ao_set(newValue, pkey: &ViewAssociatedKeys.skeletonCornerRadius) }
}
}