From fd467e667093fa7cb4d62fe79069fdace4534171 Mon Sep 17 00:00:00 2001 From: Zoe Date: Wed, 1 May 2024 12:37:19 +0900 Subject: [PATCH] =?UTF-8?q?[Feat/#12]=20=EB=8D=94=EB=AF=B8=20=EB=8D=B0?= =?UTF-8?q?=EC=9D=B4=ED=84=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AppActivityView.swift | 32 +++++++++++++++++-- .../AppActivityView.xcdatamodel/contents | 2 -- HMH_iOS/HMH_iOS.xcodeproj/project.pbxproj | 4 +++ .../Presentation/Home/HomeViewModel.swift | 23 +++++++------ 4 files changed, 47 insertions(+), 14 deletions(-) delete mode 100644 HMH_iOS/HMHDeviceActivityReport/AppActivityView.xcdatamodeld/AppActivityView.xcdatamodel/contents diff --git a/HMH_iOS/HMHDeviceActivityReport/AppActivityView.swift b/HMH_iOS/HMHDeviceActivityReport/AppActivityView.swift index 1d667131..6ea6dc21 100644 --- a/HMH_iOS/HMHDeviceActivityReport/AppActivityView.swift +++ b/HMH_iOS/HMHDeviceActivityReport/AppActivityView.swift @@ -6,13 +6,41 @@ // import SwiftUI +import FamilyControls struct AppActivityView: View { + let totalActivity: String + var body: some View { - Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/) + if let timeString = convertStringToTime(totalActivity) { + Text("\(timeString) 사용") + .font(.title2_semibold_24) + .foregroundStyle(.whiteText) + .frame(maxWidth: .infinity, alignment: .leading) + } } } +extension AppActivityView { + func convertStringToTime(_ string: String) -> String? { + let components = string.components(separatedBy: " ") + guard components.count == 3, + let hours = Int(components[0].replacingOccurrences(of: "h", with: "")), + let minutes = Int(components[1].replacingOccurrences(of: "m", with: "")), + let seconds = Int(components[2].replacingOccurrences(of: "s", with: "")) else { + return nil + } + + let hoursText = String(format: "%02d", hours) + let minutesText = String(format: "%02d", minutes) + + return "\(hoursText)시간 \(minutesText)분" + } +} + +// In order to support previews for your extension's custom views, make sure its source files are +// members of your app's Xcode target as well as members of your extension's target. You can use +// Xcode's File Inspector to modify a file's Target Membership. #Preview { - AppActivityView() + AppActivityView(totalActivity: "1h 23m") } diff --git a/HMH_iOS/HMHDeviceActivityReport/AppActivityView.xcdatamodeld/AppActivityView.xcdatamodel/contents b/HMH_iOS/HMHDeviceActivityReport/AppActivityView.xcdatamodeld/AppActivityView.xcdatamodel/contents deleted file mode 100644 index 6339467f..00000000 --- a/HMH_iOS/HMHDeviceActivityReport/AppActivityView.xcdatamodeld/AppActivityView.xcdatamodel/contents +++ /dev/null @@ -1,2 +0,0 @@ - - \ No newline at end of file diff --git a/HMH_iOS/HMH_iOS.xcodeproj/project.pbxproj b/HMH_iOS/HMH_iOS.xcodeproj/project.pbxproj index fd8143e4..a85a23ac 100644 --- a/HMH_iOS/HMH_iOS.xcodeproj/project.pbxproj +++ b/HMH_iOS/HMH_iOS.xcodeproj/project.pbxproj @@ -60,6 +60,7 @@ 368C5D992BCC41990035A1AC /* TotalActivityReport.swift in Sources */ = {isa = PBXBuildFile; fileRef = 368C5D982BCC41990035A1AC /* TotalActivityReport.swift */; }; 368C5D9B2BCC41990035A1AC /* TotalActivityView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 368C5D9A2BCC41990035A1AC /* TotalActivityView.swift */; }; 368C5DA02BCC41990035A1AC /* HMHDeviceActivityReport.appex in Embed ExtensionKit Extensions */ = {isa = PBXBuildFile; fileRef = 368C5D942BCC41990035A1AC /* HMHDeviceActivityReport.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; }; + 36EB6EC02BDCC65600E8C939 /* AppActivityView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36EB6EBF2BDCC65600E8C939 /* AppActivityView.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -142,6 +143,7 @@ 368CAA942BB9617A00FA83B3 /* DeviceActivity.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = DeviceActivity.framework; path = System/Library/Frameworks/DeviceActivity.framework; sourceTree = SDKROOT; }; 368CAABC2BB961D900FA83B3 /* ManagedSettings.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ManagedSettings.framework; path = System/Library/Frameworks/ManagedSettings.framework; sourceTree = SDKROOT; }; 368CAADD2BB97FE000FA83B3 /* HMH_iOS.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = HMH_iOS.entitlements; sourceTree = ""; }; + 36EB6EBF2BDCC65600E8C939 /* AppActivityView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppActivityView.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -400,6 +402,7 @@ 368C5D9A2BCC41990035A1AC /* TotalActivityView.swift */, 368C5D9C2BCC41990035A1AC /* Info.plist */, 368C5D9D2BCC41990035A1AC /* HMHDeviceActivityReport.entitlements */, + 36EB6EBF2BDCC65600E8C939 /* AppActivityView.swift */, ); path = HMHDeviceActivityReport; sourceTree = ""; @@ -578,6 +581,7 @@ files = ( 361C61EA2BD6B7F200EF0D8B /* Font.swift in Sources */, 368C5D9B2BCC41990035A1AC /* TotalActivityView.swift in Sources */, + 36EB6EC02BDCC65600E8C939 /* AppActivityView.swift in Sources */, 368C5D972BCC41990035A1AC /* HMHDeviceActivityReport.swift in Sources */, 368C5D992BCC41990035A1AC /* TotalActivityReport.swift in Sources */, ); diff --git a/HMH_iOS/HMH_iOS/Presentation/Home/HomeViewModel.swift b/HMH_iOS/HMH_iOS/Presentation/Home/HomeViewModel.swift index c7f7119c..4894bf4d 100644 --- a/HMH_iOS/HMH_iOS/Presentation/Home/HomeViewModel.swift +++ b/HMH_iOS/HMH_iOS/Presentation/Home/HomeViewModel.swift @@ -5,28 +5,31 @@ // Created by 이지희 on 4/11/24. // -import Foundation +import SwiftUI +import FamilyControls +import DeviceActivity import Combine +import _DeviceActivity_SwiftUI class HomeViewModel: ObservableObject { @Published var totalGoalTime: TimeInterval @Published var currentTotalUsage: TimeInterval @Published var appsUsage: [AppUsage] = [] - + init(totalGoalTime: TimeInterval = 3600, currentTotalUsage: TimeInterval = 0, appsUsage: [AppUsage] = []) { self.totalGoalTime = totalGoalTime self.currentTotalUsage = currentTotalUsage self.appsUsage = appsUsage } - + func remainingTimeForApp(appId: String) -> TimeInterval { if let appUsage = appsUsage.first(where: { $0.appId == appId }) { return max(appUsage.goalTime - appUsage.usedTime, 0) } return 0 } - + func updateUsage(forApp appId: String, time: TimeInterval) { if let index = appsUsage.firstIndex(where: { $0.appId == appId }) { appsUsage[index].usedTime += time @@ -35,12 +38,12 @@ class HomeViewModel: ObservableObject { } func generateDummyData() { - appsUsage = [ - AppUsage(appId: "1", appName: "Instagram", goalTime: 60, usedTime: 45), - AppUsage(appId: "2", appName: "YouTube", goalTime: 45, usedTime: 30), - AppUsage(appId: "3", appName: "Twitter", goalTime: 30, usedTime: 15) - ] - } + appsUsage = [ + AppUsage(appId: "1", appName: "Instagram", goalTime: 60, usedTime: 45), + AppUsage(appId: "2", appName: "YouTube", goalTime: 45, usedTime: 30), + AppUsage(appId: "3", appName: "Twitter", goalTime: 30, usedTime: 15) + ] + } } struct AppUsage: Identifiable {