feat: refactor battery and network state to use file history and save logic
This commit is contained in:
@@ -3,6 +3,7 @@ import QtQuick
|
||||
import Quickshell
|
||||
import Quickshell.Io
|
||||
import Quickshell.Networking
|
||||
import "../../../../config"
|
||||
|
||||
QtObject {
|
||||
id: root
|
||||
@@ -38,6 +39,55 @@ QtObject {
|
||||
|
||||
readonly property var networking: Networking
|
||||
|
||||
// ---- persistence: survives reload AND close (disk) ----
|
||||
// use Config.configDir (base dir) so path is stable and visible in repo
|
||||
readonly property string _historyPath: Config.configDir + "/modules/bar/states/Network/network_history.json"
|
||||
// QtObject has no default property, so hold FileView as typed property
|
||||
property FileView historyFile: FileView {
|
||||
path: _historyPath
|
||||
printErrors: true
|
||||
adapter: JsonAdapter {
|
||||
property var downloadHistory: []
|
||||
property var uploadHistory: []
|
||||
property var pingHistory: []
|
||||
}
|
||||
onLoaded: {
|
||||
function prune(arr) {
|
||||
if (!arr || typeof arr.length !== "number" || arr.length===0) return null
|
||||
const f = arr.filter(p => p && typeof p.time==="number" && typeof p.value==="number")
|
||||
if (f.length < 2) return null
|
||||
return f.length > root._maxHistory ? f.slice(-root._maxHistory) : f
|
||||
}
|
||||
const d = prune(adapter.downloadHistory)
|
||||
if (d) root.downloadHistory = d
|
||||
const u = prune(adapter.uploadHistory)
|
||||
if (u) root.uploadHistory = u
|
||||
const p = prune(adapter.pingHistory)
|
||||
if (p) root.pingHistory = p
|
||||
}
|
||||
onLoadFailed: {} // first run
|
||||
}
|
||||
// periodic save every 10s (restart-debounce would never fire while statsTimer pushes every 1s)
|
||||
property Timer saveTimer: Timer {
|
||||
interval: 10000
|
||||
repeat: true
|
||||
running: true
|
||||
onTriggered: {
|
||||
historyFile.adapter.downloadHistory = root.downloadHistory
|
||||
historyFile.adapter.uploadHistory = root.uploadHistory
|
||||
historyFile.adapter.pingHistory = root.pingHistory
|
||||
historyFile.writeAdapter()
|
||||
}
|
||||
}
|
||||
Component.onDestruction: {
|
||||
if (historyFile.loaded) {
|
||||
historyFile.adapter.downloadHistory = root.downloadHistory
|
||||
historyFile.adapter.uploadHistory = root.uploadHistory
|
||||
historyFile.adapter.pingHistory = root.pingHistory
|
||||
historyFile.writeAdapter()
|
||||
}
|
||||
}
|
||||
|
||||
function findAllConnectedDevices() {
|
||||
var devs = networking.devices.values
|
||||
var result = []
|
||||
@@ -70,7 +120,8 @@ QtObject {
|
||||
var nets = dev.networks.values
|
||||
for (var j = 0; j < nets.length; j++) {
|
||||
if (nets[j].connected) {
|
||||
sig += ":" + nets[j].name
|
||||
// include signalStrength & name so hover updates live on signal change
|
||||
sig += ":" + nets[j].name + ":" + Math.round(nets[j].signalStrength*100)
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -354,4 +405,4 @@ QtObject {
|
||||
Component.onCompleted: {
|
||||
root.updateAll()
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user