Skip to content

Commit 38805d8

Browse files
Spring WongSpring Wong
authored andcommitted
init commit
0 parents  commit 38805d8

File tree

13 files changed

+987
-0
lines changed

13 files changed

+987
-0
lines changed

SingletonRefactorSample.xcodeproj/project.pbxproj

Lines changed: 538 additions & 0 deletions
Large diffs are not rendered by default.

SingletonRefactorSample.xcodeproj/project.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// AppDelegate.swift
3+
// SingletonRefactorSample
4+
//
5+
// Created by Spring Wong on 10/9/2017.
6+
// Copyright © 2017 Spring Wong. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
@UIApplicationMain
12+
class AppDelegate: UIResponder, UIApplicationDelegate {
13+
14+
var window: UIWindow?
15+
16+
17+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
18+
// Override point for customization after application launch.
19+
return true
20+
}
21+
22+
func applicationWillResignActive(_ application: UIApplication) {
23+
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
24+
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
25+
}
26+
27+
func applicationDidEnterBackground(_ application: UIApplication) {
28+
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
29+
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
30+
}
31+
32+
func applicationWillEnterForeground(_ application: UIApplication) {
33+
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
34+
}
35+
36+
func applicationDidBecomeActive(_ application: UIApplication) {
37+
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
38+
}
39+
40+
func applicationWillTerminate(_ application: UIApplication) {
41+
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
42+
}
43+
44+
45+
}
46+
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"images" : [
3+
{
4+
"idiom" : "iphone",
5+
"size" : "29x29",
6+
"scale" : "2x"
7+
},
8+
{
9+
"idiom" : "iphone",
10+
"size" : "29x29",
11+
"scale" : "3x"
12+
},
13+
{
14+
"idiom" : "iphone",
15+
"size" : "40x40",
16+
"scale" : "2x"
17+
},
18+
{
19+
"idiom" : "iphone",
20+
"size" : "40x40",
21+
"scale" : "3x"
22+
},
23+
{
24+
"idiom" : "iphone",
25+
"size" : "60x60",
26+
"scale" : "2x"
27+
},
28+
{
29+
"idiom" : "iphone",
30+
"size" : "60x60",
31+
"scale" : "3x"
32+
},
33+
{
34+
"idiom" : "ipad",
35+
"size" : "29x29",
36+
"scale" : "1x"
37+
},
38+
{
39+
"idiom" : "ipad",
40+
"size" : "29x29",
41+
"scale" : "2x"
42+
},
43+
{
44+
"idiom" : "ipad",
45+
"size" : "40x40",
46+
"scale" : "1x"
47+
},
48+
{
49+
"idiom" : "ipad",
50+
"size" : "40x40",
51+
"scale" : "2x"
52+
},
53+
{
54+
"idiom" : "ipad",
55+
"size" : "76x76",
56+
"scale" : "1x"
57+
},
58+
{
59+
"idiom" : "ipad",
60+
"size" : "76x76",
61+
"scale" : "2x"
62+
}
63+
],
64+
"info" : {
65+
"version" : 1,
66+
"author" : "xcode"
67+
}
68+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11134" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
3+
<dependencies>
4+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11106"/>
5+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
6+
</dependencies>
7+
<scenes>
8+
<!--View Controller-->
9+
<scene sceneID="EHf-IW-A2E">
10+
<objects>
11+
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
12+
<layoutGuides>
13+
<viewControllerLayoutGuide type="top" id="Llm-lL-Icb"/>
14+
<viewControllerLayoutGuide type="bottom" id="xb3-aO-Qok"/>
15+
</layoutGuides>
16+
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
17+
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
18+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
19+
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
20+
</view>
21+
</viewController>
22+
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
23+
</objects>
24+
<point key="canvasLocation" x="53" y="375"/>
25+
</scene>
26+
</scenes>
27+
</document>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11762" systemVersion="16B2657" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
3+
<device id="retina4_7" orientation="portrait">
4+
<adaptation id="fullscreen"/>
5+
</device>
6+
<dependencies>
7+
<deployment identifier="iOS"/>
8+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11757"/>
9+
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
10+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
11+
</dependencies>
12+
<scenes>
13+
<!--View Controller-->
14+
<scene sceneID="tne-QT-ifu">
15+
<objects>
16+
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="SingletonRefactorSample" customModuleProvider="target" sceneMemberID="viewController">
17+
<layoutGuides>
18+
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
19+
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
20+
</layoutGuides>
21+
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
22+
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
23+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
24+
<subviews>
25+
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Z9f-Cr-JS4">
26+
<rect key="frame" x="16" y="608" width="343" height="51"/>
27+
<color key="backgroundColor" red="0.0012486093737952286" green="1" blue="0.0019371473553461138" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
28+
<constraints>
29+
<constraint firstAttribute="height" constant="51" id="yWv-H2-ey2"/>
30+
</constraints>
31+
<state key="normal" title="Login"/>
32+
</button>
33+
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="SSx-Hv-zgs">
34+
<rect key="frame" x="278" y="20" width="81" height="46"/>
35+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
36+
<color key="backgroundColor" red="1" green="0.45159254156253015" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
37+
<state key="normal" title="Debug"/>
38+
</button>
39+
</subviews>
40+
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
41+
<constraints>
42+
<constraint firstItem="wfy-db-euE" firstAttribute="top" secondItem="Z9f-Cr-JS4" secondAttribute="bottom" constant="8" id="7Jg-jD-oCw"/>
43+
<constraint firstAttribute="leadingMargin" secondItem="Z9f-Cr-JS4" secondAttribute="leading" id="Ypw-1a-aOp"/>
44+
<constraint firstAttribute="trailingMargin" secondItem="Z9f-Cr-JS4" secondAttribute="trailing" id="yts-92-r5q"/>
45+
</constraints>
46+
</view>
47+
<connections>
48+
<outlet property="debugButton" destination="SSx-Hv-zgs" id="D9V-QQ-dS5"/>
49+
<outlet property="loginButton" destination="Z9f-Cr-JS4" id="3Hl-Ku-wXd"/>
50+
</connections>
51+
</viewController>
52+
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
53+
</objects>
54+
<point key="canvasLocation" x="136.80000000000001" y="138.98050974512745"/>
55+
</scene>
56+
</scenes>
57+
</document>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// DataManager.swift
3+
// SingletonRefactorSample
4+
//
5+
// Created by Spring Wong on 10/9/2017.
6+
// Copyright © 2017 Spring Wong. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
class DataManager {
12+
//singleton
13+
static let sharedInstance = DataManager()
14+
15+
var isLoggedIn = false
16+
17+
//gen a random number, 1/2 probe to login success
18+
func requestCheckLogin(successHandler : (()->Void)?) {
19+
let randomNumber = arc4random_uniform(2)
20+
let randomBool = Bool(NSNumber(value: randomNumber))
21+
isLoggedIn = randomBool
22+
successHandler?()
23+
}
24+
25+
func reset() {
26+
isLoggedIn = false
27+
}
28+
}

SingletonRefactorSample/Info.plist

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>en</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleName</key>
14+
<string>$(PRODUCT_NAME)</string>
15+
<key>CFBundlePackageType</key>
16+
<string>APPL</string>
17+
<key>CFBundleShortVersionString</key>
18+
<string>1.0</string>
19+
<key>CFBundleVersion</key>
20+
<string>1</string>
21+
<key>LSRequiresIPhoneOS</key>
22+
<true/>
23+
<key>UILaunchStoryboardName</key>
24+
<string>LaunchScreen</string>
25+
<key>UIMainStoryboardFile</key>
26+
<string>Main</string>
27+
<key>UIRequiredDeviceCapabilities</key>
28+
<array>
29+
<string>armv7</string>
30+
</array>
31+
<key>UISupportedInterfaceOrientations</key>
32+
<array>
33+
<string>UIInterfaceOrientationPortrait</string>
34+
<string>UIInterfaceOrientationLandscapeLeft</string>
35+
<string>UIInterfaceOrientationLandscapeRight</string>
36+
</array>
37+
<key>UISupportedInterfaceOrientations~ipad</key>
38+
<array>
39+
<string>UIInterfaceOrientationPortrait</string>
40+
<string>UIInterfaceOrientationPortraitUpsideDown</string>
41+
<string>UIInterfaceOrientationLandscapeLeft</string>
42+
<string>UIInterfaceOrientationLandscapeRight</string>
43+
</array>
44+
</dict>
45+
</plist>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//
2+
// ViewController.swift
3+
// SingletonRefactorSample
4+
//
5+
// Created by Spring Wong on 10/9/2017.
6+
// Copyright © 2017 Spring Wong. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
class ViewController: UIViewController {
12+
13+
@IBOutlet var loginButton : UIButton!
14+
@IBOutlet var debugButton : UIButton!
15+
16+
override func viewDidLoad() {
17+
super.viewDidLoad()
18+
// Do any additional setup after loading the view, typically from a nib.
19+
20+
loginButton.addTarget(self, action: #selector(btnLoginOnClick), for: .touchUpInside)
21+
debugButton.addTarget(self, action: #selector(reset), for: .touchUpInside)
22+
}
23+
24+
override func didReceiveMemoryWarning() {
25+
super.didReceiveMemoryWarning()
26+
// Dispose of any resources that can be recreated.
27+
}
28+
29+
func reset() {
30+
DataManager.sharedInstance.reset()
31+
loginButton.isHidden = false
32+
}
33+
34+
func btnLoginOnClick() {
35+
//try to login , if false, we don't want user to login again
36+
DataManager.sharedInstance.requestCheckLogin {
37+
self.loginButton.isHidden = DataManager.sharedInstance.isLoggedIn
38+
if DataManager.sharedInstance.isLoggedIn {
39+
self.loginButton.isHidden = true
40+
}else {
41+
self.loginButton.isHidden = false
42+
let alertVC = UIAlertController(title: "Login", message: "Login Fail", preferredStyle: .alert)
43+
let cancelAction = UIAlertAction(title: "OK", style: .default, handler: { (action) in
44+
alertVC.dismiss(animated: true, completion: nil)
45+
})
46+
alertVC.addAction(cancelAction)
47+
self.present(alertVC, animated: true)
48+
}
49+
}
50+
}
51+
}
52+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>en</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleName</key>
14+
<string>$(PRODUCT_NAME)</string>
15+
<key>CFBundlePackageType</key>
16+
<string>BNDL</string>
17+
<key>CFBundleShortVersionString</key>
18+
<string>1.0</string>
19+
<key>CFBundleVersion</key>
20+
<string>1</string>
21+
</dict>
22+
</plist>

0 commit comments

Comments
 (0)