forked from SwipeCellKit/SwipeCellKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShared.swift
More file actions
130 lines (112 loc) · 4.4 KB
/
Copy pathShared.swift
File metadata and controls
130 lines (112 loc) · 4.4 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//
// Shared.swift
// MailExample
//
// Created by Jeremy Koch
// Copyright © 2017 Jeremy Koch. All rights reserved.
//
import UIKit
class IndicatorView: UIView {
var color = UIColor.clear {
didSet { setNeedsDisplay() }
}
override func draw(_ rect: CGRect) {
color.set()
UIBezierPath(ovalIn: rect).fill()
}
}
enum ActionDescriptor {
case read, unread, more, flag, trash
func title(forDisplayMode displayMode: ButtonDisplayMode) -> String? {
guard displayMode != .imageOnly else { return nil }
switch self {
case .read: return "Read"
case .unread: return "Unread"
case .more: return "More"
case .flag: return "Flag"
case .trash: return "Trash"
}
}
func image(forStyle style: ButtonStyle, displayMode: ButtonDisplayMode) -> UIImage? {
guard displayMode != .titleOnly else { return nil }
let name: String
switch self {
case .read: name = "Read"
case .unread: name = "Unread"
case .more: name = "More"
case .flag: name = "Flag"
case .trash: name = "Trash"
}
#if canImport(Combine)
if #available(iOS 13.0, *) {
let name: String
switch self {
case .read: name = "envelope.open.fill"
case .unread: name = "envelope.badge.fill"
case .more: name = "ellipsis.circle.fill"
case .flag: name = "flag.fill"
case .trash: name = "trash.fill"
}
if style == .backgroundColor {
let config = UIImage.SymbolConfiguration(pointSize: 23.0, weight: .regular)
return UIImage(systemName: name, withConfiguration: config)
} else {
let config = UIImage.SymbolConfiguration(pointSize: 22.0, weight: .regular)
let image = UIImage(systemName: name, withConfiguration: config)?.withTintColor(.white, renderingMode: .alwaysTemplate)
return circularIcon(with: color(forStyle: style), size: CGSize(width: 50, height: 50), icon: image)
}
} else {
return UIImage(named: style == .backgroundColor ? name : name + "-circle")
}
#else
return UIImage(named: style == .backgroundColor ? name : name + "-circle")
#endif
}
func color(forStyle style: ButtonStyle) -> UIColor {
#if canImport(Combine)
switch self {
case .read, .unread: return UIColor.systemBlue
case .more:
if #available(iOS 13.0, *) {
if UITraitCollection.current.userInterfaceStyle == .dark {
return UIColor.systemGray
}
return style == .backgroundColor ? UIColor.systemGray3 : UIColor.systemGray2
} else {
return #colorLiteral(red: 0.7803494334, green: 0.7761332393, blue: 0.7967314124, alpha: 1)
}
case .flag: return UIColor.systemOrange
case .trash: return UIColor.systemRed
}
#else
switch self {
case .read, .unread: return #colorLiteral(red: 0, green: 0.4577052593, blue: 1, alpha: 1)
case .more: return #colorLiteral(red: 0.7803494334, green: 0.7761332393, blue: 0.7967314124, alpha: 1)
case .flag: return #colorLiteral(red: 1, green: 0.5803921569, blue: 0, alpha: 1)
case .trash: return #colorLiteral(red: 1, green: 0.2352941176, blue: 0.1882352941, alpha: 1)
}
#endif
}
func circularIcon(with color: UIColor, size: CGSize, icon: UIImage? = nil) -> UIImage? {
let rect = CGRect(origin: .zero, size: size)
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
UIBezierPath(ovalIn: rect).addClip()
color.setFill()
UIRectFill(rect)
if let icon = icon {
let iconRect = CGRect(x: (rect.size.width - icon.size.width) / 2,
y: (rect.size.height - icon.size.height) / 2,
width: icon.size.width,
height: icon.size.height)
icon.draw(in: iconRect, blendMode: .normal, alpha: 1.0)
}
defer { UIGraphicsEndImageContext() }
return UIGraphicsGetImageFromCurrentImageContext()
}
}
enum ButtonDisplayMode {
case titleAndImage, titleOnly, imageOnly
}
enum ButtonStyle {
case backgroundColor, circular
}