This repository was archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathAppDelegate.swift
112 lines (102 loc) · 4.07 KB
/
AppDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
// AppDelegate.swift
// reddift
//
// Created by sonson on 2015/09/01.
// Copyright (c) 2015年 sonson. All rights reserved.
//
import reddift
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, ImageCache {
var session: Session?
var window: UIWindow?
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey: Any] = [:]) -> Bool {
// handle redirect URL from reddit.com
return OAuth2Authorizer.sharedInstance.receiveRedirect(url as URL, completion: {(result) -> Void in
switch result {
case .failure(let error):
print(error)
case .success(let token):
DispatchQueue.main.async(execute: { () -> Void in
do {
try OAuth2TokenRepository.save(token: token, of: token.name)
NotificationCenter.default.post(name: OAuth2TokenRepositoryDidSaveTokenName, object: nil, userInfo: nil)
} catch { print(error) }
})
}
})
}
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
// handle redirect URL from reddit.com
return OAuth2Authorizer.sharedInstance.receiveRedirect(url as URL, completion: {(result) -> Void in
switch result {
case .failure(let error):
print(error)
case .success(let token):
DispatchQueue.main.async(execute: { () -> Void in
do {
try OAuth2TokenRepository.save(token: token, of: token.name)
NotificationCenter.default.post(name: OAuth2TokenRepositoryDidSaveTokenName, object: nil, userInfo: nil)
} catch { print(error) }
})
}
})
}
func refreshSession() {
// refresh current session token
do {
try self.session?.refreshToken({ (result) -> Void in
switch result {
case .failure(let error):
print(error)
case .success(let token):
DispatchQueue.main.async(execute: { () -> Void in
print(token)
NotificationCenter.default.post(name: OAuth2TokenRepositoryDidSaveTokenName, object: nil, userInfo: nil)
})
}
})
} catch { print(error) }
}
func reloadSession() {
// reddit username is save NSUserDefaults using "currentName" key.
// create an authenticated or anonymous session object
if let currentName = UserDefaults.standard.object(forKey: "currentName") as? String {
do {
let token = try OAuth2TokenRepository.token(of: currentName)
self.session = Session(token: token)
self.refreshSession()
} catch { print(error) }
} else {
self.session = Session()
}
NotificationCenter.default.post(name: OAuth2TokenRepositoryDidUpdateTokenName, object: nil, userInfo: nil)
}
func applicationDidFinishLaunching(_ application: UIApplication) {
self.reloadSession()
// deleteAllThumbnails()
// deleteAllCaches()
DispatchQueue.global(qos: .default).async {
let html = ""
do {
if let data = html.data(using: .unicode) {
let attr = try NSAttributedString(data: data, options: [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
print(attr)
}
} catch {
}
}
}
func applicationWillEnterForeground(_ application: UIApplication) {
self.refreshSession()
}
}
extension UIApplication {
static func appDelegate() -> AppDelegate? {
if let obj = self.shared.delegate as? AppDelegate {
return obj
}
return nil
}
}