Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Example/KNContactsViewer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 52;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -255,7 +255,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
MTL_FAST_MATH = YES;
ONLY_ACTIVE_ARCH = YES;
Expand Down Expand Up @@ -309,7 +309,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
MTL_ENABLE_DEBUG_INFO = NO;
MTL_FAST_MATH = YES;
SDKROOT = iphoneos;
Expand All @@ -326,7 +326,7 @@
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = 8UXEK92PP3;
INFOPLIST_FILE = KNContactsViewer/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand All @@ -345,7 +345,7 @@
CODE_SIGN_STYLE = Automatic;
DEVELOPMENT_TEAM = 8UXEK92PP3;
INFOPLIST_FILE = KNContactsViewer/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
6 changes: 5 additions & 1 deletion Example/KNContactsViewer/CellImageTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ class CellImageTableViewController: UITableViewController {

if indexPath.section == 0 {
let option = cellImageOptions[indexPath.row]
cell.imageView?.image = UIImage(systemName: imageSource[option]!)!
if #available(iOS 13.0, *) {
cell.imageView?.image = UIImage(systemName: imageSource[option]!)!
} else {
// Fallback on earlier versions
}
cell.textLabel?.text = titleSource[option]!
}
else if indexPath.section == 1 {
Expand Down
68 changes: 35 additions & 33 deletions Sources/KNContactsPicker/KNContactCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
#if canImport(UIKit)
import UIKit

class KNContactCell: UITableViewCell {
public class KNContactCell: UITableViewCell {
public private(set) var contactModel: KNContactCellModel?
private var disabled: Bool = false

private var initialColor: UIColor {
get {
if #available(iOS 13.0, *) {
Expand All @@ -22,7 +23,7 @@ class KNContactCell: UITableViewCell {
return UIColor.white
}
}

private let profileImageView: UIImageView = {
let img = UIImageView()
let bounds = CGRect(x: 0, y: 0, width: 40, height: 40)
Expand All @@ -33,14 +34,14 @@ class KNContactCell: UITableViewCell {
width: 40,
height: img.frame.size.height )
img.contentMode = .scaleAspectFill

// enable autolayout
img.translatesAutoresizingMaskIntoConstraints = false
img.layer.cornerRadius = 20
img.clipsToBounds = true
return img
}()

private let nameLabel: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 16)
Expand All @@ -51,7 +52,7 @@ class KNContactCell: UITableViewCell {
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()

private let subtitleLabel: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 11)
Expand All @@ -62,66 +63,66 @@ class KNContactCell: UITableViewCell {
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
private let containerView: UIStackView = {

public let containerView: UIStackView = {
let view = UIStackView()
view.translatesAutoresizingMaskIntoConstraints = false
view.axis = .vertical
view.alignment = .fill
view.distribution = .fillProportionally
view.spacing = 0

return view
}()

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.contentView.addSubview(profileImageView)
self.containerView.addArrangedSubview(nameLabel)

self.contentView.addSubview(containerView)

profileImageView.centerYAnchor.constraint(equalTo: self.contentView.centerYAnchor).isActive = true
profileImageView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 10).isActive = true
profileImageView.widthAnchor.constraint(equalToConstant: 40).isActive = true
profileImageView.heightAnchor.constraint(equalToConstant: 40).isActive = true

containerView.centerYAnchor.constraint(equalTo: self.contentView.centerYAnchor).isActive = true
containerView.leadingAnchor.constraint(equalTo: self.profileImageView.trailingAnchor, constant: 10).isActive = true
containerView.trailingAnchor.constraint(equalTo: self.contentView.trailingAnchor, constant: -10).isActive = true

nameLabel.adjustsFontSizeToFitWidth = true
nameLabel.minimumScaleFactor = CGFloat(0.7)
nameLabel.font = UIFont.boldSystemFont(ofSize: nameLabel.font.pointSize)
nameLabel.leadingAnchor.constraint(equalTo: self.containerView.leadingAnchor).isActive = true
nameLabel.trailingAnchor.constraint(equalTo: self.containerView.trailingAnchor).isActive = true


self.selectionStyle = .none
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func prepareForReuse() {

public override func prepareForReuse() {
super.prepareForReuse()
self.disabled = false
}

override func setSelected(_ selected: Bool, animated: Bool) {
public override func setSelected(_ selected: Bool, animated: Bool) {
if (!disabled) {
super.setSelected(selected, animated: animated)
self.setAppropriateStyle()
}
}

func setAppropriateStyle() {
self.isSelected ? self.setCellToSelectedStyle() : self.setCellToUnselectedStyle()
self.layoutIfNeeded()
self.layoutSubviews()
}

func setCellToSelectedStyle() {
self.accessoryView?.backgroundColor = UIColor.systemBlue
self.contentView.backgroundColor = UIColor.systemBlue
Expand All @@ -132,27 +133,27 @@ class KNContactCell: UITableViewCell {
self.nameLabel.textColor = UIColor.white
self.subtitleLabel.textColor = UIColor.lightText
}

func setCellToUnselectedStyle() {
self.backgroundColor = initialColor
self.accessoryView?.backgroundColor = UIColor.clear
self.accessoryType = UITableViewCell.AccessoryType.none
self.contentView.backgroundColor = initialColor

self.nameLabel.textColor = UIColor.black
self.subtitleLabel.textColor = UIColor.lightText
if #available(iOS 13.0, *) {
self.nameLabel.textColor = UIColor.label
self.subtitleLabel.textColor = UIColor.secondaryLabel
}

}

func setDisabled(disabled: Bool) {
self.disabled = disabled

self.isUserInteractionEnabled = !self.disabled

if (self.disabled) {
self.nameLabel.textColor = UIColor.lightGray
if #available(iOS 13.0, *) {
Expand All @@ -166,23 +167,24 @@ class KNContactCell: UITableViewCell {
}
}
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {

public override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
guard let previous = previousTraitCollection else { return }

if traitCollection.userInterfaceStyle != previous.userInterfaceStyle {
self.setAppropriateStyle()
}
}

public func set(contactModel: KNContactCellModel) {
self.contactModel = contactModel
self.nameLabel.text = contactModel.getName()
self.subtitleLabel.text = contactModel.getSubtitle()

let image = contactModel.getImage(with: self.profileImageView.bounds, scaled: self.profileImageView.shouldScale)
self.profileImageView.image = image
self.profileImageView.highlightedImage = image

if !self.subtitleLabel.text!.isEmpty {
self.containerView.addArrangedSubview(subtitleLabel)
subtitleLabel.topAnchor.constraint(equalTo: self.nameLabel.bottomAnchor, constant: 0).isActive = true
Expand Down
24 changes: 12 additions & 12 deletions Sources/KNContactsPicker/KNContactCellModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,26 @@
import Contacts
import UIKit

struct KNContactCellModel {
private var contact: CNContact
public struct KNContactCellModel {
public private(set) var contact: CNContact
private var settings: KNPickerSettings
private var formatter: CNContactFormatter

init(contact: CNContact, settings: KNPickerSettings, formatter: CNContactFormatter) {
self.contact = contact
self.settings = settings
self.formatter = formatter
}

func getName() -> String {
return contact.getFullName(using: formatter)
}

func getImage(with bounds: CGRect, scaled: Bool) -> UIImage? {
let initialsBgColors = settings.contactInitialsBackgroundColor
let userProvidedImage = settings.contactCellUserProvidedImage
let order = settings.contactCellImageOptionOrder

var image: UIImage?
for item in order {
if image == nil {
Expand All @@ -46,11 +46,11 @@ struct KNContactCellModel {
break
}
}

return image

}

func getSubtitle() -> String {
let subtitleOption = settings.subtitleDisplayInfo
switch subtitleOption {
Expand All @@ -64,14 +64,14 @@ struct KNContactCellModel {
return getFirstEmailAddress()
}
}

private func getFirstPhoneNumber() -> String {
return self.contact.phoneNumbers.compactMap { String($0.value.stringValue) }.first ?? ""
}

private func getFirstEmailAddress() -> String {
return self.contact.emailAddresses.compactMap { String($0.value) }.first ?? ""
}

}
#endif
Loading