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] Home page #2

Merged
merged 8 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 26 additions & 0 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# This workflow will build a Swift project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-swift

name: Run Unit Tests

on:
pull_request:
branches:
- main

jobs:
test:
runs-on: macos-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Swift
uses: swift-actions/setup-swift@v1
with:
swift-version: '5.5'

- name: Run tests
run: swift test

36 changes: 36 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Xcode
.DS_Store
*/.DS_Store
*.xcuserstate
*.xcworkspace
xcuserdata/

# Build products
/build/
DerivedData/
*.build/

# Dependency directories
Pods/
Carthage/Build/

# Swift Package Manager
.build/

# IDE files
*.swiftpm
.idea/
*.pbxuser
*.mode1v3
*.mode2v3
*.perspective
*.perspectivev3

# macOS specific files
.DS_Store
profile

# Miscellaneous
*.log
*.xccheckout

62 changes: 61 additions & 1 deletion MacCleaner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
79D4658F2C081E070061176E /* MacCleanerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79D4658E2C081E070061176E /* MacCleanerTests.swift */; };
79D465992C081E070061176E /* MacCleanerUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79D465982C081E070061176E /* MacCleanerUITests.swift */; };
79D4659B2C081E070061176E /* MacCleanerUITestsLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79D4659A2C081E070061176E /* MacCleanerUITestsLaunchTests.swift */; };
79D465A92C081E4E0061176E /* SystemCleanup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79D465A82C081E4E0061176E /* SystemCleanup.swift */; };
79D465AC2C081E760061176E /* LargeFilesFinder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79D465AB2C081E760061176E /* LargeFilesFinder.swift */; };
79D465AE2C081E900061176E /* Uninstaller.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79D465AD2C081E900061176E /* Uninstaller.swift */; };
79D465B02C081E9F0061176E /* DeveloperTools.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79D465AF2C081E9F0061176E /* DeveloperTools.swift */; };
79D465B32C081F320061176E /* StorageInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79D465B22C081F320061176E /* StorageInfo.swift */; };
79D465B52C081F560061176E /* Dashboard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79D465B42C081F560061176E /* Dashboard.swift */; };
79D465B72C0821510061176E /* SystemCleanUp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79D465B62C0821510061176E /* SystemCleanUp.swift */; };
79D465B92C0822F40061176E /* FileInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79D465B82C0822F40061176E /* FileInfo.swift */; };
79D465BB2C0826820061176E /* SystemMonitorHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79D465BA2C0826820061176E /* SystemMonitorHelper.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -45,6 +54,15 @@
79D465942C081E070061176E /* MacCleanerUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = MacCleanerUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
79D465982C081E070061176E /* MacCleanerUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MacCleanerUITests.swift; sourceTree = "<group>"; };
79D4659A2C081E070061176E /* MacCleanerUITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MacCleanerUITestsLaunchTests.swift; sourceTree = "<group>"; };
79D465A82C081E4E0061176E /* SystemCleanup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SystemCleanup.swift; sourceTree = "<group>"; };
79D465AB2C081E760061176E /* LargeFilesFinder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LargeFilesFinder.swift; sourceTree = "<group>"; };
79D465AD2C081E900061176E /* Uninstaller.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Uninstaller.swift; sourceTree = "<group>"; };
79D465AF2C081E9F0061176E /* DeveloperTools.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeveloperTools.swift; sourceTree = "<group>"; };
79D465B22C081F320061176E /* StorageInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StorageInfo.swift; sourceTree = "<group>"; };
79D465B42C081F560061176E /* Dashboard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Dashboard.swift; sourceTree = "<group>"; };
79D465B62C0821510061176E /* SystemCleanUp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SystemCleanUp.swift; sourceTree = "<group>"; };
79D465B82C0822F40061176E /* FileInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileInfo.swift; sourceTree = "<group>"; };
79D465BA2C0826820061176E /* SystemMonitorHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SystemMonitorHelper.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -95,8 +113,9 @@
79D4657B2C081E050061176E /* MacCleaner */ = {
isa = PBXGroup;
children = (
79D465B12C081F230061176E /* Model */,
79D465A72C081E3C0061176E /* Views */,
79D4657C2C081E050061176E /* MacCleanerApp.swift */,
79D4657E2C081E050061176E /* ContentView.swift */,
79D465802C081E060061176E /* Assets.xcassets */,
79D465852C081E060061176E /* MacCleaner.entitlements */,
79D465822C081E060061176E /* Preview Content */,
Expand Down Expand Up @@ -129,6 +148,38 @@
path = MacCleanerUITests;
sourceTree = "<group>";
};
79D465A72C081E3C0061176E /* Views */ = {
isa = PBXGroup;
children = (
79D465AA2C081E670061176E /* ContentView */,
79D465A82C081E4E0061176E /* SystemCleanup.swift */,
79D465AB2C081E760061176E /* LargeFilesFinder.swift */,
79D465AD2C081E900061176E /* Uninstaller.swift */,
79D465AF2C081E9F0061176E /* DeveloperTools.swift */,
79D465B42C081F560061176E /* Dashboard.swift */,
);
path = Views;
sourceTree = "<group>";
};
79D465AA2C081E670061176E /* ContentView */ = {
isa = PBXGroup;
children = (
79D4657E2C081E050061176E /* ContentView.swift */,
);
path = ContentView;
sourceTree = "<group>";
};
79D465B12C081F230061176E /* Model */ = {
isa = PBXGroup;
children = (
79D465B22C081F320061176E /* StorageInfo.swift */,
79D465B62C0821510061176E /* SystemCleanUp.swift */,
79D465B82C0822F40061176E /* FileInfo.swift */,
79D465BA2C0826820061176E /* SystemMonitorHelper.swift */,
);
path = Model;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -259,8 +310,17 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
79D465B72C0821510061176E /* SystemCleanUp.swift in Sources */,
79D4657F2C081E050061176E /* ContentView.swift in Sources */,
79D465B32C081F320061176E /* StorageInfo.swift in Sources */,
79D465B52C081F560061176E /* Dashboard.swift in Sources */,
79D4657D2C081E050061176E /* MacCleanerApp.swift in Sources */,
79D465A92C081E4E0061176E /* SystemCleanup.swift in Sources */,
79D465B02C081E9F0061176E /* DeveloperTools.swift in Sources */,
79D465AE2C081E900061176E /* Uninstaller.swift in Sources */,
79D465AC2C081E760061176E /* LargeFilesFinder.swift in Sources */,
79D465B92C0822F40061176E /* FileInfo.swift in Sources */,
79D465BB2C0826820061176E /* SystemMonitorHelper.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
uuid = "A89C2A97-62B1-492F-BC76-6F7147D4BA49"
type = "1"
version = "2.0">
</Bucket>
24 changes: 0 additions & 24 deletions MacCleaner/ContentView.swift

This file was deleted.

7 changes: 1 addition & 6 deletions MacCleaner/MacCleanerApp.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
//
// MacCleanerApp.swift
// MacCleaner
//
// Created by Vinicius Mangueira on 29/05/24.
//
// Copyright © 2024 MacCleaner, LLC. All rights reserved.

import SwiftUI

Expand Down
42 changes: 42 additions & 0 deletions MacCleaner/Model/FileInfo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright © 2024 MacCleaner, LLC. All rights reserved.

import Foundation

struct FileDetail: Identifiable {
let id = UUID()
let name: String
let size: Int64
let modificationDate: Date
}

func findLargeAndOldFiles(in directory: String, largerThan size: Int64, olderThan days: Int) -> [FileDetail] {
var results = [FileDetail]()
let fileManager = FileManager.default
let calendar = Calendar.current
let dateThreshold = calendar.date(byAdding: .day, value: -days, to: Date())!

do {
let urls = try fileManager.contentsOfDirectory(
at: URL(fileURLWithPath: directory),
includingPropertiesForKeys: [.fileSizeKey, .contentModificationDateKey],
options: .skipsHiddenFiles
)

for url in urls {
let resourceValues = try url.resourceValues(forKeys: [.fileSizeKey, .contentModificationDateKey])
if let fileSize = resourceValues.fileSize, let modificationDate = resourceValues.contentModificationDate {
if fileSize > size, modificationDate < dateThreshold {
results.append(FileDetail(
name: url.lastPathComponent,
size: Int64(fileSize),
modificationDate: modificationDate
))
}
}
}
} catch {
print("Error while enumerating files \(directory): \(error.localizedDescription)")
}

return results
}
31 changes: 31 additions & 0 deletions MacCleaner/Model/StorageInfo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright © 2024 MacCleaner, LLC. All rights reserved.

import Foundation

struct StorageInfo {
var totalSpace: Int64
var freeSpace: Int64
var usedSpace: Int64
}

func getStorageInfo() -> StorageInfo? {
if let homeDirectory = try? FileManager.default.url(
for: .userDirectory,
in: .localDomainMask,
appropriateFor: nil,
create: false
) {
do {
let attributes = try FileManager.default.attributesOfFileSystem(forPath: homeDirectory.path)
if let totalSpace = attributes[.systemSize] as? Int64,
let freeSpace = attributes[.systemFreeSize] as? Int64
{
let usedSpace = totalSpace - freeSpace
return StorageInfo(totalSpace: totalSpace, freeSpace: freeSpace, usedSpace: usedSpace)
}
} catch {
print("Error retrieving storage information: \(error.localizedDescription)")
}
}
return nil
}
24 changes: 24 additions & 0 deletions MacCleaner/Model/SystemCleanUp.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright © 2024 MacCleaner, LLC. All rights reserved.

import Foundation

func performSystemCleanup() -> Bool {
let tempDirectories = [
NSTemporaryDirectory(),
"/var/folders",
]

do {
for dir in tempDirectories {
let files = try FileManager.default.contentsOfDirectory(atPath: dir)
for file in files {
let filePath = (dir as NSString).appendingPathComponent(file)
try FileManager.default.removeItem(atPath: filePath)
}
}
return true
} catch {
print("Error during cleanup: \(error.localizedDescription)")
return false
}
}
75 changes: 75 additions & 0 deletions MacCleaner/Model/SystemMonitorHelper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright © 2023 Wendy's International, LLC. All rights reserved.

import Foundation

class SystemMonitorHelper {
static func getCPUUsage() -> Double {
var cpuInfo: processor_info_array_t!
var numCPUs: mach_msg_type_number_t = 0
var prevIdleTicks: UInt64 = 0
var prevTotalTicks: UInt64 = 0

var numCPUsCopy = numCPUs

let err = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &numCPUs, &cpuInfo, &numCPUsCopy)
if err == KERN_SUCCESS {
let cpuLoadInfo = cpuInfo!
.withMemoryRebound(to: Int32.self, capacity: Int(numCPUsCopy)) { ptr in
ptr
}
var totalTicks: UInt64 = 0
var idleTicks: UInt64 = 0
for cpu in 0 ..< Int(numCPUsCopy) {
totalTicks += UInt64(cpuLoadInfo[cpu])
}
idleTicks = UInt64(cpuLoadInfo[Int(CPU_STATE_IDLE)])
let totalTicksDiff = Double(totalTicks - prevTotalTicks)
let idleTicksDiff = Double(idleTicks - prevIdleTicks)
let usage = (totalTicksDiff - idleTicksDiff) / totalTicksDiff * 100.0
prevIdleTicks = idleTicks
prevTotalTicks = totalTicks
return usage
} else {
print("Error getting CPU usage: \(String(describing: mach_error_string(err)))")
return 0.0
}
}

static func getMemoryUsage() -> Double {
var taskInfo = mach_task_basic_info()
var count = mach_msg_type_number_t(MemoryLayout<mach_task_basic_info>.size / MemoryLayout<integer_t>.size)
let kerr: kern_return_t = withUnsafeMutablePointer(to: &taskInfo) {
$0.withMemoryRebound(to: integer_t.self, capacity: 1) {
task_info(
mach_task_self_,
task_flavor_t(MACH_TASK_BASIC_INFO),
$0,
&count
)
}
}
if kerr == KERN_SUCCESS {
let usedMemory = taskInfo.resident_size
let totalMemory = ProcessInfo.processInfo.physicalMemory
return Double(usedMemory) / Double(totalMemory) * 100.0
} else {
print("Error getting memory usage: \(String(describing: mach_error_string(kerr)))")
return 0.0
}
}

static func getDiskUsage() -> Double {
do {
let systemAttributes = try FileManager.default.attributesOfFileSystem(forPath: "/")
if let totalSize = systemAttributes[.systemSize] as? Int64,
let freeSize = systemAttributes[.systemFreeSize] as? Int64
{
let usedSize = totalSize - freeSize
return Double(usedSize) / Double(totalSize) * 100.0
}
} catch {
print("Error getting disk usage: \(error.localizedDescription)")
}
return 0.0
}
}
Loading
Loading