// SettingsPage.ets
import common from '@ohos.app.ability.common';
import preference from '@ohos.data.preference';
import { BusinessError } from '@ohos.base';
@Entry
@Component
struct SettingsPage {
@State themeMode: boolean = false;
@State soundEffect: number = 50;
@State playMode: string = 'sequence';
@State showTips: boolean = true;
private context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
private prefFileName: string = 'music_player_settings';
private preferenceHelper: preference.Preferences | null = null;
aboutToAppear() {
this.initPreferences();
}
// 初始化首选项
async initPreferences() {
try {
this.preferenceHelper = await preference.getPreferences(this.context, this.prefFileName);
// 从首选项加载已有设置
this.themeMode = await this.preferenceHelper.get('theme_mode', false);
this.soundEffect = await this.preferenceHelper.get('sound_effect', 50);
this.playMode = await this.preferenceHelper.get('play_mode', 'sequence');
this.showTips = await this.preferenceHelper.get('show_tips', true);
} catch (error: BusinessError) {
console.error(`Failed to get preferences: ${error.message}`);
}
}
// 保存首选项
async savePreference(key: string, value: any) {
if (!this.preferenceHelper) return;
try {
if (typeof value === 'boolean') {
await this.preferenceHelper.put(key, value);
} else if (typeof value === 'number') {
await this.preferenceHelper.put(key, value);
} else if (typeof value === 'string') {
await this.preferenceHelper.put(key, value);
}
// 提交更改
await this.preferenceHelper.flush();
console.info(`Preference saved: ${key}=${value}`);
} catch (error: BusinessError) {
console.error(`Failed to save preference: ${error.message}`);
}
}
build() {
Column() {
// 标题栏
Stack({ alignContent: Alignment.Center }) {
Text('音乐播放器设置')
.fontSize(24)
.fontWeight(FontWeight.Bold)
}
.width('100%')
.height(80)
.backgroundColor('#F5F5F5')
// 首选项列表
List() {
// 主题模式切换
ListItem() {
SwitchPreference({
name: 'theme_mode',
title: '暗色模式',
summary: '开启后使用暗色主题',
checked: this.themeMode
})
.onChange((newValue: boolean) => {
this.themeMode = newValue;
this.savePreference('theme_mode', newValue);
})
}
// 音效调节
ListItem() {
SliderPreference({
name: 'sound_effect',
title: '音效强度',
summary: `当前: ${this.soundEffect}%`,
value: this.soundEffect,
min: 0,
max: 100,
step: 5
})
.onChange((newValue: number) => {
this.soundEffect = newValue;
this.savePreference('sound_effect', newValue);
})
}
// 播放模式选择
ListItem() {
Column({ space: 10 }) {
Text('播放模式')
.fontSize(18)
.fontWeight(FontWeight.Medium)
.width('100%')
RadioPreferenceGroup() {
RadioPreference({
name: 'play_mode',
title: '顺序播放',
selected: this.playMode === 'sequence'
})
.onChange(() => {
this.playMode = 'sequence';
this.savePreference('play_mode', 'sequence');
})
RadioPreference({
name: 'play_mode',
title: '单曲循环',
selected: this.playMode === 'loop'
})
.onChange(() => {
this.playMode = 'loop';
this.savePreference('play_mode', 'loop');
})
RadioPreference({
name: 'play_mode',
title: '随机播放',
selected: this.playMode === 'shuffle'
})
.onChange(() => {
this.playMode = 'shuffle';
this.savePreference('play_mode', 'shuffle');
})
}
.layoutWeight(1)
}
.width('100%')
.padding(15)
}
// 提示信息开关
ListItem() {
SwitchPreference({
name: 'show_tips',
title: '显示操作提示',
summary: '开启后在操作时显示提示信息',
checked: this.showTips
})
.onChange((newValue: boolean) => {
this.showTips = newValue;
this.savePreference('show_tips', newValue);
})
}
// 关于页面入口
ListItem() {
Preference({
name: 'about',
title: '关于',
summary: '查看应用版本和版权信息'
})
.onClick(() => {
// 这里可以添加跳转到关于页面的逻辑
console.info('Navigate to about page');
})
}
}
.width('100%')
.height('100%')
.margin({ top: 10 })
}
.width('100%')
.height('100%')
}
}
评论