Skip to content

Commit

Permalink
save user-rules into UserDefaults
Browse files Browse the repository at this point in the history
  • Loading branch information
yanue committed Mar 6, 2019
1 parent f13f588 commit 8517451
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "V2rayU/ConfigWindow.swift"
timestampString = "573574739.490474"
timestampString = "573575018.389261"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "260"
Expand All @@ -26,7 +26,7 @@
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "V2rayU/ConfigWindow.swift"
timestampString = "573574739.491349"
timestampString = "573575018.390221"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "264"
Expand Down
7 changes: 6 additions & 1 deletion V2rayU/PacUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ func GeneratePACFile() -> Bool {
let str = String(data: data, encoding: String.Encoding.utf8)
var lines = str!.components(separatedBy: CharacterSet.newlines)

// read userRules from UserDefaults
let userRules = UserDefaults.get(forKey: .userRules)
if userRules != nil {
try userRules!.data(using: String.Encoding.utf8)?.write(to: URL(fileURLWithPath: PACUserRuleFilePath), options: .atomic)
}

do {
let userRuleStr = try String(contentsOfFile: PACUserRuleFilePath, encoding: String.Encoding.utf8)
let userRuleLines = userRuleStr.components(separatedBy: CharacterSet.newlines)
Expand All @@ -47,7 +53,6 @@ func GeneratePACFile() -> Bool {
} catch {
NSLog("Not found user-rule.txt")
}

// Filter empty and comment lines
lines = lines.filter({ (s: String) -> Bool in
if s.isEmpty {
Expand Down
13 changes: 11 additions & 2 deletions V2rayU/UserRulesWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ class UserRulesWindowController: NSWindowController {
override func windowDidLoad() {
super.windowDidLoad()

let str = try? String(contentsOfFile: PACUserRuleFilePath, encoding: String.Encoding.utf8)
userRulesView.string = str!
// read userRules from UserDefaults
let txt = UserDefaults.get(forKey: .userRules)
if txt != nil {
userRulesView.string = txt!
} else {
let str = try? String(contentsOfFile: PACUserRuleFilePath, encoding: String.Encoding.utf8)
userRulesView.string = str!
}
}

@IBAction func didCancel(_ sender: AnyObject) {
Expand All @@ -29,6 +35,9 @@ class UserRulesWindowController: NSWindowController {
@IBAction func didOK(_ sender: AnyObject) {
if let str = userRulesView?.string {
do {
// save user rules into UserDefaults
UserDefaults.set(forKey: .userRules, value: str)

try str.data(using: String.Encoding.utf8)?.write(to: URL(fileURLWithPath: PACUserRuleFilePath), options: .atomic)

if GeneratePACFile() {
Expand Down
2 changes: 2 additions & 0 deletions V2rayU/Util.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ extension UserDefaults {
case autoLaunch
// pac|manual|global
case runMode
// use rules
case userRules
}

static func setBool(forKey key: KEY, value: Bool) {
Expand Down
2 changes: 1 addition & 1 deletion V2rayUTool/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class V2rayUTool: NSObject {
let commitRet = SCPreferencesCommitChanges(prefRef)
let applyRet = SCPreferencesApplyChanges(prefRef)
SCPreferencesSynchronize(prefRef)
// AuthorizationFree(authRef, kAuthorizationFlagDefaults)
// AuthorizationFree(authRef, kAuthorizationFlagDefaults)
Swift.print("after SCPreferencesCommitChanges: commitRet = \(commitRet), applyRet = \(applyRet)")
}
}
Expand Down

0 comments on commit 8517451

Please sign in to comment.