Skip to content

Commit

Permalink
Merge pull request #7 from devpolant/bugfix/dark-mode
Browse files Browse the repository at this point in the history
Dark mode support
  • Loading branch information
devpolant committed May 8, 2021
2 parents 10363a9 + f0c094d commit 1ebd683
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Example/NativeUIExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ final class ViewController: UIViewController {
present(alert, animated: true)

case .system:
let cancelAction = UIAlertAction(title: "Cancel", style: .default)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
let confirmAction = UIAlertAction(title: "Confirm", style: .default)

let alert = UIAlertController(
Expand Down
12 changes: 6 additions & 6 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
PODS:
- NativeUI (1.1.0):
- NativeUI/Core (= 1.1.0)
- NativeUI/Alert (1.1.0):
- NativeUI (1.2.1):
- NativeUI/Core (= 1.2.1)
- NativeUI/Alert (1.2.1):
- NativeUI/Utils
- NativeUI/Core (1.1.0):
- NativeUI/Core (1.2.1):
- NativeUI/Alert
- NativeUI/Utils (1.1.0)
- NativeUI/Utils (1.2.1)

DEPENDENCIES:
- NativeUI (from `../`)
Expand All @@ -15,7 +15,7 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
NativeUI: d82fff2460d835a9fbd645902462e2b1d9573507
NativeUI: b79f2438d4df95c876cc85c0819979521bc89367

PODFILE CHECKSUM: bb46d7bf1ae3b119e00a9331a12cd0a4b5cac170

Expand Down
2 changes: 1 addition & 1 deletion NativeUI.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "NativeUI"
s.version = "1.2.0"
s.version = "1.2.1"
s.summary = "Library that includes customizable replacements for native UIKit components"

s.description = <<-DESC
Expand Down
4 changes: 4 additions & 0 deletions NativeUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
3A858E2A2646B8B500758898 /* SeparatorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A858E292646B8B500758898 /* SeparatorView.swift */; };
3AB073C22439FF7F0092DF66 /* NativeUI.h in Headers */ = {isa = PBXBuildFile; fileRef = 3AB073C02439FF7F0092DF66 /* NativeUI.h */; settings = {ATTRIBUTES = (Public, ); }; };
3AB073CC243A01140092DF66 /* SelectionFeedbackGenerator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AB073CB243A01140092DF66 /* SelectionFeedbackGenerator.swift */; };
3AB073D2243A017A0092DF66 /* AlertPresentationAnimator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AB073CD243A017A0092DF66 /* AlertPresentationAnimator.swift */; };
Expand All @@ -17,6 +18,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
3A858E292646B8B500758898 /* SeparatorView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SeparatorView.swift; sourceTree = "<group>"; };
3AB073BD2439FF7F0092DF66 /* NativeUI.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = NativeUI.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3AB073C02439FF7F0092DF66 /* NativeUI.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NativeUI.h; sourceTree = "<group>"; };
3AB073C12439FF7F0092DF66 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
Expand Down Expand Up @@ -90,6 +92,7 @@
isa = PBXGroup;
children = (
3AB073CB243A01140092DF66 /* SelectionFeedbackGenerator.swift */,
3A858E292646B8B500758898 /* SeparatorView.swift */,
);
path = Utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -177,6 +180,7 @@
3AB073D5243A017A0092DF66 /* AlertView.swift in Sources */,
3AB073D6243A017A0092DF66 /* AlertViewController.swift in Sources */,
3AB073D3243A017A0092DF66 /* Alert.swift in Sources */,
3A858E2A2646B8B500758898 /* SeparatorView.swift in Sources */,
3AB073CC243A01140092DF66 /* SelectionFeedbackGenerator.swift in Sources */,
3AB073D4243A017A0092DF66 /* AlertActionSequenceView.swift in Sources */,
3AB073D2243A017A0092DF66 /* AlertPresentationAnimator.swift in Sources */,
Expand Down
11 changes: 4 additions & 7 deletions NativeUI/Sources/Alert/AlertActionSequenceView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ protocol AlertActionSequenceViewDelegate: AnyObject {
struct AlertActionSequenceViewModel {
let actions: [Alert.Action]
let disabledTintColor: UIColor?
let separatorColor: UIColor
let separatorWidth: CGFloat
}

final class AlertActionSequenceView: UIControl {
Expand Down Expand Up @@ -122,7 +120,7 @@ final class AlertActionSequenceView: UIControl {
}

for action in viewModel.actions.dropFirst() {
let separator = makeButtonSeparatorView(viewModel: viewModel)
let separator = makeButtonSeparatorView()
stackView.addArrangedSubview(separator)

let actionView = makeActionView(for: action, disabledTintColor: viewModel.disabledTintColor)
Expand Down Expand Up @@ -167,11 +165,10 @@ final class AlertActionSequenceView: UIControl {
}
}

private func makeButtonSeparatorView(viewModel: AlertActionSequenceViewModel) -> UIView {
let separatorView = UIView()
private func makeButtonSeparatorView() -> UIView {
let separatorView = SeparatorView()
separatorView.translatesAutoresizingMaskIntoConstraints = false
separatorView.backgroundColor = viewModel.separatorColor
separatorView.widthAnchor.constraint(equalToConstant: viewModel.separatorWidth).isActive = true
separatorView.axis = .vertical
return separatorView
}

Expand Down
26 changes: 10 additions & 16 deletions NativeUI/Sources/Alert/AlertView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ final class AlertView: UIView {

weak var delegate: AlertViewDelegate?

private let separatorColor: UIColor = UIColor.lightGray

// MARK: - Subviews

// MARK: Blur

private lazy var blurView: UIVisualEffectView = {
let blurEffect = UIBlurEffect(style: .extraLight)
let blurStyle: UIBlurEffect.Style
if #available(iOS 13, *) {
blurStyle = .systemMaterial
} else {
blurStyle = .extraLight
}
let blurEffect = UIBlurEffect(style: blurStyle)

let effectView = UIVisualEffectView(effect: blurEffect)
effectView.translatesAutoresizingMaskIntoConstraints = false
Expand Down Expand Up @@ -80,9 +84,9 @@ final class AlertView: UIView {
}()

private lazy var contentSeparatorView: UIView = {
let separatorView = UIView()
separatorView.backgroundColor = separatorColor
let separatorView = SeparatorView()
separatorView.translatesAutoresizingMaskIntoConstraints = false
separatorView.axis = .horizontal
return separatorView
}()

Expand Down Expand Up @@ -144,10 +148,6 @@ final class AlertView: UIView {
actionsStackView.addArrangedSubview(contentSeparatorView)
actionsStackView.addArrangedSubview(actionSequenceView)

NSLayoutConstraint.activate([
contentSeparatorView.heightAnchor.constraint(equalToConstant: Layout.separatorThickness)
])

[titleLabel, messageLabel].forEach {
$0.setContentHuggingPriority(.required, for: .vertical)
$0.setContentCompressionResistancePriority(.required, for: .vertical)
Expand Down Expand Up @@ -208,9 +208,7 @@ final class AlertView: UIView {
if !viewModel.actions.isEmpty {
let actionsViewModel = AlertActionSequenceViewModel(
actions: viewModel.actions,
disabledTintColor: viewModel.disabledTintColor,
separatorColor: separatorColor,
separatorWidth: Layout.separatorThickness
disabledTintColor: viewModel.disabledTintColor
)
actionSequenceView.setup(viewModel: actionsViewModel)
}
Expand All @@ -222,10 +220,6 @@ final class AlertView: UIView {

private enum Layout {

static var separatorThickness: CGFloat {
return 1.0 / UIScreen.main.scale
}

enum Content {
static let top: CGFloat = 20
static let bottom: CGFloat = 20
Expand Down
105 changes: 105 additions & 0 deletions NativeUI/Sources/Utils/SeparatorView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//
// SeparatorView.swift
// NativeUI
//
// Created by Anton Poltoratskyi on 08.05.2021.
// Copyright © 2021 Anton Poltoratskyi. All rights reserved.
//

import UIKit

final class SeparatorView: UIView {

enum Axis {
case vertical
case horizontal
}

var axis: Axis = .horizontal {
didSet {
updateAxis()
}
}

// MARK: - Subviews

private lazy var contentView: UIView = {
let contentView = UIView()
contentView.translatesAutoresizingMaskIntoConstraints = false

let lightThemeColor = UIColor.lightGray
let darkThemeColor = UIColor.darkGray

if #available(iOS 13.0, *) {
contentView.backgroundColor = UIColor { traitCollection in
switch traitCollection.userInterfaceStyle {
case .light, .unspecified:
return lightThemeColor
case .dark:
return darkThemeColor
@unknown default:
return lightThemeColor
}
}
} else {
contentView.backgroundColor = lightThemeColor
}

addSubview(contentView)

return contentView
}()

private lazy var heightConstraint = heightAnchor.constraint(equalToConstant: separatorThickness(for: traitCollection))
private lazy var widthConstraint = widthAnchor.constraint(equalToConstant: separatorThickness(for: traitCollection))

// MARK: - Init

override init(frame: CGRect) {
super.init(frame: frame)
initialize()
}

required init?(coder: NSCoder) {
super.init(coder: coder)
initialize()
}

// MARK: - Base Setup

private func initialize() {
setContentHuggingPriority(.required, for: .vertical)
setContentHuggingPriority(.required, for: .horizontal)
setContentCompressionResistancePriority(.required, for: .vertical)
setContentCompressionResistancePriority(.required, for: .horizontal)

NSLayoutConstraint.activate([
contentView.leadingAnchor.constraint(equalTo: leadingAnchor),
contentView.trailingAnchor.constraint(equalTo: trailingAnchor),
contentView.topAnchor.constraint(equalTo: topAnchor),
contentView.bottomAnchor.constraint(equalTo: bottomAnchor)
])

updateAxis()
}

private func updateAxis() {
switch axis {
case .horizontal:
widthConstraint.isActive = false
heightConstraint.isActive = true
case .vertical:
widthConstraint.isActive = true
heightConstraint.isActive = false
}
}

private func separatorThickness(for traitCollection: UITraitCollection?) -> CGFloat {
return 1.0 / max(1, traitCollection?.displayScale ?? 1)
}

public override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
heightConstraint.constant = separatorThickness(for: traitCollection)
widthConstraint.constant = separatorThickness(for: traitCollection)
}
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[![Swift](https://img.shields.io/badge/Swift-5.0-orange.svg)](https://swift.org)
[![Xcode](https://img.shields.io/badge/Xcode-11.0-blue.svg)](https://developer.apple.com/xcode)
[![Xcode](https://img.shields.io/badge/Xcode-12.0-blue.svg)](https://developer.apple.com/xcode)
[![CocoaPods Compatible](https://img.shields.io/cocoapods/v/NativeUI.svg)](https://cocoapods.org/pods/NativeUI)
[![MIT](https://img.shields.io/badge/License-MIT-red.svg)](https://opensource.org/licenses/MIT)

Expand All @@ -16,15 +16,15 @@

```ruby
target 'MyApp' do
pod 'NativeUI', '~> 1.2'
pod 'NativeUI', '~> 1.2.1'
end
```

If you don't need to connect all UI components you may use subspecs like:

```ruby
target 'MyApp' do
pod 'NativeUI/Alert', '~> 1.2'
pod 'NativeUI/Alert', '~> 1.2.1'
end
```

Expand Down

0 comments on commit 1ebd683

Please sign in to comment.