Skip to content

A draggable button that helps you change your backend environments on demand.

License

Notifications You must be signed in to change notification settings

scalefocus/EnvChanger

Repository files navigation

EnvChanger 🧙‍♂️

Build Status Version License Platform

About

EnvChanger is a simple tool that helps developers and testers to switch between backend environments really quickly without having to download a specific build.

Displays a button in the top left corner that when selected, presents an alert with the given possible environments allowing users to easily swap them.

Preview

Example

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

Usage

  1. We assume you hold the list of your backend environments in an enum:
enum Environments: String {
    case production = "https://production.server.com/"
    case staging = "https://staging.server.com/"
    case development = "https://development.server.com"
    case testing = "https://10.0.1.1/"
    case edge = "edge.server.com"
}
  1. Your enum needs to conform to the EnvironmentRepresentable protocol, by implementing environmentTitle:
enum Environments: String, EnvironmentRepresentable {
    case production = "https://production.server.com/"
    case staging = "https://staging.server.com/"
    case development = "https://development.server.com"
    case testing = "https://10.0.1.1/"
    case edge = "edge.server.com"
    
    var environmentTitle: String {
        rawValue
    }
}
  1. Create an instance of EnvChangerController in your AppDelegate didFinishLaunchingWithOptions, passing your environment enum:
var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
    /// Instantiate to customise envButton `style` and `position` if needed.
    let envButtonConfig = EnvButtonConfiguration(style: .title("env"),
                                                 startingPosition: .init(y: .center, x: .center))
    
    /// Instantiate and pass handler for environment selection.
    let envChanger = EnvChangerController(envs: NetworkService.Environments.self,
                                          window: window,
                                          buttonConfiguration: envButtonConfig) { selectedEnvironment in
        NetworkService.activeEnvironment = selectedEnvironment.environmentTitle
    }
    
    /// If an environment has been saved, set is as the active environment.
    if !envChanger.savedEnvironment.isEmpty {
        NetworkService.activeEnvironment = envChanger.savedEnvironment
    }
    
    return true
}

More Usage

Access the saved environment:

envChanger.savedEnvironment

Note: The chosen environment is saved in UserDefaults.

Resizes the button with the specified height/width.

resizeFrame(newWidth: CGFloat, newHeight: CGFloat)  

Note: If an image is set, the imageEdgeInsets are calculated and set as '(height + width) / 2'.

Additional Notes

  • If no button image/title is passed in the constructor, by default it will set the button title to 'EN'.
  • If a button title AND image is specified, the image is implemented and the title set will not be set.

Installation

EnvChanger is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'EnvChanger'

Author

Gavril Tonev, gavril.tonev@upnetix.com
Teodor Marinov, teodor.marinov@upnetix.com

License

EnvChanger is available under the MIT license.

Contributing

A new version is released by pushing an updated tag to master:

# Make changes, commit files
git tag -a 1.2.3 -m "Release 1.2.3"
git push --follow-tags

About

A draggable button that helps you change your backend environments on demand.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors 4

  •  
  •  
  •  
  •