Skip to content

Commit

Permalink
refactor: global timer observer counting
Browse files Browse the repository at this point in the history
Signed-off-by: 82Flex <[email protected]>
  • Loading branch information
Lessica committed Jan 15, 2024
1 parent e4c7a87 commit 6a89dd1
Show file tree
Hide file tree
Showing 19 changed files with 78 additions and 109 deletions.
2 changes: 1 addition & 1 deletion Reveil/DataModels/EntryExporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation

struct EntryExporter: Encodable {
let moduleClass: String
let moduleName: String
let moduleName: ModuleName
let basicEntries: [BasicEntry]
let usageEntry: UsageEntry<Double>?

Expand Down
37 changes: 29 additions & 8 deletions Reveil/GlobalTimer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,49 @@ import Combine
import Foundation

protocol GlobalTimerObserver {
func globalTimerEventOccurred(_ timer: GlobalTimer) -> Void
var globalName: String { get }
func eventOccurred(globalTimer timer: GlobalTimer) -> Void
}

final class GlobalTimer: ObservableObject {
struct Observer {
let value: any GlobalTimerObserver
var registeredCount: Int
}

static let shared = GlobalTimer()
private var observers = Set<AnyHashable>()
private var observers = [ModuleName: Observer]()

lazy var timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { [unowned self] _ in
self.observers.forEach { observer in
(observer as? GlobalTimerObserver)?.globalTimerEventOccurred(self)
if observers.isEmpty {
return
}
observers.forEach { [unowned self] (key: ModuleName, observer: Observer) in
observer.value.eventOccurred(globalTimer: self)
}
}

private init() {
timer.fire()
}

func addObserver<T>(_ observer: T) where T: GlobalTimerObserver, T: Hashable {
observers.insert(AnyHashable(observer))
func addObserver<T>(_ observer: T) where T: GlobalTimerObserver {
let globalName = observer.globalName
if observers[globalName] != nil {
observers[globalName]?.registeredCount += 1
return
}
observers[globalName] = Observer(value: observer, registeredCount: 1)
}

func removeObserver<T>(_ observer: T) where T: GlobalTimerObserver, T: Hashable {
observers.remove(AnyHashable(observer))
func removeObserver<T>(_ observer: T) where T: GlobalTimerObserver {
let globalName = observer.globalName
guard observers[globalName] != nil else {
return
}
observers[globalName]?.registeredCount -= 1
if observers[globalName]?.registeredCount == 0 {
observers.removeValue(forKey: globalName)
}
}
}
17 changes: 4 additions & 13 deletions Reveil/Pages/DashboardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

import SwiftUI

struct DashboardView: View {
struct DashboardView: View, GlobalTimerObserver {
let id = UUID()
let globalName: String = String(describing: Dashboard.self)

@StateObject private var viewModel = Dashboard.shared
@StateObject private var securityModel = Security.shared
Expand Down Expand Up @@ -89,20 +90,10 @@ struct DashboardView: View {
viewModel.anyListView(key: entry.key)
.environmentObject(HighlightedEntryKey(object: entry.key))
}, label: { Color.clear })
.contentShape(Rectangle())
.contentShape(Rectangle())
}
}

extension DashboardView: GlobalTimerObserver, Hashable {
static func == (lhs: DashboardView, rhs: DashboardView) -> Bool {
lhs.id == rhs.id
}

func hash(into hasher: inout Hasher) {
hasher.combine(id)
}

func globalTimerEventOccurred(_ timer: GlobalTimer) {
func eventOccurred(globalTimer timer: GlobalTimer) {
viewModel.updateEntries()
}
}
Expand Down
15 changes: 3 additions & 12 deletions Reveil/Pages/Details/BatteryInformationListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

import SwiftUI

struct BatteryInformationListView: View, Identifiable, ModuleListView {
struct BatteryInformationListView: View, Identifiable, ModuleListView, GlobalTimerObserver {
let id = UUID()
let module: Module = BatteryInformation.shared
let globalName: String = String(describing: BatteryInformation.self)

init() {}

Expand Down Expand Up @@ -37,18 +38,8 @@ struct BatteryInformationListView: View, Identifiable, ModuleListView {
GlobalTimer.shared.removeObserver(self)
}
}
}

extension BatteryInformationListView: GlobalTimerObserver, Hashable {
static func == (lhs: BatteryInformationListView, rhs: BatteryInformationListView) -> Bool {
lhs.id == rhs.id
}

func hash(into hasher: inout Hasher) {
hasher.combine(id)
}

func globalTimerEventOccurred(_ timer: GlobalTimer) {
func eventOccurred(globalTimer timer: GlobalTimer) {
module.updateEntries()
}
}
Expand Down
15 changes: 3 additions & 12 deletions Reveil/Pages/Details/CPUInformationListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

import SwiftUI

struct CPUInformationListView: View, Identifiable, ModuleListView {
struct CPUInformationListView: View, Identifiable, ModuleListView, GlobalTimerObserver {
let id = UUID()
let module: Module = CPUInformation.shared
let globalName: String = String(describing: CPUInformation.self)

init() {}

Expand Down Expand Up @@ -37,18 +38,8 @@ struct CPUInformationListView: View, Identifiable, ModuleListView {
GlobalTimer.shared.removeObserver(self)
}
}
}

extension CPUInformationListView: GlobalTimerObserver, Hashable {
static func == (lhs: CPUInformationListView, rhs: CPUInformationListView) -> Bool {
lhs.id == rhs.id
}

func hash(into hasher: inout Hasher) {
hasher.combine(id)
}

func globalTimerEventOccurred(_ timer: GlobalTimer) {
func eventOccurred(globalTimer timer: GlobalTimer) {
module.updateEntries()
}
}
Expand Down
3 changes: 3 additions & 0 deletions Reveil/Pages/Details/DeviceInformationListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SwiftUI

struct DeviceInformationListView: View, ModuleListView {
let module: Module = DeviceInformation.shared
let globalName: String = String(describing: DeviceInformation.self)

init() {}

Expand All @@ -22,6 +23,8 @@ struct DeviceInformationListView: View, ModuleListView {
DetailsListView(basicEntries: module.basicEntries)
.navigationTitle(module.moduleName)
}

func eventOccurred(globalTimer timer: GlobalTimer) { }
}

// MARK: - Previews
Expand Down
15 changes: 3 additions & 12 deletions Reveil/Pages/Details/DiskSpaceListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

import SwiftUI

struct DiskSpaceListView: View, Identifiable, ModuleListView {
struct DiskSpaceListView: View, Identifiable, ModuleListView, GlobalTimerObserver {
let id = UUID()
let module: Module = DiskSpace.shared
let globalName: String = String(describing: DiskSpace.self)

init() {}

Expand Down Expand Up @@ -37,18 +38,8 @@ struct DiskSpaceListView: View, Identifiable, ModuleListView {
GlobalTimer.shared.removeObserver(self)
}
}
}

extension DiskSpaceListView: GlobalTimerObserver, Hashable {
static func == (lhs: DiskSpaceListView, rhs: DiskSpaceListView) -> Bool {
lhs.id == rhs.id
}

func hash(into hasher: inout Hasher) {
hasher.combine(id)
}

func globalTimerEventOccurred(_ timer: GlobalTimer) {
func eventOccurred(globalTimer timer: GlobalTimer) {
module.updateEntries()
}
}
Expand Down
3 changes: 3 additions & 0 deletions Reveil/Pages/Details/FileSystemListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SwiftUI

struct FileSystemListView: View, ModuleListView {
let module: Module = FileSystems.shared
let globalName: String = String(describing: FileSystems.self)

init?(entryKey _: EntryKey) { nil }

Expand All @@ -27,6 +28,8 @@ struct FileSystemListView: View, ModuleListView {
entries = FileSystems.shared.entries(fs: item)
}
}

func eventOccurred(globalTimer timer: GlobalTimer) { }
}

// MARK: - Previews
Expand Down
3 changes: 3 additions & 0 deletions Reveil/Pages/Details/FileSystemsListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SwiftUI

struct FileSystemsListView: View, ModuleListView {
let module: Module = FileSystems.shared
let globalName: String = String(describing: FileSystems.self)

init() {}

Expand Down Expand Up @@ -48,6 +49,8 @@ struct FileSystemsListView: View, ModuleListView {
items = FileSystems.shared.items
}
}

func eventOccurred(globalTimer timer: GlobalTimer) { }
}

// MARK: - Previews
Expand Down
15 changes: 3 additions & 12 deletions Reveil/Pages/Details/MemoryInformationListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

import SwiftUI

struct MemoryInformationListView: View, Identifiable, ModuleListView {
struct MemoryInformationListView: View, Identifiable, ModuleListView, GlobalTimerObserver {
let id = UUID()
let module: Module = MemoryInformation.shared
let globalName: String = String(describing: MemoryInformation.self)

init() {}

Expand Down Expand Up @@ -37,18 +38,8 @@ struct MemoryInformationListView: View, Identifiable, ModuleListView {
GlobalTimer.shared.removeObserver(self)
}
}
}

extension MemoryInformationListView: GlobalTimerObserver, Hashable {
static func == (lhs: MemoryInformationListView, rhs: MemoryInformationListView) -> Bool {
lhs.id == rhs.id
}

func hash(into hasher: inout Hasher) {
hasher.combine(id)
}

func globalTimerEventOccurred(_ timer: GlobalTimer) {
func eventOccurred(globalTimer timer: GlobalTimer) {
module.updateEntries()
}
}
Expand Down
15 changes: 3 additions & 12 deletions Reveil/Pages/Details/NetworkDetailListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

import SwiftUI

struct NetworkDetailListView: View, Identifiable, ModuleListView {
struct NetworkDetailListView: View, Identifiable, ModuleListView, GlobalTimerObserver {
let id = UUID()
let module: Module = NetworkDetails.shared
let globalName: String = String(describing: NetworkDetails.self)

init?(entryKey: EntryKey) {
switch entryKey {
Expand Down Expand Up @@ -51,18 +52,8 @@ struct NetworkDetailListView: View, Identifiable, ModuleListView {
GlobalTimer.shared.removeObserver(self)
}
}
}

extension NetworkDetailListView: GlobalTimerObserver, Hashable {
static func == (lhs: NetworkDetailListView, rhs: NetworkDetailListView) -> Bool {
lhs.id == rhs.id
}

func hash(into hasher: inout Hasher) {
hasher.combine(id)
}

func globalTimerEventOccurred(_ timer: GlobalTimer) {
func eventOccurred(globalTimer timer: GlobalTimer) {
if let module = module as? NetworkDetails {
module.update(prefix: item)
}
Expand Down
3 changes: 3 additions & 0 deletions Reveil/Pages/Details/NetworkDetailsListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SwiftUI

struct NetworkDetailsListView: View, ModuleListView {
let module: Module = NetworkDetails.shared
let globalName: String = String(describing: NetworkDetails.self)

init() {}

Expand Down Expand Up @@ -42,6 +43,8 @@ struct NetworkDetailsListView: View, ModuleListView {
items = NetworkPrefix.categoryCases
}
}

func eventOccurred(globalTimer timer: GlobalTimer) { }
}

// MARK: - Previews
Expand Down
3 changes: 3 additions & 0 deletions Reveil/Pages/Details/NetworkInterfaceListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SwiftUI

struct NetworkInterfaceListView: View, ModuleListView {
let module: Module = NetworkInterfaces.shared
let globalName: String = String(describing: NetworkInterfaces.self)

init?(entryKey _: EntryKey) { nil }

Expand All @@ -27,6 +28,8 @@ struct NetworkInterfaceListView: View, ModuleListView {
entries = NetworkInterfaces.shared.entries(interface: item)
}
}

func eventOccurred(globalTimer timer: GlobalTimer) { }
}

// MARK: - Previews
Expand Down
3 changes: 3 additions & 0 deletions Reveil/Pages/Details/NetworkInterfacesListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SwiftUI

struct NetworkInterfacesListView: View, ModuleListView {
let module: Module = NetworkInterfaces.shared
let globalName: String = String(describing: NetworkInterfaces.self)

init() {}

Expand Down Expand Up @@ -48,6 +49,8 @@ struct NetworkInterfacesListView: View, ModuleListView {
items = NetworkInterfaces.shared.items
}
}

func eventOccurred(globalTimer timer: GlobalTimer) { }
}

// MARK: - Previews
Expand Down
15 changes: 3 additions & 12 deletions Reveil/Pages/Details/NetworkUsageListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

import SwiftUI

struct NetworkUsageListView: View, Identifiable, ModuleListView {
struct NetworkUsageListView: View, Identifiable, ModuleListView, GlobalTimerObserver {
let id = UUID()
let module: Module = NetworkUsage.shared
let globalName: String = String(describing: NetworkUsage.self)

init() {}

Expand Down Expand Up @@ -37,18 +38,8 @@ struct NetworkUsageListView: View, Identifiable, ModuleListView {
GlobalTimer.shared.removeObserver(self)
}
}
}

extension NetworkUsageListView: GlobalTimerObserver, Hashable {
static func == (lhs: NetworkUsageListView, rhs: NetworkUsageListView) -> Bool {
lhs.id == rhs.id
}

func hash(into hasher: inout Hasher) {
hasher.combine(id)
}

func globalTimerEventOccurred(_ timer: GlobalTimer) {
func eventOccurred(globalTimer timer: GlobalTimer) {
module.updateEntries()
}
}
Expand Down
Loading

0 comments on commit 6a89dd1

Please sign in to comment.