Skip to content

Commit

Permalink
feat(shortcuts): update AppIntentsHandler.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
jurajhilje committed Sep 4, 2024
1 parent 5b43150 commit a2a8f7c
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 51 deletions.
124 changes: 124 additions & 0 deletions IVPNClient/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,99 @@ class AppDelegate: UIResponder {
}
}

private func userActivityConnect() {
DispatchQueue.delay(0.75) {
if UserDefaults.shared.networkProtectionEnabled {
Application.shared.connectionManager.resetRulesAndConnectShortcut(closeApp: true, actionType: .connect)
return
}
Application.shared.connectionManager.connectShortcut(closeApp: true, actionType: .connect)
}
}

private func userActivityDisconnect() {
DispatchQueue.delay(0.75) {
if UserDefaults.shared.networkProtectionEnabled {
Application.shared.connectionManager.resetRulesAndDisconnectShortcut(closeApp: true, actionType: .disconnect)
return
}
Application.shared.connectionManager.disconnectShortcut(closeApp: true, actionType: .disconnect)
}
}

private func userActivityAntiTrackerEnable() {
DispatchQueue.async {
if let viewController = UIApplication.topViewController() {
if Application.shared.settings.connectionProtocol.tunnelType() == .ipsec {
viewController.showAlert(title: "IKEv2 not supported", message: "AntiTracker is supported only for OpenVPN and WireGuard protocols.") { _ in
}
return
}

UserDefaults.shared.set(true, forKey: UserDefaults.Key.isAntiTracker)
NotificationCenter.default.post(name: Notification.Name.AntiTrackerUpdated, object: nil)
if UIApplication.topViewController() as? MainViewController != nil {
NotificationCenter.default.post(name: Notification.Name.EvaluateReconnect, object: nil)
} else {
viewController.evaluateReconnect(sender: viewController.view)
}
}
}
}

private func userActivityAntiTrackerDisable() {
DispatchQueue.async {
if let viewController = UIApplication.topViewController() {
UserDefaults.shared.set(false, forKey: UserDefaults.Key.isAntiTracker)
NotificationCenter.default.post(name: Notification.Name.AntiTrackerUpdated, object: nil)
if UIApplication.topViewController() as? MainViewController != nil {
NotificationCenter.default.post(name: Notification.Name.EvaluateReconnect, object: nil)
} else {
viewController.evaluateReconnect(sender: viewController.view)
}
}
}
}

private func userActivityCustomDNSEnable() {
DispatchQueue.async {
if let viewController = UIApplication.topViewController() {
if Application.shared.settings.connectionProtocol.tunnelType() == .ipsec {
viewController.showAlert(title: "IKEv2 not supported", message: "Custom DNS is supported only for OpenVPN and WireGuard protocols.") { _ in
}
return
}

guard !UserDefaults.shared.customDNS.isEmpty else {
viewController.showAlert(title: "", message: "Please enter DNS server info")
return
}

UserDefaults.shared.set(true, forKey: UserDefaults.Key.isCustomDNS)
NotificationCenter.default.post(name: Notification.Name.CustomDNSUpdated, object: nil)
if UIApplication.topViewController() as? MainViewController != nil {
NotificationCenter.default.post(name: Notification.Name.EvaluateReconnect, object: nil)
} else {
viewController.evaluateReconnect(sender: viewController.view)
}
}
}
}

private func userActivityCustomDNSDisable() {
DispatchQueue.async {
if let viewController = UIApplication.topViewController() {
UserDefaults.shared.set(false, forKey: UserDefaults.Key.isCustomDNS)
NotificationCenter.default.post(name: Notification.Name.CustomDNSUpdated, object: nil)
if UIApplication.topViewController() as? MainViewController != nil {
NotificationCenter.default.post(name: Notification.Name.EvaluateReconnect, object: nil)
} else {
viewController.evaluateReconnect(sender: viewController.view)
}
}
}
}

private func startPurchaseObserver() {
PurchaseManager.shared.delegate = self
PurchaseManager.shared.startObserver()
Expand Down Expand Up @@ -268,6 +361,37 @@ extension AppDelegate: UIApplicationDelegate {
return true
}

func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
if let url = userActivity.webpageURL {
let endpoint = url.lastPathComponent
handleURLEndpoint(endpoint)
return false
}

guard Application.shared.authentication.isLoggedIn, Application.shared.serviceStatus.isActive else {
return false
}

switch userActivity.activityType {
case UserActivityType.Connect:
userActivityConnect()
case UserActivityType.Disconnect:
userActivityDisconnect()
case UserActivityType.AntiTrackerEnable:
userActivityAntiTrackerEnable()
case UserActivityType.AntiTrackerDisable:
userActivityAntiTrackerDisable()
case UserActivityType.CustomDNSEnable:
userActivityCustomDNSEnable()
case UserActivityType.CustomDNSDisable:
userActivityCustomDNSDisable()
default:
log(.info, message: "No such user activity")
}

return false
}

}

// MARK: - PurchaseManagerDelegate -
Expand Down
78 changes: 27 additions & 51 deletions IVPNClient/Scenes/MainScreen/AppIntentsHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extension MainViewController {
Application.shared.connectionManager.resetRulesAndConnectShortcut(closeApp: true, actionType: .connect)
return
}
Application.shared.connectionManager.connectShortcut(closeApp: true, actionType: .connect)
Application.shared.connectionManager.connect()
}
}

Expand All @@ -28,80 +28,56 @@ extension MainViewController {
Application.shared.connectionManager.resetRulesAndDisconnectShortcut(closeApp: true, actionType: .disconnect)
return
}
Application.shared.connectionManager.disconnectShortcut(closeApp: true, actionType: .disconnect)
Application.shared.connectionManager.disconnect()
}
}

@objc func intentAntiTrackerEnable() {
DispatchQueue.async {
if let viewController = UIApplication.topViewController() {
if Application.shared.settings.connectionProtocol.tunnelType() == .ipsec {
viewController.showAlert(title: "IKEv2 not supported", message: "AntiTracker is supported only for OpenVPN and WireGuard protocols.") { _ in
}
return
}

UserDefaults.shared.set(true, forKey: UserDefaults.Key.isAntiTracker)
NotificationCenter.default.post(name: Notification.Name.AntiTrackerUpdated, object: nil)
if UIApplication.topViewController() as? MainViewController != nil {
NotificationCenter.default.post(name: Notification.Name.EvaluateReconnect, object: nil)
} else {
viewController.evaluateReconnect(sender: viewController.view)
if Application.shared.settings.connectionProtocol.tunnelType() == .ipsec {
self.showAlert(title: "IKEv2 not supported", message: "AntiTracker is supported only for OpenVPN and WireGuard protocols.") { _ in
}
return
}

UserDefaults.shared.set(true, forKey: UserDefaults.Key.isAntiTracker)
NotificationCenter.default.post(name: Notification.Name.AntiTrackerUpdated, object: nil)
self.evaluateReconnect(sender: self.view)
}
}

@objc func intentAntiTrackerDisable() {
DispatchQueue.async {
if let viewController = UIApplication.topViewController() {
UserDefaults.shared.set(false, forKey: UserDefaults.Key.isAntiTracker)
NotificationCenter.default.post(name: Notification.Name.AntiTrackerUpdated, object: nil)
if UIApplication.topViewController() as? MainViewController != nil {
NotificationCenter.default.post(name: Notification.Name.EvaluateReconnect, object: nil)
} else {
viewController.evaluateReconnect(sender: viewController.view)
}
}
UserDefaults.shared.set(false, forKey: UserDefaults.Key.isAntiTracker)
NotificationCenter.default.post(name: Notification.Name.AntiTrackerUpdated, object: nil)
self.evaluateReconnect(sender: self.view)
}
}

@objc func intentCustomDNSEnable() {
DispatchQueue.async {
if let viewController = UIApplication.topViewController() {
if Application.shared.settings.connectionProtocol.tunnelType() == .ipsec {
viewController.showAlert(title: "IKEv2 not supported", message: "Custom DNS is supported only for OpenVPN and WireGuard protocols.") { _ in
}
return
}

guard !UserDefaults.shared.customDNS.isEmpty else {
viewController.showAlert(title: "", message: "Please enter DNS server info")
return
}

UserDefaults.shared.set(true, forKey: UserDefaults.Key.isCustomDNS)
NotificationCenter.default.post(name: Notification.Name.CustomDNSUpdated, object: nil)
if UIApplication.topViewController() as? MainViewController != nil {
NotificationCenter.default.post(name: Notification.Name.EvaluateReconnect, object: nil)
} else {
viewController.evaluateReconnect(sender: viewController.view)
if Application.shared.settings.connectionProtocol.tunnelType() == .ipsec {
self.showAlert(title: "IKEv2 not supported", message: "Custom DNS is supported only for OpenVPN and WireGuard protocols.") { _ in
}
return
}

guard !UserDefaults.shared.customDNS.isEmpty else {
self.showAlert(title: "", message: "Please enter DNS server info")
return
}

UserDefaults.shared.set(true, forKey: UserDefaults.Key.isCustomDNS)
NotificationCenter.default.post(name: Notification.Name.CustomDNSUpdated, object: nil)
self.evaluateReconnect(sender: self.view)
}
}

@objc func intentCustomDNSDisable() {
DispatchQueue.async {
if let viewController = UIApplication.topViewController() {
UserDefaults.shared.set(false, forKey: UserDefaults.Key.isCustomDNS)
NotificationCenter.default.post(name: Notification.Name.CustomDNSUpdated, object: nil)
if UIApplication.topViewController() as? MainViewController != nil {
NotificationCenter.default.post(name: Notification.Name.EvaluateReconnect, object: nil)
} else {
viewController.evaluateReconnect(sender: viewController.view)
}
}
UserDefaults.shared.set(false, forKey: UserDefaults.Key.isCustomDNS)
NotificationCenter.default.post(name: Notification.Name.CustomDNSUpdated, object: nil)
self.evaluateReconnect(sender: self.view)
}
}

Expand Down

0 comments on commit a2a8f7c

Please sign in to comment.