Skip to content

Commit

Permalink
add 1.0.3 extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
osmanyildirim committed Mar 6, 2024
1 parent 00bb194 commit 582491d
Show file tree
Hide file tree
Showing 32 changed files with 636 additions and 111 deletions.
2 changes: 1 addition & 1 deletion OYExtensions.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "OYExtensions"
s.version = "1.0.2"
s.version = "1.0.3"
s.summary = "Some useful extension for Swift"

s.homepage = "https://github.com/osmanyildirim/OYExtensions.git"
Expand Down
4 changes: 4 additions & 0 deletions Sources/Core/OYDateFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public enum OYDateFormat {

/// `dd-MM-yyyy`
case short

/// `dd.MM.yy`
case shortest

/// `dd.MM.yyyy`
case shortDot
Expand Down Expand Up @@ -53,6 +56,7 @@ extension OYDateFormat {
switch self {
case .`default`: return "dd.MM.yyyy HH:mm:ss"
case .short: return "dd-MM-yyyy"
case .shortest: return "dd.MM.yy"
case .shortDot: return "dd.MM.yyyy"
case .time: return "HH:mm:ss"
case .isoYear: return "yyyy"
Expand Down
3 changes: 3 additions & 0 deletions Sources/Core/OYError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,7 @@ public enum OYError: Error {

/// User denies authorization to access tracking
case advertisingTrackingIsNotEnabled

/// Permission could not be declared
case permissionError
}
43 changes: 43 additions & 0 deletions Sources/CoreGraphics/UIEdgeInsets+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// UIEdgeInsets+Extensions.swift
// OYExtensions
//
// Created by osmanyildirim
//

import UIKit

extension UIEdgeInsets {
/// `UIEdgeInsets.oy_init(0, 10, 20, 30)` → output → UIEdgeInsets(top: 0.0, left: 10.0, bottom: 20.0, right: 30.0)
public static func oy_init(_ top: CGFloat, _ left: CGFloat, _ bottom: CGFloat, _ right: CGFloat) -> UIEdgeInsets {
UIEdgeInsets(top: top, left: left, bottom: bottom, right: right)
}

/// let value = UIEdgeInsets(top: 0, left: 10, bottom: 20, right: 30)
/// `value.oy_top` → output → 0.0
public var oy_top: CGFloat {
get { top }
set(value) { top = value }
}

/// let value = UIEdgeInsets(top: 0, left: 10, bottom: 20, right: 30)
/// `value.oy_bottom` → output → 20.0
public var oy_bottom: CGFloat {
get { bottom }
set(value) { bottom = value }
}

/// let value = UIEdgeInsets(top: 0, left: 10, bottom: 20, right: 30)
/// `value.oy_left` → output → 10.0
public var oy_left: CGFloat {
get { self.left }
set(value) { self.left = value }
}

/// let value = UIEdgeInsets(top: 0, left: 10, bottom: 20, right: 30)
/// `value.oy_right` → output → 30.0
public var oy_right: CGFloat {
get { self.right }
set(value) { self.right = value }
}
}
25 changes: 25 additions & 0 deletions Sources/CoreGraphics/UIRectCorner+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// UIRectCorner+Extensions.swift
// OYExtensions
//
// Created by osmanyildirim
//

import Foundation

extension UIRectCorner {
public var caCornerMask: CACornerMask {
switch self {
case .topLeft:
return .layerMinXMinYCorner
case .bottomLeft:
return .layerMinXMaxYCorner
case .topRight:
return .layerMaxXMinYCorner
case .bottomRight:
return .layerMaxXMaxYCorner
default:
return []
}
}
}
10 changes: 8 additions & 2 deletions Sources/Foundation/AttributedString+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ extension NSAttributedString {
}

extension NSMutableAttributedString {
/// Apply font attribute
func oy_applyFont(_ font: UIFont) {
/// Set font attribute
func oy_set(font: UIFont) {
let base = font.fontDescriptor
let range = NSRange(location: 0, length: length)
beginEditing()
Expand All @@ -59,4 +59,10 @@ extension NSMutableAttributedString {
}
endEditing()
}

func oy_set(textAlignment: NSTextAlignment) {
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = textAlignment
addAttribute(.paragraphStyle, value: paragraph, range: NSRange(location: 0, length: length))
}
}
33 changes: 20 additions & 13 deletions Sources/Foundation/Bundle+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ extension Bundle {
}
task.resume()
}

/// Checks if the app is installed from the app store
/// `Bundle.main.oy_isAppInstalledFromAppStore`→ output → true
public var oy_isAppInstalledFromAppStore: Bool {
return Bundle.main.appStoreReceiptURL?.lastPathComponent == "sandboxReceipt"
}

/// `Bundle.main.oy_appBuildNumber`→ output → "1"
public var oy_appBuildNumber: String? { oy_infoPlistValue(key: "CFBundleVersion") as? String }
Expand Down Expand Up @@ -98,12 +104,19 @@ extension Bundle {
return !applicationClassName.responds(to: Selector(("shared")))
}

/// `Bundle.main.oy_infoPlist(type: [String: Any].self)`
public func oy_infoPlist<T>(type: T.Type = [String: Any].self) -> T? {
oy_read(file: "Info", fileType: "plist", type: type)
/// `Bundle.main.oy_infoPlistAllData` → output → Dictionary<String, Any>
public var oy_infoPlistAllData: [String: Any] {
oy_read(type: [String: Any].self, name: "Info", fileType: "plist") ?? [:]
}

/// `Bundle.main.oy_read(type: [String: Any].self, name: "Info", fileType: "plist")`→ output → Dictionary<String, Any>
public func oy_read<T>(type: T.Type, name: String, fileType: String) -> T? {
guard let data = oy_data(name, fileType: fileType) else { return nil }
guard let result = try? PropertyListSerialization.propertyList(from: data, options: [], format: nil) as? T else { return nil }
return result
}

/// `Bundle.main.oy_infoPlistValue(key: "SecretKey")`
/// `Bundle.main.oy_infoPlistValue(key: "SecretKey")`→ output → "SecretValue"
public func oy_infoPlistValue(key: String) -> Any? {
object(forInfoDictionaryKey: key)
}
Expand All @@ -115,11 +128,9 @@ extension Bundle {
return decoded
}

/// `Bundle.main.oy.read(resource: "Info", fileType: "plist", type: Model.self)`
public func oy_read<T>(file: String, fileType: String? = nil, type: T.Type) -> T? {
guard let data = oy_data(file, fileType: fileType) else { return nil }
guard let result = try? PropertyListSerialization.propertyList(from: data, options: [], format: nil) as? T else { return nil }
return result
/// `Bundle.main.oy_path(name: "sample", fileType: "html")`
public func oy_path(_ name: String, fileType: String? = nil) -> URL? {
return url(forResource: name, withExtension: fileType)
}

/// `Bundle.main.oy_write(resource: "Sample", fileType: "plist", value: newInstance)`
Expand All @@ -130,10 +141,6 @@ extension Bundle {
}

extension Bundle {
private func oy_path(_ resource: String, fileType: String? = nil) -> URL? {
return url(forResource: resource, withExtension: fileType)
}

private func oy_data(_ resource: String, fileType: String?) -> Data? {
guard let url = oy_path(resource, fileType: fileType) else { return nil }
guard let data = try? Data(contentsOf: url) else { return nil }
Expand Down
5 changes: 5 additions & 0 deletions Sources/Foundation/Data+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ extension Data {
public func oy_decode<T: Decodable>(decoder: JSONDecoder = JSONDecoder()) throws -> T {
try decoder.decode(T.self, from: self)
}

/// `data.oy_decode(with: nil)`
public func oy_decode<T: Decodable>(with decoder: JSONDecoder?) -> T? {
try? (decoder ?? JSONDecoder()).decode(T.self, from: self)
}

/// `data.oy_string()`
public func oy_string(encoding: String.Encoding = .utf8) -> String? {
Expand Down
22 changes: 11 additions & 11 deletions Sources/Foundation/Date+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,10 @@ extension Date {
}

/// date1 is "03.02.2023 12:45"
/// `date1.start(of: .year)` → output → "01.01.2023 00:00:00"
/// `date1.start(of: .month)` → output → "01.02.2023 00:00:00"
/// `date1.start(of: .hour)` → output → "03.02.2023 12:00:00"
/// `date1.start(of: .minute)` → output → "03.02.2023 12:45:00"
/// `date1.oy_start(of: .year)` → output → "01.01.2023 00:00:00"
/// `date1.oy_start(of: .month)` → output → "01.02.2023 00:00:00"
/// `date1.oy_start(of: .hour)` → output → "03.02.2023 12:00:00"
/// `date1.oy_start(of: .minute)` → output → "03.02.2023 12:45:00"
public func oy_start(of component: Calendar.Component) -> Date {
if component == .day {
return Calendar.current.startOfDay(for: self)
Expand All @@ -295,11 +295,11 @@ extension Date {
}

/// date1 is "03.02.2023 12:45"
/// `date1.start(of: .year)` → output → "31.12.2023 23:59:59"
/// `date1.start(of: .month)` → output → "29.02.2023 23:59:59"
/// `date1.start(of: .hour)` → output → "03.02.2023 12:59:59"
/// `date1.start(of: .minute)` → output → "03.02.2023 12:45:59"
public func end(of component: Calendar.Component) -> Date {
/// `date1.oy_end(of: .year)` → output → "31.12.2023 23:59:59"
/// `date1.oy_end(of: .month)` → output → "29.02.2023 23:59:59"
/// `date1.oy_end(of: .hour)` → output → "03.02.2023 12:59:59"
/// `date1.oy_end(of: .minute)` → output → "03.02.2023 12:45:59"
public func oy_end(of component: Calendar.Component) -> Date {
let date = oy_start(of: component)
var components: DateComponents? {
switch component {
Expand Down Expand Up @@ -441,7 +441,7 @@ extension Date {

extension String {
/// `"17-05-2023".oy_date(format: .short)` → output → (Date) $R0 = 2023-05-17 00:00:00 UTC
public func oy_date(format: OYDateFormat = .default) -> Date {
format.dateFormatter.date(from: self) ?? Date()
public func oy_date(format: OYDateFormat = .default) -> Date? {
format.dateFormatter.date(from: self)
}
}
16 changes: 16 additions & 0 deletions Sources/Foundation/Decodable+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@
import Foundation

extension Decodable {
/// Init Decodable object with JSON Data
///
/// let jsonData = """
/// {
/// "id": 1,
/// "name": "Josefina",
/// "surname": "Calvo"
/// }
/// """.oy_data!
///
/// let decoded = User(from: jsonData)
public init?(from data: Data, using decoder: JSONDecoder = .init()) {
guard let decoded = try? decoder.decode(Self.self, from: data) else { return nil }
self = decoded
}

/// `ClassSample.oy_className` → output → "ClassSample"
public static var oy_className: String {
String(describing: self)
Expand Down
4 changes: 2 additions & 2 deletions Sources/Foundation/DispatchQueue+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extension DispatchQueue {

/// `DispatchQueue.oy_asyncOnMain { }`
public static func oy_asyncOnMain(_ execute: @escaping @convention(block) () -> Void) {
if pthread_main_np() != 0 {
if oy_isMainQueue {
execute()
} else {
DispatchQueue.main.async(execute: execute)
Expand All @@ -29,7 +29,7 @@ extension DispatchQueue {

/// `DispatchQueue.oy_syncOnMain { }`
public static func oy_syncOnMain(_ execute: @escaping @convention(block) () -> Void) {
if pthread_main_np() != 0 {
if oy_isMainQueue {
execute()
} else {
DispatchQueue.main.sync(execute: execute)
Expand Down
4 changes: 3 additions & 1 deletion Sources/Gesture/OYLongPressGesture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import UIKit
public final class OYLongPressGesture: UILongPressGestureRecognizer, OYBaseGesture {
private var longPressAction: (() -> Void)?

public convenience init(completion: (() -> Void)?) {
public convenience init(minimumPressDuration: CGFloat = 0.3, completion: (() -> Void)?) {
self.init()
longPressAction = completion

self.minimumPressDuration = minimumPressDuration
addTarget(self, action: #selector(didLongPress(_:)))
}

Expand Down
28 changes: 28 additions & 0 deletions Sources/Other/OYSaveImage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// OYSaveImage.swift
// OYExtensions
//
// Created by osmanyildirim
//

import Foundation

struct OYSaveImage {
final class delegate: NSObject {
let completion: ((Error?) -> Void)?

init(completion: ((Error?) -> Void)?) {
self.completion = completion
}

@objc func savedImage(_ image: UIImage, error: Error?, context: UnsafeMutableRawPointer?) {
self.completion?(error)
}
}

#warning("yorum ekle")
func saveImage(image: UIImage, completion: ((Error?) -> Void)?) {
let delegate = delegate(completion: completion)
UIImageWriteToSavedPhotosAlbum(image, delegate, #selector(delegate.savedImage(_: error: context:)), nil)
}
}
16 changes: 13 additions & 3 deletions Sources/SwiftStdlib/Array+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extension Array {
/// `[1, 2, 3, 4, 1].oy_item(at: 2)` → output → 3
/// `[1, 2, 3, 4, 1].oy_item(at: 9)` → output → nil
public func oy_item(at index: Int) -> Element? {
if Int(index) >= count {
guard count > index else {
return nil
}
return self[index]
Expand Down Expand Up @@ -49,12 +49,12 @@ extension Array {
enumerated().filter { indexSet.contains($0.offset) }.map(\.element)
}

/// `["a", "b", "c", "d"].prefix(2)` → output → ["a", "b"]
/// `["a", "b", "c", "d"].oy_items(prefix: 2)` → output → ["a", "b"]
public func oy_items(prefix: Int) -> [Element] {
Array(self.prefix(prefix))
}

/// `["a", "b", "c", "d"].prefix(2)` → output → ["c", "d"]
/// `["a", "b", "c", "d"].oy_items(suffix: 2)` → output → ["c", "d"]
public func oy_items(suffix: Int) -> [Element] {
Array(self.suffix(suffix))
}
Expand Down Expand Up @@ -299,6 +299,16 @@ extension Array {
let index = Int.random(in: 0..<count)
return self[index]
}

/// `[1, 2, 3, 4, 5].oy_shuffle()` → output → [3, 4, 1, 5, 2]
public mutating func oy_shuffle() {
self.shuffle()
}

/// `[1, 2, 3, 4, 5].oy_shuffled` → output → [3, 4, 1, 5, 2]
public var oy_shuffled: Self {
return shuffled()
}

/// struct Person {
/// var id: Int
Expand Down
23 changes: 23 additions & 0 deletions Sources/SwiftStdlib/CaseIterable+Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// CaseIterable+Extensions.swift
// OYExtensions
//
// Created by osmanyildirim
//

import Foundation

extension CaseIterable {
/// Get random element of CaseIterable enum
/// `Foo.oy_random`
public static var oy_random: Self? {
var generator = SystemRandomNumberGenerator()
return randomCaseIterableElement(using: &generator)
}
}

extension CaseIterable {
private static func randomCaseIterableElement(using generator: inout some RandomNumberGenerator) -> Self? {
allCases.randomElement(using: &generator)
}
}
Loading

0 comments on commit 582491d

Please sign in to comment.