Skip to content

Commit

Permalink
Improve persist
Browse files Browse the repository at this point in the history
  • Loading branch information
lslqtz committed Sep 30, 2024
1 parent dd00af8 commit 643bc88
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
12 changes: 9 additions & 3 deletions Sources/bclm_loop/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,12 @@ struct BCLMLoop: ParsableCommand {
func run() {
if persist(false) {
print("Already persisting! Re-persist...\n")
_ = removePlist()
}

if updatePlist(targetBatteryLevel: targetBatteryLevel, targetBatteryMargin: targetBatteryMargin) && persist(true) {
print("Success!")
}
updatePlist(targetBatteryLevel: targetBatteryLevel, targetBatteryMargin: targetBatteryMargin)
_ = persist(true)
}
}

Expand All @@ -452,7 +455,10 @@ struct BCLMLoop: ParsableCommand {
if !persist(false) {
fputs("Already not persisting!\n", stderr)
}
removePlist()

if removePlist() {
print("Success!")
}
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions Sources/bclm_loop/persist.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func isPersistent() -> Bool {
}
}

func updatePlist(targetBatteryLevel: Int, targetBatteryMargin: Int) {
func updatePlist(targetBatteryLevel: Int, targetBatteryMargin: Int) -> Bool {
let preferences =
Preferences(
Label: plist,
Expand All @@ -89,15 +89,21 @@ func updatePlist(targetBatteryLevel: Int, targetBatteryMargin: Int) {
do {
let data = try encoder.encode(preferences)
try data.write(to: path)
return true
} catch {
print(error)
}

return false
}

func removePlist() {
func removePlist() -> Bool {
do {
try FileManager.default.removeItem(at: URL(fileURLWithPath: plist_path))
return true
} catch {
print(error)
}

return false
}

0 comments on commit 643bc88

Please sign in to comment.