Toggle navigation
Toggle navigation
This project
Loading...
Sign in
developOne
/
harmonyPool
Go to a project
Toggle navigation
Projects
Groups
Snippets
Help
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
xugenyuan
2024-06-03 18:44:34 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
7ba8bbe9695da7281477bb1845361da2e7c23528
7ba8bbe9
1 parent
a7a9841b
ref |> 解决未登录状态无法保存设置中的开关状态问题
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
77 additions
and
66 deletions
sight_harmony/commons/wdKit/src/main/ets/utils/SPHelper.ets
sight_harmony/features/wdComponent/src/main/ets/components/setting/AccountAndSecurityLayout.ets
sight_harmony/features/wdComponent/src/main/ets/components/setting/MineSettingComponent.ets
sight_harmony/features/wdComponent/src/main/ets/model/MineSettingDatasModel.ets
sight_harmony/commons/wdKit/src/main/ets/utils/SPHelper.ets
View file @
7ba8bbe
import data_preferences from '@ohos.data.preferences';
import { Logger } from './Logger';
import { JSON } from '@kit.ArkTS';
const TAG = 'SPHelper'
const logEnable = false
/**
* sp存储,单条数据,value<1k;业务数据超过1k的,建议使用KVStoreHelper
*/
...
...
@@ -12,10 +15,14 @@ export class SPHelper {
static init(context: Context) {
SPHelper.context = context;
if (context) {
Logger.debug(TAG, '初始化context')
}
}
static setSpFilename(spFilename: string) {
SPHelper.spFilename = spFilename;
Logger.debug(TAG, '设置文件名: ' + spFilename)
}
// 静态属性
...
...
@@ -37,47 +44,75 @@ export class SPHelper {
}
async save(key: string, value: data_preferences.ValueType) {
try {
const preferences: data_preferences.Preferences = await this.getVideoPreferences();
await preferences.put(key, value)
await preferences.flush()
if (logEnable) {
Logger.debug(TAG, '保存 key: ' + key + " value => " + value)
}
} catch (e) {
Logger.error(TAG, '保存 key: ' + key + " value => " + value + " 报错:" + JSON.stringify(e))
}
}
saveSync(key: string, value: data_preferences.ValueType) {
const preferences: data_preferences.Preferences = this.getVideoPreferencesSync();
preferences.putSync(key, value)
preferences.flush().then(() => {
Logger.debug(TAG, 'saveSync flush success')
if (logEnable) {
Logger.debug(TAG, 'sync保存 key: ' + key + " value => " + value)
}
}).catch((error: object) => {
Logger.
debug(TAG, 'saveSync flush failed: '
+ JSON.stringify(error))
Logger.
error(TAG, 'sync保存 key: ' + key + " value => " + value + " 报错:"
+ JSON.stringify(error))
});
}
async get(key: string, defValue: data_preferences.ValueType): Promise<data_preferences.ValueType> {
const preferences: data_preferences.Preferences = await this.getVideoPreferences();
return await preferences.get(key, defValue);
const data = await preferences.get(key, defValue);
if (logEnable) {
Logger.debug(TAG, '获取 key: ' + key + " value => " + data + "| 默认值:" + defValue)
}
return data
}
getSync(key: string, defValue: data_preferences.ValueType): data_preferences.ValueType {
const preferences: data_preferences.Preferences = this.getVideoPreferencesSync();
return preferences.getSync(key, defValue);
const data = preferences.getSync(key, defValue);
if (logEnable) {
Logger.debug(TAG, 'sync获取 key: ' + key + " value => " + data + "| 默认值:" + defValue)
}
return data
}
async has(key: string): Promise<boolean> {
const preferences: data_preferences.Preferences = await this.getVideoPreferences();
return await preferences.has(key);
const data = await preferences.has(key);
if (logEnable) {
Logger.debug(TAG, 'has key: ' + key + ' => ' + data)
}
return data
}
hasSync(key: string): boolean {
const preferences: data_preferences.Preferences = this.getVideoPreferencesSync();
return preferences.hasSync(key);
const data = preferences.hasSync(key);
if (logEnable) {
Logger.debug(TAG, 'synchas key: ' + key + ' => ' + data)
}
return data
}
async delete(key: string) {
const preferences: data_preferences.Preferences = await this.getVideoPreferences();
preferences.delete(key).then(async () => {
await preferences.flush();
if (logEnable) {
Logger.debug(TAG, '删除 key: ' + key)
}
}).catch((err: Error) => {
// Logger.error(TAG, 'Failed to delete the key. Cause: ' + err
);
Logger.error(TAG, '删除 key失败:' + JSON.stringify(err)
);
});
}
...
...
@@ -85,9 +120,11 @@ export class SPHelper {
const preferences: data_preferences.Preferences = this.getVideoPreferencesSync();
preferences.deleteSync(key)
preferences.flush().then(() => {
Logger.debug(TAG, 'deleteSync flush success')
if (logEnable) {
Logger.debug(TAG, 'sync删除 key: ' + key)
}
}).catch((error: object) => {
Logger.
debug(TAG, 'deleteSync flush failed: ' + JSON.stringify(error))
Logger.
error(TAG, 'sync删除 key失败:' + JSON.stringify(error));
});
}
...
...
@@ -95,8 +132,9 @@ export class SPHelper {
this.getVideoPreferences().then(async (preferences: data_preferences.Preferences) => {
preferences.clearSync()
await preferences.flush()
Logger.debug(TAG, 'sync清除所有数据');
}).catch((err: Error) => {
// Logger.error(TAG, 'get the preferences failed, Cause: ' + err
);
Logger.error(TAG, 'sync清除所有数据,失败:' + JSON.stringify(err)
);
});
}
...
...
sight_harmony/features/wdComponent/src/main/ets/components/setting/AccountAndSecurityLayout.ets
View file @
7ba8bbe
import { SpConstants } from 'wdConstant';
import { Logger, SPHelper, ToastUtils, EmitterEventId, EmitterUtils, DateTimeUtils, CustomToast } from 'wdKit';
import {MineMainSettingFunctionItem} from '../../viewmodel/MineMainSettingFunctionItem';
import
MineSettingDatasModel
from '../../model/MineSettingDatasModel';
import
{ MineSettingDatasModel }
from '../../model/MineSettingDatasModel';
import router from '@ohos.router';
import { WDRouterPage, WDRouterRule } from 'wdRouter';
import { Params } from 'wdBean';
...
...
sight_harmony/features/wdComponent/src/main/ets/components/setting/MineSettingComponent.ets
View file @
7ba8bbe
...
...
@@ -9,13 +9,15 @@ import { Params } from 'wdBean';
// import { common } from '@kit.AbilityKit';
import fs from '@ohos.file.fs';
import { CustomCacheDialog } from './CustomCacheDialog';
import
MineSettingDatasModel
from '../../model/MineSettingDatasModel';
import
{ MineSettingDatasModel }
from '../../model/MineSettingDatasModel';
import { MineMainSettingFunctionItem } from '../../viewmodel/MineMainSettingFunctionItem';
import common from '@ohos.app.ability.common';
import dataPreferences from '@ohos.data.preferences';
import { TitleBackComponent } from './TitleBackComponent';
import { MyCustomDialog } from '../reusable/MyCustomDialog';
import { TrackingButton, TrackConstants } from 'wdTracking/Index';
import { JSON } from '@kit.ArkTS';
import { HttpUtils } from 'wdNetwork/Index';
@Component
export struct MineSettingComponent {
...
...
@@ -82,10 +84,9 @@ export struct MineSettingComponent {
async getSettingPageData() {
let oldList = MineSettingDatasModel.getMineMainSettingFunctionItemData();
let userId=await SPHelper.default.get(SpConstants.USER_ID,'') as string
if(userId==''){
this.listData=oldList.slice(1,oldList.length)
}else {
if (!HttpUtils.isLogin()) {
this.listData = oldList.slice(1,oldList.length)
} else {
this.listData = oldList;
}
...
...
@@ -167,6 +168,7 @@ export struct MineSettingComponent {
.margin({ left: `${this.calcHeight(81)}lpx`, right: `${this.calcHeight(29)}lpx` })
.selectedColor("#ED2800")
.onChange((isOn: boolean) => {
Logger.debug("SPHelper", "数据 : " + JSON.stringify(item))
if(item.itemType=='push_switch'){
trackButtonClick("settingPagePushSwitch")
//推送
...
...
sight_harmony/features/wdComponent/src/main/ets/model/MineSettingDatasModel.ets
View file @
7ba8bbe
...
...
@@ -12,74 +12,49 @@ const TAG = "MineSettingDatasModel"
/**
* 我的设置页面 所有数据 获取封装类
*/
class MineSettingDatasModel{
private static instance: MineSettingDatasModel;
mainSettingData:Array<MineMainSettingFunctionItem> = []
accountAndSecurityData:Array<MineMainSettingFunctionItem> = []
private constructor() { }
/**
* 单例模式
* @returns
*/
public static getInstance(): MineSettingDatasModel {
if (!MineSettingDatasModel.instance) {
MineSettingDatasModel.instance = new MineSettingDatasModel();
}
return MineSettingDatasModel.instance;
}
export class MineSettingDatasModel {
/**
* 评论 关注 收藏 等7个数据
* 包含名字和图标
*/
getMineMainSettingFunctionItemData():MineMainSettingFunctionItem[]{
if(this.mainSettingData.length === 7){
return this.mainSettingData
}
this.mainSettingData = []
this.mainSettingData.push(new MineMainSettingFunctionItem(null, '账户与安全', '18888888888', 0, false,"account"))
static getMineMainSettingFunctionItemData() {
let mainSettingData: MineMainSettingFunctionItem[] = []
mainSettingData.push(new MineMainSettingFunctionItem(null, '账户与安全', '18888888888', 0, false,"account"))
let pushState=SPHelper.default.getSync(SpConstants.SETTING_PUSH_SWITCH,false) as boolean
this.mainSettingData.push(new MineMainSettingFunctionItem(null, '接收推送', null, 1, pushState,"push_switch"))
this.mainSettingData.push(new MineMainSettingFunctionItem(null, '隐私设罝', null, 0, false,"private_setting"))
mainSettingData.push(new MineMainSettingFunctionItem(null, '接收推送', null, 1, pushState,"push_switch"))
mainSettingData.push(new MineMainSettingFunctionItem(null, '隐私设罝', null, 0, false,"private_setting"))
let wifiState=SPHelper.default.getSync(SpConstants.SETTING_WIFI_IMAGE_SWITCH,false) as boolean
this.
mainSettingData.push(new MineMainSettingFunctionItem(null, '仅wifi网络加载图片', null, 1, wifiState,"wifi_switch"))
mainSettingData.push(new MineMainSettingFunctionItem(null, '仅wifi网络加载图片', null, 1, wifiState,"wifi_switch"))
let videoState=SPHelper.default.getSync(SpConstants.SETTING_WIFI_VIDEO_SWITCH,false) as boolean
this.
mainSettingData.push(new MineMainSettingFunctionItem(null, 'wifi网络情况下自动播放视频', null, 1, videoState,"video_switch"))
mainSettingData.push(new MineMainSettingFunctionItem(null, 'wifi网络情况下自动播放视频', null, 1, videoState,"video_switch"))
let suspensionState=SPHelper.default.getSync(SpConstants.SETTING_SUSPENSION_SWITCH,false) as boolean
this.
mainSettingData.push(new MineMainSettingFunctionItem(null, '开启播放器悬浮窗', null, 1, suspensionState,"suspensionState_switch"))
mainSettingData.push(new MineMainSettingFunctionItem(null, '开启播放器悬浮窗', null, 1, suspensionState,"suspensionState_switch"))
// this.mainSettingData.push(new MineMainSettingFunctionItem(null, null, null, 2, null,""))
this.
mainSettingData.push(new MineMainSettingFunctionItem(null, '清理缓存', '32MB', 0, false,"clear_cache"))
mainSettingData.push(new MineMainSettingFunctionItem(null, '清理缓存', '32MB', 0, false,"clear_cache"))
// this.mainSettingData.push(new MineMainSettingFunctionItem(null, '评价我们', null, 0, false,""))
return
this.
mainSettingData
return mainSettingData
}
/**
* 评论 关注 收藏 等7个数据
* 包含名字和图标
*/
getAccountAndSecuritySettingData():MineMainSettingFunctionItem[]{
if(this.accountAndSecurityData.length === 7){
return this.accountAndSecurityData
}
this.accountAndSecurityData = []
this.accountAndSecurityData.push(new MineMainSettingFunctionItem(null, '更换手机号', '18888888888', 0, false,""))
this.accountAndSecurityData.push(new MineMainSettingFunctionItem(null, '设置密码', null, 0, false,""))
this.accountAndSecurityData.push(new MineMainSettingFunctionItem(null, null, null, 2, null,""))
static getAccountAndSecuritySettingData():MineMainSettingFunctionItem[]{
let accountAndSecurityData: MineMainSettingFunctionItem[] = []
accountAndSecurityData.push(new MineMainSettingFunctionItem(null, '更换手机号', '18888888888', 0, false,""))
accountAndSecurityData.push(new MineMainSettingFunctionItem(null, '设置密码', null, 0, false,""))
accountAndSecurityData.push(new MineMainSettingFunctionItem(null, null, null, 2, null,""))
// this.accountAndSecurityData.push(new MineMainSettingFunctionItem($r('app.media.account_qqicon'), '绑定QQ', '立即绑定', 0, false,""))
// this.accountAndSecurityData.push(new MineMainSettingFunctionItem($r('app.media.account_wechaticon'), '绑定微信', '立即绑定', 0, false,""))
// this.accountAndSecurityData.push(new MineMainSettingFunctionItem($r('app.media.account_weiboicon'), '绑定新浪微博', '立即绑定', 0, false,""))
// this.accountAndSecurityData.push(new MineMainSettingFunctionItem($r('app.media.account_appleicon'), 'Apple ID', null, 0, false,""))
// this.accountAndSecurityData.push(new MineMainSettingFunctionItem(null, null, null, 2, null,""))
this.
accountAndSecurityData.push(new MineMainSettingFunctionItem(null, '注销账号', null, 0, false,""))
accountAndSecurityData.push(new MineMainSettingFunctionItem(null, '注销账号', null, 0, false,""))
return
this.
accountAndSecurityData
return accountAndSecurityData
}
...
...
@@ -113,10 +88,10 @@ class MineSettingDatasModel{
/**
* 判断是否设置过密码
*/
checkSetPassword(): Promise<CheckSetPasswordItem> {
static
checkSetPassword(): Promise<CheckSetPasswordItem> {
return new Promise<CheckSetPasswordItem>((success, error) => {
Logger.info(TAG, `checkSetPassword start`);
this
.fetchCheckSetPassword().then((navResDTO: ResponseDTO<CheckSetPasswordItem>) => {
MineSettingDatasModel
.fetchCheckSetPassword().then((navResDTO: ResponseDTO<CheckSetPasswordItem>) => {
if (!navResDTO || navResDTO.code != 0) {
error(null)
return
...
...
@@ -131,13 +106,9 @@ class MineSettingDatasModel{
})
}
fetchCheckSetPassword() {
static
fetchCheckSetPassword() {
let url = HttpUrlUtils.checkSetPassword()
return WDHttp.get<ResponseDTO<CheckSetPasswordItem>>(url)
};
}
const mineSettingDatasModel = MineSettingDatasModel.getInstance()
export default mineSettingDatasModel as MineSettingDatasModel
// export default MineMainSettingFunctionItem as MineMainSettingFunctionItem
...
...
Please
register
or
login
to post a comment