Skip to content
This repository was archived by the owner on Dec 16, 2024. It is now read-only.

Commit 1abec95

Browse files
authored
Merge pull request #18 from yimajo/use_token_caches
毎回ログインさせてたのを改善した
2 parents 1a4a231 + 1fb0e3c commit 1abec95

File tree

6 files changed

+69
-11
lines changed

6 files changed

+69
-11
lines changed

MeetupTweet/AppDelegate.swift

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,16 @@ class AppDelegate: NSObject, NSApplicationDelegate {
5353
return Observable.create { [unowned self] observer in
5454

5555
self.requestHandle = oauthSwift.authorize(withCallbackURL: "\(self.callBackHost)://", success: { credential, response, parameters in
56-
57-
self.oauthClient = OAuthClient(consumerKey: consumerKey, consumerSecret: consumerSecret, accessToken: credential.oauthToken, accessTokenSecret: credential.oauthTokenSecret)
56+
57+
UserDefaults.setToken(credential.oauthToken)
58+
UserDefaults.setTokenSecret(credential.oauthTokenSecret)
59+
60+
self.oauthClient = OAuthClient(
61+
consumerKey: consumerKey,
62+
consumerSecret: consumerSecret,
63+
accessToken: credential.oauthToken,
64+
accessTokenSecret: credential.oauthTokenSecret
65+
)
5866

5967
observer.onNext(true)
6068

@@ -65,4 +73,22 @@ class AppDelegate: NSObject, NSApplicationDelegate {
6573
return Disposables.create()
6674
}
6775
}
76+
77+
func applyToken() {
78+
guard let consumerKey = UserDefaults.consumerKey(),
79+
let consumerSecret = UserDefaults.consumerSecret(),
80+
let token = UserDefaults.token(),
81+
let tokenSecret = UserDefaults.tokenSecret() else {
82+
83+
return
84+
}
85+
86+
self.oauthClient = OAuthClient(
87+
consumerKey: consumerKey,
88+
consumerSecret: consumerSecret,
89+
accessToken: token,
90+
accessTokenSecret: tokenSecret
91+
)
92+
93+
}
6894
}

MeetupTweet/Base.lproj/Main.storyboard

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,13 @@
302302
<modifierMask key="keyEquivalentModifierMask"/>
303303
</menuItem>
304304
<menuItem isSeparatorItem="YES" id="osT-xY-EjA"/>
305+
<menuItem title="Show Login" id="xyw-7a-v39">
306+
<modifierMask key="keyEquivalentModifierMask"/>
307+
<connections>
308+
<segue destination="AmD-ro-1ro" kind="modal" id="U9R-h4-M9D"/>
309+
</connections>
310+
</menuItem>
311+
<menuItem isSeparatorItem="YES" id="fnu-PY-yUN"/>
305312
<menuItem title="Preferences…" keyEquivalent="," id="hWp-Qc-U1e"/>
306313
<menuItem isSeparatorItem="YES" id="vbj-5j-zMV"/>
307314
<menuItem title="Quit" keyEquivalent="q" id="Ib2-2u-3oL">
@@ -376,4 +383,7 @@ AAAAAAAAAAAAAAAACnA
376383
</mutableData>
377384
</image>
378385
</resources>
386+
<inferredMetricsTieBreakers>
387+
<segue reference="Ykf-aF-LDN"/>
388+
</inferredMetricsTieBreakers>
379389
</document>

MeetupTweet/Classes/Infra/UserDefaults.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ struct UserDefaults {
1212

1313
fileprivate static let consumerKeyName = "consumerKey"
1414
fileprivate static let consumerSecretName = "consumerSecret"
15-
15+
private static let tokenKeyName = "tokenKey"
16+
private static let tokenSecretName = "tokenSecret"
17+
1618
static func setConsumerKey(_ consumerKey: String?) {
1719
Foundation.UserDefaults.standard.setValue(consumerKey, forKey: self.consumerKeyName)
1820
}
@@ -28,4 +30,20 @@ struct UserDefaults {
2830
static func consumerSecret() -> String? {
2931
return Foundation.UserDefaults.standard.string(forKey: consumerSecretName)
3032
}
33+
34+
static func setToken(_ token: String) {
35+
Foundation.UserDefaults.standard.setValue(token, forKey: tokenKeyName)
36+
}
37+
38+
static func setTokenSecret(_ tokenSecret: String) {
39+
Foundation.UserDefaults.standard.setValue(tokenSecret, forKey: tokenSecretName)
40+
}
41+
42+
static func token() -> String? {
43+
return Foundation.UserDefaults.standard.string(forKey: tokenKeyName)
44+
}
45+
46+
static func tokenSecret() -> String? {
47+
return Foundation.UserDefaults.standard.string(forKey: tokenSecretName)
48+
}
3149
}

MeetupTweet/Classes/ViewControllers/AuthViewController.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,22 @@ class AuthViewController: NSViewController {
3535

3636
override func viewDidLoad() {
3737
super.viewDidLoad()
38-
38+
3939
let authViewModel = AuthViewModel(
4040
consumerKey: consumerKeyTextFeild.rx.text.orEmpty.asObservable(),
4141
consumerSecret: consumerSecretTextField.rx.text.orEmpty.asObservable(),
4242
authrorizeTap: authorizeButton.rx.tap.asObservable()
4343
)
44-
44+
4545
authViewModel.validated
4646
.bind(to: authorizeButton.rx.isEnabled)
4747
.addDisposableTo(disposeBag)
48-
48+
4949
authViewModel.authorized
5050
.subscribe(onNext: { [unowned self] authorize in
5151
self.dismiss(nil)
52-
}, onError: { error in
53-
print(error)
52+
}, onError: { error in
53+
print(error)
5454
}).addDisposableTo(disposeBag)
5555
}
5656

MeetupTweet/Classes/ViewControllers/TweetSearchViewController.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,15 @@ class TweetSearchViewController: NSViewController {
6262
})
6363
.addDisposableTo(disposeBag)
6464

65+
AppDelegate.sharedInstance.applyToken()
6566
}
6667

6768
override func viewWillAppear() {
6869
super.viewWillAppear()
69-
self.performSegue(withIdentifier: NSStoryboardSegue.Identifier("AuthSegueIdentifier"), sender: nil)
70+
71+
if AppDelegate.sharedInstance.oauthClient == nil {
72+
performSegue(withIdentifier: NSStoryboardSegue.Identifier("AuthSegueIdentifier"), sender: nil)
73+
}
7074
}
7175
}
7276

MeetupTweet/Info.plist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<key>CFBundlePackageType</key>
1818
<string>APPL</string>
1919
<key>CFBundleShortVersionString</key>
20-
<string>1.2</string>
20+
<string>1.3</string>
2121
<key>CFBundleSignature</key>
2222
<string>????</string>
2323
<key>CFBundleURLTypes</key>
@@ -32,7 +32,7 @@
3232
</dict>
3333
</array>
3434
<key>CFBundleVersion</key>
35-
<string>1.2</string>
35+
<string>1.3</string>
3636
<key>LSMinimumSystemVersion</key>
3737
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
3838
<key>LSUIElement</key>

0 commit comments

Comments
 (0)