Skip to content
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
32 changes: 14 additions & 18 deletions SPAR_iOS/SPAR/SPAR.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
3D505B792DBB170700510486 /* InfoRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D505B782DBB170700510486 /* InfoRow.swift */; };
3D5994F32DB2B79400E9215B /* LoginView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D5994F22DB2B79400E9215B /* LoginView.swift */; };
3D5994F52DB2BA1200E9215B /* BackgroundAnimationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D5994F42DB2BA1200E9215B /* BackgroundAnimationView.swift */; };
3D5994F82DB2C08500E9215B /* DeviceDetail.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D5994F72DB2C08500E9215B /* DeviceDetail.swift */; };
3D5994FA2DB307B400E9215B /* HomeViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D5994F92DB307B400E9215B /* HomeViewModel.swift */; };
3D5994FC2DB30B0100E9215B /* LoginViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D5994FB2DB30B0100E9215B /* LoginViewModel.swift */; };
3D5994FF2DB3FFBE00E9215B /* NetworkService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D5994FE2DB3FFBE00E9215B /* NetworkService.swift */; };
Expand Down Expand Up @@ -120,7 +119,6 @@
3D505B782DBB170700510486 /* InfoRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InfoRow.swift; sourceTree = "<group>"; };
3D5994F22DB2B79400E9215B /* LoginView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginView.swift; sourceTree = "<group>"; };
3D5994F42DB2BA1200E9215B /* BackgroundAnimationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BackgroundAnimationView.swift; sourceTree = "<group>"; };
3D5994F72DB2C08500E9215B /* DeviceDetail.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceDetail.swift; sourceTree = "<group>"; };
3D5994F92DB307B400E9215B /* HomeViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeViewModel.swift; sourceTree = "<group>"; };
3D5994FB2DB30B0100E9215B /* LoginViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginViewModel.swift; sourceTree = "<group>"; };
3D5994FE2DB3FFBE00E9215B /* NetworkService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkService.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -267,14 +265,6 @@
path = Login;
sourceTree = "<group>";
};
3D5994F62DB2C05E00E9215B /* Device detail Page */ = {
isa = PBXGroup;
children = (
3D5994F72DB2C08500E9215B /* DeviceDetail.swift */,
);
path = "Device detail Page";
sourceTree = "<group>";
};
3D5994FD2DB3FF0700E9215B /* NetworkService */ = {
isa = PBXGroup;
children = (
Expand All @@ -285,6 +275,19 @@
path = NetworkService;
sourceTree = "<group>";
};
3D63FB062DC1B4FA006F53DE /* View */ = {
isa = PBXGroup;
children = (
3DF9BA502D67762C00D0CC62 /* HomeView.swift */,
3DCE458A2D68C19F00FACBB8 /* SplashScreenView.swift */,
3D07B5322D851C490085B32C /* Onboarding */,
3D07B5342D85C3740085B32C /* ChartUI */,
3D5994F12DB2ACB300E9215B /* Login */,
3D505B4B2DB9C19000510486 /* Device */,
);
path = View;
sourceTree = "<group>";
};
3D8423A52DB14F86009CF847 /* SPARTests */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -318,20 +321,14 @@
3D98C7F02D4FE04500462EF4 /* SPAR */ = {
isa = PBXGroup;
children = (
3D505B4B2DB9C19000510486 /* Device */,
3D63FB062DC1B4FA006F53DE /* View */,
3D5994FD2DB3FF0700E9215B /* NetworkService */,
3D5994F62DB2C05E00E9215B /* Device detail Page */,
3D5994F12DB2ACB300E9215B /* Login */,
3D07B5362D85C3910085B32C /* Data */,
3D07B5352D85C3860085B32C /* ViewModel */,
3D07B5342D85C3740085B32C /* ChartUI */,
3D07B5332D851C640085B32C /* Utilities */,
3D07B5322D851C490085B32C /* Onboarding */,
3D07B5312D851C1D0085B32C /* Extensions */,
3D98C7F12D4FE04500462EF4 /* SPARApp.swift */,
3D98C7F32D4FE04500462EF4 /* ContentView.swift */,
3DCE458A2D68C19F00FACBB8 /* SplashScreenView.swift */,
3DF9BA502D67762C00D0CC62 /* HomeView.swift */,
3D98C7F52D4FE04700462EF4 /* Assets.xcassets */,
3D98C7F72D4FE04700462EF4 /* Preview Content */,
);
Expand Down Expand Up @@ -480,7 +477,6 @@
3D505B4D2DB9C1BE00510486 /* DeviceOptions.swift in Sources */,
3D505B5B2DB9E3F700510486 /* ProcessViewModel.swift in Sources */,
3D505B632DBAAE2C00510486 /* DiskUsage.swift in Sources */,
3D5994F82DB2C08500E9215B /* DeviceDetail.swift in Sources */,
3DCE45912D68C7E700FACBB8 /* View+Extension.swift in Sources */,
3D505B592DB9E14700510486 /* MemoryUsageViewModel.swift in Sources */,
3D505B552DB9DD5500510486 /* ProcessDetailPage.swift in Sources */,
Expand Down
3 changes: 0 additions & 3 deletions SPAR_iOS/SPAR/SPAR/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ enum AppView: Hashable {
case splash
case onboarding
case home
case detailPage
case login
}

Expand All @@ -28,8 +27,6 @@ struct ContentView: View {
OnboardingView(currentView: $currentView)
case .home:
HomeView(currentView: $currentView)
case .detailPage:
DeviceDetail(currentView: $currentView)
case .login:
LoginView(currentView: $currentView)
}
Expand Down
56 changes: 49 additions & 7 deletions SPAR_iOS/SPAR/SPAR/Data/CPUUsage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,38 @@

import Foundation

// MARK: - CpuCoreUsage model
struct CpuCoreUsage: Codable {
let core: Int
let usage: Double
}

struct CpuUsage: Decodable {
// Fixed CpuUsage struct with better JSON handling
struct CpuUsage: Codable {
let id: Int
let totalCpuLoad: Double
let userId: Int
let deviceId: String
let timestamp: String
let perCoreUsage: [CpuCoreUsage]

// Custom encoding for when we need to convert back to the API format
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode(id, forKey: .id)
try container.encode(totalCpuLoad, forKey: .totalCpuLoad)
try container.encode(userId, forKey: .userId)
try container.encode(deviceId, forKey: .deviceId)
try container.encode(timestamp, forKey: .timestamp)

// Convert perCoreUsage to a JSON string
let perCoreData = try JSONEncoder().encode(perCoreUsage)
if let perCoreString = String(data: perCoreData, encoding: .utf8) {
try container.encode(perCoreString, forKey: .perCoreUsageJson)
}
}

enum CodingKeys: String, CodingKey {
case id, totalCpuLoad, userId, deviceId, timestamp, perCoreUsageJson
}
Expand All @@ -33,17 +52,39 @@ struct CpuUsage: Decodable {
deviceId = try container.decode(String.self, forKey: .deviceId)
timestamp = try container.decode(String.self, forKey: .timestamp)

// Try to decode the perCoreUsageJson string
let jsonString = try container.decode(String.self, forKey: .perCoreUsageJson)

// No cleaning, just decode directly
if let jsonData = jsonString.data(using: .utf8) {
perCoreUsage = (try? JSONDecoder().decode([CpuCoreUsage].self, from: jsonData)) ?? []
} else {
perCoreUsage = []
// First attempt: direct decoding
if let data = jsonString.data(using: .utf8) {
do {
perCoreUsage = try JSONDecoder().decode([CpuCoreUsage].self, from: data)
return
} catch {
print("First parse attempt failed: \(error)")
}
}

// Second attempt: clean the string and try again
let cleanedJsonString = jsonString
.replacingOccurrences(of: "\\\"", with: "\"")
.replacingOccurrences(of: "\\\\", with: "\\")

if let data = cleanedJsonString.data(using: .utf8) {
do {
perCoreUsage = try JSONDecoder().decode([CpuCoreUsage].self, from: data)
return
} catch {
print("Second parse attempt failed: \(error)")
}
}

// If all else fails, provide default values
print("Using default values for perCoreUsage")
perCoreUsage = []
}

// Manual initializer for sample/mock data
// Convenience initializer (not used in decoding)
init(id: Int, totalCpuLoad: Double, perCoreUsage: [CpuCoreUsage], userId: Int, deviceId: String, timestamp: String) {
self.id = id
self.totalCpuLoad = totalCpuLoad
Expand All @@ -53,3 +94,4 @@ struct CpuUsage: Decodable {
self.timestamp = timestamp
}
}

10 changes: 5 additions & 5 deletions SPAR_iOS/SPAR/SPAR/Data/MockData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ struct MockData {
""".data(using: .utf8)!
static let sampleCPUUsageData = """
{
"id": 7,
"totalCpuLoad": 42.5,
"perCoreUsageJson": "[{\"core\":1,\"usage\":35.0}]",
"id": 8,
"totalCpuLoad": 5.145907157059862,
"perCoreUsageJson": "[{\\"core\\":1,\\"usage\\":8.544345751027443},{\\"core\\":2,\\"usage\\":6.293496720055942},{\\"core\\":3,\\"usage\\":16.68324711500199},{\\"core\\":4,\\"usage\\":19.07660579449822},{\\"core\\":5,\\"usage\\":1.1985844017094018},{\\"core\\":6,\\"usage\\":1.0950489099589356},{\\"core\\":7,\\"usage\\":1.8733957798593288},{\\"core\\":8,\\"usage\\":2.554001268654225},{\\"core\\":9,\\"usage\\":0.8346409374687009},{\\"core\\":10,\\"usage\\":1.2519614062030515},{\\"core\\":11,\\"usage\\":1.0449704537108135},{\\"core\\":12,\\"usage\\":1.1484275889697537}]",
"userId": 1,
"deviceId": "331330ac-5f82-43b0-9d39-84e1f7e7e358",
"timestamp": "2025-04-22T15:57:10.351457"
"deviceId": "47af4ef0-2c9f-4962-95f1-6b206ec305e6",
"timestamp": "2025-04-29T18:02:05.970927"
}
""".data(using: .utf8)!
static let sampleDiskIOUsageData = """
Expand Down
58 changes: 0 additions & 58 deletions SPAR_iOS/SPAR/SPAR/Device detail Page/DeviceDetail.swift

This file was deleted.

127 changes: 0 additions & 127 deletions SPAR_iOS/SPAR/SPAR/Device/BatteryDetailView.swift

This file was deleted.

Loading
Loading