forked from passwa11/ClashX
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add update external resources action
- Loading branch information
1 parent
6cf0b5e
commit 1d2abc3
Showing
11 changed files
with
207 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// | ||
// BaseAction.swift | ||
// ClashX | ||
// | ||
// Created by yicheng on 2023/9/4. | ||
// Copyright © 2023 west2online. All rights reserved. | ||
// | ||
|
||
protocol BaseStaticAction { | ||
static func run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// | ||
// UpdateExternalResourceAction.swift | ||
// ClashX | ||
// | ||
// Created by yicheng on 2023/9/4. | ||
// Copyright © 2023 west2online. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
enum UpdateExternalResourceAction:BaseStaticAction { | ||
static func run() { | ||
ApiRequest.requestExternalProviderNames { provider in | ||
let group = DispatchGroup() | ||
var successCount = 0 | ||
let totalCount = provider.proxies.count + provider.rules.count | ||
if totalCount == 0 { | ||
self.onFinished(success: 0, total: 0, fails: []) | ||
return | ||
} | ||
var fails = [String]() | ||
for name in provider.proxies { | ||
group.enter() | ||
ApiRequest.updateProvider(name: name, type: .proxy) { success in | ||
if success { successCount += 1 } else { | ||
fails.append(name) | ||
} | ||
group.leave() | ||
} | ||
} | ||
|
||
for name in provider.rules { | ||
group.enter() | ||
ApiRequest.updateProvider(name: name, type: .rule) { success in | ||
if success { successCount += 1 } else { | ||
fails.append(name) | ||
} | ||
group.leave() | ||
} | ||
} | ||
|
||
group.notify(queue: .main) { | ||
self.onFinished(success: successCount, total: totalCount, fails: fails) | ||
} | ||
|
||
} | ||
} | ||
|
||
private static func onFinished(success:Int, total: Int, fails: [String]) { | ||
var info = String(format: NSLocalizedString("total: %d, success: %d", comment: ""), total, success) | ||
if !fails.isEmpty { | ||
info.append(String(format: NSLocalizedString("fails: %@", comment: ""), fails.joined(separator: " "))) | ||
} | ||
NSUserNotificationCenter.default.post(title: NSLocalizedString("Update external resource complete", comment: ""), info: info) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.