frenzy-ios/Reader/AppDelegate.swift

91 lines
4.0 KiB
Swift
Raw Normal View History

2021-12-08 02:58:02 +00:00
//
// AppDelegate.swift
// Reader
//
// Created by Shadowfacts on 10/29/21.
//
import UIKit
import WebKit
import OSLog
2021-12-08 02:58:02 +00:00
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
swizzleWKWebView()
2021-12-08 02:58:02 +00:00
return true
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
2022-01-15 19:09:30 +00:00
return UISceneConfiguration(name: "main", sessionRole: connectingSceneSession.role)
2021-12-08 02:58:02 +00:00
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
2022-01-15 19:09:30 +00:00
override func buildMenu(with builder: UIMenuBuilder) {
if builder.system == .main {
var children: [UIMenuElement] = LocalData.accounts.map { account in
var title = account.instanceURL.host!
if let port = account.instanceURL.port, port != 80 && port != 443 {
title += ":\(port)"
}
let state: UIAction.State
if let activeScene = UIApplication.shared.connectedScenes.first(where: { $0.activationState == .foregroundActive }),
(activeScene.delegate as! SceneDelegate).fervorController?.account?.id == account.id {
state = .on
} else {
state = .off
}
return UIAction(title: title, attributes: [], state: state) { _ in
let activity = NSUserActivity.activateAccount(account)
let options = UIScene.ActivationRequestOptions()
#if targetEnvironment(macCatalyst)
options.collectionJoinBehavior = .disallowed
#endif
UIApplication.shared.requestSceneSessionActivation(nil, userActivity: activity, options: options, errorHandler: nil)
}
}
children.append(UIAction(title: "Add Account...", handler: { _ in
let activity = NSUserActivity.addAccount()
let options = UIScene.ActivationRequestOptions()
#if targetEnvironment(macCatalyst)
options.collectionJoinBehavior = .disallowed
#endif
UIApplication.shared.requestSceneSessionActivation(nil, userActivity: activity, options: options, errorHandler: nil)
}))
let account = UIMenu(title: "Account", image: nil, identifier: nil, options: [], children: children)
builder.insertSibling(account, afterMenu: .file)
}
}
private func swizzleWKWebView() {
let selector = Selector(("_updateScrollViewBackground"))
var originalIMP: IMP?
let imp = imp_implementationWithBlock({ (self: WKWebView) in
if let originalIMP = originalIMP {
let original = unsafeBitCast(originalIMP, to: (@convention(c) (WKWebView, Selector) -> Void).self)
original(self, selector)
} else {
os_log(.error, "Missing originalIMP for -[WKWebView _updateScrollViewBackground], did WebKit change?")
}
self.scrollView.indicatorStyle = .default
} as (@convention(block) (WKWebView) -> Void))
originalIMP = class_replaceMethod(WKWebView.self, selector, imp, "v@:")
}
2021-12-08 02:58:02 +00:00
}