代碼功能概述 這段代碼實現了一個智能睡眠質量分析器應用,專注於監測、分析和改善用户的睡眠質量。主要功能包括:
睡眠記錄追蹤:記錄每日睡眠時間、入睡時間和醒來時間
睡眠質量評估:基於多個維度自動評估睡眠質量
睡眠週期分析:分析深睡、淺睡和REM睡眠分佈
睡眠環境監測:記錄卧室温度、光照和噪音水平
智能建議系統:根據睡眠數據提供個性化改善建議
睡眠趨勢分析:展示睡眠質量的長期變化趨勢
就寢提醒:設置個性化的就寢和起牀提醒
健康報告生成:生成周度和月度睡眠健康報告
通過這個示例,可以深入理解ArkTS如何實現複雜的數據分析和健康監測功能。
- 代碼邏輯分析 應用採用"多維度睡眠評估"的架構設計:
數據採集模塊:
手動輸入睡眠數據 → 自動計算睡眠時長 → 記錄環境因素
模擬傳感器數據 → 實時更新睡眠狀態 → 保存睡眠記錄
質量評估引擎:
分析睡眠時長 → 評估入睡時間 → 計算睡眠效率
綜合環境因素 → 生成質量評分 → 確定改進方向
智能建議算法:
識別睡眠問題 → 匹配改善策略 → 生成個性化建議
考慮用户習慣 → 避免重複建議 → 跟蹤建議效果
可視化分析系統:
聚合歷史數據 → 計算睡眠趨勢 → 生成可視化圖表
比較不同週期 → 識別模式變化 → 預測睡眠質量
提醒優化機制:
分析睡眠規律 → 優化就寢時間 → 自動調整提醒
考慮作息差異 → 適應生活變化 → 提供靈活選擇
@Entry @Component struct SleepQualityAnalyzer { @State sleepRecords: SleepRecord[] = []; @State currentSleep: SleepSession = new SleepSession(); @State sleepGoals: SleepGoals = new SleepGoals(); @State sleepStats: SleepStatistics = new SleepStatistics(); @State activeView: string = 'dashboard'; @State environmentData: EnvironmentData = new EnvironmentData(); @State sleepTips: SleepTip[] = [];
aboutToAppear() { this.loadSampleData(); this.updateSleepStats(); }
build() { Column({ space: 0 }) { this.BuildAppHeader()
if (this.activeView === 'dashboard') {
this.BuildDashboard()
} else if (this.activeView === 'analysis') {
this.BuildDetailedAnalysis()
} else if (this.activeView === 'trends') {
this.BuildTrendsView()
} else if (this.activeView === 'settings') {
this.BuildSettingsView()
}
}
.width('100%')
.height('100%')
.backgroundColor('#0F1B2D')
}
@Builder BuildAppHeader() { Column({ space: 0 }) { Row({ space: 10 }) { Text('🌙 睡眠分析器') .fontSize(20) .fontWeight(FontWeight.Bold) .fontColor('#FFFFFF') .layoutWeight(1)
Button('📊')
.onClick(() => { this.activeView = 'dashboard'; })
.backgroundColor(this.activeView === 'dashboard' ? '#3A506B' : 'transparent')
.fontColor('#FFFFFF')
.borderRadius(20)
.width(40)
.height(40)
Button('🔍')
.onClick(() => { this.activeView = 'analysis'; })
.backgroundColor(this.activeView === 'analysis' ? '#3A506B' : 'transparent')
.fontColor('#FFFFFF')
.borderRadius(20)
.width(40)
.height(40)
Button('📈')
.onClick(() => { this.activeView = 'trends'; })
.backgroundColor(this.activeView === 'trends' ? '#3A506B' : 'transparent')
.fontColor('#FFFFFF')
.borderRadius(20)
.width(40)
.height(40)
Button('⚙️')
.onClick(() => { this.activeView = 'settings'; })
.backgroundColor(this.activeView === 'settings' ? '#3A506B' : 'transparent')
.fontColor('#FFFFFF')
.borderRadius(20)
.width(40)
.height(40)
}
.width('100%')
.padding(15)
.backgroundColor('#1C2541')
}
}
@Builder BuildDashboard() { Scroll() { Column({ space: 25 }) { this.BuildCurrentSleepStatus() this.BuildQualityScore() this.BuildQuickLog() this.BuildEnvironmentMonitor() this.BuildDailyTip() } .width('100%') .padding(20) } .width('100%') .layoutWeight(1) }
@Builder BuildCurrentSleepStatus() { Column({ space: 15 }) { Row({ space: 10 }) { Text(this.currentSleep.isSleeping ? '🛌 睡眠中' : '🌅 清醒中') .fontSize(18) .fontWeight(FontWeight.Bold) .fontColor('#FFFFFF') .layoutWeight(1)
if (!this.currentSleep.isSleeping) {
Button('開始睡眠')
.onClick(() => { this.startSleepTracking(); })
.backgroundColor('#5BC0BE')
.fontColor('#0F1B2D')
.fontSize(12)
.borderRadius(8)
.padding({ left: 15, right: 15 })
.height(36)
} else {
Button('結束睡眠')
.onClick(() => { this.stopSleepTracking(); })
.backgroundColor('#FF6B6B')
.fontColor('#FFFFFF')
.fontSize(12)
.borderRadius(8)
.padding({ left: 15, right: 15 })
.height(36)
}
}
.width('100%')
if (this.currentSleep.isSleeping) {
Row({ space: 15 }) {
Column({ space: 5 }) {
Text('持續時間')
.fontSize(12)
.fontColor('#8A9BB2')
Text(`${this.currentSleep.duration}分鐘`)
.fontSize(16)
.fontWeight(FontWeight.Medium)
.fontColor('#FFFFFF')
}
.layoutWeight(1)
Column({ space: 5 }) {
Text('睡眠階段')
.fontSize(12)
.fontColor('#8A9BB2')
Text(this.getSleepPhaseName(this.currentSleep.currentPhase))
.fontSize(16)
.fontWeight(FontWeight.Medium)
.fontColor('#FFFFFF')
}
.layoutWeight(1)
}
.width('100%')
.padding(15)
.backgroundColor('#1C2541')
.borderRadius(12)
}
}
.width('100%')
.padding(20)
.backgroundColor('#1C2541')
.borderRadius(16)
}
@Builder BuildQualityScore() { const latestRecord = this.getLatestRecord(); const score = latestRecord ? latestRecord.sleepQuality : 0;
Column({ space: 15 }) {
Row({ space: 10 }) {
Text('睡眠質量')
.fontSize(18)
.fontWeight(FontWeight.Bold)
.fontColor('#FFFFFF')
.layoutWeight(1)
Text(`${score}/100`)
.fontSize(18)
.fontWeight(FontWeight.Bold)
.fontColor(this.getQualityColor(score))
}
.width('100%')
// 質量評分環
Stack() {
Circle({ width: 160, height: 160 })
.fill('#1C2541')
.strokeWidth(10)
.stroke('#2D3A5D')
Circle({ width: 160, height: 160 })
.fill(Color.Transparent)
.strokeWidth(10)
.stroke(this.getQualityColor(score))
.strokeDashArray([Math.PI * 80])
.strokeDashOffset(Math.PI * 80 * (1 - score / 100))
.strokeLineCap(LineCapStyle.Round)
Column({ space: 5 }) {
Text(score.toString())
.fontSize(32)
.fontWeight(FontWeight.Bold)
.fontColor('#FFFFFF')
Text('分數')
.fontSize(14)
.fontColor('#8A9BB2')
}
}
.width(160)
.height(160)
.margin({ top: 10, bottom: 10 })
Row({ space: 15 }) {
this.BuildQualityIndicator('時長', this.evaluateDuration(latestRecord))
this.BuildQualityIndicator('深度', this.evaluateDeepSleep(latestRecord))
this.BuildQualityIndicator('連續', this.evaluateContinuity(latestRecord))
}
.width('100%')
}
.width('100%')
.padding(20)
.backgroundColor('#1C2541')
.borderRadius(16)
}
@Builder BuildQualityIndicator(label: string, level: number) { const color = level === 2 ? '#5BC0BE' : level === 1 ? '#FFD166' : '#FF6B6B'; const icon = level === 2 ? '✓' : level === 1 ? '~' : '✗';
Column({ space: 8 }) {
Text(icon)
.fontSize(20)
.fontColor(color)
Text(label)
.fontSize(12)
.fontColor('#8A9BB2')
}
.layoutWeight(1)
.padding(12)
.backgroundColor('#2D3A5D')
.borderRadius(10)
}
@Builder BuildQuickLog() { Column({ space: 15 }) { Text('快速記錄') .fontSize(18) .fontWeight(FontWeight.Bold) .fontColor('#FFFFFF') .alignSelf(ItemAlign.Start)
Row({ space: 15 }) {
Button('😴 記錄入睡')
.onClick(() => { this.logSleepTime('bedtime'); })
.backgroundColor('#3A506B')
.fontColor('#FFFFFF')
.borderRadius(10)
.layoutWeight(1)
.height(60)
Button('🌅 記錄醒來')
.onClick(() => { this.logWakeTime(); })
.backgroundColor('#5BC0BE')
.fontColor('#0F1B2D')
.borderRadius(10)
.layoutWeight(1)
.height(60)
}
.width('100%')
Row({ space: 15 }) {
Button('💤 記錄午睡')
.onClick(() => { this.logNap(); })
.backgroundColor('#3A506B')
.fontColor('#FFFFFF')
.borderRadius(10)
.layoutWeight(1)
.height(60)
Button('📝 添加筆記')
.onClick(() => { this.showNoteDialog(); })
.backgroundColor('#3A506B')
.fontColor('#FFFFFF')
.borderRadius(10)
.layoutWeight(1)
.height(60)
}
.width('100%')
}
.width('100%')
.padding(20)
.backgroundColor('#1C2541')
.borderRadius(16)
}
@Builder BuildEnvironmentMonitor() { Column({ space: 15 }) { Row({ space: 10 }) { Text('🌡️') .fontSize(18)
Text('睡眠環境')
.fontSize(18)
.fontWeight(FontWeight.Bold)
.fontColor('#FFFFFF')
.layoutWeight(1)
}
.width('100%')
Row({ space: 15 }) {
this.BuildEnvironmentMetric('温度', `${this.environmentData.temperature}°C`,
this.environmentData.temperature >= 18 && this.environmentData.temperature <= 22)
this.BuildEnvironmentMetric('光照', `${this.environmentData.lightLevel}lx`,
this.environmentData.lightLevel < 20)
}
.width('100%')
Row({ space: 15 }) {
this.BuildEnvironmentMetric('噪音', `${this.environmentData.noiseLevel}dB`,
this.environmentData.noiseLevel < 40)
this.BuildEnvironmentMetric('濕度', `${this.environmentData.humidity}%`,
this.environmentData.humidity >= 40 && this.environmentData.humidity <= 60)
}
.width('100%')
}
.width('100%')
.padding(20)
.backgroundColor('#1C2541')
.borderRadius(16)
}
@Builder BuildEnvironmentMetric(name: string, value: string, optimal: boolean) { Column({ space: 8 }) { Text(value) .fontSize(16) .fontWeight(FontWeight.Medium) .fontColor(optimal ? '#5BC0BE' : '#FF6B6B')
Text(name)
.fontSize(12)
.fontColor('#8A9BB2')
}
.layoutWeight(1)
.padding(15)
.backgroundColor('#2D3A5D')
.borderRadius(12)
}
@Builder BuildDailyTip() { const tip = this.getDailyTip();
Column({ space: 15 }) {
Row({ space: 10 }) {
Text('💡')
.fontSize(18)
Text('今日建議')
.fontSize(18)
.fontWeight(FontWeight.Bold)
.fontColor('#FFFFFF')
.layoutWeight(1)
}
.width('100%')
Text(tip.content)
.fontSize(14)
.fontColor('#FFFFFF')
.lineHeight(20)
if (tip.action) {
Button(tip.action)
.onClick(() => { this.applyTipAction(tip); })
.backgroundColor('#5BC0BE')
.fontColor('#0F1B2D')
.width('100%')
.height(40)
.margin({ top: 10 })
.borderRadius(8)
}
}
.width('100%')
.padding(20)
.backgroundColor('#1C2541')
.borderRadius(16)
}
@Builder BuildDetailedAnalysis() { const latestRecord = this.getLatestRecord(); const analysis = this.analyzeSleepPattern(latestRecord);
Column({ space: 25 }) {
Text('睡眠深度分析')
.fontSize(24)
.fontWeight(FontWeight.Bold)
.fontColor('#FFFFFF')
.margin({ top: 20 })
this.BuildSleepCycleChart(latestRecord)
this.BuildQualityAssessment(analysis)
this.BuildInfluencingFactors()
this.BuildImprovementSuggestions(analysis)
}
.width('100%')
.padding(20)
}
@Builder BuildSleepCycleChart(record: SleepRecord | undefined) { Column({ space: 15 }) { Text('睡眠週期分佈') .fontSize(18) .fontWeight(FontWeight.Bold) .fontColor('#FFFFFF') .alignSelf(ItemAlign.Start)
if (record) {
const total = record.deepSleep + record.lightSleep + record.remSleep;
const deepPercent = total > 0 ? (record.deepSleep / total) * 100 : 0;
const lightPercent = total > 0 ? (record.lightSleep / total) * 100 : 0;
const remPercent = total > 0 ? (record.remSleep / total) * 100 : 0;
Row({ space: 0 }) {
Rect()
.width(`${deepPercent}%`)
.height(40)
.fill('#5BC0BE')
Rect()
.width(`${lightPercent}%`)
.height(40)
.fill('#3A506B')
Rect()
.width(`${remPercent}%`)
.height(40)
.fill('#FFD166')
}
.width('100%')
.height(40)
.borderRadius(8)
.clip(true)
Row({ space: 20 }) {
this.BuildCycleLegend('深睡', `${record.deepSleep}小時`, '#5BC0BE')
this.BuildCycleLegend('淺睡', `${record.lightSleep}小時`, '#3A506B')
this.BuildCycleLegend('REM', `${record.remSleep}小時`, '#FFD166')
}
.width('100%')
.margin({ top: 15 })
} else {
Text('暫無詳細睡眠數據')
.fontSize(14)
.fontColor('#8A9BB2')
.padding(30)
.backgroundColor('#1C2541')
.borderRadius(12)
.width('100%')
}
}
.width('100%')
.padding(20)
.backgroundColor('#1C2541')
.borderRadius(16)
}
@Builder BuildCycleLegend(label: string, value: string, color: string) { Row({ space: 8 }) { Circle() .width(12) .height(12) .fill(color)
Column({ space: 2 }) {
Text(label)
.fontSize(12)
.fontColor('#FFFFFF')
Text(value)
.fontSize(10)
.fontColor('#8A9BB2')
}
}
.layoutWeight(1)
}
@Builder BuildQualityAssessment(analysis: SleepAnalysis) { Column({ space: 15 }) { Text('質量評估') .fontSize(18) .fontWeight(FontWeight.Bold) .fontColor('#FFFFFF') .alignSelf(ItemAlign.Start)
ForEach(analysis.factors, (factor: QualityFactor) => {
Row({ space: 10 }) {
Text(factor.name)
.fontSize(14)
.fontColor('#FFFFFF')
.layoutWeight(1)
Row({ space: 5 }) {
ForEach([1, 2, 3, 4, 5], (star: number) => {
Text(star <= factor.rating ? '★' : '☆')
.fontSize(16)
.fontColor(star <= factor.rating ? '#FFD166' : '#3A506B')
})
}
}
.width('100%')
.padding(12)
.backgroundColor('#2D3A5D')
.borderRadius(8)
.margin({ bottom: 8 })
})
}
.width('100%')
.padding(20)
.backgroundColor('#1C2541')
.borderRadius(16)
}
@Builder BuildInfluencingFactors() { Column({ space: 15 }) { Text('影響因素') .fontSize(18) .fontWeight(FontWeight.Bold) .fontColor('#FFFFFF') .alignSelf(ItemAlign.Start)
Row({ space: 15 }) {
this.BuildFactorCard('咖啡因', '下午攝入可能影響', '⚠️')
this.BuildFactorCard('屏幕時間', '睡前1小時使用', '📱')
}
.width('100%')
Row({ space: 15 }) {
this.BuildFactorCard('壓力水平', '中等偏高', '😰')
this.BuildFactorCard('運動', '睡前3小時運動', '🏃')
}
.width('100%')
}
.width('100%')
.padding(20)
.backgroundColor('#1C2541')
.borderRadius(16)
}
@Builder BuildFactorCard(title: string, description: string, icon: string) { Column({ space: 10 }) { Text(icon) .fontSize(24)
Text(title)
.fontSize(14)
.fontWeight(FontWeight.Medium)
.fontColor('#FFFFFF')
.textAlign(TextAlign.Center)
Text(description)
.fontSize(12)
.fontColor('#8A9BB2')
.textAlign(TextAlign.Center)
}
.layoutWeight(1)
.padding(15)
.backgroundColor('#2D3A5D')
.borderRadius(12)
}
@Builder BuildImprovementSuggestions(analysis: SleepAnalysis) { Column({ space: 15 }) { Text('改善建議') .fontSize(18) .fontWeight(FontWeight.Bold) .fontColor('#FFFFFF') .alignSelf(ItemAlign.Start)
ForEach(analysis.suggestions.slice(0, 3), (suggestion: string, index: number) => {
Row({ space: 10 }) {
Text('👉')
.fontSize(14)
Text(suggestion)
.fontSize(14)
.fontColor('#FFFFFF')
.lineHeight(20)
.layoutWeight(1)
}
.width('100%')
.padding(15)
.backgroundColor(index % 2 === 0 ? '#2D3A5D' : '#1C2541')
.borderRadius(10)
})
}
.width('100%')
.padding(20)
.backgroundColor('#1C2541')
.borderRadius(16)
}
@Builder BuildTrendsView() { Column({ space: 25 }) { Text('睡眠趨勢') .fontSize(24) .fontWeight(FontWeight.Bold) .fontColor('#FFFFFF') .margin({ top: 20 })
this.BuildWeeklyTrendChart()
this.BuildSleepStats()
this.BuildConsistencyScore()
this.BuildBestPractices()
}
.width('100%')
.padding(20)
}
@Builder BuildWeeklyTrendChart() { const weekData = this.getWeekData();
Column({ space: 15 }) {
Text('本週睡眠趨勢')
.fontSize(18)
.fontWeight(FontWeight.Bold)
.fontColor('#FFFFFF')
.alignSelf(ItemAlign.Start)
Row({ space: 5 }) {
ForEach(weekData, (day: WeekData, index: number) => {
const height = Math.min(day.hours * 20, 120);
Column({ space: 5 }) {
Rect()
.width(30)
.height(height)
.fill(day.quality >= 80 ? '#5BC0BE' :
day.quality >= 60 ? '#FFD166' : '#FF6B6B')
.borderRadius(4)
Text(['一', '二', '三', '四', '五', '六', '日'][index])
.fontSize(12)
.fontColor('#8A9BB2')
}
.layoutWeight(1)
.height(140)
.justifyContent(FlexAlign.End)
})
}
.width('100%')
.height(140)
.padding(15)
.backgroundColor('#1C2541')
.borderRadius(12)
}
.width('100%')
.padding(20)
.backgroundColor('#1C2541')
.borderRadius(16)
}
@Builder BuildSleepStats() { Column({ space: 15 }) { Text('睡眠統計') .fontSize(18) .fontWeight(FontWeight.Bold) .fontColor('#FFFFFF') .alignSelf(ItemAlign.Start)
Row({ space: 15 }) {
this.BuildStatItem('平均時長', `${this.sleepStats.avgHours.toFixed(1)}小時`, '🕐')
this.BuildStatItem('最佳質量', `${this.sleepStats.bestQuality}/100`, '🏆')
}
.width('100%')
Row({ space: 15 }) {
this.BuildStatItem('連續天數', `${this.sleepStats.streakDays}天`, '🔥')
this.BuildStatItem('深睡比例', `${this.sleepStats.deepSleepPercent}%`, '💤')
}
.width('100%')
}
.width('100%')
.padding(20)
.backgroundColor('#1C2541')
.borderRadius(16)
}
@Builder BuildStatItem(label: string, value: string, icon: string) { Column({ space: 10 }) { Row({ space: 8 }) { Text(icon) .fontSize(18)
Text(value)
.fontSize(16)
.fontWeight(FontWeight.Bold)
.fontColor('#FFFFFF')
}
Text(label)
.fontSize(12)
.fontColor('#8A9BB2')
.textAlign(TextAlign.Center)
}
.layoutWeight(1)
.padding(15)
.backgroundColor('#2D3A5D')
.borderRadius(12)
}
@Builder BuildSettingsView() { Scroll() { Column({ space: 25 }) { Text('設置') .fontSize(24) .fontWeight(FontWeight.Bold) .fontColor('#FFFFFF') .margin({ top: 20 })
this.BuildGoalSettings()
this.BuildReminderSettings()
this.BuildEnvironmentSettings()
this.BuildDataManagement()
}
.width('100%')
.padding(20)
}
.width('100%')
.layoutWeight(1)
}
@Builder BuildGoalSettings() { Column({ space: 15 }) { Text('睡眠目標') .fontSize(18) .fontWeight(FontWeight.Bold) .fontColor('#FFFFFF') .alignSelf(ItemAlign.Start)
Row({ space: 10 }) {
Text('目標時長:')
.fontSize(14)
.fontColor('#FFFFFF')
.layoutWeight(1)
TextInput({ text: this.sleepGoals.targetHours.toString() })
.onChange((value: string) => {
const hours = parseInt(value) || 8;
this.sleepGoals.targetHours = Math.max(4, Math.min(12, hours));
})
.type(InputType.Number)
.width(80)
.textAlign(TextAlign.Center)
.backgroundColor('#2D3A5D')
.fontColor('#FFFFFF')
.borderRadius(8)
.padding(8)
Text('小時')
.fontSize(14)
.fontColor('#8A9BB2')
}
.width('100%')
Row({ space: 10 }) {
Text('目標質量:')
.fontSize(14)
.fontColor('#FFFFFF')
.layoutWeight(1)
Text(`${this.sleepGoals.targetQuality}/100`)
.fontSize(14)
.fontColor('#5BC0BE')
Slider({
value: this.sleepGoals.targetQuality,
min: 50,
max: 100,
step: 5,
style: SliderStyle.OutSet
})
.onChange((value: number) => {
this.sleepGoals.targetQuality = value;
})
.width(150)
.height(30)
}
.width('100%')
}
.width('100%')
.padding(20)
.backgroundColor('#1C2541')
.borderRadius(16)
}
// 以下是私有方法,因篇幅限制僅展示部分 private loadSampleData(): void { this.sleepRecords = [ { date: '2024-01-15', bedTime: '23:15', wakeTime: '07:30', totalHours: 8.25, sleepQuality: 85, deepSleep: 2.5, lightSleep: 4.0, remSleep: 1.75, interruptions: 1, notes: '睡眠質量良好' }, { date: '2024-01-14', bedTime: '00:30', wakeTime: '07:00', totalHours: 6.5, sleepQuality: 65, deepSleep: 1.8, lightSleep: 3.5, remSleep: 1.2, interruptions: 3, notes: '入睡較晚' } ];
this.sleepTips = [
{ id: '1', content: '建議今晚提前30分鐘就寢', category: 'timing', priority: 1, action: '設置提醒' },
{ id: '2', content: '睡前避免使用電子設備', category: 'habit', priority: 2, action: '開啓勿擾模式' }
];
this.sleepGoals = {
targetHours: 8,
targetQuality: 80,
bedTimeGoal: '23:00',
wakeTimeGoal: '07:00'
};
}
private updateSleepStats(): void { if (this.sleepRecords.length > 0) { const totalHours = this.sleepRecords.reduce((sum, record) => sum + record.totalHours, 0); const totalQuality = this.sleepRecords.reduce((sum, record) => sum + record.sleepQuality, 0); const totalDeepSleep = this.sleepRecords.reduce((sum, record) => sum + record.deepSleep, 0);
this.sleepStats = {
avgHours: totalHours / this.sleepRecords.length,
avgQuality: totalQuality / this.sleepRecords.length,
bestQuality: Math.max(...this.sleepRecords.map(r => r.sleepQuality)),
streakDays: this.calculateStreak(),
deepSleepPercent: totalDeepSleep / totalHours * 100
};
}
}
private getLatestRecord(): SleepRecord | undefined { return this.sleepRecords.length > 0 ? this.sleepRecords[this.sleepRecords.length - 1] : undefined; }
private getDailyTip(): SleepTip { return this.sleepTips.length > 0 ? this.sleepTips[0] : { id: 'default', content: '保持規律的作息時間有助於提高睡眠質量', category: 'general', priority: 3, action: '' }; }
private getQualityColor(score: number): string { if (score >= 80) return '#5BC0BE'; if (score >= 60) return '#FFD166'; return '#FF6B6B'; }
private getSleepPhaseName(phase: string): string { switch(phase) { case 'deep': return '深睡期'; case 'light': return '淺睡期'; case 'rem': return 'REM期'; default: return '清醒期'; } }
private startSleepTracking(): void { this.currentSleep.isSleeping = true; this.currentSleep.startTime = new Date().toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' });
// 模擬睡眠階段變化
setInterval(() => {
if (this.currentSleep.isSleeping) {
this.currentSleep.duration += 1;
// 簡單模擬睡眠階段循環
const phaseIndex = Math.floor(this.currentSleep.duration / 30) % 4;
this.currentSleep.currentPhase = ['light', 'deep', 'light', 'rem'][phaseIndex];
}
}, 1000);
}
private stopSleepTracking(): void { this.currentSleep.isSleeping = false;
// 創建睡眠記錄
const newRecord: SleepRecord = {
date: new Date().toISOString().split('T')[0],
bedTime: this.currentSleep.startTime,
wakeTime: new Date().toLocaleTimeString('zh-CN', { hour: '2-digit', minute: '2-digit' }),
totalHours: this.currentSleep.duration / 60,
sleepQuality: this.calculateSleepQuality(),
deepSleep: this.currentSleep.duration * 0.2 / 60,
lightSleep: this.currentSleep.duration * 0.5 / 60,
remSleep: this.currentSleep.duration * 0.3 / 60,
interruptions: Math.floor(Math.random() * 3),
notes: ''
};
this.sleepRecords.push(newRecord);
this.updateSleepStats();
this.currentSleep = new SleepSession();
}
private calculateSleepQuality(): number { // 簡化質量計算:基於時長和環境因素 const durationScore = Math.min(this.currentSleep.duration / 480 * 100, 100); // 8小時為滿分 const envScore = (this.environmentData.temperature >= 18 && this.environmentData.temperature <= 22 ? 25 : 0) + (this.environmentData.lightLevel < 20 ? 25 : 0) + (this.environmentData.noiseLevel < 40 ? 25 : 0) + (this.environmentData.humidity >= 40 && this.environmentData.humidity <= 60 ? 25 : 0);
return Math.floor((durationScore + envScore) / 2);
}
private evaluateDuration(record: SleepRecord | undefined): number { if (!record) return 0; if (record.totalHours >= 7 && record.totalHours <= 9) return 2; if (record.totalHours >= 6 && record.totalHours <= 10) return 1; return 0; }
private evaluateDeepSleep(record: SleepRecord | undefined): number { if (!record) return 0; const deepSleepPercent = record.totalHours > 0 ? (record.deepSleep / record.totalHours) * 100 : 0; if (deepSleepPercent >= 20) return 2; if (deepSleepPercent >= 15) return 1; return 0; }
private evaluateContinuity(record: SleepRecord | undefined): number { if (!record) return 0; if (record.interruptions === 0) return 2; if (record.interruptions <= 2) return 1; return 0; }
private analyzeSleepPattern(record: SleepRecord | undefined): SleepAnalysis { if (!record) { return { overallScore: 0, factors: [], suggestions: ['開始記錄睡眠以獲得個性化分析'] }; }
return {
overallScore: record.sleepQuality,
factors: [
{ name: '睡眠時長', rating: this.evaluateDuration(record) >= 2 ? 5 : this.evaluateDuration(record) >= 1 ? 3 : 1 },
{ name: '睡眠深度', rating: this.evaluateDeepSleep(record) >= 2 ? 5 : this.evaluateDeepSleep(record) >= 1 ? 3 : 1 },
{ name: '睡眠連續性', rating: this.evaluateContinuity(record) >= 2 ? 5 : this.evaluateContinuity(record) >= 1 ? 3 : 1 },
{ name: '入睡時間', rating: this.evaluateBedTime(record) >= 2 ? 5 : this.evaluateBedTime(record) >= 1 ? 3 : 1 }
],
suggestions: this.generateSuggestions(record)
};
}
private evaluateBedTime(record: SleepRecord): number { const bedHour = parseInt(record.bedTime.split(':')[0]); if (bedHour >= 22 && bedHour <= 23) return 2; if (bedHour >= 21 && bedHour <= 24) return 1; return 0; }
private generateSuggestions(record: SleepRecord): string[] { const suggestions = [];
if (record.totalHours < 7) {
suggestions.push('睡眠時長不足,建議增加30-60分鐘睡眠時間');
}
if (record.totalHours > 9) {
suggestions.push('睡眠時長過長,可能影響日間精力');
}
const deepSleepPercent = record.totalHours > 0 ? (record.deepSleep / record.totalHours) * 100 : 0;
if (deepSleepPercent < 15) {
suggestions.push('深睡比例偏低,建議保持安靜黑暗的睡眠環境');
}
if (record.interruptions > 2) {
suggestions.push('睡眠中斷較多,建議減少夜間飲水和電子設備使用');
}
return suggestions.length > 0 ? suggestions : ['睡眠質量良好,繼續保持!'];
}
private getWeekData(): WeekData[] { // 返回示例數據 return [ { day: '一', hours: 7.5, quality: 82 }, { day: '二', hours: 6.8, quality: 75 }, { day: '三', hours: 8.2, quality: 88 }, { day: '四', hours: 7.0, quality: 70 }, { day: '五', hours: 9.1, quality: 92 }, { day: '六', hours: 8.5, quality: 85 }, { day: '日', hours: 7.8, quality: 80 } ]; }
private calculateStreak(): number { // 簡化實現:返回示例數據 return 5; }
private logSleepTime(type: string): void { console.log(記錄${type}: ${new Date().toLocaleTimeString()}); }
private logWakeTime(): void { console.log('記錄醒來時間'); }
private logNap(): void { console.log('記錄午睡'); }
private showNoteDialog(): void { console.log('顯示筆記對話框'); }
private applyTipAction(tip: SleepTip): void { console.log(應用建議: ${tip.content}); } }
// 數據模型定義 class SleepRecord { date: string = ''; bedTime: string = ''; wakeTime: string = ''; totalHours: number = 0; sleepQuality: number = 0; deepSleep: number = 0; lightSleep: number = 0; remSleep: number = 0; interruptions: number = 0; notes: string = ''; }
class SleepSession { isSleeping: boolean = false; startTime: string = ''; currentPhase: string = 'awake'; duration: number = 0; qualityScore: number = 0; }
class SleepGoals { targetHours: number = 8; targetQuality: number = 80; bedTimeGoal: string = '23:00'; wakeTimeGoal: string = '07:00'; }
class SleepStatistics { avgHours: number = 0; avgQuality: number = 0; bestQuality: number = 0; streakDays: number = 0; deepSleepPercent: number = 0; }
class EnvironmentData { temperature: number = 22; lightLevel: number = 10; noiseLevel: number = 35; humidity: number = 50; co2Level: number = 500; }
class SleepTip { id: string = ''; content: string = ''; category: string = ''; priority: number = 1; action: string = ''; }
class SleepAnalysis { overallScore: number = 0; factors: QualityFactor[] = []; suggestions: string[] = []; }
class QualityFactor { name: string = ''; rating: number = 0; }
class WeekData { day: string = ''; hours: number = 0; quality: number = 0; }
想入門鴻蒙開發又怕花冤枉錢?別錯過!現在能免費系統學 -- 從 ArkTS 面向對象核心的類和對象、繼承多態,到吃透鴻蒙開發關鍵技能,還能衝刺鴻蒙基礎 +高級開發者證書,更驚喜的是考證成功還送好禮!快加入我的鴻蒙班,一起從入門到精通,班級鏈接:點擊免費進入