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

Commit

Permalink
Merge pull request #18 from yimajo/use_token_caches
Browse files Browse the repository at this point in the history
毎回ログインさせてたのを改善した
  • Loading branch information
yimajo authored Aug 28, 2018
2 parents 1a4a231 + 1fb0e3c commit 1abec95
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 11 deletions.
30 changes: 28 additions & 2 deletions MeetupTweet/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,16 @@ class AppDelegate: NSObject, NSApplicationDelegate {
return Observable.create { [unowned self] observer in

self.requestHandle = oauthSwift.authorize(withCallbackURL: "\(self.callBackHost)://", success: { credential, response, parameters in

self.oauthClient = OAuthClient(consumerKey: consumerKey, consumerSecret: consumerSecret, accessToken: credential.oauthToken, accessTokenSecret: credential.oauthTokenSecret)

UserDefaults.setToken(credential.oauthToken)
UserDefaults.setTokenSecret(credential.oauthTokenSecret)

self.oauthClient = OAuthClient(
consumerKey: consumerKey,
consumerSecret: consumerSecret,
accessToken: credential.oauthToken,
accessTokenSecret: credential.oauthTokenSecret
)

observer.onNext(true)

Expand All @@ -65,4 +73,22 @@ class AppDelegate: NSObject, NSApplicationDelegate {
return Disposables.create()
}
}

func applyToken() {
guard let consumerKey = UserDefaults.consumerKey(),
let consumerSecret = UserDefaults.consumerSecret(),
let token = UserDefaults.token(),
let tokenSecret = UserDefaults.tokenSecret() else {

return
}

self.oauthClient = OAuthClient(
consumerKey: consumerKey,
consumerSecret: consumerSecret,
accessToken: token,
accessTokenSecret: tokenSecret
)

}
}
10 changes: 10 additions & 0 deletions MeetupTweet/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,13 @@
<modifierMask key="keyEquivalentModifierMask"/>
</menuItem>
<menuItem isSeparatorItem="YES" id="osT-xY-EjA"/>
<menuItem title="Show Login" id="xyw-7a-v39">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<segue destination="AmD-ro-1ro" kind="modal" id="U9R-h4-M9D"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="fnu-PY-yUN"/>
<menuItem title="Preferences…" keyEquivalent="," id="hWp-Qc-U1e"/>
<menuItem isSeparatorItem="YES" id="vbj-5j-zMV"/>
<menuItem title="Quit" keyEquivalent="q" id="Ib2-2u-3oL">
Expand Down Expand Up @@ -376,4 +383,7 @@ AAAAAAAAAAAAAAAACnA
</mutableData>
</image>
</resources>
<inferredMetricsTieBreakers>
<segue reference="Ykf-aF-LDN"/>
</inferredMetricsTieBreakers>
</document>
20 changes: 19 additions & 1 deletion MeetupTweet/Classes/Infra/UserDefaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ struct UserDefaults {

fileprivate static let consumerKeyName = "consumerKey"
fileprivate static let consumerSecretName = "consumerSecret"

private static let tokenKeyName = "tokenKey"
private static let tokenSecretName = "tokenSecret"

static func setConsumerKey(_ consumerKey: String?) {
Foundation.UserDefaults.standard.setValue(consumerKey, forKey: self.consumerKeyName)
}
Expand All @@ -28,4 +30,20 @@ struct UserDefaults {
static func consumerSecret() -> String? {
return Foundation.UserDefaults.standard.string(forKey: consumerSecretName)
}

static func setToken(_ token: String) {
Foundation.UserDefaults.standard.setValue(token, forKey: tokenKeyName)
}

static func setTokenSecret(_ tokenSecret: String) {
Foundation.UserDefaults.standard.setValue(tokenSecret, forKey: tokenSecretName)
}

static func token() -> String? {
return Foundation.UserDefaults.standard.string(forKey: tokenKeyName)
}

static func tokenSecret() -> String? {
return Foundation.UserDefaults.standard.string(forKey: tokenSecretName)
}
}
10 changes: 5 additions & 5 deletions MeetupTweet/Classes/ViewControllers/AuthViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@ class AuthViewController: NSViewController {

override func viewDidLoad() {
super.viewDidLoad()

let authViewModel = AuthViewModel(
consumerKey: consumerKeyTextFeild.rx.text.orEmpty.asObservable(),
consumerSecret: consumerSecretTextField.rx.text.orEmpty.asObservable(),
authrorizeTap: authorizeButton.rx.tap.asObservable()
)

authViewModel.validated
.bind(to: authorizeButton.rx.isEnabled)
.addDisposableTo(disposeBag)

authViewModel.authorized
.subscribe(onNext: { [unowned self] authorize in
self.dismiss(nil)
}, onError: { error in
print(error)
}, onError: { error in
print(error)
}).addDisposableTo(disposeBag)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,15 @@ class TweetSearchViewController: NSViewController {
})
.addDisposableTo(disposeBag)

AppDelegate.sharedInstance.applyToken()
}

override func viewWillAppear() {
super.viewWillAppear()
self.performSegue(withIdentifier: NSStoryboardSegue.Identifier("AuthSegueIdentifier"), sender: nil)

if AppDelegate.sharedInstance.oauthClient == nil {
performSegue(withIdentifier: NSStoryboardSegue.Identifier("AuthSegueIdentifier"), sender: nil)
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions MeetupTweet/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.2</string>
<string>1.3</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleURLTypes</key>
Expand All @@ -32,7 +32,7 @@
</dict>
</array>
<key>CFBundleVersion</key>
<string>1.2</string>
<string>1.3</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>LSUIElement</key>
Expand Down

0 comments on commit 1abec95

Please sign in to comment.