mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-15 00:34:08 +09:00
feat: moved all modules to the new way of reader mode with a small refactoring
This commit is contained in:
@@ -56,12 +56,12 @@ internal class ClockReader: Reader<Date> {
|
||||
public class Clock: Module {
|
||||
private let popupView: Popup = Popup()
|
||||
private let portalView: Portal
|
||||
private let settingsView: Settings = Settings()
|
||||
private let settingsView: Settings = Settings(.clock)
|
||||
|
||||
private var reader: ClockReader = ClockReader(.clock)
|
||||
private var reader: ClockReader?
|
||||
|
||||
static var list: [Clock_t] {
|
||||
if let objects = Store.shared.data(key: "\(Clock.title)_list") {
|
||||
if let objects = Store.shared.data(key: "\(ModuleType.clock.rawValue)_list") {
|
||||
let decoder = JSONDecoder()
|
||||
if let objectsDecoded = try? decoder.decode(Array.self, from: objects) as [Clock_t] {
|
||||
return objectsDecoded
|
||||
@@ -71,7 +71,7 @@ public class Clock: Module {
|
||||
}
|
||||
|
||||
public init() {
|
||||
self.portalView = Portal("Clock", list: Clock.list)
|
||||
self.portalView = Portal(.clock, list: Clock.list)
|
||||
|
||||
super.init(
|
||||
popup: self.popupView,
|
||||
@@ -80,18 +80,16 @@ public class Clock: Module {
|
||||
)
|
||||
guard self.available else { return }
|
||||
|
||||
self.reader.callbackHandler = { [weak self] value in
|
||||
guard let value else { return }
|
||||
self.reader = ClockReader(.clock) { [weak self] value in
|
||||
self?.callback(value)
|
||||
}
|
||||
|
||||
self.addReader(self.reader)
|
||||
self.reader.readyCallback = { [weak self] in
|
||||
self?.readyHandler()
|
||||
}
|
||||
self.setReaders([self.reader])
|
||||
}
|
||||
|
||||
private func callback(_ value: Date) {
|
||||
private func callback(_ value: Date?) {
|
||||
guard let value else { return }
|
||||
|
||||
var clocks: [Clock_t] = Clock.list
|
||||
var widgetList: [Stack_t] = []
|
||||
|
||||
@@ -117,7 +115,6 @@ public class Clock: Module {
|
||||
}
|
||||
|
||||
extension Clock {
|
||||
static let title: String = "Clock"
|
||||
static let localID: String = UUID().uuidString
|
||||
static var local: Clock_t {
|
||||
Clock_t(id: Clock.localID, name: localizedString("Local time"), format: "yyyy-MM-dd HH:mm:ss", tz: "local")
|
||||
|
||||
@@ -20,8 +20,8 @@ public class Portal: NSStackView, Portal_p {
|
||||
private var oneContainer: NSGridView = NSGridView()
|
||||
private var multiplyContainer: ScrollableStackView = ScrollableStackView(orientation: .horizontal)
|
||||
|
||||
init(_ name: String, list: [Clock_t]) {
|
||||
self.name = name
|
||||
init(_ module: ModuleType, list: [Clock_t]) {
|
||||
self.name = module.rawValue
|
||||
|
||||
super.init(frame: NSRect.zero)
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ internal class Settings: NSStackView, Settings_v, NSTableViewDelegate, NSTableVi
|
||||
|
||||
private var list: [Clock_t] {
|
||||
get {
|
||||
if let objects = Store.shared.data(key: "\(Clock.title)_list") {
|
||||
if let objects = Store.shared.data(key: "\(self.title)_list") {
|
||||
let decoder = JSONDecoder()
|
||||
if let objectsDecoded = try? decoder.decode(Array.self, from: objects) as [Clock_t] {
|
||||
return objectsDecoded
|
||||
@@ -32,16 +32,17 @@ internal class Settings: NSStackView, Settings_v, NSTableViewDelegate, NSTableVi
|
||||
}
|
||||
set {
|
||||
if newValue.isEmpty {
|
||||
Store.shared.remove("\(Clock.title)_list")
|
||||
Store.shared.remove("\(self.title)_list")
|
||||
} else {
|
||||
let encoder = JSONEncoder()
|
||||
if let encoded = try? encoder.encode(newValue){
|
||||
Store.shared.set(key: "\(Clock.title)_list", value: encoded)
|
||||
Store.shared.set(key: "\(self.title)_list", value: encoded)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var title: String
|
||||
private var selectedRow: Int = -1
|
||||
|
||||
private let scrollView = NSScrollView()
|
||||
@@ -49,7 +50,9 @@ internal class Settings: NSStackView, Settings_v, NSTableViewDelegate, NSTableVi
|
||||
private var footerView: NSStackView? = nil
|
||||
private var deleteButton: NSButton? = nil
|
||||
|
||||
public init() {
|
||||
public init(_ module: ModuleType) {
|
||||
self.title = module.rawValue
|
||||
|
||||
super.init(frame: NSRect.zero)
|
||||
|
||||
self.orientation = .vertical
|
||||
|
||||
Reference in New Issue
Block a user