Coder Social home page Coder Social logo

bottlerocketstudios / ios-scotty Goto Github PK

View Code? Open in Web Editor NEW
13.0 11.0 5.0 251 KB

Scotty is a framework designed to make app routing simpler and safer.

License: Apache License 2.0

Ruby 4.58% Swift 94.15% Objective-C 1.27%
swift routing deeplink ios

ios-scotty's Introduction

Scotty

CI Status Version Carthage compatible License Platform codecov

Purpose

This library provides a simple abstraction around the various entry points to an iOS application. URLs, application shortcut items, user activities, notification responses, and even custom types can be converted into a Route. These routes represent the various destinations your app can deep link too, allowing you to have a single code path through which all application links are executed.

Key Concepts

  • Route - An abstract representation of the code required to move your application from its root state to a specific final destination.
  • RouteController - An object that is created with a root view controller, and handles the execution of Routes.
  • RouteAction - An action that can be taken by the destination of a Route when travel has completed.

Usage

An instance of the RouteController should be created and retained somewhere accessible by your Application Delegate so that the relevant callbacks can be forwarded on to it. Although the RouteController can be wrapped and treated as a singleton, it does not have to be.

class AppDelegate: UIResponder, UIApplicationDelegate {
    var routeController: RouteController<UITabBarController>?
    //In this implementation, our root view controller is a UITabBarController

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]?) -> Bool {
        if let window = window, let rootVC = window.rootViewController as? UITabBarController {
            routeController = RouteController(root: rootVC)
        }

        return true
    }

    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        return routeController?.open(url.route) ?? false
    }

    func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
        return routeController?.open(userActivity) ?? false
    }

    func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) {
        completionHandler(routeController?.open(shortcutItem) ?? false)
    }
}

In order to make URL/NSUserActivity/UIApplicationShortcutItem compatible with the RouteController, they will need to be extended to vend Route objects. A simple implementation might look like this:

extension URL {

    public var route: Route<UITabBarController>? {
        let components = URLComponents(url: self, resolvingAgainstBaseURL: false)!
        return Route.route(forIdentifier: components.path)
    }
}

When dealing with URLs, in addition to vending Route objects, you will also need to configure your app with its own URL scheme. More information on this process is available from Apple.

Creating Routes

Creating a new route is as simple as creating a new Route instance.

extension Route where Root == UITabBarController {
    static var leftTab: Route {
		return Route(identifier: .leftTabRoute) { root, options -> Bool in
            root = 0

            if let routeRespondableController = root.selectedViewController as? RouteRespondable {
                routeRespondableController.setRouteAction {
                    DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
                        print("LeftTab successfully reached!")
                    }
                }
            }

            return true
        }
    }
}

The above example creates a static instance of the Route struct designed to travel to the leftmost tab of the sample application. The identifier is used to differentiate this route from others when converting between types that vend routes (such as URL).

The trailing closure is the mechanism of routing. Given the instance of your root view controller, as well as any options provided with the routing request, this closure should execute the required changes to the view controller hierarchy to reach the destination. If the destination can successfully be reached, this closure should return true. Otherwise, it should return false.

Route Actions

Route Actions are closures that can be executed at particular route destinations. If the route conforms to the RouteRespondable protocol, it will have a public routeAction property which can be set inside the route. When this destination is reached, it will indicate to the routeAction an appropriate time at which to execute.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

  • iOS 9.0+
  • Swift 5.0

Installation

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/BottleRocketStudios/iOS-Scotty.git", from: "2.1.0")
]

CocoaPods

Add the following to your Podfile:

pod 'Scotty'

You will also need to make sure you're opting into using frameworks:

use_frameworks!

Then run pod install with CocoaPods 0.36 or newer.

Carthage

Add the following to your Cartfile:

github "BottleRocketStudios/iOS-Scotty"

Run carthage update and follow the steps as described in Carthage's README.

Contributing

See the CONTRIBUTING document. Thank you, contributors!

ios-scotty's People

Contributors

achappell avatar br-earl-gaspard avatar br-tyler-milner avatar earlgaspard avatar ganttastic avatar jobsismyhomeboy avatar tylermilner avatar wmcginty avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ios-scotty's Issues

Version 2.0

There are some API changes in this release (the removal of Options, so it will need to be replaced with [AnyHashable: Any]. Technically that would mean we should move to 2.0. What do you all think @tylermilner @earlgaspard .

Once we settle on a version number I'll begin the release process.

Release Checklist

  • Create release branch
  • Update version number for all targets
  • Update version number in Podspec
  • Validate README.md is still current
  • Create pull request into master
  • Create version number tag in Git
  • Publish release on GitHub
  • Publish release on Cocoapods trunk

Version 2.1.1

Release checklist:

  • Create release branch
  • Update version number for all targets
  • Update version number in Podspec
  • Validate README.md is still current
  • Update CHANGELOG.md for the new release
  • Create pull request into master
  • Create version number tag in Git
  • Publish release on GitHub
  • Publish release on Cocoapods trunk

Swift 5

Migrate to support Swift 5

Version 2.1.0

Changes needed:

  • Remove .swift-version file
  • Add s.swift_version = '5.0' to pod spec

Release checklist:

  • Create release branch
  • Update version number for all targets
  • Update version number in Podspec
  • Validate README.md is still current
  • Update CHANGELOG.md for the new release
  • Create pull request into master
  • Create version number tag in Git
  • Publish release on GitHub
  • Publish release on Cocoapods trunk

Can Scotty be simplified?

While attempting to implement Scotty on a project, I ran into a few issues with the way it supports generics and the overall setup and learning curve. After reviewing a few projects that used it, there were discrepancies between each project's implementation. So I decided to opt out of using Scotty and used a much simpler implementation that did not use generics and worked well with coordinators. I would like to discuss these changes with interested people.

Version 1.1

Proposed 1.1.0 Changelog

  • Adjusted project structure to better support Travis-CI. CI is fully up-and-running on all currently supported platforms. Carthage is now required to work on the library and run the example projects (Cocoapods is no longer used). Clone the repo, run carthage update, and then open Scotty.xcworkspace to get started.
  • Some aspects of the routing interface have been simplified. The previous combination of Routable (a protocol) and AnyRoute (a type erased struct conforming to Routable) have been replaced by a single Route struct. This struct is still generic over it's RootViewController, but this change makes it easier to create new routes, while maintaining type safety. In addition, the RouteConvertible protocol was removed. This allows the same type to be converted to different Route objects, with different RootViewController, allowing more flexibility.
  • The RouteActionable protocol has been renamed to RouteRespondable. It's requirements remain unchanged.

Release checklist:

  • Update Podspec (version and git tag)
  • Validate Readme

Version 2.0.1

Changes:

  • Project updated for Xcode 10.

Release checklist:

  • Create release branch
  • Update version number for all targets
  • Update version number in Podspec
  • Validate README.md is still current
  • Update CHANGELOG.md for the new release
  • Create pull request into master
  • Create version number tag in Git
  • Publish release on GitHub
  • Publish release on Cocoapods trunk

Release 2.2.0

  • Create release branch
  • Update version number for all targets
  • Update version number in Podspec
  • Validate README.md is still current
  • Update CHANGELOG.md for the new release
  • Create pull request into master
  • Create version number tag in Git
  • Publish release on GitHub
  • Publish release on Cocoapods trunk

Remove CODEBEAT

The benefits of using CODEBEAT are no longer worth the time to fix the issues it calls out.

Remove the following:

  • CODEBEAT badge in Readme
  • .codebeatsettings file
  • Webhook in Settings
  • Deploy keys in Settings

After removing the RouteConvertible protocol the documentation is out of date

The following bit of code:

    func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
        return routeController?.open(url) ?? false
    }

Will generate the following error:

Cannot convert value of type 'URL' to expected argument type 'Route<UINavigationController>?'

However, change the call to be return routeController?.open(url.route) and it'll work, given that the previous extension to the URL class was made.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.