Skip to content

Commit

Permalink
fix: data drop when out of table
Browse files Browse the repository at this point in the history
  • Loading branch information
noppefoxwolf committed May 30, 2021
1 parent ce273b9 commit 732041b
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 20 deletions.
12 changes: 9 additions & 3 deletions Example/Shared/CustomComplication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ import DebugMenu

public class CustomComplication: ComplicationPresentable {
public init() {}
public var title: String = "Date"
private var text: String = ""
public func startMonitoring() {}
public func stopMonitoring() {}
public let fetcher: MetricsFetcher = .text {
public func update() {
let formatter = DateFormatter()
formatter.dateFormat = "HH:mm:ss"
return formatter.string(from: Date())
text = formatter.string(from: Date())
}
public var fetcher: MetricsFetcher {
.text { [weak self] in
self?.text ?? ""
}
}
public var title: String = "Date"
}
1 change: 1 addition & 0 deletions Sources/DebugMenu/Entity/ComplicationPresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public protocol ComplicationPresentable {
var title: String { get }
func startMonitoring()
func stopMonitoring()
func update()
var fetcher: MetricsFetcher { get }
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ public class CPUGraphComplication: ComplicationPresentable {
private var data: [Double] = []
public func startMonitoring() {}
public func stopMonitoring() {}
public func update() {
let metrics = Device.current.cpuUsage()
data.append(Double(metrics * 100.0))
}
public var fetcher: MetricsFetcher {
.graph { [weak self] in
guard let self = self else { return [] }
let metrics = Device.current.cpuUsage()
self.data.append(Double(metrics * 100.0))
return self.data
self?.data ?? []
}
}
}
12 changes: 9 additions & 3 deletions Sources/DebugMenu/Plugin/Complication/CPUUsageComplication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@

import Foundation

public struct CPUUsageComplication: ComplicationPresentable {
public class CPUUsageComplication: ComplicationPresentable {
public init() {}
public let title: String = "CPU"
private var text: String = ""
public func startMonitoring() {}
public func stopMonitoring() {}
public let fetcher: MetricsFetcher = .text {
Device.current.localizedCPUUsage
public func update() {
text = Device.current.localizedCPUUsage
}
public var fetcher: MetricsFetcher {
.text { [weak self] in
self?.text ?? ""
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions Sources/DebugMenu/Plugin/Complication/FPSComplication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class FPSComplication: ComplicationPresentable {
public init() {}

public func startMonitoring() {
displayLink = .init(target: self, selector: #selector(update))
displayLink = .init(target: self, selector: #selector(updateDisplayLink))
displayLink?.preferredFramesPerSecond = 0
displayLink?.add(to: .main, forMode: .common)
}
Expand All @@ -29,7 +29,7 @@ public class FPSComplication: ComplicationPresentable {
displayLink = nil
}

@objc func update(_ displayLink: CADisplayLink) {
@objc func updateDisplayLink(_ displayLink: CADisplayLink) {
if displayLink.timestamp - lastupdated > 1.0 {
currentFPS = updateCount
updateCount = 1
Expand All @@ -39,6 +39,10 @@ public class FPSComplication: ComplicationPresentable {
}
}

public func update() {

}

public var fetcher: MetricsFetcher {
.text { [weak self] in
guard let self = self else { return "-" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@

import Foundation

public struct GPUMemoryUsageComplication: ComplicationPresentable {
public class GPUMemoryUsageComplication: ComplicationPresentable {
public init() {}
public let title: String = "GPU MEM"
private var text: String = ""
public func startMonitoring() {}
public func stopMonitoring() {}
public let fetcher: MetricsFetcher = .text {
Device.current.localizedGPUMemory
public func update() {
text = Device.current.localizedGPUMemory
}
public var fetcher: MetricsFetcher {
.text { [weak self] in
self?.text ?? ""
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ public class IntervalComplication: ComplicationPresentable {
}
}.store(in: &cancellables)
}

public func stopMonitoring() {
cancellables = []
}

public func update() {

}

public var fetcher: MetricsFetcher {
.interval { [weak self] in
guard let self = self else { return [] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@

import Foundation

public struct MemoryUsageComplication: ComplicationPresentable {
public class MemoryUsageComplication: ComplicationPresentable {
public init() {}
public let title: String = "MEM"
private var text: String = ""
public func startMonitoring() {}
public func stopMonitoring() {}
public let fetcher: MetricsFetcher = .text {
Device.current.localizedMemoryUsage
public func update() {
text = Device.current.localizedMemoryUsage
}
public var fetcher: MetricsFetcher {
.text { [weak self] in
self?.text ?? ""
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public class NetworkUsageComplication: ComplicationPresentable {

public func startMonitoring() {
Timer.publish(every: 1, on: .main, in: .default).autoconnect().sink { [weak self] _ in
self?.update()
self?.updateNetworkUsage()
}.store(in: &cancellables)
}

public func stopMonitoring() {
cancellables = []
}

private func update() {
private func updateNetworkUsage() {
let networkUsage = Device.current.networkUsage()
if let lastUsage = lastNetworkUsage, let newUsage = networkUsage {
sendPerSec = newUsage.sent - lastUsage.sent
Expand All @@ -36,6 +36,10 @@ public class NetworkUsageComplication: ComplicationPresentable {
lastNetworkUsage = networkUsage
}

public func update() {

}

public var fetcher: MetricsFetcher {
.text { [weak self] in
guard let self = self else { return "-" }
Expand Down
1 change: 1 addition & 0 deletions Sources/DebugMenu/View/WidgetView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class WidgetView: UIVisualEffectView {
}

private func reloadData() {
complications.forEach({ $0.update() })
tableView.reloadData()
}
}
Expand Down

0 comments on commit 732041b

Please sign in to comment.