From 52ffdd519637b6a88003ea6425edbd5452de352c Mon Sep 17 00:00:00 2001 From: Filipp Kosenko Date: Tue, 3 Sep 2024 17:11:05 +0300 Subject: [PATCH] Initial commit --- SimpleDropdown.podspec | 20 + SimpleDropdown.xcodeproj/project.pbxproj | 373 ++++++++++++++++++ .../contents.xcworkspacedata | 7 + .../xcshareddata/IDEWorkspaceChecks.plist | 8 + SimpleDropdown/SimpleDropdown.h | 17 + Source/SimpleDropdown.swift | 193 +++++++++ 6 files changed, 618 insertions(+) create mode 100644 SimpleDropdown.podspec create mode 100644 SimpleDropdown.xcodeproj/project.pbxproj create mode 100644 SimpleDropdown.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 SimpleDropdown.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist create mode 100644 SimpleDropdown/SimpleDropdown.h create mode 100644 Source/SimpleDropdown.swift diff --git a/SimpleDropdown.podspec b/SimpleDropdown.podspec new file mode 100644 index 0000000..ada7c4f --- /dev/null +++ b/SimpleDropdown.podspec @@ -0,0 +1,20 @@ +Pod::Spec.new do |s| + s.name = "SimpleDropdown" + s.version = "1.0.0" + s.swift_version = "5.5" + s.summary = "Simple dropdown view" + s.description = "А simple dropdown that can be easily integrated into your app layout and customize to suit your design." + s.homepage = "https://github.com/idapgroup/SimpleDropdown" + s.license = { :type => "New BSD", :file => "LICENSE" } + s.author = { "IDAP Group" => "hello@idapgroup.com" } + s.source = { :git => "https://github.com/idapgroup/SimpleDropdown.git", + :tag => s.version.to_s } + + # Platform setup + s.requires_arc = true + s.ios.deployment_target = '15.0' + + # Preserve the layout of headers in the Module directory + s.header_mappings_dir = 'Source' + s.source_files = 'Source/**/*.{swift,h,m,c,cpp}' +end diff --git a/SimpleDropdown.xcodeproj/project.pbxproj b/SimpleDropdown.xcodeproj/project.pbxproj new file mode 100644 index 0000000..282cca5 --- /dev/null +++ b/SimpleDropdown.xcodeproj/project.pbxproj @@ -0,0 +1,373 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 56; + objects = { + +/* Begin PBXBuildFile section */ + 22D8824F2C874C1F002C6127 /* SimpleDropdown.h in Headers */ = {isa = PBXBuildFile; fileRef = 22D8824E2C874C1F002C6127 /* SimpleDropdown.h */; settings = {ATTRIBUTES = (Public, ); }; }; + 22D882592C874D54002C6127 /* SimpleDropdown.swift in Sources */ = {isa = PBXBuildFile; fileRef = 22D882582C874D54002C6127 /* SimpleDropdown.swift */; }; + 22D8825B2C874E21002C6127 /* SimpleDropdown.podspec in Resources */ = {isa = PBXBuildFile; fileRef = 22D8825A2C874E21002C6127 /* SimpleDropdown.podspec */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + 22D8824B2C874C1F002C6127 /* SimpleDropdown.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SimpleDropdown.framework; sourceTree = BUILT_PRODUCTS_DIR; }; + 22D8824E2C874C1F002C6127 /* SimpleDropdown.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SimpleDropdown.h; sourceTree = ""; }; + 22D882582C874D54002C6127 /* SimpleDropdown.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SimpleDropdown.swift; sourceTree = ""; }; + 22D8825A2C874E21002C6127 /* SimpleDropdown.podspec */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = SimpleDropdown.podspec; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + 22D882482C874C1F002C6127 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + 22D882412C874C1F002C6127 = { + isa = PBXGroup; + children = ( + 22D882552C874C8D002C6127 /* Source */, + 22D8825A2C874E21002C6127 /* SimpleDropdown.podspec */, + 22D8824D2C874C1F002C6127 /* SimpleDropdown */, + 22D8824C2C874C1F002C6127 /* Products */, + ); + sourceTree = ""; + }; + 22D8824C2C874C1F002C6127 /* Products */ = { + isa = PBXGroup; + children = ( + 22D8824B2C874C1F002C6127 /* SimpleDropdown.framework */, + ); + name = Products; + sourceTree = ""; + }; + 22D8824D2C874C1F002C6127 /* SimpleDropdown */ = { + isa = PBXGroup; + children = ( + 22D8824E2C874C1F002C6127 /* SimpleDropdown.h */, + ); + path = SimpleDropdown; + sourceTree = ""; + }; + 22D882552C874C8D002C6127 /* Source */ = { + isa = PBXGroup; + children = ( + 22D882582C874D54002C6127 /* SimpleDropdown.swift */, + ); + path = Source; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXHeadersBuildPhase section */ + 22D882462C874C1F002C6127 /* Headers */ = { + isa = PBXHeadersBuildPhase; + buildActionMask = 2147483647; + files = ( + 22D8824F2C874C1F002C6127 /* SimpleDropdown.h in Headers */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXHeadersBuildPhase section */ + +/* Begin PBXNativeTarget section */ + 22D8824A2C874C1F002C6127 /* SimpleDropdown */ = { + isa = PBXNativeTarget; + buildConfigurationList = 22D882522C874C1F002C6127 /* Build configuration list for PBXNativeTarget "SimpleDropdown" */; + buildPhases = ( + 22D882462C874C1F002C6127 /* Headers */, + 22D882472C874C1F002C6127 /* Sources */, + 22D882482C874C1F002C6127 /* Frameworks */, + 22D882492C874C1F002C6127 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = SimpleDropdown; + productName = SimpleDropdown; + productReference = 22D8824B2C874C1F002C6127 /* SimpleDropdown.framework */; + productType = "com.apple.product-type.framework"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 22D882422C874C1F002C6127 /* Project object */ = { + isa = PBXProject; + attributes = { + BuildIndependentTargetsInParallel = 1; + LastUpgradeCheck = 1540; + TargetAttributes = { + 22D8824A2C874C1F002C6127 = { + CreatedOnToolsVersion = 15.4; + LastSwiftMigration = 1540; + }; + }; + }; + buildConfigurationList = 22D882452C874C1F002C6127 /* Build configuration list for PBXProject "SimpleDropdown" */; + compatibilityVersion = "Xcode 14.0"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = 22D882412C874C1F002C6127; + productRefGroup = 22D8824C2C874C1F002C6127 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 22D8824A2C874C1F002C6127 /* SimpleDropdown */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + 22D882492C874C1F002C6127 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 22D8825B2C874E21002C6127 /* SimpleDropdown.podspec in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + 22D882472C874C1F002C6127 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 22D882592C874D54002C6127 /* SimpleDropdown.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 22D882502C874C1F002C6127 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + CURRENT_PROJECT_VERSION = 1; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + ENABLE_USER_SCRIPT_SANDBOXING = YES; + GCC_C_LANGUAGE_STANDARD = gnu17; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 17.5; + LOCALIZATION_PREFERS_STRING_CATALOGS = YES; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)"; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + VERSIONING_SYSTEM = "apple-generic"; + VERSION_INFO_PREFIX = ""; + }; + name = Debug; + }; + 22D882512C874C1F002C6127 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + ASSETCATALOG_COMPILER_GENERATE_SWIFT_ASSET_SYMBOL_EXTENSIONS = YES; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + CURRENT_PROJECT_VERSION = 1; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_USER_SCRIPT_SANDBOXING = YES; + GCC_C_LANGUAGE_STANDARD = gnu17; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 17.5; + LOCALIZATION_PREFERS_STRING_CATALOGS = YES; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + SDKROOT = iphoneos; + SWIFT_COMPILATION_MODE = wholemodule; + VALIDATE_PRODUCT = YES; + VERSIONING_SYSTEM = "apple-generic"; + VERSION_INFO_PREFIX = ""; + }; + name = Release; + }; + 22D882532C874C1F002C6127 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + BUILD_LIBRARY_FOR_DISTRIBUTION = YES; + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + CURRENT_PROJECT_VERSION = 1; + DEFINES_MODULE = YES; + DYLIB_COMPATIBILITY_VERSION = 1; + DYLIB_CURRENT_VERSION = 1; + DYLIB_INSTALL_NAME_BASE = "@rpath"; + ENABLE_MODULE_VERIFIER = YES; + GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_KEY_NSHumanReadableCopyright = ""; + INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; + IPHONEOS_DEPLOYMENT_TARGET = 15.0; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + "@loader_path/Frameworks", + ); + MARKETING_VERSION = 1.0; + MODULE_VERIFIER_SUPPORTED_LANGUAGES = "objective-c objective-c++"; + MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu17 gnu++20"; + PRODUCT_BUNDLE_IDENTIFIER = idap.SimpleDropdown; + PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; + SKIP_INSTALL = YES; + SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_INSTALL_OBJC_HEADER = NO; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; + 22D882542C874C1F002C6127 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + BUILD_LIBRARY_FOR_DISTRIBUTION = YES; + CLANG_ENABLE_MODULES = YES; + CODE_SIGN_STYLE = Automatic; + CURRENT_PROJECT_VERSION = 1; + DEFINES_MODULE = YES; + DYLIB_COMPATIBILITY_VERSION = 1; + DYLIB_CURRENT_VERSION = 1; + DYLIB_INSTALL_NAME_BASE = "@rpath"; + ENABLE_MODULE_VERIFIER = YES; + GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_KEY_NSHumanReadableCopyright = ""; + INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; + IPHONEOS_DEPLOYMENT_TARGET = 15.0; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + "@loader_path/Frameworks", + ); + MARKETING_VERSION = 1.0; + MODULE_VERIFIER_SUPPORTED_LANGUAGES = "objective-c objective-c++"; + MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu17 gnu++20"; + PRODUCT_BUNDLE_IDENTIFIER = idap.SimpleDropdown; + PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; + SKIP_INSTALL = YES; + SWIFT_EMIT_LOC_STRINGS = YES; + SWIFT_INSTALL_OBJC_HEADER = NO; + SWIFT_VERSION = 5.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 22D882452C874C1F002C6127 /* Build configuration list for PBXProject "SimpleDropdown" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 22D882502C874C1F002C6127 /* Debug */, + 22D882512C874C1F002C6127 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + 22D882522C874C1F002C6127 /* Build configuration list for PBXNativeTarget "SimpleDropdown" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 22D882532C874C1F002C6127 /* Debug */, + 22D882542C874C1F002C6127 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = 22D882422C874C1F002C6127 /* Project object */; +} diff --git a/SimpleDropdown.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/SimpleDropdown.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/SimpleDropdown.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/SimpleDropdown.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/SimpleDropdown.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/SimpleDropdown.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/SimpleDropdown/SimpleDropdown.h b/SimpleDropdown/SimpleDropdown.h new file mode 100644 index 0000000..0a25949 --- /dev/null +++ b/SimpleDropdown/SimpleDropdown.h @@ -0,0 +1,17 @@ +// +// SimpleDropdown.h +// +// Created by Filipp Kosenko on 03.09.2024. +// + +#import + +//! Project version number for SimpleDropdown. +FOUNDATION_EXPORT double SimpleDropdownVersionNumber; + +//! Project version string for SimpleDropdown. +FOUNDATION_EXPORT const unsigned char SimpleDropdownVersionString[]; + +// In this header, you should import all the public headers of your framework using statements like #import + + diff --git a/Source/SimpleDropdown.swift b/Source/SimpleDropdown.swift new file mode 100644 index 0000000..c71aeb8 --- /dev/null +++ b/Source/SimpleDropdown.swift @@ -0,0 +1,193 @@ +// +// SimpleDropdown.swift +// +// Created by Filipp Kosenko on 03.09.2024. +// Copyright © 2024 IDAP. All rights reserved. +// + +import UIKit + +class DropdownView: UIView, UITableViewDelegate, UITableViewDataSource { + + // MARK: - + // MARK: Variables + + public var headerConfigurator: ((UIView, String) -> Void)? + public var cellConfigurator: ((UITableViewCell, String) -> Void)? + + private var options: [String] = [] + private var defaultViewPlaceHolder: String + private var buttonColor: UIColor = .white + private var currentOption: String { + didSet { + if let configurator = self.headerConfigurator { + configurator(self.customHeaderView ?? self.button, self.currentOption) + } + } + } + private var customHeaderView: UIView? + private var customCellType: UITableViewCell.Type? + private var customCellIdentifier: String? + private var tableViewHeight: CGFloat? + private var isDropdownOpen = false + + private let button = UIButton(type: .system) + private let tableView = UITableView() + + // MARK: - + // MARK: Init + + public init(options: [String], defaultViewPlaceHolder: String? = "Click to select", tableViewHeight: CGFloat? = nil, customCellType: UITableViewCell.Type? = nil, customCellIdentifier: String? = nil, customHeaderView: UIView? = nil) { + self.defaultViewPlaceHolder = defaultViewPlaceHolder ?? "Click to select" + self.currentOption = self.defaultViewPlaceHolder + self.customCellType = customCellType + self.customCellIdentifier = customCellIdentifier + self.customHeaderView = customHeaderView + self.tableViewHeight = tableViewHeight + + super.init(frame: CGRect()) + + self.options = options + self.prepareView() + } + + required init?(coder: NSCoder) { + self.defaultViewPlaceHolder = "Click to select" + self.currentOption = self.defaultViewPlaceHolder + self.tableViewHeight = self.tableViewHeight ?? nil + + super.init(coder: coder) + + self.prepareView() + } + + // MARK: - + // MARK: Public + + public func updateCurrentOption(option: String) { + self.currentOption = option + } + + // MARK: - + // MARK: Private + + private func prepareView() { + if let customHeaderView = self.customHeaderView { + self.prepareCustomHeader(view: customHeaderView) + } else { + self.prepareDefaultHeader() + } + self.prepareTableView() + } + + private func prepareCustomHeader(view: UIView) { + addSubview(view) + view.translatesAutoresizingMaskIntoConstraints = false + + let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.toggleDropdown)) + view.addGestureRecognizer(tapGesture) + + NSLayoutConstraint.activate([ + view.leadingAnchor.constraint(equalTo: self.leadingAnchor), + view.trailingAnchor.constraint(equalTo: self.trailingAnchor), + view.topAnchor.constraint(equalTo: self.topAnchor), + view.heightAnchor.constraint(equalToConstant: 44) + ]) + } + + private func prepareDefaultHeader() { + self.button.setTitle(self.defaultViewPlaceHolder, for: .normal) + self.button.backgroundColor = self.buttonColor + self.button.addTarget(self, action: #selector(self.toggleDropdown), for: .touchUpInside) + addSubview(self.button) + + self.button.translatesAutoresizingMaskIntoConstraints = false + NSLayoutConstraint.activate([ + self.button.leadingAnchor.constraint(equalTo: self.leadingAnchor), + self.button.trailingAnchor.constraint(equalTo: self.trailingAnchor), + self.button.topAnchor.constraint(equalTo: self.topAnchor), + self.button.heightAnchor.constraint(equalToConstant: 44) + ]) + } + + private func prepareTableView() { + self.tableView.delegate = self + self.tableView.dataSource = self + self.tableView.isHidden = true + self.tableView.layer.borderWidth = 1 + self.tableView.layer.borderColor = UIColor.lightGray.cgColor + + if let customCellType = self.customCellType, let customCellIdentifier = self.customCellIdentifier { + self.tableView.register(customCellType, forCellReuseIdentifier: customCellIdentifier) + } else { + self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "defaultCell") + } + } + + @objc private func toggleDropdown() { + self.isDropdownOpen.toggle() + + if self.isDropdownOpen { + self.showDropdown() + } else { + self.hideDropdown() + } + } + + private func showDropdown() { + guard let window = self.window else { return } + + let buttonFrame = self.convert(self.bounds, to: window) + let tableHeight = self.tableViewHeight ?? CGFloat(min(self.options.count, 5)) * 44.0 + + self.tableView.frame = CGRect( + x: buttonFrame.origin.x, + y: buttonFrame.origin.y + buttonFrame.height, + width: buttonFrame.width, + height: tableHeight + ) + window.addSubview(self.tableView) + self.tableView.isHidden = false + self.tableView.reloadData() + } + + private func hideDropdown() { + self.tableView.removeFromSuperview() + } + + // MARK: - + // MARK: UITableViewDataSource + + func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { + return self.options.count + } + + func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { + let identifier = self.customCellIdentifier ?? "defaultCell" + let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath) + + let option = self.options[indexPath.row] + if let configurator = self.cellConfigurator { + configurator(cell, option) + } else { + cell.textLabel?.text = option + } + + return cell + } + + // MARK: - + // MARK: UITableViewDelegate + + func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { + let selectedOption = self.options[indexPath.row] + if let configurator = self.headerConfigurator { + configurator(self.customHeaderView ?? self.button, selectedOption) + } else { + self.button.setTitle(selectedOption, for: .normal) + } + self.currentOption = selectedOption + + self.toggleDropdown() + } +}