From 91e690614500cdcd2bc4e978e972f933a7e8bc48 Mon Sep 17 00:00:00 2001 From: Vlad Date: Wed, 19 Aug 2020 15:47:31 +0300 Subject: [PATCH] Add Animatable to TIUIKitCore and add TIActivityIndicators --- Package.swift | 4 +- TIActivityIndicators/README.md | 44 ++++++++++++++ ...ivityIndicatorView+ActivityIndicator.swift | 29 +++++++++ .../UIActivityIndicatorView+Animatable.swift | 26 ++++++++ .../Sources/Protocols/ActivityIndicator.swift | 34 +++++++++++ .../Protocols/ActivityIndicatorHolder.swift | 36 +++++++++++ .../Sources/Views/AnyActivityIndicator.swift | 60 +++++++++++++++++++ .../Sources/Protocols/Animatable.swift | 30 ++++++++++ 8 files changed, 262 insertions(+), 1 deletion(-) create mode 100644 TIActivityIndicators/README.md create mode 100644 TIActivityIndicators/Sources/Extensions/UIActivityIndicatorView/UIActivityIndicatorView+ActivityIndicator.swift create mode 100644 TIActivityIndicators/Sources/Extensions/UIActivityIndicatorView/UIActivityIndicatorView+Animatable.swift create mode 100644 TIActivityIndicators/Sources/Protocols/ActivityIndicator.swift create mode 100644 TIActivityIndicators/Sources/Protocols/ActivityIndicatorHolder.swift create mode 100644 TIActivityIndicators/Sources/Views/AnyActivityIndicator.swift create mode 100644 TIUIKitCore/Sources/Protocols/Animatable.swift diff --git a/Package.swift b/Package.swift index 7c2250b7..923173ab 100644 --- a/Package.swift +++ b/Package.swift @@ -8,10 +8,12 @@ let package = Package( ], products: [ .library(name: "TITransitions", targets: ["TITransitions"]), - .library(name: "TIUIKitCore", targets: ["TIUIKitCore"]) + .library(name: "TIUIKitCore", targets: ["TIUIKitCore"]), + .library(name: "TIActivityIndicators", targets: ["TIActivityIndicators"]) ], targets: [ .target(name: "TITransitions", path: "TITransitions/Sources"), .target(name: "TIUIKitCore", path: "TIUIKitCore/Sources"), + .target(name: "TIActivityIndicators", dependencies: ["TIUIKitCore"], path: "TIActivityIndicators/Sources") ] ) diff --git a/TIActivityIndicators/README.md b/TIActivityIndicators/README.md new file mode 100644 index 00000000..67af07a2 --- /dev/null +++ b/TIActivityIndicators/README.md @@ -0,0 +1,44 @@ +# TIActivityIndicators + +Bunch of protocols and views of Activity Indicators. + +# Protocols + +## ActivityIndicator +Protocol that describes basic activity indicator which conforms to Animatable. + +```swift + +public protocol ActivityIndicator { + + /// Type of view. Should be instance of UIView with basic animation actions. + associatedtype View: UIView, Animatable + + /// The underlying view. + var view: View { get } +} +``` + +## ActivityIndicatorHolder +Protocol that describes placeholder view, containing activity indicator. + +```swift + +public protocol ActivityIndicatorHolder: class { + var activityIndicator: Animatable { get } + var indicatorOwner: UIView { get } +} +``` + +# Views + +## AnyActivityIndicator +Type that performs some kind of type erasure for ActivityIndicator. + +# Usage examples + +- Loading Indicator in PaginationWrapper of main framework LeadKit. + +# Installation via SPM + +You can install this framework as a target of LeadKit. diff --git a/TIActivityIndicators/Sources/Extensions/UIActivityIndicatorView/UIActivityIndicatorView+ActivityIndicator.swift b/TIActivityIndicators/Sources/Extensions/UIActivityIndicatorView/UIActivityIndicatorView+ActivityIndicator.swift new file mode 100644 index 00000000..b6184c33 --- /dev/null +++ b/TIActivityIndicators/Sources/Extensions/UIActivityIndicatorView/UIActivityIndicatorView+ActivityIndicator.swift @@ -0,0 +1,29 @@ +// +// Copyright (c) 2020 Touch Instinct +// +// 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 + +extension UIActivityIndicatorView: ActivityIndicator { + public var view: UIActivityIndicatorView { + self + } +} diff --git a/TIActivityIndicators/Sources/Extensions/UIActivityIndicatorView/UIActivityIndicatorView+Animatable.swift b/TIActivityIndicators/Sources/Extensions/UIActivityIndicatorView/UIActivityIndicatorView+Animatable.swift new file mode 100644 index 00000000..2d709284 --- /dev/null +++ b/TIActivityIndicators/Sources/Extensions/UIActivityIndicatorView/UIActivityIndicatorView+Animatable.swift @@ -0,0 +1,26 @@ +// +// Copyright (c) 2020 Touch Instinct +// +// 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 +import TIUIKitCore + +extension UIActivityIndicatorView: Animatable {} diff --git a/TIActivityIndicators/Sources/Protocols/ActivityIndicator.swift b/TIActivityIndicators/Sources/Protocols/ActivityIndicator.swift new file mode 100644 index 00000000..f84598d7 --- /dev/null +++ b/TIActivityIndicators/Sources/Protocols/ActivityIndicator.swift @@ -0,0 +1,34 @@ +// +// Copyright (c) 2020 Touch Instinct +// +// 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 +import TIUIKitCore + +/// Protocol that describes basic activity indicator. +public protocol ActivityIndicator { + + /// Type of view. Should be instance of UIView with basic animation actions. + associatedtype View: UIView, Animatable + + /// The underlying view. + var view: View { get } +} diff --git a/TIActivityIndicators/Sources/Protocols/ActivityIndicatorHolder.swift b/TIActivityIndicators/Sources/Protocols/ActivityIndicatorHolder.swift new file mode 100644 index 00000000..b48e6f6a --- /dev/null +++ b/TIActivityIndicators/Sources/Protocols/ActivityIndicatorHolder.swift @@ -0,0 +1,36 @@ +// +// Copyright (c) 2020 Touch Instinct +// +// 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 +import TIUIKitCore + +/// Protocol that describes placeholder view, containing activity indicator. +public protocol ActivityIndicatorHolder: class { + var activityIndicator: Animatable { get } + var indicatorOwner: UIView { get } +} + +public extension ActivityIndicatorHolder where Self: UIView { + var indicatorOwner: UIView { + return self + } +} diff --git a/TIActivityIndicators/Sources/Views/AnyActivityIndicator.swift b/TIActivityIndicators/Sources/Views/AnyActivityIndicator.swift new file mode 100644 index 00000000..d3f2c643 --- /dev/null +++ b/TIActivityIndicators/Sources/Views/AnyActivityIndicator.swift @@ -0,0 +1,60 @@ +// +// Copyright (c) 2020 Touch Instinct +// +// 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 +import TIUIKitCore + +/// Type that performs some kind of type erasure for ActivityIndicator. +public struct AnyActivityIndicator: Animatable { + + private let backgroundView: UIView + private let animatableView: Animatable + + /// Initializer with indicator that should be wrapped. + /// + /// - Parameter _: indicator for wrapping. + public init(_ base: Indicator) where Indicator: ActivityIndicator { + animatableView = base.view + backgroundView = base.view + } + + /// Initializer with placeholder view, that wraps indicator. + /// + /// - Parameter loadingIndicatorHolder: placeholder view, containing indicator. + public init(activityIndicatorHolder: ActivityIndicatorHolder) { + animatableView = activityIndicatorHolder.activityIndicator + backgroundView = activityIndicatorHolder.indicatorOwner + } + + /// The background view. + var view: UIView { + return backgroundView + } + + public func startAnimating() { + animatableView.startAnimating() + } + + public func stopAnimating() { + animatableView.stopAnimating() + } +} diff --git a/TIUIKitCore/Sources/Protocols/Animatable.swift b/TIUIKitCore/Sources/Protocols/Animatable.swift new file mode 100644 index 00000000..927884dd --- /dev/null +++ b/TIUIKitCore/Sources/Protocols/Animatable.swift @@ -0,0 +1,30 @@ +// +// Copyright (c) 2020 Touch Instinct +// +// 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. +// + +/// Protocol that ensures that specific type support basic animation actions. +public protocol Animatable { + + /// Method that starts animation. + func startAnimating() + /// Method that stops animation. + func stopAnimating() +}