Skip to content

Commit

Permalink
Added spinner preset.
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanvorobei committed Nov 4, 2021
1 parent 9efef20 commit 1cdfeda
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 20 deletions.
Binary file not shown.
16 changes: 11 additions & 5 deletions Example App/iOS Example/Controllers/PresetsController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,35 @@ class PresetsController: SPDiffableTableController {

fileprivate var presets: [AlertPresetModel] {
return [
AlertPresetModel(
.init(
name: "Done",
title: "Added to Library",
message: nil,
preset: .done
),
AlertPresetModel(
.init(
name: "Error",
title: "Oops",
message: "Please try again later",
preset: .error
),
AlertPresetModel(
.init(
name: "Heart",
title: "Love",
message: "We'll recommend more like this for you",
preset: .heart
),
AlertPresetModel(
.init(
name: "Spinner (Loading)",
title: "Please, wait",
message: "It take some time",
preset: .spinner
),
.init(
name: "Custom Image",
title: "Custom Image",
message: "Passed UIImage object for preset with style custom.",
preset: .custom(UIImage.init(systemName: "pencil.and.outline")!)
preset: .custom(UIImage.init(systemName: "pencil.and.outline")!.withRenderingMode(.alwaysOriginal))
),
]
}
Expand Down
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ If you like the project, don't forget to `put star ★`<br>Check out my other li
- [Layout](#layout)
- [Dismiss by Tap](#dismiss-by-tap)
- [Haptic](#haptic)
- [Shared Configuration](#shared-configuration)
- [Spinner](#spinner)
- [Shared Appearance](#shared-appearance)
- [SwiftUI](#swiftui)
- [Сontribution](#сontribution)
- [Other Projects](#other-projects)
Expand Down Expand Up @@ -107,9 +108,12 @@ For change duration of present time, create alert view and call `present` method

```swift
let alertView = SPAlertView(title: "Complete", preset: .done)
alertView.present(duration: 3)
alertView.duration = 4
alertView.present()
```

If you don't want to dimiss alert in time, set `dismissInTime` to `false`. After it `duration` property will be ignored.

### Layout

For customise layout and margins, use `layout` property. You can manage margins for each side, icon size and space between image and titles:
Expand Down Expand Up @@ -137,7 +141,19 @@ alertView.present(duration: 1.5, haptic: .success, completion: nil)

You can remove duration and completion, its have default values.

### Shared Configuration
### Spinner

I added preset `.spinner`, for use it simple call this:

```swift
let
let alertView = SPAlertView(title: "Please, wait", preset: .spinner)
alertView.present()
```

By default for this preset `dismissInTime` disabled and need manually dismiss alert.

### Shared Appearance

Also you can change some default values for alerts. For example you can change default duration and corner radius for alert with next code:

Expand Down
2 changes: 1 addition & 1 deletion SPAlert.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = 'SPAlert'
s.version = '3.4.1'
s.version = '3.5.0'
s.summary = 'Native alert from Apple Music & Feedback. Contains Done, Heart & Message and other presets.'
s.homepage = 'https://github.com/ivanvorobei/SPAlert'
s.source = { :git => 'https://github.com/ivanvorobei/SPAlert.git', :tag => s.version }
Expand Down
3 changes: 2 additions & 1 deletion Sources/SPAlert/Extensions/SwiftUIExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ extension View {
isPresent.wrappedValue = false
alertCompletion?()
}
alertView.present(duration: duration, haptic: haptic, completion: alertDismiss)
alertView.duration = duration
alertView.present(haptic: haptic, completion: alertDismiss)
}
return self
}
Expand Down
51 changes: 51 additions & 0 deletions Sources/SPAlert/Preset Views/SPAlertSpinnerView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// The MIT License (MIT)
// Copyright © 2020 Ivan Vorobei ([email protected])
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import UIKit

class SPAlertSpinnerView: UIView {

let activityIndicatorView: UIActivityIndicatorView = {
if #available(iOS 13.0, *) {
return UIActivityIndicatorView(style: .large)
} else {
return UIActivityIndicatorView()
}
}()

init() {
super.init(frame: .zero)
self.backgroundColor = .clear
addSubview(activityIndicatorView)
activityIndicatorView.startAnimating()
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func layoutSubviews() {
super.layoutSubviews()
activityIndicatorView.sizeToFit()
activityIndicatorView.center = .init(x: frame.width / 2, y: frame.height / 2)
}

}
3 changes: 1 addition & 2 deletions Sources/SPAlert/SPAlert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public enum SPAlert {
*/
public static func present(title: String, message: String? = nil, preset: SPAlertIconPreset, completion: (() -> Void)? = nil) {
let alertView = SPAlertView(title: title, message: message, preset: preset)
let haptic = preset.getHaptic()
alertView.present(haptic: haptic, completion: completion)
alertView.present(haptic: preset.haptic, completion: completion)
}

/**
Expand Down
19 changes: 18 additions & 1 deletion Sources/SPAlert/SPAlertIconPreset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public enum SPAlertIconPreset {
case done
case error
case heart
case spinner

case custom(_ image: UIImage)
}
Expand All @@ -43,18 +44,20 @@ public extension SPAlertIconPreset {
case .done: return SPAlertIconDoneView()
case .error: return SPAlertIconErrorView()
case .heart: return SPAlertIconHeartView()
case .spinner: return SPAlertSpinnerView()
case .custom(let image):
let imageView = UIImageView(image: image)
imageView.contentMode = .scaleAspectFit
return imageView
}
}

func getHaptic() -> SPAlertHaptic {
var haptic: SPAlertHaptic {
switch self {
case .done: return .success
case .error: return .error
case .heart: return .success
case .spinner: return .none
case .custom(_): return .none
}
}
Expand Down Expand Up @@ -118,6 +121,20 @@ public extension SPAlertLayout {
),
spaceBetweenIconAndTitle: 39
)
case .spinner:
self.init(
iconSize: .init(
width: 16,
height: 16
),
margins: .init(
top: 58,
left: Self.defaultHorizontalInset,
bottom: 27,
right: Self.defaultHorizontalInset
),
spaceBetweenIconAndTitle: 39
)
case .custom(_):
self.init(
iconSize: .init(
Expand Down
31 changes: 24 additions & 7 deletions Sources/SPAlert/SPAlertView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ open class SPAlertView: UIView {
}
}

/**
SPAlert: Automatically dismiss in time or not. Duration of dismiss can be changed by property `duration`.
*/
@objc dynamic open var dismissInTime: Bool = true

/**
SPAlert: Duration for showing alert. If `dismissInTime` disabled, this property ignoring.
*/
@objc dynamic open var duration: TimeInterval = 1.5

// MARK: - Views
Expand All @@ -75,6 +83,14 @@ open class SPAlertView: UIView {

public init(title: String, message: String? = nil, preset: SPAlertIconPreset) {
super.init(frame: CGRect.zero)

switch preset {
case .spinner:
self.dismissInTime = false
default:
self.dismissInTime = true
}

commonInit()
layout = SPAlertLayout(for: preset)
setTitle(title)
Expand Down Expand Up @@ -174,10 +190,6 @@ open class SPAlertView: UIView {
}

open func present(haptic: SPAlertHaptic = .success, completion: (() -> Void)? = nil) {
present(duration: self.duration, haptic: haptic, completion: completion)
}

open func present(duration: TimeInterval, haptic: SPAlertHaptic = .success, completion: (() -> Void)? = nil) {

if self.presentWindow == nil {
self.presentWindow = UIApplication.shared.keyWindow
Expand Down Expand Up @@ -207,12 +219,17 @@ open class SPAlertView: UIView {
UIView.animate(withDuration: presentDismissDuration, animations: {
self.alpha = 1
self.transform = CGAffineTransform.identity
}, completion: { finished in
}, completion: { [weak self] finished in
guard let self = self else { return }

if let iconView = self.iconView as? SPAlertIconAnimatable {
iconView.animate()
}
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + duration) {
self.dismiss()

if self.dismissInTime {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + self.duration) {
self.dismiss()
}
}
})
}
Expand Down

0 comments on commit 1cdfeda

Please sign in to comment.