[ios] feat: implement the ColorPicker class

Signed-off-by: Kiryl Kaveryn <kirylkaveryn@gmail.com>
This commit is contained in:
Kiryl Kaveryn 2024-02-19 22:35:52 +04:00 committed by Alexander Borsuk
parent 187556248f
commit aa74bd0e03
2 changed files with 90 additions and 2 deletions

View file

@ -466,8 +466,10 @@
ED1080A72B791CFE0023F27E /* SocialMediaCollectionViewHeader.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED1080A62B791CFE0023F27E /* SocialMediaCollectionViewHeader.swift */; };
ED1263AB2B6F99F900AD99F3 /* UIView+AddSeparator.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED1263AA2B6F99F900AD99F3 /* UIView+AddSeparator.swift */; };
ED3EAC202B03C88100220A4A /* BottomTabBarButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED3EAC1F2B03C88100220A4A /* BottomTabBarButton.swift */; };
ED9966802B94FBC20083CE55 /* ColorPicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = ED99667D2B94FBC20083CE55 /* ColorPicker.swift */; };
EDBD68072B625724005DD151 /* LocationServicesDisabledAlert.xib in Resources */ = {isa = PBXBuildFile; fileRef = EDBD68062B625724005DD151 /* LocationServicesDisabledAlert.xib */; };
EDBD680B2B62572E005DD151 /* LocationServicesDisabledAlert.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDBD680A2B62572E005DD151 /* LocationServicesDisabledAlert.swift */; };
EDC3573B2B7B5029001AE9CA /* CALayer+SetCorner.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDC3573A2B7B5029001AE9CA /* CALayer+SetCorner.swift */; };
EDE243DD2B6D2E640057369B /* AboutController.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDE243D52B6CF3980057369B /* AboutController.swift */; };
EDE243E52B6D3F400057369B /* OSMView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDE243E42B6D3F400057369B /* OSMView.swift */; };
EDE243E72B6D55610057369B /* InfoView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDE243E02B6D3EA00057369B /* InfoView.swift */; };
@ -477,7 +479,6 @@
EDFDFB4C2B722C9C0013A44C /* InfoTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDFDFB4B2B722C9C0013A44C /* InfoTableViewCell.swift */; };
EDFDFB522B726F1A0013A44C /* ButtonsStackView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDFDFB512B726F1A0013A44C /* ButtonsStackView.swift */; };
EDFDFB612B74E2500013A44C /* DonationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDFDFB602B74E2500013A44C /* DonationView.swift */; };
EDC3573B2B7B5029001AE9CA /* CALayer+SetCorner.swift in Sources */ = {isa = PBXBuildFile; fileRef = EDC3573A2B7B5029001AE9CA /* CALayer+SetCorner.swift */; };
F607C1881C032A8800B53A87 /* resources-hdpi_clear in Resources */ = {isa = PBXBuildFile; fileRef = F607C1831C032A8800B53A87 /* resources-hdpi_clear */; };
F607C18A1C032A8800B53A87 /* resources-hdpi_dark in Resources */ = {isa = PBXBuildFile; fileRef = F607C1841C032A8800B53A87 /* resources-hdpi_dark */; };
F623DA6C1C9C2731006A3436 /* opening_hours_how_to_edit.html in Resources */ = {isa = PBXBuildFile; fileRef = F623DA6A1C9C2731006A3436 /* opening_hours_how_to_edit.html */; };
@ -1344,8 +1345,10 @@
ED3EAC1F2B03C88100220A4A /* BottomTabBarButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BottomTabBarButton.swift; sourceTree = "<group>"; };
ED48BBB817C2B1E2003E7E92 /* CircleView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CircleView.h; sourceTree = "<group>"; };
ED48BBB917C2B1E2003E7E92 /* CircleView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CircleView.m; sourceTree = "<group>"; };
ED99667D2B94FBC20083CE55 /* ColorPicker.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ColorPicker.swift; sourceTree = "<group>"; };
EDBD68062B625724005DD151 /* LocationServicesDisabledAlert.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = LocationServicesDisabledAlert.xib; sourceTree = "<group>"; };
EDBD680A2B62572E005DD151 /* LocationServicesDisabledAlert.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationServicesDisabledAlert.swift; sourceTree = "<group>"; };
EDC3573A2B7B5029001AE9CA /* CALayer+SetCorner.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CALayer+SetCorner.swift"; sourceTree = "<group>"; };
EDE243D52B6CF3980057369B /* AboutController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AboutController.swift; sourceTree = "<group>"; };
EDE243E02B6D3EA00057369B /* InfoView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoView.swift; sourceTree = "<group>"; };
EDE243E42B6D3F400057369B /* OSMView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OSMView.swift; sourceTree = "<group>"; };
@ -1355,7 +1358,6 @@
EDFDFB4B2B722C9C0013A44C /* InfoTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoTableViewCell.swift; sourceTree = "<group>"; };
EDFDFB512B726F1A0013A44C /* ButtonsStackView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonsStackView.swift; sourceTree = "<group>"; };
EDFDFB602B74E2500013A44C /* DonationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DonationView.swift; sourceTree = "<group>"; };
EDC3573A2B7B5029001AE9CA /* CALayer+SetCorner.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CALayer+SetCorner.swift"; sourceTree = "<group>"; };
EE026F0511D6AC0D00645242 /* classificator.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = classificator.txt; path = ../../data/classificator.txt; sourceTree = SOURCE_ROOT; };
EE164810135CEE49003B8A3E /* 06_code2000.ttf */ = {isa = PBXFileReference; lastKnownFileType = file; name = 06_code2000.ttf; path = ../../data/06_code2000.ttf; sourceTree = SOURCE_ROOT; };
EE583CBA12F773F00042CBE3 /* unicode_blocks.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; name = unicode_blocks.txt; path = ../../data/unicode_blocks.txt; sourceTree = "<group>"; };
@ -2936,6 +2938,14 @@
path = Location;
sourceTree = "<group>";
};
ED99667C2B94FBC20083CE55 /* ColorPicker */ = {
isa = PBXGroup;
children = (
ED99667D2B94FBC20083CE55 /* ColorPicker.swift */,
);
path = ColorPicker;
sourceTree = "<group>";
};
EDFDFB412B7108090013A44C /* AboutController */ = {
isa = PBXGroup;
children = (
@ -3148,6 +3158,7 @@
F6E2FBFB1E097B9F0083EBEC /* UI */ = {
isa = PBXGroup;
children = (
ED99667C2B94FBC20083CE55 /* ColorPicker */,
F69018B51E9E5FEB00B3C10B /* Autoupdate */,
34E7760D1F14B165003040B3 /* AvailableArea */,
349D1AC21E2E325B004A2006 /* BottomMenu */,
@ -4082,6 +4093,7 @@
34AB66051FC5AA320078E451 /* MWMNavigationDashboardManager+Entity.mm in Sources */,
993DF12A23F6BDB100AC231A /* Style.swift in Sources */,
34ABA6171C2D185C00FE1BEC /* MWMAuthorizationOSMLoginViewController.mm in Sources */,
ED9966802B94FBC20083CE55 /* ColorPicker.swift in Sources */,
993DF10423F6BDB100AC231A /* UIView+styleName.swift in Sources */,
998927302449DE1500260CE2 /* TabBarArea.swift in Sources */,
995739042355CAA30019AEE7 /* PageIndicator.swift in Sources */,

View file

@ -0,0 +1,76 @@
enum ColorPickerType {
case defaultColorPicker(UIColor)
case bookmarkColorPicker(BookmarkColor)
}
final class ColorPicker: NSObject {
static let shared = ColorPicker()
private var onUpdateColorHandler: ((UIColor) -> Void)?
// MARK: - Public
/// Presents a color picker view controller modally from a specified root view controller.
///
/// - Uses native color picker on the iOS 14.0+ for the `defaultColorPicker` type on iPhone and iPad.
/// - For the rest of the iOS versions, `bookmarkColorPicker` type and iPad designed for Mac uses a custom color picker.
///
func present(from rootViewController: UIViewController, pickerType: ColorPickerType, completionHandler: ((UIColor) -> Void)?) {
onUpdateColorHandler = completionHandler
let colorPickerViewController: UIViewController
switch pickerType {
case .defaultColorPicker(let color):
if #available(iOS 14.0, *), !ProcessInfo.processInfo.isiOSAppOnMac {
colorPickerViewController = defaultColorPickerViewController(with: color)
} else {
colorPickerViewController = bookmarksColorPickerViewController(with: BookmarkColor.bookmarkColor(from: color) ?? .none)
}
case .bookmarkColorPicker(let bookmarkColor):
colorPickerViewController = bookmarksColorPickerViewController(with: bookmarkColor)
}
rootViewController.present(colorPickerViewController, animated: true)
}
// MARK: - Private
@available(iOS 14.0, *)
private func defaultColorPickerViewController(with selectedColor: UIColor) -> UIViewController {
let colorPickerController = UIColorPickerViewController()
colorPickerController.supportsAlpha = false
colorPickerController.selectedColor = selectedColor
colorPickerController.delegate = self
return colorPickerController
}
private func bookmarksColorPickerViewController(with selectedColor: BookmarkColor) -> UIViewController {
let bookmarksColorViewController = BookmarkColorViewController(bookmarkColor: selectedColor)
bookmarksColorViewController.delegate = self
// The navigation controller is used for getting the navigation item with the title and the close button.
let navigationController = UINavigationController(rootViewController: bookmarksColorViewController)
return navigationController
}
}
// MARK: - BookmarkColorViewControllerDelegate
extension ColorPicker: BookmarkColorViewControllerDelegate {
func bookmarkColorViewController(_ viewController: BookmarkColorViewController, didSelect bookmarkColor: BookmarkColor) {
onUpdateColorHandler?(bookmarkColor.color)
onUpdateColorHandler = nil
viewController.dismiss(animated: true)
}
}
// MARK: - UIColorPickerViewControllerDelegate
extension ColorPicker: UIColorPickerViewControllerDelegate {
@available(iOS 14.0, *)
func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
onUpdateColorHandler?(viewController.selectedColor)
onUpdateColorHandler = nil
viewController.dismiss(animated: true, completion: nil)
}
@available(iOS 14.0, *)
func colorPickerViewControllerDidSelectColor(_ viewController: UIColorPickerViewController) {
onUpdateColorHandler?(viewController.selectedColor)
}
}