Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/code updates #44

Merged
merged 8 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
866 changes: 425 additions & 441 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

68 changes: 13 additions & 55 deletions Sentinel/Classes/Core/CustomInfoTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@

import SwiftUI

@objcMembers
public class CustomInfoTool: NSObject, Tool {
/// Tool which gives the ability to show info + value pairs
public struct CustomInfoTool: Tool {

// MARK: - Public properties

public let name: String

@ViewBuilder
public var content: any View {
SentinelListView(title: name, items: createToolTable(with: info).sections)
}

// MARK: - Internal properties

let info: [Section]
Expand All @@ -33,6 +28,16 @@ public class CustomInfoTool: NSObject, Tool {

// MARK: - Extensions -

// MARK: - UI

public extension CustomInfoTool {

var content: any View {
SentinelListView(title: name, items: createToolTable(with: info).sections)
}

}

// MARK: - Helpers

extension CustomInfoTool {
Expand All @@ -48,50 +53,3 @@ extension CustomInfoTool {
}

}

extension CustomInfoTool {

public class Section {

// MARK: - Internal properties

let title: String?
let items: [Item]

// MARK: - Lifecycle

public init(title: String? = nil, items: [Item]) {
self.title = title
self.items = items
}
}

public class Item {

// MARK: - Internal properties

let title: String
let value: String

// MARK: - Lifecycle

public init(title: String, value: String) {
self.title = title
self.value = value
}
}

}

// MARK: - Equatable and Identifiable conformance

extension CustomInfoTool.Item: Equatable, Identifiable {

public var id: String {
title
}

public static func == (lhs: CustomInfoTool.Item, rhs: CustomInfoTool.Item) -> Bool {
lhs.title == rhs.title
}
}
62 changes: 62 additions & 0 deletions Sentinel/Classes/Core/CustomInfoToolSection.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// CustomInfoToolSection.swift
// Sentinel
//
// Created by Zvonimir Medak on 18.12.2024..
//

import Foundation

public extension CustomInfoTool {

/// Section Item for the CustomInfoTool which expects CustomInfoTool.Item for its items
struct Section {

// MARK: - Internal properties

let title: String?
let items: [Item]

// MARK: - Lifecycle

public init(title: String? = nil, items: [Item]) {
self.title = title
self.items = items
}
}

/// Row Item for the CustomInfoTool which will show a title + value pair
struct Item {

// MARK: - Internal properties

let title: String
let value: String

// MARK: - Lifecycle

public init(title: String, value: String) {
self.title = title
self.value = value
}
}

}

// MARK: - Equatable conformance

extension CustomInfoTool.Item: Equatable {

public static func == (lhs: CustomInfoTool.Item, rhs: CustomInfoTool.Item) -> Bool {
lhs.title == rhs.title
}
}

// MARK: - Identifiable conformance

extension CustomInfoTool.Item: Identifiable {

public var id: String {
title
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@

import SwiftUI

/// Item protocol which can be used with ToolTable to show a custom UI
public protocol CustomToolTableItem: Identifiable, Equatable {
var title: String { get }
@ViewBuilder var content: any View { get }
}

// MARK: - Extensions -

// MARK: - Identifiable helper

public extension CustomToolTableItem {
var id: String { title }
}
23 changes: 14 additions & 9 deletions Sentinel/Classes/Core/Internal/ApplicationTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import Foundation
import SwiftUI

final class ApplicationTool: Tool {
/// Tool which shows Plist information about the App
struct ApplicationTool: Tool {

// MARK: - Public properties -

Expand All @@ -20,7 +21,7 @@ final class ApplicationTool: Tool {

// MARK: - Private properties -

private lazy var tool = CustomInfoTool(
private let tool = CustomInfoTool(
name: "Application",
info: [
CustomInfoTool.Section(
Expand All @@ -42,8 +43,13 @@ final class ApplicationTool: Tool {
)
]
)

// MARK: - Internal properties -
}

// MARK: - Extensions -

// MARK: - UI

extension ApplicationTool {

var toolTable: ToolTable {
tool.createToolTable(with: tool.info)
Expand All @@ -52,22 +58,21 @@ final class ApplicationTool: Tool {
var content: any View {
SentinelListView(title: name, items: toolTable.sections)
}

}

// MARK: - Internal extension
// MARK: - Info helpers

extension ApplicationTool {

func stringFromPlist(for key: CFString) -> String {
static func stringFromPlist(for key: CFString) -> String {
stringFromPlist(for: key as String)
}

func stringFromPlist(for key: String) -> String {
static func stringFromPlist(for key: String) -> String {
Bundle.main.object(forInfoDictionaryKey: key).map { String(describing: $0) } ?? ""
}

var bundleAllInfos: [CustomInfoTool.Item] {
static var bundleAllInfos: [CustomInfoTool.Item] {
Bundle.main.infoDictionary?
.map { CustomInfoTool.Item(title: $0.key, value: String(describing: $0.value)) }
?? []
Expand Down
36 changes: 20 additions & 16 deletions Sentinel/Classes/Core/Internal/DeviceTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import Foundation
import SwiftUI

final class DeviceTool: Tool {
/// Tool which shows current device information
struct DeviceTool: Tool {

// MARK: - Public properties

Expand All @@ -18,19 +19,9 @@ final class DeviceTool: Tool {

public init() {}

// MARK: - Internal properties

var toolTable: ToolTable {
tool.createToolTable(with: tool.info)
}

var content: any View {
SentinelListView(title: name, items: toolTable.sections)
}

// MARK: - Private properties -

private lazy var tool = CustomInfoTool(
private let tool = CustomInfoTool(
name: "Device",
info: [
CustomInfoTool.Section(title: "Device", items: [
Expand All @@ -47,15 +38,28 @@ final class DeviceTool: Tool {

// MARK: - Extensions -

// MARK: - Helpers
// MARK: - UI

extension DeviceTool {

var toolTable: ToolTable {
tool.createToolTable(with: tool.info)
}

var content: any View {
SentinelListView(title: name, items: toolTable.sections)
}
}

// MARK: - Info helpers

private extension DeviceTool {

var systemVersion: String {
static var systemVersion: String {
"\(UIDevice.current.systemName) \(UIDevice.current.systemVersion)"
}

var batteryState: String {
static var batteryState: String {
switch UIDevice.current.batteryState {
case .charging:
"Charging at: \(calculateBatteryPercentage(with: UIDevice.current.batteryLevel.description))%"
Expand All @@ -68,7 +72,7 @@ private extension DeviceTool {
}
}

func calculateBatteryPercentage(with amount: String) -> String {
static func calculateBatteryPercentage(with amount: String) -> String {
guard let batteryLevel = Double(amount) else { return "Unknown" }
return "\(batteryLevel * 100.0)"
}
Expand Down
10 changes: 5 additions & 5 deletions Sentinel/Classes/Core/Internal/SentinelInternal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@ extension Sentinel {
) {
let tabItems = createTabItems(
with: tools,
preferences: preferences,
viewController: viewController
preferences: preferences
)

let tabBarController = UIHostingController(rootView: SentinelTabBarView(tabs: tabItems))
viewController.present(tabBarController, animated: true)
}
}

// MARK: - Private extension
// MARK: - Extensions -

// MARK: - Helpers

private extension Sentinel {

func createTabItems(
with tools: [Tool],
preferences: [ToolTableSection],
viewController: UIViewController
preferences: [ToolTableSection]
) -> [SentinelTabItem] {
[
SentinelTabItem(tab: .device),
Expand Down
9 changes: 9 additions & 0 deletions Sentinel/Classes/Core/Internal/SentinelTabItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ struct SentinelTabItem {
init(tab: SentinelTab) {
self.tab = tab
}
}

// MARK: - Extensions -

// MARK: - Helpers

extension SentinelTabItem {

var barItemTitle: String {
switch tab {
Expand Down Expand Up @@ -45,6 +52,8 @@ struct SentinelTabItem {
}
}

// MARK: - Private helpers

private extension SentinelTabItem {

var toolTable: ToolTable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@

import Foundation

class CPUInfoProvider {

struct CPUInfoProvider {

/// Fetches the number of cores the devices has
var numberOfCores: Int {
return ProcessInfo().processorCount
ProcessInfo().processorCount
}


/// Fetches the percentage of the CPU currently being used by the App
var currentUsage: Double {
var totalUsageOfCPU: Double = 0.0
var threadsList: thread_act_array_t?
var threadsCount = mach_msg_type_number_t(0)
let threadsResult = withUnsafeMutablePointer(to: &threadsList) {
return $0.withMemoryRebound(to: thread_act_array_t?.self, capacity: 1) {
$0.withMemoryRebound(to: thread_act_array_t?.self, capacity: 1) {
task_threads(mach_task_self_, $0, &threadsCount)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ struct MemoryUsage {
let total: Int64
}

class MemoryInfoProvider {

struct MemoryInfoProvider {

/// Fetches the current memory usage of the App
var currentUsage: MemoryUsage {
var taskInfo = task_vm_info_data_t()
var count = mach_msg_type_number_t(MemoryLayout<task_vm_info>.size) / 4
Expand Down
Loading