Created
July 28, 2019 09:04
-
-
Save zaimramlan/804c9aaf313ed1d1e01859ca43022d0a to your computer and use it in GitHub Desktop.
[PLAYGROUND] Simplify Storyboard Instantiation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
enum Storyboard: String { | |
case SignUp | |
case TopUp | |
} | |
enum ViewController { | |
enum SignUp: String { | |
case createAccount = "CreateAccountVC" | |
} | |
enum TopUp: String { | |
case enterAmount = "EnterAmountVC" | |
} | |
} | |
extension UIStoryboard { | |
static func instantiate(signUp viewController: ViewController.SignUp, bundle resource: BundleType? = nil) { | |
print(Storyboard.SignUp.rawValue) | |
print(viewController.rawValue) | |
print(fetchBundle(for: resource ?? .base)) | |
} | |
static func instantiate(topUp viewController: ViewController.TopUp, bundle resource: BundleType? = nil) { | |
print(Storyboard.TopUp.rawValue) | |
print(viewController.rawValue) | |
print(fetchBundle(for: resource ?? .base)) | |
} | |
private static func fetchBundle(for resource: BundleType) -> Bundle? { | |
if let path = Bundle.main.path(forResource: resource.rawValue, ofType: "lproj") { | |
return Bundle(path: bundlePath) | |
} | |
return nil | |
} | |
enum BundleType { | |
case base, localized | |
var rawValue: String { | |
switch self { | |
case .base: | |
return "Base" | |
case .localized: | |
// fetch user's chosen language | |
return "Thai" | |
} | |
} | |
} | |
} | |
UIStoryboard.instantiate(topUp: .enterAmount) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment