A Swifty way to toggle your features.

Overview

SwiftyToggler Banner

Swift 3.1 CocoaPods Compatible Carthage compatible Platform Twitter

Requirements

  • iOS 8.0+
  • Xcode 8.1+
  • Swift 3.1+

Installation

CocoaPods

Create a Podfile file in the root of your application and the following content:

use_frameworks!

target '<Your Target Name>' do
    pod 'SwiftyToggler', '~> 1.0.0'
end

Then, run pod install.

Carthage

Create a Cartfile file in the root of your application and the following content:

github "MarcoSantarossa/SwiftyToggler" ~> 1.0.0

Then, run carthage update to build the framework and drag the built SwiftyToggler.framework into your Xcode project.

Swift Package Manager

Create a Package.swift file in the root of your application and the following content:

import PackageDescription

let package = Package(
  name: "YourApp",
  dependencies: [
    .Package(url: "https://github.com/MarcoSantarossa/SwiftyToggler.git", “1.0.0”)
  ]
)

Usage

Create A Feature

There are two ways to create a feature:

Implementing FeatureProtocol

The library provides the protocol FeatureProtocol which you can implement:

class MyProtocol: FeatureProtocol {

	private let parentViewController: UIViewController
	private let featureViewController: UIViewController

	var isEnabled: Bool = false

	init(parentViewController: UIViewController) {
		self.parentViewController = parentViewController
		featureViewController = UIViewController()
	}

	// Main function of Feature. It's called when the future has been run.
	func main() {
		parentViewController.present(featureViewController, animated: true, completion: nil)
	}

	// It's called when the future has been disabled.
	func dispose() {
		featureViewController.dismiss(animated: true, completion: nil)
	}
}

Extending Feature [Recommended]

The library provides the class Feature<T> which you can extend. It has a generic property which is the payload of the feature. The payload is a clean way to manage the dependencies of your feature:

struct MyPayload {
    let parentViewController: UIViewController
}

class MyFeature: Feature<MyPayload> {

	private let featureViewController: UIViewController

	override init(payload: MyPayload?, isEnabled: Bool = false) {
		featureViewController = UIViewController()

		super.init(payload: payload, isEnabled: isEnabled)
	}

    override func main() {
        payload?.parentViewController.present(featureViewController, animated: true, completion: nil)
    }

    override func dispose() {
        featureViewController.dismiss(animated: true, completion: nil)
    }
}

Add A Feature

class ViewController: UIViewController {

	override func viewDidLoad() {
		super.viewDidLoad()

		let featurePayload = MyPayload(parentViewController: self)
		let feature = MyFeature(payload: featurePayload)
		FeaturesManager.shared.add(feature: feature, name: "MyFeature", shouldRunWhenEnabled: true)
	}
}

Remove A Feature

FeaturesManager.shared.remove(featureName: "MyFeature")
// or
FeaturesManager.shared.removeAll()

Run A Feature

do {
	let isRunning = try FeaturesManager.shared.run(featureName: "MyFeature")
	print("Is the feature running: \(isRunning)")
} catch SwiftyTogglerError.featureNotFound {
	print("Feature not found")
} catch {}

Enable/Disable A Feature

do {
	try FeaturesManager.shared.setEnable(true, featureName: "MyFeature")
} catch SwiftyTogglerError.featureNotFound {
	print("Feature not found")
} catch {}

Changes Observer

You can observer when a feature changes the value isEnable:

class ViewController: UIViewController {

	override func viewWillAppear(_ animated: Bool) {
		super.viewWillAppear(animated)

		FeaturesManager.shared.addChangesObserver(self)
	}

	override func viewWillDisappear(_ animated: Bool) {
		super.viewWillDisappear(animated)

		FeaturesManager.shared.removeChangesObserver(self)
	}
}

extension ViewController: FeatureChangesObserver {
	func featureDidChange(name: String, isEnabled: Bool) {

	}
}

Present Features List

Presenting Mode Screenshots

Modal

do {
	try FeaturesManager.shared.presentModalFeaturesList()
} catch SwiftyTogglerError.modalFeaturesListAlreadyPresented {
	print("Features List already presented as modal")
} catch {}

Child View Controller

class ViewController: UIViewController {

	override func viewDidLoad() {
		super.viewDidLoad()

		FeaturesManager.shared.presentFeaturesList(in: self)
	}
}

Advanced

shouldRunWhenEnabled

You can use this flag to run the feature as soon as it's enabled. You can set it with:

FeaturesManager.shared.add(feature: feature, name: "MyFeature", shouldRunWhenEnabled: true)
// or
do {
	try FeaturesManager.shared.update(featureName: "MyFeature", shouldRunWhenEnabled: true)
} catch SwiftyTogglerError.featureNotFound {
	print("Feature not found")
} catch {}

TODO

[ ] Add another example in example project.

[ ] Add feature dependencies.

[ ] Update features list table if a new feature is added when the table is visible.

[ ] Call dispose() when a feature is removed.

[ ] Allow async call to feature main() and dispose().

Communication

  • If you found a bug, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request.

Credits

SwiftyToggler is owned and maintained by Marco Santarossa. You can follow him on Twitter at @MarcoSantaDev.

License

SwiftyToggler is released under the MIT license. See LICENSE for details.

You might also like...
iOS application to tell the time in the British way 🇬🇧⏰
iOS application to tell the time in the British way 🇬🇧⏰

Tell Time 🇬🇧 ⏰ As a French guy in London, when people told me the time, I was always lost. Now thanks to this app, I can confirm what I hear and wha

A library of data structures for working with collections of identifiable elements in an ergonomic, performant way.
A library of data structures for working with collections of identifiable elements in an ergonomic, performant way.

Swift Identified Collections A library of data structures for working with collections of identifiable elements in an ergonomic, performant way. Motiv

The easiest way to install and switch between multiple versions of Xcode - with a mouse click.
The easiest way to install and switch between multiple versions of Xcode - with a mouse click.

Xcodes.app The easiest way to install and switch between multiple versions of Xcode. If you're looking for a command-line version of Xcodes.app, try x

A mobile application project designed for everybody which provides the easiest way to make searchs for public services
A mobile application project designed for everybody which provides the easiest way to make searchs for public services

A mobile application project designed for everybody which provides the easiest way to make searchs for public services

A MVVM Project using two way binding with DidSet swift feature

FawryTask Description Fawry Task is a MVVM Project using two way binding with DidSet swift feature , follow Solid princepl by uncle bob esecially Sing

VerticalTabView is a native way to display paged vertical content in SwiftUI.

VerticalTabView 🔝 VTabView is a native way to display paged vertical content in SwiftUI. To work it makes use of the new iOS 14 TabView PageTabViewSt

Introducing SwiftUI. A declarative way to create User Interfaces with Swift.

SwiftUI - Landmarks Introducing SwiftUI. A declarative way to create User Interfaces with Swift. SwiftUI was introduced at WWDC 2019 by Apple. It is t

A library for building applications in a consistent and understandable way, with composition, testing, and ergonomics in mind
A library for building applications in a consistent and understandable way, with composition, testing, and ergonomics in mind

A library for building applications in a consistent and understandable way, with composition, testing, and ergonomics in mind.

 ReactionButton is a control that allows developers to add this functionality to their apps in an easy way.
ReactionButton is a control that allows developers to add this functionality to their apps in an easy way.

Since Facebook introduced reactions in 2016, it became a standard in several applications as a way for users to interact with content. ReactionButton is a control that allows developers to add this functionality to their apps in an easy way.

Releases(1.0.0)
Owner
Marco Santarossa
Professional Engineer since 2011. Lead iOS Engineer. Tech Editor @raywenderlich.
Marco Santarossa
Swift UI features and general iOS development coding lab

SwiftUI & iOS development concepts code lab Contains sample projects with documentation Overview SwiftUI provides views, controls and layout structure

Samuel Owino 4 Dec 14, 2022
An application on the iPad for people who cook. It comes with features such as smart recipes, recipe management and ingredient inventory tracking.

ChopChop ChopChop is an application on the iPad for people who cook. It comes with features such as smart recipes, recipe management and ingredient in

Seow Alex 0 Dec 23, 2021
News App 📱 built to demonstrate the use of SwiftUI 3 features, Async/Await, CoreData and MVVM architecture pattern.

Box Feed News App ?? built to demonstrate the use of following features, SwiftUI 3 Async/Await AsyncImage List Refreshable Swipe Actions Separator Cor

Sameer Nawaz 113 Dec 20, 2022
Visualize your dividend growth. DivRise tracks dividend prices of your stocks, gives you in-depth information about dividend paying stocks like the next dividend date and allows you to log your monthly dividend income.

DivRise DivRise is an iOS app written in Pure SwiftUI that tracks dividend prices of your stocks, gives you in-depth information about dividend paying

Kevin Li 78 Oct 17, 2022
Shazam, out of your way

SLAM SLAM: an app I made in 24 hours that’s like Shazam, but out of your way. I used it as an excuse to play with ShazamKit, the new framework made by

Linus Skucas 8 Jan 24, 2022
ReleaseNotesKit - a brand new, elegant, and extremely simple way to present the recent version’s release notes to your users

ReleaseNotesKit This is ReleaseNotesKit, a brand new, elegant, and extremely simple way to present the recent version’s release notes to your users. R

Swapnanil Dhol 22 Jun 30, 2022
Pegase is a beautifully easy tool to keep track of your financial life on all your macOS

Pegase ?? Features ?? Documentation Personal account software Pegase is a beautifully easy tool to keep track of your financial life on all your macOS

null 2 Oct 12, 2021
iCloud Drive is Apple's essential connection between all your devices, Mac, iPhone, iPad, even your Windows PC.

iCloud Drive is Apple's essential connection between all your devices, Mac, iPhone, iPad, even your Windows PC.While the cost of storage seems expensive in comparison to other online storage services, its advantage is that it works natively across all your devices.

MindInventory 12 Jul 29, 2022
iOS Trakt Client - Keep track of your favorite TV shows and movies on your iPhone. (Under development)

CouchTracker Keep track of your favorite movies and tv shows on your iPhone Setup for development You will need Xcode 11.2.1 Swift 5.1.2 Run the follo

Pietro Caselani 42 Apr 19, 2022
Show off your GitHub contributions from your lock screen 📱

GitHubContributionsiOS V2 NOTICE: V2 is published. It is a complete rewrite using SwiftUI and Catalyst. Source code are now hosted on the version/2.x

JustZht 478 Dec 16, 2022