RResultBuilder is DSL library based on Result Builder

Overview

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.

You might also like...
DGLabelSize - Functions that calculate the size of uilabel based on different string lengths
DGLabelSize - Functions that calculate the size of uilabel based on different string lengths

DGLabelSize Functions that calculate the size of uilabel based on different stri

A Swift app, named 'iPose', for iPhone's pose measurement based on Swift.

iPhone's pose measurement based on Swift. This is a Swift app, named 'iPose', for iPhone's pose measurement based on Swift. This is a side project to

Cross-Platform, Protocol-Oriented Programming base library to complement the Swift Standard Library. (Pure Swift, Supports Linux)

SwiftFoundation Cross-Platform, Protocol-Oriented Programming base library to complement the Swift Standard Library. Goals Provide a cross-platform in

ZIP Foundation is a library to create, read and modify ZIP archive files.
ZIP Foundation is a library to create, read and modify ZIP archive files.

ZIP Foundation is a library to create, read and modify ZIP archive files. It is written in Swift and based on Apple's libcompression for high performa

macOS system library in Swift

SystemKit A macOS system library in Swift based off of libtop, from Apple's top implementation. For an example usage of this library, see dshb, a macO

Swift library to develop custom Alexa Skills
Swift library to develop custom Alexa Skills

AlexaSkillsKit AlexaSkillsKit is a Swift library that allows you to develop custom skills for Amazon Alexa, the voice service that powers Echo. It tak

🏹 Bow is a cross-platform library for Typed Functional Programming in Swift
🏹 Bow is a cross-platform library for Typed Functional Programming in Swift

Bow is a cross-platform library for Typed Functional Programming in Swift. Documentation All documentation and API reference is published in our websi

Butterfly is a lightweight library for integrating bug-report and feedback features with shake-motion event.
Butterfly is a lightweight library for integrating bug-report and feedback features with shake-motion event.

Butterfly is a lightweight library for integrating bug-report and feedback features with shake-motion event. Goals of this project One of th

Focus is an Optics library for Swift (where Optics includes Lens, Prisms, and Isos)

Focus Focus is an Optics library for Swift (where Optics includes Lens, Prisms, and Isos) that is inspired by Haskell's Lens library. Introduction Foc

Releases(1.2.1)
Owner
Rakuten, Inc.
Rakuten, Inc.
A result builder that allows to define shape building closures

ShapeBuilder A result builder implementation that allows to define shape building closures and variables. Problem In SwiftUI, you can end up in a situ

Daniel Peter 47 Dec 2, 2022
A collection of useful result builders for Swift and Foundation value types

Swift Builders A collection of useful result builders for Swift and Foundation value types. Motivation Arrays, dictionaries, and other collection-base

David Roman 3 Oct 14, 2022
A DSL for Data Manipulation

Underscore.m About Underscore.m Underscore.m is a small utility library to facilitate working with common data structures in Objective-C. It tries to

Robb Böhnke 1.5k Oct 4, 2022
Display LaTeX using SwiftUI & LaTeX DSL

Swift LaTeX Display LaTeX using MathJax. The package also provides a custom LaTeX DSL, which enables you write LaTeX the way you write SwiftUI Views.

Vaida 2 Oct 28, 2022
🗃 Powerful and easy to use Swift Query Builder for Vapor 3.

⚠️ This lib is DEPRECATED ⚠️ please use SwifQL with Bridges Quick Intro struct PublicUser: Codable { var name: String var petName: String

iMike 145 Sep 10, 2022
An eject button for Interface Builder to generate swift code

Eject Eject is a utility to transition from Interface Builder to programatic view layout. This is done by using code generation to create a .swift fil

Rightpoint 524 Dec 29, 2022
👷‍♀️ Closure-based delegation without memory leaks

Delegated 2.0 Delegated is a super small package that helps you avoid retain cycles when using closure-based delegation. New Medium post here. Origina

Oleg Dreyman 703 Oct 8, 2022
📦 KeyPath dynamicMemberLookup based syntax sugar for Swift.

DuctTape ?? KeyPath dynamicMemberLookup based syntax sugar for Swift. let label: UILabel = UILabel().ductTape .numberOfLines(0) .textColor(.re

Taiki Suzuki 171 Nov 4, 2022
Automatically set your keyboard's backlight based on your Mac's ambient light sensor.

QMK Ambient Backlight Automatically set your keyboard's backlight based on your Mac's ambient light sensor. Compatibility macOS Big Sur or later, a Ma

Karl Shea 29 Aug 6, 2022
Proper YAML support for Objective-C. Based on recommended libyaml.

YAML.framework for Objective-C Based on C LibYAML library (http://pyyaml.org/wiki/LibYAML) by Kirill Simonov. YAML.framework provides support for YAML

Mirek Rusin 236 Aug 29, 2022