VDKit

master

This repository contains useful extensions on Foundation, UIKit and SwiftUI
dankinsoid/VDKit

VDKit

CI Status Version License Platform

Description

This repository contains useful extensions on Foundation, UIKit and SwiftUI

Usage

VDChain

Combination of @dynamicMemberLookup with KeyPathes and callAsFunction

let label = UILabel()~
  .text("Text")
  .textColor(.red)
  .font(.system(24))
  .apply()

VDDates

Date struct provides very little functionality, any operations with dates must be implemented through Calendar in very unintuitive, complex and difficult to remember ways. To simplify operations with dates, this library provides a simple and intuitive syntax.

Some examples

let afterTomorrow: Date = .today + 2.days
//or .today + .days(2)
//or Date.today.adding(2 * .day)
let difference = date2 - date1
let daysBetweenDates = difference.days
//or date2.interval(of: .day, from: date1)
let weeksBetweenDates = difference.weeks
let hours = Date().component(.hour)
//or Date().hour()
let someDate = Date(year: 1994, month: 10, day: 4) 
let startOfMonth = Date().start(of: .month)
let lastMonth = Date().end(of: .year)
let lastDay = Date().end(of: .year, accuracy: .day)
let nextYear = Date().next(.year)
let nextLeapYear = Date().nearest([.month: 2, .day: 29], in: .future)?.start(of: .year)
let monthLenght = Date().count(of: .day, in: .month)
for month in (date1...date2).each(.month) {...}
let weekdayName = Date().name(of: .weekday)
if let date = Date(from: dateString, format: "dd.MM.yyyy") {...}
let dateString = Date().string("dd.MM.yyyy")
let iso860String = Date().iso860
let defaultDateString = Date().string(date: .long, time: .short)
let relativeDateString = Date().string("dd.MM.yyyy",
                            relative: [
                                .day(1): "Tomorrow",
                                .day(0): "Today, HH:mm",
                                .day(-1): "Yesterday",
                                .week(0): "EEEE",       
                                .year(0): "dd.MM"
                            ]
                        )

Any function contains additional parameters with default values such as:

calendar: Calendar = .default
locale: Locale = .default
timezone: TimeZone = .default

where Calendar.default, Locale.default and TimeZone.default - static variables that you can change. So you can use custom Calendar in each function

let dayOfMonth = Date().position(of: .day, in: .month, calendar: customCalendar)

Or you can set your own default value for all functions

Calendar.default = customCalendar

VDBuilders

  • ArrayBuilder<T> - result builder to create arrays
  • ComposeBuilder
  • SingleBuilder

UIKitIntegration

Easy integration UIKit elements to SwiftUI code. This realization uses @autoclosures in order to avoid UIView re-creation. § operator creates UIKitView, UIKitView supports chaining to update UIView. .uiKitViewEnvironment modifier allows to set UIViews properties as environments via chaining.

@State var text: String 
let textColor: Color 

var body: some View {
  VStack {
    Text(text)
      .foregroundColor(textColor)
  
    UILabel()§
      .text(text)
      .contentPriority.horizontal.compression(.required)
    
    UIKitView {
      UILabel()
    } update: { label, context in
      label.text = text
    }
    
    UIImageView()
  }
  .uiKitViewEnvironment(for: UILabel.self)
  .textColor(textColor.ui)
  .tintColor(.red)
}

VDLayout

SwiftUI like syntaxis for UIKit via function builders and chaining

class YourView: LtView {

  @SubviewsBuilder
  override func layout() -> [SubviewProtocol] {
    UIStackView(.vertical) { 
      UILabel()~
        .textColor(.black)
        .font(.systemFont(ofSize: 20))

      someView { 
        someImageView~
          .image(someImage)
      }

      Text("SubviewsBuilder supports SwiftUI views too")
    }
  }
}

VDLayout is good for use with ConstraintsOperators, just need make Constraints<Item> impelemts SubviewProtocol

UIView { 
  label
    .height(30)
    .width(someView.width / 2 + 10)
    .centerX(0)
    .edges(.vertical).equal(to: 10)
}

UIKitEnvironment

view.environments.someValue = 0

extension UIViewEnvironment {
  var someValue: Int {
    get { self[\.someValue] ?? 0 }
    set { self[\.someValue] = newValue }
  }
}

Installation

  1. Swift Package Manager

Create a Package.swift file.

// swift-tools-version:5.0
import PackageDescription

let package = Package(
  name: "SomeProject",
  dependencies: [
    .package(url: "https://github.com/dankinsoid/VDKit.git", from: "1.193.0")
  ],
  targets: [
    .target(name: "SomeProject", dependencies: ["VDKit"])
  ]
)
$ swift build

Author

dankinsoid, voidilov@gmail.com

License

VDAnimation is available under the MIT license. See the LICENSE file for more info.

Description

  • Swift Tools 5.6.0
View More Packages from this Author

Dependencies

  • None
Last updated: Tue Apr 02 2024 12:56:10 GMT-0900 (Hawaii-Aleutian Daylight Time)