Skip to content

Commit

Permalink
Moved shared code into PiStatsKit framework package (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
insidegui authored Jul 11, 2023
1 parent 37b3088 commit e36855d
Show file tree
Hide file tree
Showing 19 changed files with 476 additions and 448 deletions.
1 change: 1 addition & 0 deletions PiStatsMobile/IntentHandler/IntentHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import Intents
import PiStatsKit

class IntentHandler: INExtension, SelectPiholeIntentHandling {

Expand Down
8 changes: 8 additions & 0 deletions PiStatsMobile/PiStatsKit/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
26 changes: 26 additions & 0 deletions PiStatsMobile/PiStatsKit/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "PiStatsKit",
platforms: [.iOS(.v14)],
products: [
.library(
name: "PiStatsKit",
type: .dynamic,
targets: ["PiStatsKit"]
),
],
dependencies: [
.package(url: "https://github.com/Bunn/SwiftHole", from: "3.0.0"),
.package(url: "https://github.com/Bunn/PiMonitor", from: "0.0.6"),
],
targets: [
.target(name: "PiStatsKit", dependencies: [
.product(name: "SwiftHole", package: "SwiftHole"),
.product(name: "PiMonitor", package: "PiMonitor"),
]),
]
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

import Foundation

struct Constants {
static let appGroup: String = {
public struct Constants {
public static let appGroup: String = {
/// The PiStatsAppGroupID is included in each bundle's Info.plist with the processed value from the xcconfig file.
guard let id = Bundle.main.infoDictionary?["PiStatsAppGroupID"] as? String, !id.isEmpty else {
assertionFailure("Expected Info.plist for \(Bundle.main.bundleURL.lastPathComponent) to include non-empty PiStatsAppGroupID")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
import Foundation
import Combine

class DisableTimeItem: Identifiable, Hashable {
public class DisableTimeItem: Identifiable, Hashable {
internal init(timeInterval: TimeInterval) {
self.timeInterval = timeInterval
}

static func == (lhs: DisableTimeItem, rhs: DisableTimeItem) -> Bool {
public static func == (lhs: DisableTimeItem, rhs: DisableTimeItem) -> Bool {
return lhs.id == rhs.id
}
func hash(into hasher: inout Hasher) {
public func hash(into hasher: inout Hasher) {
hasher.combine(id)
}

Expand All @@ -27,27 +27,29 @@ class DisableTimeItem: Identifiable, Hashable {
return f
}()

let id = UUID()
@Published var timeInterval: TimeInterval
var title: String {
public let id = UUID()
@Published public var timeInterval: TimeInterval

public var title: String {
formatter.string(from: timeInterval) ?? "-"
}
}

class DisableDurationManager: ObservableObject {
public final class DisableDurationManager: ObservableObject {
public static let shared = DisableDurationManager(userPreferences: .shared)

private let userPreferences: UserPreferences
@Published var items = [DisableTimeItem]()
@Published public var items = [DisableTimeItem]()
private var disableTimeCancellable: AnyCancellable?
private var timeIntervalCancellables: [AnyCancellable]?

internal init(userPreferences: UserPreferences) {
public init(userPreferences: UserPreferences) {
self.userPreferences = userPreferences
self.items = userPreferences.disableTimes.map { DisableTimeItem(timeInterval: $0) }
setupCancellables()
}

func saveDurationTimes() {
public func saveDurationTimes() {
userPreferences.disableTimes = items.map { $0.timeInterval }
update()
}
Expand All @@ -58,7 +60,7 @@ class DisableDurationManager: ObservableObject {
So this is to force the UI to be refreshed once a property changes.
I'm pretty sure there is a best way of doing this though :(
*/
func setupCancellables() {
public func setupCancellables() {
disableTimeCancellable = $items.receive(on: DispatchQueue.main).sink { [weak self] _ in
self?.saveDurationTimes()
}
Expand All @@ -67,16 +69,16 @@ class DisableDurationManager: ObservableObject {
} }
}

func update() {
public func update() {
objectWillChange.send()
}

func addNewItem() {
public func addNewItem() {
items.append(DisableTimeItem(timeInterval: 0))
setupCancellables()
}

func delete(at offsets: IndexSet) {
public func delete(at offsets: IndexSet) {
items.remove(atOffsets: offsets)
setupCancellables()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@

import os.log

struct Logger {
func osLog(category: String) -> OSLog {
public struct Logger {
public init() { }

public func osLog(category: String) -> OSLog {
return OSLog(subsystem: "PiStats", category: category)
}

func osLog<Subject>(describing instance: Subject) -> OSLog {
public func osLog<Subject>(describing instance: Subject) -> OSLog {
return osLog(category: String(describing: instance))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ import PiMonitor
import Combine
import os.log

class Pihole: Identifiable, ObservableObject {
public class Pihole: Identifiable, ObservableObject {
private let log = Logger().osLog(describing: Pihole.self)
private(set) var metrics: PiMetrics?
private(set) var active = false
public private(set) var metrics: PiMetrics?
public private(set) var active = false
private lazy var keychainToken = APIToken(accountName: self.id.uuidString)
private let servicesTimeout: TimeInterval = 10

var displayName: String?
var address: String
var piMonitorPort: Int?
var hasPiMonitor: Bool = false
let id: UUID
var secure: Bool
public var displayName: String?
public var address: String
public var piMonitorPort: Int?
public var hasPiMonitor: Bool = false
public let id: UUID
public var secure: Bool

@Published var actionError: String?
@Published var pollingError: String?
@Published private(set) var summary: Summary? {
@Published public var actionError: String?
@Published public var pollingError: String?

@Published public private(set) var summary: Summary? {
didSet {
if summary?.status.lowercased() == "enabled" {
active = true
Expand All @@ -41,7 +41,7 @@ class Pihole: Identifiable, ObservableObject {
}
}

var apiToken: String {
public var apiToken: String {
get {
keychainToken.token
}
Expand All @@ -50,15 +50,15 @@ class Pihole: Identifiable, ObservableObject {
}
}

var port: Int? {
public var port: Int? {
getPort(address)
}

var host: String {
public var host: String {
address.components(separatedBy: ":").first ?? ""
}

var title: String {
public var title: String {
if let name = displayName {
return name
}
Expand All @@ -73,7 +73,7 @@ class Pihole: Identifiable, ObservableObject {
PiMonitor(host: host, port: piMonitorPort ?? 8088, timeoutInterval: servicesTimeout, secure: secure)
}

required init(from decoder: Decoder) throws {
public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
id = try container.decode(UUID.self, forKey: .id)
address = try container.decode(String.self, forKey: .address)
Expand Down Expand Up @@ -113,7 +113,7 @@ class Pihole: Identifiable, ObservableObject {
}
}

static func previewData() -> Pihole {
public static func previewData() -> Pihole {
let pihole = Pihole(address: "127.0.0.1")
pihole.hasPiMonitor = true
return pihole
Expand Down Expand Up @@ -217,7 +217,7 @@ extension Pihole {
}
}

static func restoreAll() -> [Pihole] {
public static func restoreAll() -> [Pihole] {
if let piHoleList = UserDefaults.shared().object(forKey: Pihole.piHoleListKey) as? Data {
let decoder = JSONDecoder()

Expand All @@ -232,24 +232,24 @@ extension Pihole {
return [Pihole]()
}

static func restore(_ uuid: UUID) -> Pihole? {
public static func restore(_ uuid: UUID) -> Pihole? {
return Pihole.restoreAll().filter { $0.id == uuid }.first
}
}

extension Pihole: Hashable {
static func == (lhs: Pihole, rhs: Pihole) -> Bool {
public static func == (lhs: Pihole, rhs: Pihole) -> Bool {
return lhs.id == rhs.id
}

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

extension Pihole: Codable {

enum CodingKeys: CodingKey {
public enum CodingKeys: CodingKey {
case id
case address
case displayName
Expand All @@ -258,7 +258,7 @@ extension Pihole: Codable {
case secure
}

func encode(to encoder: Encoder) throws {
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .id)
try container.encode(address, forKey: .address)
Expand Down
Loading

0 comments on commit e36855d

Please sign in to comment.