Swiftpack.co -  rakutentech/ios-rresultbuilders as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
rakutentech/ios-rresultbuilders
Open Source DSL framework based on @resultbuilder and swift 5.4 compatibility
.package(url: "https://github.com/rakutentech/ios-rresultbuilders.git", from: "1.0.1")

RResultBuilders

Build Swift Version License CocoaPods Compatible
Swift Package Manager Xcode Platform

RResultBuilder is DSL library based on Result Builder

Features

  • Supports
    • NSAttributedString
    • UIAlertController
  • DSL way defining attributed string
  • Building attributed strings is type safe
  • DSL way of constructing ActionSheet, AlertController
  • Swift 5.4 compatible
  • Supports control statements(including optional checking) within DSL body builder
  • Reduces boilerplate code
  • Fully tested code
  • Distribution with Swift Package Manager and CocoaPods

Requirements

  • iOS 11.0+
  • macOS 10.11+
  • watchOS 4.0+
  • Xcode 12.5+

Installation

CocoaPods

You can use CocoaPods to install RResultBuilders by adding it to your Podfile:

platform :ios, '11.0'
use_frameworks!
pod 'RResultBuilders', '1.0.0'

Swift Package Manager

Just add this dependency in Package.swift Open your project in Xcode 11, navigate to Menu -> Swift Packages -> Add Package Dependency and enter

.package('https://github.com/rakutentech/ios-rresultbuilders', from: 1.0.0)

Usage

Attributed string

Following components that helps in building attributed string in easy way

  • RText: This component used to construct attributed text from given string
  • RLink: This component used to construct link in attributed text
  • RImageAttachment: This component used to construct image attachment in attributed text
  • Miscelleneaous
    • REmpty: Just empty component
    • RSpace: This component used to insert space and default is one space
    • RLineBreak: This component used to insert new line and default is one line break
Regular
import RResultBuilders
NSAttributedString {
    RText("I love swift and SwiftUI ???")
        .stroke(width: 2, color: .red)
}
.font(.systemFont(ofSize: 50))
Control statements

If..else

import RResultBuilders

let optionalText: String? = "iPhone12 Pro Max"
NSAttributedString {    
    if let text = optionalText {
        RText(text)
            .font(.systemFont(ofSize: 20))
            .foregroundColor(.red)
    } else {
        RText("Optional else Text")
    }
}
.font(.boldSystemFont(ofSize: 40))

switch..case

enum Apple { case iPhone, mac, airpod }
let appleDevice = Apple.iPhone
NSAttributedString {
    switch appleDevice {
    case .iPhone:
        RText("This is iPhone")
            .foregroundColor(.blue)
    default:
        RText("Apple future device")
    }
}
.font(.boldSystemFont(ofSize: 40))

for..in loop

let appleDevices = [<.....>]
NSAttributedString {
    for device in appleDevices {
        RText(device.rawValue)
            .foregroundColor(device.color)
            .font(device.font)
        RSpace()
    }
}
.font(.boldSystemFont(ofSize: 40))

Alert / Actionsheet

There are dedicated actions those can be used to construct alert or action sheet

  • DefaultAction: This action is default type with UIAlertAction.Style as default
  • CancelAction: This action is cancel type with UIAlertAction.Style as cancel
  • DestructiveAction: This action is destructive type with UIAlertAction.Style as destructive
Regular
import RResultBuilders
UIAlertController(
    title: "Delete all data?",
    message: "All your data will be deleted!") {
    DestructiveAction("Yes, Delete it All") {
        print("Deleting all data")
    }
    DefaultAction("Show More Options") {
        print("showing more options")
    }
    CancelAction("No, Don't Delete Anything")
}
With control statements

If..else

import RResultBuilders

let optionalText: String? = "Show More optionals"
UIAlertController(
    title: "Delete all data?",
    message: "All your data will be deleted!") {
    
    // Optional unwrapping
    if let text = optionalText {
        DefaultAction(text) {
            print("showing more optionals")
        }
    }
    CancelAction("No, Don't Delete Anything")
}

switch..case

let appleDevice = Apple.iPhone
UIAlertController(
    title: "Delete all data?",
    message: "All your data will be deleted!") {    
    // Switch case
    switch appleDevice {
    case .iPhone:
        DefaultAction("Show More iPhone") {
            print("showing more iPhone")
        }
    default:
        DefaultAction("Show More appleDevice") {
            print("showing more appleDevice")
        }
    }
    CancelAction("No, Don't Delete Anything")
}

for..in loop

let actions = [<.....>]
UIAlertController(
    title: "Delete all data?",
    message: "All your data will be deleted!") {
    for action in actions {
        action
    }
}

Screenshot

Example Image1 Example Image2

Example

  • Open and Run the project inside Example folder and find various use cases

Contribute

We welcome you for the contribution to RResultBuilders, check the CONTRIBUTING. If you find any issues or want to suggest your brilliant ideas please feel free to create pull request.

Meta

Distributed under the MIT license. See LICENSE for more information.

GitHub

link
Stars: 21
Last commit: 5 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API