Skip to content
New issue

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

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

Already on GitHub? # to your account

Feat [#12] 이용시간 통계뷰 구현 #20

Merged
merged 19 commits into from
May 6, 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
46 changes: 46 additions & 0 deletions HMH_iOS/HMHDeviceActivityReport/AppActivityView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// AppActivityView.swift
// HMHDeviceActivityReport
//
// Created by 이지희 on 4/27/24.
//

import SwiftUI
import FamilyControls

struct AppActivityView: View {
let totalActivity: String

var body: some View {
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)
Comment on lines +34 to +35
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💊 코드 포멧팅을 변경하여, 3시간 3분 이런 타입으로 가는 것 제안드립니다!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넵 이 부분 놓쳤네요 꼼꼼한 피드백 감사합니다~


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(totalActivity: "1h 23m")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.developer.family-controls</key>
<true/>
<key>com.apple.security.application-groups</key>
<array>
<string>group.HMH</string>
</array>
</dict>
</plist>
20 changes: 20 additions & 0 deletions HMH_iOS/HMHDeviceActivityReport/HMHDeviceActivityReport.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// HMHDeviceActivityReport.swift
// HMHDeviceActivityReport
//
// Created by 이지희 on 4/15/24.
//

import DeviceActivity
import SwiftUI

@main
struct HMHDeviceActivityReport: DeviceActivityReportExtension {
var body: some DeviceActivityReportScene {
// Create a report for each DeviceActivityReport.Context that your app supports.
TotalActivityReport { totalActivity in
TotalActivityView(totalActivity: totalActivity)
}
// Add more reports here...
}
}
17 changes: 17 additions & 0 deletions HMH_iOS/HMHDeviceActivityReport/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>EXAppExtensionAttributes</key>
<dict>
<key>EXExtensionPointIdentifier</key>
<string>com.apple.deviceactivityui.report-extension</string>
</dict>
<key>UIAppFonts</key>
<array>
<string>Pretendard-Regular.otf</string>
<string>Pretendard-SemiBold.otf</string>
<string>Pretendard-Medium.otf</string>
</array>
</dict>
</plist>
39 changes: 39 additions & 0 deletions HMH_iOS/HMHDeviceActivityReport/TotalActivityReport.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// TotalActivityReport.swift
// HMHDeviceActivityReport
//
// Created by 이지희 on 4/15/24.
//

import DeviceActivity
import SwiftUI

extension DeviceActivityReport.Context {
// If your app initializes a DeviceActivityReport with this context, then the system will use
// your extension's corresponding DeviceActivityReportScene to render the contents of the
// report.
static let totalActivity = Self("Total Activity")
}

struct TotalActivityReport: DeviceActivityReportScene {
// Define which context your scene will represent.
let context: DeviceActivityReport.Context = .totalActivity

// Define the custom configuration and the resulting view for this report.
let content: (String) -> TotalActivityView

func makeConfiguration(representing data: DeviceActivityResults<DeviceActivityData>) async -> String {
// Reformat the data into a configuration that can be used to create
// the report's view.

let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.day, .hour, .minute, .second]
formatter.unitsStyle = .abbreviated
formatter.zeroFormattingBehavior = .dropAll

let totalActivityDuration = await data.flatMap { $0.activitySegments }.reduce(0, {
$0 + $1.totalActivityDuration
})
return formatter.string(from: totalActivityDuration) ?? "No activity data"
}
}
45 changes: 45 additions & 0 deletions HMH_iOS/HMHDeviceActivityReport/TotalActivityView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// TotalActivityView.swift
// HMHDeviceActivityReport
//
// Created by 이지희 on 4/15/24.
//

import SwiftUI

struct TotalActivityView: View {
let totalActivity: String

var body: some View {
if let timeString = convertStringToTime(totalActivity) {
Text("\(timeString) 사용")
.font(.title2_semibold_24)
.foregroundStyle(.whiteText)
.frame(maxWidth: .infinity, alignment: .leading)
}
}
}

extension TotalActivityView {
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: "%d", hours)
let minutesText = String(format: "%d", 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 {
TotalActivityView(totalActivity: "1h 23m")
}
Loading